diff --git a/src/components/Editor.tsx b/src/components/Editor.tsx index 63ab0e5c..8695a9e5 100644 --- a/src/components/Editor.tsx +++ b/src/components/Editor.tsx @@ -180,18 +180,27 @@ export const Editor = memo(function Editor({ const tab = tabs.find(t => t.entry.path === activeTabPath) if (!tab) return - if (cache.has(activeTabPath)) { - // Instant switch — use cached blocks - editor.replaceBlocks(editor.document, cache.get(activeTabPath)!) - } else { - // First open — parse markdown - const [, body] = splitFrontmatter(tab.content) - const preprocessed = preProcessWikilinks(body) - editor.tryParseMarkdownToBlocks(preprocessed).then(blocks => { - const withWikilinks = injectWikilinks(blocks) - editor.replaceBlocks(editor.document, withWikilinks) - cache.set(activeTabPath, withWikilinks) - }) + try { + if (cache.has(activeTabPath)) { + // Instant switch — use cached blocks + editor.replaceBlocks(editor.document, cache.get(activeTabPath)!) + } else { + // First open — parse markdown + const [, body] = splitFrontmatter(tab.content) + const preprocessed = preProcessWikilinks(body) + editor.tryParseMarkdownToBlocks(preprocessed).then(blocks => { + const withWikilinks = injectWikilinks(blocks) + try { + editor.replaceBlocks(editor.document, withWikilinks) + } catch (err) { + console.error('Failed to replace blocks:', err) + return + } + cache.set(activeTabPath, withWikilinks) + }) + } + } catch (err) { + console.error('Failed to swap editor content:', err) } }, [activeTabPath, tabs, editor]) diff --git a/src/hooks/useNoteActions.ts b/src/hooks/useNoteActions.ts index 5bd29f09..aee0f650 100644 --- a/src/hooks/useNoteActions.ts +++ b/src/hooks/useNoteActions.ts @@ -111,10 +111,7 @@ export function useNoteActions( return } - // Set active immediately for instant visual feedback - setActiveTabPath(entry.path) - - // Load content async + // Load content async, then add tab and set active together try { const content = isTauri() ? await invoke('get_note_content', { path: entry.path }) @@ -123,12 +120,14 @@ export function useNoteActions( if (prev.some((t) => t.entry.path === entry.path)) return prev return [...prev, { entry, content }] }) + setActiveTabPath(entry.path) } catch (err) { console.warn('Failed to load note content:', err) setTabs((prev) => { if (prev.some((t) => t.entry.path === entry.path)) return prev return [...prev, { entry, content: '' }] }) + setActiveTabPath(entry.path) } }, [])