Fix editor not rendering on every other source mode switch

This commit is contained in:
Yuri Karamian
2026-06-19 11:47:11 +02:00
parent c5a45131a5
commit f8371aaa57
+32 -30
View File
@@ -3392,7 +3392,15 @@
function createEditor(content: string) { function createEditor(content: string) {
if (!editorElement) return; 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; isLargeDoc = content.length > LARGE_DOC_CHARS;
const html = markdownToHtml(content); const html = markdownToHtml(content);
@@ -4616,37 +4624,31 @@
// Only act if we have a loaded note // Only act if we have a loaded note
if (!loadedPath) return; if (!loadedPath) return;
// untrack: prevent flushSave() (inside destroyEditor) from adding if (isSource && !lastSourceMode) {
// $editorDirty/$activeNote as effect dependencies, which causes // Switching TO source: extract markdown from editor
// the effect to re-run and race with tick().then() — resulting in sourceContent = editor ? editorToMarkdown() : ($activeNote?.content ?? '');
// the editor failing to render on every other source-mode switch. resetSourceHistory(sourceContent);
untrack(() => { lastSourceMode = true;
if (isSource && !lastSourceMode) { } else if (!isSource && lastSourceMode) {
// Switching TO source: extract markdown from editor lastSourceMode = false;
sourceContent = editor ? editorToMarkdown() : ($activeNote?.content ?? ''); if (isMobile) {
resetSourceHistory(sourceContent); // Mobile: editor stays in DOM, just update its content
lastSourceMode = true; const content = sourceContent || ($activeNote?.content ?? '');
} else if (!isSource && lastSourceMode) { if (editor) {
lastSourceMode = false; ignoreNextUpdate = true;
if (isMobile) { editor.commands.setContent(markdownToHtml(content));
// 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);
}
});
} }
} 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) // Tauri drag-drop listener for OS file drops (browser DragEvent doesn't have files in Tauri)