From f8371aaa578174435c576b17950eb48a19d49874 Mon Sep 17 00:00:00 2001 From: Yuri Karamian Date: Fri, 19 Jun 2026 11:47:11 +0200 Subject: [PATCH] Fix editor not rendering on every other source mode switch --- src/lib/components/Editor.svelte | 62 ++++++++++++++++---------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/src/lib/components/Editor.svelte b/src/lib/components/Editor.svelte index 62ca55b..2a5a00a 100644 --- a/src/lib/components/Editor.svelte +++ b/src/lib/components/Editor.svelte @@ -3392,7 +3392,15 @@ function createEditor(content: string) { if (!editorElement) return; - destroyEditor(); + // Don't call destroyEditor here — it sets editorReady=false which + // triggers reactivity and races with the tick().then() in the + // source-mode swap. Just destroy the editor instance directly. + if (editor) { + editor.destroy(); + editor = null; + } + mathObserver?.disconnect(); + mathObserver = null; isLargeDoc = content.length > LARGE_DOC_CHARS; const html = markdownToHtml(content); @@ -4616,37 +4624,31 @@ // Only act if we have a loaded note if (!loadedPath) return; - // untrack: prevent flushSave() (inside destroyEditor) from adding - // $editorDirty/$activeNote as effect dependencies, which causes - // the effect to re-run and race with tick().then() — resulting in - // the editor failing to render on every other source-mode switch. - untrack(() => { - if (isSource && !lastSourceMode) { - // Switching TO source: extract markdown from editor - sourceContent = editor ? editorToMarkdown() : ($activeNote?.content ?? ''); - resetSourceHistory(sourceContent); - lastSourceMode = true; - } else if (!isSource && lastSourceMode) { - lastSourceMode = false; - if (isMobile) { - // Mobile: editor stays in DOM, just update its content - const content = sourceContent || ($activeNote?.content ?? ''); - if (editor) { - ignoreNextUpdate = true; - editor.commands.setContent(markdownToHtml(content)); - } - } else { - // Desktop: destroy old editor, wait for DOM swap, then recreate. - destroyEditor(); - const content = sourceContent || ($activeNote?.content ?? ''); - tick().then(() => { - if (editorElement && !editor) { - createEditor(content); - } - }); + if (isSource && !lastSourceMode) { + // Switching TO source: extract markdown from editor + sourceContent = editor ? editorToMarkdown() : ($activeNote?.content ?? ''); + resetSourceHistory(sourceContent); + lastSourceMode = true; + } else if (!isSource && lastSourceMode) { + lastSourceMode = false; + if (isMobile) { + // Mobile: editor stays in DOM, just update its content + const content = sourceContent || ($activeNote?.content ?? ''); + if (editor) { + ignoreNextUpdate = true; + editor.commands.setContent(markdownToHtml(content)); } + } else { + // Desktop: destroy old editor, wait for DOM swap, then recreate. + destroyEditor(); + const content = sourceContent || ($activeNote?.content ?? ''); + tick().then(() => { + if (editorElement && !editor) { + createEditor(content); + } + }); } - }); + } }); // Tauri drag-drop listener for OS file drops (browser DragEvent doesn't have files in Tauri)