Skip to content

[lexical-yjs] Bug Fix: Stop collab v1 from wiping a paragraph when a peer removes the text node being typed in - #9378

Closed
zurfyx wants to merge 1 commit into
mainfrom
zurfyx/collab-v1-orphaned-text
Closed

zurfyx wants to merge 1 commit into
mainfrom
zurfyx/collab-v1-orphaned-text

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

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.

@vercel

vercel Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Oct 22, 2026 2:15pm UTC
lexical-playground Ready Ready Preview Oct 22, 2026 2:15pm UTC

Request Review

…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
zurfyx force-pushed the zurfyx/collab-v1-orphaned-text branch from 8c05911 to 9c03358 Compare October 22, 2026 14:14
@zurfyx zurfyx changed the title [lexical-yjs][lexical-react] Bug Fix: Stop collab v1 from wiping a paragraph when a peer removes the text node being typed in [lexical-yjs] Bug Fix: Stop collab v1 from wiping a paragraph when a peer removes the text node being typed in Oct 22, 2026
@zurfyx
zurfyx marked this pull request as ready for review October 22, 2026 14:14
@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9378

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9378

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9378

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9378

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9378

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9378

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9378

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9378

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9378

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9378

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9378

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9378

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9378

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9378

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9378

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9378

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9378

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9378

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9378

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9378

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9378

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9378

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9378

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9378

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9378

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9378

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9378

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9378

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9378

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9378

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9378

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9378

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9378

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9378

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9378

commit: 9c03358

@zurfyx zurfyx closed this Oct 23, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 9c03358f Deployed Oct 22, 2026 by vercel[bot]
Preview – lexical-playground — 9c03358f Deployed Oct 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants