Preserve undo history when applying note edits

Merged
CO
2mo ago
+147-11

Apply external content through editor transactions as separate undo steps, avoid redundant saves, and cover undo and redo across replacements.


[!NOTE]
Medium Risk
Changes how external content merges with local edits, undo history, and what triggers persistence—core note editor behavior, though scoped and covered by new tests.

Overview
External initialContent updates no longer reset the ProseMirror editor with updateState (editable mode). They apply via a replaceWith transaction wrapped in closeHistory, tagged with externalContentSync, so remote/collaborative replacements become their own undo step while prior local edits stay on the stack.

docChangeListenerPlugin now treats those sync transactions separately: they fire onDocumentChange (normalized doc, e.g. autolinks) but not the debounced handleChange persist path, including when input rules append follow-up transactions. The first real user edit after a sync calls closeHistory so undo grouping stays sensible.

The note editor also skips redundant sync when stored and incoming JSON are equivalent (reference or deep equality), and read-only mode still uses updateState. New tests cover undo/redo across external edits and sync-without-save behavior.

Reviewed by Cursor Bugbot for commit 9e5381f5af6d1322daf302b593bea9b048632a88. Bugbot is set up for automated code reviews on this repo. Configure here.