Skip to content

[lexical-yjs] Bug Fix: Write a collab v2 slot edit to Yjs after a remote change next to its host - #9376

Closed
zurfyx wants to merge 1 commit into
mainfrom
zurfyx/collab-v2-slot-sync
Closed

zurfyx wants to merge 1 commit into
mainfrom
zurfyx/collab-v2-slot-sync

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Description

With collab v2, after a peer inserts a block right before or after a host
node that has named slots (or removes the host's neighbour), the receiving
peer's next edit inside one of that host's slots is never written to its
Yjs document. The edit shows locally but never reaches the other peers, and
is lost if the editor is closed before another edit in the same slot
re-sends the full text. No concurrency is needed.

Applying the remote change clones the host on the receiving peer (its
__prev/__next change), so the host no longer passes the identity check in
$updateYFragment and falls back to the structural $equalYTypePNode. For
the children channel that comparison is deep, but $equalSlots only checked
that the slot's shared type maps to the slot root node. Typing in a slot's
paragraph clones the text node, not the slot root, so the slot compared equal,
the host's mapping was repointed, and the dirty slot was never visited.

$equalSlots now compares each slot like the children channel: the slot node
must map to that shared type and $equalYTypePNode must report its subtree
equal. A false negative only costs a recursion into the host, which then
diffs its slots in place through $updateSlotsYType.

Test plan

New packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts wires
two v2 peers like useYjsCollaborationV2 (observer for remote changes,
update listener for local ones) over a synchronous in-memory transport. Peer
A changes a sibling of a slotted host, then peer B types one character in the
host's slot; the test checks B's own Yjs document and A's editor. A control
case edits the slot without changing the host's siblings.

Before

$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts
 ❯ |unit| packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts (4 tests | 3 failed) 47ms
     × a slot edit after a remote change of a paragraph inserted right before the host is written to yjs 30ms
     × a slot edit after a remote change of a paragraph inserted right after the host is written to yjs 5ms
     × a slot edit after a remote change of the host's previous sibling removed is written to yjs 5ms
     ✓ a slot edit with an unchanged host sibling list is written to yjs (control) 5ms

AssertionError: expected 'cap text' to be 'Ycap text' // Object.is equality
Expected: "Ycap text"
Received: "cap text"
 ❯ packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts:270:37
    268|       expect(readCaption(b.editor)).toBe('Ycap text');
    269|       // Written to B's own yjs doc...
    270|       expect(yjsCaption(b.binding)).toBe('Ycap text');

 Test Files  1 failed (1)
      Tests  3 failed | 1 passed (4)

After

$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts
 ✓ |unit| packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.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)

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

…ote change next to its host

## Description

With collab v2, after a peer inserts a block right before or after a host
node that has named slots (or removes the host's neighbour), the receiving
peer's next edit inside one of that host's slots is never written to its
Yjs document. The edit shows locally but never reaches the other peers, and
is lost if the editor is closed before another edit in the same slot
re-sends the full text. No concurrency is needed.

Applying the remote change clones the host on the receiving peer (its
`__prev`/`__next` change), so the host no longer passes the identity check in
`$updateYFragment` and falls back to the structural `$equalYTypePNode`. For
the children channel that comparison is deep, but `$equalSlots` only checked
that the slot's shared type maps to the slot root node. Typing in a slot's
paragraph clones the text node, not the slot root, so the slot compared equal,
the host's mapping was repointed, and the dirty slot was never visited.

`$equalSlots` now compares each slot like the children channel: the slot node
must map to that shared type and `$equalYTypePNode` must report its subtree
equal. A false negative only costs a recursion into the host, which then
diffs its slots in place through `$updateSlotsYType`.

## Test plan

New `packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts` wires
two v2 peers like `useYjsCollaborationV2` (observer for remote changes,
update listener for local ones) over a synchronous in-memory transport. Peer
A changes a sibling of a slotted host, then peer B types one character in the
host's slot; the test checks B's own Yjs document and A's editor. A control
case edits the slot without changing the host's siblings.

### Before

```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts
 ❯ |unit| packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts (4 tests | 3 failed) 47ms
     × a slot edit after a remote change of a paragraph inserted right before the host is written to yjs 30ms
     × a slot edit after a remote change of a paragraph inserted right after the host is written to yjs 5ms
     × a slot edit after a remote change of the host's previous sibling removed is written to yjs 5ms
     ✓ a slot edit with an unchanged host sibling list is written to yjs (control) 5ms

AssertionError: expected 'cap text' to be 'Ycap text' // Object.is equality
Expected: "Ycap text"
Received: "cap text"
 ❯ packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts:270:37
    268|       expect(readCaption(b.editor)).toBe('Ycap text');
    269|       // Written to B's own yjs doc...
    270|       expect(yjsCaption(b.binding)).toBe('Ycap text');

 Test Files  1 failed (1)
      Tests  3 failed | 1 passed (4)
```

### After

```
$ npx vitest --project unit --no-watch packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.test.ts
 ✓ |unit| packages/lexical-yjs/src/__tests__/unit/SlotSyncV2Peers.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)

```

`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.
@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026

Copy link
Copy Markdown
lexical

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

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/yjs

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

commit: 9a772cf

@zurfyx zurfyx closed this Oct 23, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 9a772cfa Deployed Oct 22, 2026 by vercel[bot]
Preview – lexical-playground — 9a772cfa 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