Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…peer removes the text node being typed in
## Description
In collab v1 a TextNode is a `Y.Map` embed followed by its characters in the
parent's `Y.XmlText`. When one peer removes a TextNode while another peer
types into it, the typed characters survive in Yjs with no embed in front of
them. `CollabElementNode.applyChildrenYjsDelta` handled that "dangling" text
with `this._xmlText.delete(offset, length)`, where `offset` is relative to
the removed text node (0 in that branch) rather than an index into the
parent. The delete therefore started at the beginning of the parent and
removed unrelated siblings, it counted characters that later ops in the same
delta delete anyway, and it was replayed to every peer. For example, if a
peer removes the first text node of `"aaa " "BOLD" " ccc"` while someone types
in `"aaa "`, the whole paragraph is lost everywhere; wrapping the first word
of a paragraph in a comment mark while a peer types at its end has the same
result. The insert path had the same problem for concurrent text that lands
where no text node owns it.
Characters that have no text node are now kept as map-less (`_normalized`)
holder text nodes while the delta is applied, so later delta offsets still
line up (`getPositionFromElementAndOffset` skips the embed for such nodes).
Once the whole delta has been applied, each orphan is either merged into the
text node directly before it, which is where the characters sit in Yjs, or,
if there is none, deleted from the shared type at its absolute offset (only
those characters). The deletion is made with the binding as origin, so it is
not re-applied locally. As before, deletion wins: text typed into a node that
a peer removed may still be dropped, but neighbouring content is never
touched.
## Test plan
New `packages/lexical-yjs/src/__tests__/unit/SyncV1OrphanedText.test.ts`
wires two v1 peers like `useYjsCollaboration` over an in-memory transport that
queues updates while offline, with both Yjs clientID orders. While offline,
one peer types inside the first text node and the other removes it, or one
peer wraps the first word in a MarkNode and the other types at the end. After
reconnecting, both editors and a fresh reload of each peer's Yjs document must
match and keep the rest of the paragraph.
### Before
```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SyncV1OrphanedText.test.ts
❯ |unit| packages/lexical-yjs/src/__tests__/unit/SyncV1OrphanedText.test.ts (4 tests | 4 failed) 70ms
❯ collab v1: orphaned text (clientIDs [1,2]) (2)
× keeps the rest of the paragraph when a peer removes the text node being typed in 38ms
× keeps the paragraph when a peer wraps its first word in a mark while another types at the end 14ms
❯ collab v1: orphaned text (clientIDs [2,1]) (2)
× keeps the rest of the paragraph when a peer removes the text node being typed in 9ms
× keeps the paragraph when a peer wraps its first word in a mark while another types at the end 8ms
AssertionError: expected 'paragraph()' to be 'paragraph("BOLD"{1} " ccc")' // Object.is equality
Expected: "paragraph("BOLD"{1} " ccc")"
Received: "paragraph()"
AssertionError: expected 'paragraph()' to be 'paragraph(mark("Revenue") " grew 4% i…' // Object.is equality
Expected: "paragraph(mark("Revenue") " grew 4% in Q3. more")"
Received: "paragraph()"
AssertionError: expected 'paragraph()' to be 'paragraph(mark("Revenue") " grew 4% i…' // Object.is equality
Expected: "paragraph(mark("Revenue") " grew 4% in Q3.")"
Received: "paragraph()"
Test Files 1 failed (1)
Tests 4 failed (4)
```
### After
```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SyncV1OrphanedText.test.ts
✓ |unit| packages/lexical-yjs/src/__tests__/unit/SyncV1OrphanedText.test.ts (4 tests) 48ms
Test Files 1 passed (1)
Tests 4 passed (4)
$ npx vitest --project unit --no-watch packages/lexical-yjs packages/lexical-react/src/__tests__/unit/Collab* packages/lexical-react/src/__tests__/unit/LexicalCollaborationPlugin.test.tsx
Test Files 17 passed (17)
Tests 174 passed (174)
```
The existing v1 test "Should collaborate basic text deletion conflicts
between two clients" (deletion wins when one client removes all text while
another appends) is unchanged and passes.
`pnpm run test-unit`: 393 files, 9541 tests passed (1 skipped).
`pnpm run tsc`, `pnpm run flow` (0 errors), ESLint and Prettier on the
changed files pass.
Run headless in Node/jsdom with an in-memory transport only; not exercised in a
real browser or over a real provider.
zurfyx
force-pushed
the
zurfyx/collab-v1-orphaned-text
branch
from
October 22, 2026 14:14
8c05911 to
9c03358
Compare
zurfyx
marked this pull request as ready for review
October 22, 2026 14:14
zurfyx
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7 and
potatowagon
as code owners
October 22, 2026 14:14
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/yjs
commit: |
potatowagon
approved these changes
Oct 22, 2026
This branch was successfully deployed
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.
Description
In collab v1 a TextNode is a
Y.Mapembed followed by its characters in theparent's
Y.XmlText. When one peer removes a TextNode while another peertypes into it, the typed characters survive in Yjs with no embed in front of
them.
CollabElementNode.applyChildrenYjsDeltahandled that "dangling" textwith
this._xmlText.delete(offset, length), whereoffsetis relative tothe removed text node (0 in that branch) rather than an index into the
parent. The delete therefore started at the beginning of the parent and
removed unrelated siblings, it counted characters that later ops in the same
delta delete anyway, and it was replayed to every peer. For example, if a
peer removes the first text node of
"aaa " "BOLD" " ccc"while someone typesin
"aaa ", the whole paragraph is lost everywhere; wrapping the first wordof a paragraph in a comment mark while a peer types at its end has the same
result. The insert path had the same problem for concurrent text that lands
where no text node owns it.
Characters that have no text node are now kept as map-less (
_normalized)holder text nodes while the delta is applied, so later delta offsets still
line up (
getPositionFromElementAndOffsetskips the embed for such nodes).Once the whole delta has been applied, each orphan is either merged into the
text node directly before it, which is where the characters sit in Yjs, or,
if there is none, deleted from the shared type at its absolute offset (only
those characters). The deletion is made with the binding as origin, so it is
not re-applied locally. As before, deletion wins: text typed into a node that
a peer removed may still be dropped, but neighbouring content is never
touched.
Test plan
New
packages/lexical-yjs/src/__tests__/unit/SyncV1OrphanedText.test.tswires two v1 peers like
useYjsCollaborationover an in-memory transport thatqueues updates while offline, with both Yjs clientID orders. While offline,
one peer types inside the first text node and the other removes it, or one
peer wraps the first word in a MarkNode and the other types at the end. After
reconnecting, both editors and a fresh reload of each peer's Yjs document must
match and keep the rest of the paragraph.
Before
After
The existing v1 test "Should collaborate basic text deletion conflicts
between two clients" (deletion wins when one client removes all text while
another appends) is unchanged and passes.
pnpm run test-unit: 393 files, 9541 tests passed (1 skipped).pnpm run tsc,pnpm run flow(0 errors), ESLint and Prettier on thechanged files pass.
Run headless in Node/jsdom with an in-memory transport only; not exercised in a
real browser or over a real provider.