Add resetOnChange option to collapseUnchanged #9
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "yurks/collapse-reset-on-change"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
When
collapseUnchangedis enabled and the document is replaced wholesale — for example via:the merge view shows broken layout: collapsed regions appear in the wrong places, spacers misalign, and the diff becomes unreadable.
This happens because the collapsed-unchanged state field only maps existing collapse decorations through document changes. It never rebuilds them from the current diff. After a full document replacement, the old collapse ranges are mapped across a massive change and end up stale and misplaced.
A workaround exists — calling
mergeView.reconfigure({ collapseUnchanged: { margin: 3, minSize: 6 } })alongside the content update — but that forces consumers to know about an internal implementation detail.Why this matters
This pattern is extremely common when using
@codemirror/mergewith reactive frameworks like React or Vue:doc={sideA}/:doc="sideA")collapseUnchangedenabled, every such update produces a broken view unless the consumer also callsreconfigureAny integration that treats the merge view as a controlled component and pushes new document values from outside will hit this.
Solution
Add an opt-in
resetOnChangeflag to the existingcollapseUnchangedconfig:When
resetOnChange: true, the collapsed ranges are rebuilt from scratch whenever the diff changes (detected viaChunkFieldupdate, which fires on both editors). Whenfalse(the default), behavior is unchanged — collapsed sections and manual expansions are preserved across incremental edits.Implementation details:
StateFieldis now created insidecollapseUnchanged()so it can close overmargin,minSize, andresetOnChange. This replaces the previous global field +.init()pattern and gives each merge view its own field instance (no cross-view interference).MergeConfig(split view) andUnifiedMergeConfig(unified view).Trade-off
With
resetOnChange: true, manually expanded collapsed sections are re-collapsed on the next content change. This is intentional and documented: the flag is meant for controlled/reactive use cases where content is replaced externally, not for interactive editing where the user expands sections and then types.Add resetOnChange option to collapseUnchangedto AddresetOnChangeoption tocollapseUnchangedPlease don't send me AI slop. Both your diagnosis and your patch are absolutely useless. Dumping that on me is just rude.
But I guess there's a bug report somewhere hidden in there. Attached patch should make the library handle this better.
ai slop, useless, rude.. are you in the god mode, dude? In case it's all useless for you, isn't it time to retire?
On subj, isn't your fix a breaking change for in-place diff editing cases? That was only the reason for resetOnChange option instead of just fixing that.
Pull request closed