diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx index 0c089b9a8f..bdbf058692 100644 --- a/apps/client/src/widgets/type_widgets/code/Code.tsx +++ b/apps/client/src/widgets/type_widgets/code/Code.tsx @@ -165,9 +165,20 @@ export function CodeEditor({ parentComponent, ntxId, containerRef: externalConta useTriliumEvent("executeWithCodeEditor", async ({ resolve, ntxId: eventNtxId }) => { if (eventNtxId !== ntxId) return; await initialized.current.promise(); + if (!codeEditorRef.current) return; resolve(codeEditorRef.current!); }); + useTriliumEvent("noteContextRemoved", async ({ ntxIds: eventNtxIds }) => { + if (!ntxId || !eventNtxIds.includes(ntxId)) return; + + const cm = codeEditorRef.current; + if (cm) { + cm.destroy(); + codeEditorRef.current = null; + } + }); + useTriliumEvent("executeWithContentElement", async ({ resolve, ntxId: eventNtxId}) => { if (eventNtxId !== ntxId) return; await initialized.current.promise(); diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsx index 15c235f3f9..f118512991 100644 --- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx +++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx @@ -179,6 +179,16 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext resolve(editor); }); + useTriliumEvent("noteContextRemoved", async ({ ntxIds: eventNtxIds }) => { + if (!ntxId || !eventNtxIds.includes(ntxId)) return; + + const watchdog = watchdogRef.current; + if (!watchdog) return; + + await watchdog.destroy(); + watchdogRef.current = null; + }); + async function waitForEditor() { await initialized.current; const editor = watchdogRef.current?.editor; diff --git a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx index caa0302b8c..e243b6cf59 100644 --- a/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx +++ b/apps/client/src/widgets/type_widgets/text/ReadOnlyText.tsx @@ -50,6 +50,15 @@ export default function ReadOnlyText({ note, noteContext, ntxId }: TypeWidgetPro resolve($(contentRef.current)); }); + useTriliumEvent("noteContextRemoved", ({ ntxIds: eventNtxIds }) => { + if (!ntxId || !eventNtxIds.includes(ntxId)) return; + + if (contentRef.current) { + contentRef.current.innerHTML = ""; + } + contentRef.current = null; + }); + return (