fix(collaborative-prosemirror): never throw restoring the selection on a remote update - #1048
Open
mmamedel wants to merge 1 commit into
Open
Conversation
…n a remote update
ProseMirrorFacade.set() maps the previous selection through the applied
transaction and reinstates it with TextSelection.create(), which throws
("TextSelection endpoint not pointing into a node with inline content") when
the mapped position no longer points into inline content — e.g. when a remote
update removes the trailing blocks that held the local caret.
Because set() also runs inside PeritextBinding.bind()'s initial syncFromModel(),
that exception aborts the entire binding: the editor stays permanently unwired
and every subsequent local edit is silently lost (nothing throws afterwards).
Use TextSelection.between(), which clamps to the nearest valid text selection
instead of throwing. The same hazard exists in setSelection() (remote
presence/CRDT-space selections around block boundaries) — guarded identically.
The new spec reproduces the exact production failure: a caret in a trailing
paragraph + a remote set() that removes it fails with the quoted error before
this change and passes after; a second case pins that in-range selections are
still restored exactly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ProseMirrorFacade.set()maps the previous selection through the applied transaction and reinstates it withTextSelection.create():When a remote update changes the document such that the mapped position no longer points into inline content — e.g. it removes the trailing blocks that held the local caret —
TextSelection.create()throws:The consequence is worse than a caret glitch:
set()also runs insidePeritextBinding.bind()'s initialsyncFromModel(), so the exception aborts the entire binding. The editor stays permanently unwired, and every subsequent local edit is applied to ProseMirror only and silently lost — nothing throws afterwards, which makes it very hard to diagnose.We hit this in production in a collaborative screenplay editor built on these packages: a client resuming a session (server snapshot replacing the body while the caret sat in later content) lost all subsequent typing on reload.
Fix
Use
TextSelection.between(), ProseMirror's non-throwing constructor, which clamps to the nearest valid text selection. The same hazard exists insetSelection()(a CRDT-space selection can resolve around block boundaries mid-sync) — guarded identically.Tests
New spec
ProseMirrorFacade.selection-restore.spec.ts:set()that removes it) — fails with the quoted error before this change, passes after;All 13 existing
collaborative-prosemirrorsuites pass unchanged (719 tests).