Repository navigation
[lexical-yjs] Bug Fix: Local range selection grows when collaborator … - #8611
Conversation
…types at right edge (meta#8608)
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
left a comment
There was a problem hiding this comment.
The unit tests fail and the prettier check is also failing pnpm run ci-check and pnpm run test-unit can be used locally to reproduce these failures.
|
Hi @etrepum, While reproducing the unit test failure, I found that the original regression assertion (checking the local selection text after a remote update) doesn't appear to be stable in the shared collaboration test harness, since the local selection state doesn't reliably survive the round-trip in that environment. I have an alternative regression test that validates the underlying invariant directly instead:
The test still runs through both V1 and V2 binding paths and is specifically validating the behavior fixed by this change. Would you prefer that approach, or would you rather keep the regression test focused on observable selection behavior? Happy to implement either approach, I just wanted to confirm before making assumptions about the preferred testing strategy here. |
|
Review (Navi — Sherry's AI assistant) The core logic in this PR is sound and well-reasoned: ✅ What I verified:
Assessment: The production code is correct and safe. Waiting on test fix (author discussing approach with maintainer). NOT ready to approve until CI green. |
|
Writing an e2e test instead of a unit test would be the typical approach when a real browser environment is better |
|
Hi again @etrepum, I investigated an e2e approach and put together a prototype in After running it both with and without the fix applied, the test produced the same result in both cases, so it wasn't able to distinguish the broken behavior from the fixed behavior. I added some diagnostics while tracing through the synchronization path and confirmed that the local selection state changes during the collaboration update in a way that causes both code paths to converge on the same observable result in this test setup. As a result, I wasn't able to construct a reliable e2e regression test for this issue using the current collaboration harness. The unit test, on the other hand, consistently fails without the fix and passes with it because it asserts the underlying Yjs relative position invariant directly rather than relying on browser level selection behavior. Before making further changes, I wanted to check what direction you'd prefer here:
Happy to continue in whichever direction you think is most appropriate. |
|
Any approach that you can write a passing test for, that fails without the patch, is fine so long as it doesn't cause other regressions (unless those regressions are test expectations that specifically look for the behavior that this is changing, in which case the expectations should change). |
…types at right edge (meta#8608) Regression test rewritten to assert the Yjs relative-position invariant directly (focusPos.assoc === -1, resolved index stable after remote insert) instead of relying on selection state surviving a jsdom round-trip.
6f36b8c to
fc35fe8
Compare
|
Thanks for clarifying. I've updated the regression test to assert the underlying relative-position invariant directly. I verified that the test fails without the patch and passes with the patch for both the V1 and V2 collaboration bindings. I've also rerun The updated test and fix have now been pushed. |
etrepum
left a comment
There was a problem hiding this comment.
Looks good, confirmed with manual two-browser QA that it behaves correctly in a real situation. Refactored the tests a little bit to use RangeSelection APIs for selection/input which is a bit closer to how the editors work with real input.
potatowagon
left a comment
There was a problem hiding this comment.
Reviewed by Navi (Tater Thoughts Bobblehead) on behalf of @potatowagon.
LGTM ✅ — Solid fix for collaborative selection growth bug (#8608).
What this does: Fixes a bug where a local user's range selection would incorrectly grow to include text typed by a remote collaborator at the selection boundary. The fix passes assoc (association) parameters to Yjs createRelativePositionFromTypeIndex so that the right endpoint of a selection sticks to the last selected character (assoc=-1) rather than drifting rightward when remote inserts land at the boundary.
What I checked:
- ✅ Logic correctness: The assoc assignment is correct — for a forward selection (non-backward), the focus is the right edge and needs assoc=-1 (left-sticky), while the anchor is the left edge and keeps assoc=0 (right-sticky). For backward selections, the roles reverse correctly. Collapsed carets keep assoc=0 on both sides (preserving existing typing-follows-caret behavior).
- ✅ Both V1 and V2 bindings: The fix is applied symmetrically to
createRelativePosition(V1) andcreateRelativePositionV2(V2). - ✅ Flow types updated:
libdefs/yjs.jscorrectly adds the optionalassoc?: numberparameter to match yjs's actual API signature. - ✅ Comprehensive test: The new test (95 lines) validates that after a remote insert at the right edge of a selection, the relative position resolves to the same index as before — with clear documentation explaining why the test asserts on RelativePosition rather than $getSelection() (shared global document selection limitation of the test harness).
- ✅ www compat: No breaking changes to exports or public API. The
assocparameter is purely internal to SyncCursors. Meta's internal www codebase consuming lexical-yjs won't be affected. - ✅ CI: All checks green (CLA, core-tests, e2e-tests, Vercel).
Safe to land. Clean, well-tested fix for a real collaborative editing UX bug.
Description
Current behavior:
When two users edit the same document via
@lexical/yjs, a remote user typing immediately after the right edge of a local user's range selection causes that selection to silently expand to include the newly typed characters. This also means the expanded text lands in the clipboard if the local user presses Ctrl+C — a silent content leak.Root cause:
createRelativePositionandcreateRelativePositionV2inSyncCursors.tscallY.createRelativePositionFromTypeIndexwithout passing the thirdassocargument, so it defaults to0(right-associated) for both endpoints. A right-associated position at the end of anXmlTextresolves totype._lengthafter every insert — drifting the right edge of the selection rightward with each new character typed by a remote peer.Changes introduced by this PR:
For a non-collapsed range selection, each endpoint now receives the correct associativity:
assoc = 0): anchors to the first selected character — insertions before the selection stay outside.assoc = -1): anchors to the last selected character — insertions after the selection stay outside.assoc = 0on both sides (unchanged) so the caret follows typing normally.The
assocparameter is threaded throughcreateRelativePosition(V1) and both return sites ofcreateRelativePositionV2(text path and element path). The Flow libdef forcreateRelativePositionFromTypeIndexis updated to accept the optional third argument. No protocol or schema change —RelativePosition.associs already encoded in the Yjs wire format; older peers ignore the field.Closes #8608
Test plan
Before
Steps to reproduce (documented in #8608):
After
Regression test added in
packages/lexical-react/src/__tests__/unit/Collaboration.test.tsinside the existingdescribe.each([[false], [true]])block, covering both V1 and V2 binding paths:"foo bar"and sets a forward range selection over it"ZZZ"at the right edge of that selectionsel.getTextContent() === 'foo bar'on client 1 after the Yjs update propagatesRun with: