diff --git a/components/notes/InlineMarkdownEditor.tsx b/components/notes/InlineMarkdownEditor.tsx index 526a5fbef..404810207 100644 --- a/components/notes/InlineMarkdownEditor.tsx +++ b/components/notes/InlineMarkdownEditor.tsx @@ -486,6 +486,11 @@ export const InlineMarkdownEditor = React.memo(function InlineMarkdownEditor({ const { t } = useI18n(); const editorRef = useRef(null); const latestMarkdownRef = useRef(value); + // Last markdown value we accepted from props into the editor. Diverging + // latestMarkdownRef means an in-progress local draft — do not clobber it + // when sync / another notes surface publishes a newer committed snapshot. + const syncedPropValueRef = useRef(value); + const containerRef = useRef(null); const lastLinkActivationRef = useRef<{ href: string; at: number } | null>(null); const [hostPicker, setHostPicker] = useState({ @@ -530,7 +535,15 @@ export const InlineMarkdownEditor = React.memo(function InlineMarkdownEditor({ }, [hostCandidates, hostPicker.query]); useEffect(() => { - if (latestMarkdownRef.current === value) return; + if (latestMarkdownRef.current === value) { + syncedPropValueRef.current = value; + return; + } + if (latestMarkdownRef.current !== syncedPropValueRef.current) { + // Local draft is ahead of the last applied prop; keep Lexical content. + return; + } + syncedPropValueRef.current = value; latestMarkdownRef.current = value; editorRef.current?.setMarkdown(value); }, [value]); diff --git a/components/notes/NotesManager.perf.test.ts b/components/notes/NotesManager.perf.test.ts index afa0a97bd..5723a98b7 100644 --- a/components/notes/NotesManager.perf.test.ts +++ b/components/notes/NotesManager.perf.test.ts @@ -68,6 +68,14 @@ test("notes manager prefetches the MDXEditor chunk when becoming active", () => ); }); +test("mode toggle flushes ref-only content drafts before remounting the editor", () => { + assert.match( + managerSource, + /flushNoteDraft\(\);\s*\n\s*setNoteEditorMode/, + "preview/edit remount must see the in-progress body", + ); +}); + test("host-link annotation does not re-run on every markdown value keystroke", () => { const editorSource = readFileSync( new URL("./InlineMarkdownEditor.tsx", import.meta.url), @@ -79,4 +87,13 @@ test("host-link annotation does not re-run on every markdown value keystroke", ( "value in annotateHostLinks effect deps walks the DOM on every keystroke", ); assert.match(editorSource, /\[annotateHostLinks, editorMode\]/); + assert.match( + editorSource, + /syncedPropValueRef/, + "external note publishes must not clobber an in-progress local draft", + ); + assert.match( + editorSource, + /latestMarkdownRef\.current !== syncedPropValueRef\.current/, + ); }); diff --git a/components/notes/NotesManager.tsx b/components/notes/NotesManager.tsx index df4cef856..08fa0757e 100644 --- a/components/notes/NotesManager.tsx +++ b/components/notes/NotesManager.tsx @@ -679,9 +679,14 @@ export const NotesManager: React.FC = ({ data-note-mode-switch aria-label={label} className="app-no-drag h-8 w-8 shrink-0 rounded-md p-0 text-muted-foreground transition-colors hover:bg-secondary/70 hover:text-foreground" - onClick={() => setNoteEditorMode((currentMode) => ( - currentMode === "edit" ? "preview" : "edit" - ))} + onClick={() => { + // Flush ref-only content drafts before remounting MDX on mode + // toggle so preview/edit sees the in-progress body. + flushNoteDraft(); + setNoteEditorMode((currentMode) => ( + currentMode === "edit" ? "preview" : "edit" + )); + }} >