Skip to content

fix(core): the sync binding and the document diff pair blocks by their ID - #3380

Merged
nperez0111 merged 10 commits into
mainfrom
fix/y-doc-diff-by-block-id
Nov 26, 2026
Merged

nperez0111 merged 10 commits into
mainfrom
fix/y-doc-diff-by-block-id

Conversation

@YousefED

@YousefED YousefED commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

blockMatchNodes (@blocknote/core/y) now pairs two block containers only if they have the same block ID. The sync binding, docDiffToDelta and the snapshot preview use this rule.

Rationale

The rule paired the changed nodes of a list by their type only. With an insert or a removal next to a changed block of the same type, a diff paired a block with its neighbour.

An example with the blocks a, b and c. One user inserts a paragraph before b and changes the text of b, in one transaction. At the same time, another user types "!" in b. The binding changed b into the new paragraph and inserted b again. After the sync, the "!" was in the new paragraph.

Changes

  • blockMatchNodes compares the id attribute of two block containers. A container with a different ID is a different block, also if its content is the same.
  • A layout block (a block that is its own node, for example a column) pairs by its type, as on main.
  • docDiffToDelta uses blockMatchNodes.

An earlier version of this pull request also paired two containers with different IDs if their content was the same (a compare of the content fingerprint). The last commit removes that compare.

Impact

  • A block with a new ID is a new block. If an application replaces a block with a new block (replaceBlocks), text that another user types in the old block at that moment is lost. A replace of the full document with a new parse loses all text that is typed at that moment. Typing and a paste keep the ID of a block.
  • The version diff shows a block with a different ID as a deleted block and an inserted block. Six browser snapshots show this for the removal of the only block and of a block with children.
  • Time. A replace of all blocks with new IDs compares each removed block with each inserted block. In a bound editor with 2,000 blocks: 670 ms with new text (main: 670 to 695 ms), 635 ms with the same text (main: 500 to 520 ms). The diff alone with 5,000 blocks: 805 ms (main: 205 ms). A change of one block in a large document takes the same time as on main. A repair belongs in the binding: it can find a full replace and not compare block by block.

Known limit: a document that has two blocks with one ID

This limit is accepted. The document already has a duplicate of a block, and this pull request can make that situation worse.

Two blocks with one ID exist after two users do one of these to the same block at the same time, or offline: a type change, a far move, an indent. The binding writes each of these as a removal and an insert, so each user inserts a copy. main has this too.

An editor that loads such a document gives the copies new IDs (the unique ID extension does this, also in a read-only editor), and the binding writes that change. On main it is a change of the id attribute in place. With this pull request a block with a new ID is a new block, so it is a removal and an insert:

  • Text that another user types in a copy at the time of a load is lost.
  • Two clients that load at the same time each add their own copies: 5 blocks in place of 3.
  • A client that starts from an older local copy of the document (for example in IndexedDB) adds its own copies too, at any later time.
  • The version view shows the copies with no author.

The repair of the cause is out of scope: a type change and a move that do not make a second copy. A smaller repair, not built: the unique ID extension does not rename content that the binding renders. The double ID then stays, as in the editors that are open today.

Testing

  • y/extensions/blockMatchNodes.browser.test.ts: 14 tests with live editors and a second user. Three of them state the known limit: the number of blocks after two loads (5, and 8 for a move), and the lost text.
  • y/utils.test.ts: tests of docDiffToDelta. A block with a different ID is a removed block and an inserted block, also with the same text.
  • versionDiffAttribution.browser.test.ts and the snapshot "versioning diff: Heading vs list item": the two copies show without their authors (the known limit).
  • The Node tests and the browser tests of the changed files pass in Chromium, Firefox and WebKit.

Additional Notes

  • More cases are examined (a block and a layout block with a new ID, with a second user). Their tests are not in this pull request yet.
  • getTableDimensions in blockMatchNodes.ts reads td.attrs.colspan as a number. In a delta that value is an operation object, so the result is always 1. That code path is switched off, and this pull request does not change it.
  • The text lens (a later pull request) uses docDiffToDelta to write a change to a Y type.

`docDiffToDelta` paired the changed nodes of a list by their type only
(`blockMatchNodes`). With an insert or a removal next to a changed block of
the same type, the diff then paired a block with its neighbour: it rewrote
one block into the other, and inserted or removed the wrong block. When such
a diff applies to a Y type, a concurrent change of another client to the
block lands in the wrong block.

`docDiffToDelta` now uses `blockMatchNodesById`: the rule of
`blockMatchNodes`, and the two nodes must have the same block ID. The two
documents of a diff are versions of one document, thus a block keeps its ID.
A block with a different ID is a removed block and an inserted block, also
if its content is the same. This also applies to the version diff of
`DiffVersioningExtension`.

The sync binding and the snapshot preview still use `blockMatchNodes`. The
binding has the same fault. A TODO at `blockMatchNodesById` says why the
check is not in `blockMatchNodes` yet: with the check in the binding, a
version diff loses the authors of two copies of a block that two users
changed the type of at the same time, and the cause is not fully known.

Tests:
- New in `utils.test.ts`: three tests of `docDiffToDelta` (an insert and a
  removal next to a changed block, and a block with a different ID).
- `DiffVersioningExtension.test.ts`: the fixture gives the two versions of
  a block one ID (before, two editors gave them two IDs). New: a block with
  a different ID shows as a deleted block and an inserted block.
The block ID check is now in `blockMatchNodes` itself. Thus, the sync
binding (`customCompare`), `docDiffToDelta` and the snapshot preview
(`splitChangedBlocks`) all pair two blocks only if they have the same block
ID. `blockMatchNodesById` goes.

Before, the binding paired the changed blocks of a list by their type only.
With a transaction that inserts or removes a block next to a changed block,
or that changes the type of a block next to a changed block, a block of the
Y.Doc got the content of its neighbour. Text that another user typed in the
block at the same time went into a different block, or was lost.

Tests (new file `blockMatchNodes.test.ts`, with live editors):
- 8 cases of a transaction of one user while another user types. Four of
  them fail without the block ID.
- A document with two blocks with one ID (two users changed the type of a
  block, or moved a block, at the same time): the open editors keep the two
  copies, and one new client that loads the document keeps the number of
  blocks.
- Three open faults, as `it.fails`: an editor that loads such a document
  gives the copies new IDs, and the binding now writes that as a removal
  and an insert of each copy. Two clients that load at the same time then
  add copies (5 blocks in place of 3, 8 in place of 6), and text that a user
  types in a copy at that time is lost.

Existing tests that fail with this commit:
- `versionDiffAttribution.test.ts`, "shows both copies when two users
  change the type concurrently": the version view loses the authors of the
  inserted copies. The cause is the same change of IDs, in the preview.
- `v1BindingSnapshotDiff.test.ts`, "diffs two v1-shaped snapshots through
  the new binding": its fixture gives the block a new ID (the next commit).
`buildV1Snapshots` made the second version with `replaceBlocks`, which
gives the block a new ID in the same Y container. With the block ID in
`blockMatchNodes`, `splitChangedBlocks` then sees a different block, and
the diff shows the old block and the new block ("Version 1Version 2").

The test is for the diff of the text in one block ("Version 12"). Thus, the
fixture now changes the block with `updateBlock`, which keeps its ID. The
assertions did not change.

A real document of the old binding can have a block ID that changed in the
same Y container. The version diff shows such a block as a removed block and
an inserted block now.
With the block ID in `blockMatchNodes`, a block with a new ID was a
different block. That is wrong for one case: a change of the ID in place.

A document can have two blocks with one ID: two users changed the type of
a block, or moved a block, at the same time. An editor that loads such a
document gives the copies new IDs (the unique ID extension), and the binding
writes that change. As a removal and an insert of each copy:
- two clients that load the document at the same time each added their own
  copies (5 blocks in place of 3, 8 in place of 6),
- text that a user typed in a copy at that time was lost,
- the version view lost the authors of the copies ("shows both copies when
  two users change the type concurrently").

Now two nodes also pair if their block IDs are their only difference
(`contentWithoutBlockIds`: the node as a string with sorted keys, without
the `id` attributes). The binding then changes the `id` attribute in place,
as before the block ID check.

A consequence: two different blocks with the same content pair too, for
example a removed empty paragraph and a new empty paragraph in one run of
changes. The binding then gives the old block the new ID.

Tests:
- `blockMatchNodes.test.ts`: the three `it.fails` tests of the commit
  before this one are normal tests now.
- `versionDiffAttribution.test.ts`: "shows both copies when two users
  change the type concurrently" passes again, without a change.
- `utils.test.ts`: "replaces a block with a different ID, also if the text
  is the same" asserted the opposite rule. It is now two tests: a block
  with a different ID and a different text is replaced, and a block whose
  only change is its ID changes in place.

All tests of core pass: 116 files, 1,623 tests, 20 expected failures.
@vercel

vercel Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
blocknote Error Error Nov 26, 2026 12:34pm UTC
blocknote-website Error Error Nov 26, 2026 12:34pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@blocknote/ariakit

npm i https://pkg.pr.new/@blocknote/ariakit@3380

@blocknote/code-block

npm i https://pkg.pr.new/@blocknote/code-block@3380

@blocknote/core

npm i https://pkg.pr.new/@blocknote/core@3380

@blocknote/diagram-block

npm i https://pkg.pr.new/@blocknote/diagram-block@3380

@blocknote/mantine

npm i https://pkg.pr.new/@blocknote/mantine@3380

@blocknote/math-block

npm i https://pkg.pr.new/@blocknote/math-block@3380

@blocknote/react

npm i https://pkg.pr.new/@blocknote/react@3380

@blocknote/server-util

npm i https://pkg.pr.new/@blocknote/server-util@3380

@blocknote/shadcn

npm i https://pkg.pr.new/@blocknote/shadcn@3380

@blocknote/xl-ai

npm i https://pkg.pr.new/@blocknote/xl-ai@3380

@blocknote/xl-docx-exporter

npm i https://pkg.pr.new/@blocknote/xl-docx-exporter@3380

@blocknote/xl-email-exporter

npm i https://pkg.pr.new/@blocknote/xl-email-exporter@3380

@blocknote/xl-multi-column

npm i https://pkg.pr.new/@blocknote/xl-multi-column@3380

@blocknote/xl-odt-exporter

npm i https://pkg.pr.new/@blocknote/xl-odt-exporter@3380

@blocknote/xl-pdf-exporter

npm i https://pkg.pr.new/@blocknote/xl-pdf-exporter@3380

@blocknote/xl-typst-exporter

npm i https://pkg.pr.new/@blocknote/xl-typst-exporter@3380

commit: 1b10647

…k content

`blockMatchNodes` pairs two blocks with different block IDs if only the ID
changed. The rule of the commit before this one found that with a compare
of the two nodes as strings: the JSON of each delta, with sorted keys and
without the `id` attributes, and a cache for these strings.

The rule is now one condition. Two block containers with different IDs pair
if the block content node is the same (its type, its props and its text):
`firstChild(a).fingerprint === firstChild(b).fingerprint`. lib0 computes
and keeps the fingerprint of each delta, and it sorts the attributes
itself. Thus `contentWithoutBlockIds`, `sortedWithoutBlockIds` and the
cache go. The rule file has 123 code lines (before: 154, main: 113).

Two differences in behavior:
- The blocks in a block are not in the compare. A block with a new ID, the
  same content and different child blocks pairs now. The diff goes into
  the block and pairs each child block with the same rule.
- The ID is compared only for block containers. A layout block (a block
  that is its own node, for example a column) pairs by its type, as on
  main.

Time: a replace of all 800 blocks of a document in a bound editor took 393
and 422 ms (two runs, jsdom), against 344 and 354 ms with the string
compare, and 295 and 307 ms on main. For each pair of nodes, the rule reads
the first child of the two nodes, and the string compare had its cache.

Tests:
- New in `utils.test.ts`: "changes the ID of a block in place, also if the
  blocks in it are different". It fails with the string compare.
- No other test changes. The 14 tests of `blockMatchNodes.test.ts` and the
  version view test pass.
- All tests of core pass: 116 files, 1,624 tests, 20 expected failures.
  Lint with types for `packages/core`: no findings.
Main reorganized the tests. Three test files of this branch had a conflict:

- DiffVersioningExtension.test.ts and utils.test.ts: the cases of this
  branch are now in the test style of main (the global `it` with the
  `setupEditor` fixture).
- v1BindingSnapshotDiff.test.ts: main moved the fixture of this test to
  v1BindingSnapshotDiff.fixture.ts. The change of this branch (the fixture
  keeps the block ID for an edit) is now in that file.

blockMatchNodes.test.ts still has the old test style. The next commit
changes it.
The tests need a DOM, thus the file is a browser test now
(blockMatchNodes.browser.test.ts). The editors come from the `setupEditor`
fixture, and each test destroys the Y.Docs that it makes. The 14 cases and
their assertions do not change.
The sync binding and the document diff pair blocks by their ID. When a
user removes the only block or a parent block, the editor makes a new
empty block with a new ID. The diff now shows the old block as deleted and
the new block as inserted. Before, it showed one block with a changed ID
and deleted text.

This changes 6 tests: 3 in addRemoveBlocks (the document of the editor in
suggestion mode) and 3 in versioning (the list of changes).
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
PR Preview Action v1.8.2
Preview removed because the pull request was closed.
2026-11-26 14:01 UTC

A block container with a different ID is a different block, also if its
content is the same. The compare of the content fingerprint is removed.

The tests do not follow this change yet. The tests that expect the old
rule fail: a change of the ID in place, and the load of a document that
has two blocks with one ID.
A block with a different ID is a different block, also with the same
content. Seven tests expected the earlier rule:

- y/utils.test.ts: a block with a different ID and the same text is a
  removed block and an inserted block, together with the blocks in it.
- blockMatchNodes.browser.test.ts, a document with two blocks with one ID
  (a known limit): two clients that load at the same time each add their
  own copies (5 blocks, and 8), and text that a user types in a copy at the
  time of a load is lost.
- versionDiffAttribution.browser.test.ts and the snapshot "versioning diff:
  Heading vs list item" (the same known limit): the version view shows the
  two copies without their authors.
@nperez0111
nperez0111 marked this pull request as ready for review November 26, 2026 14:00
@nperez0111

Copy link
Copy Markdown
Contributor

Makes sense to me block IDs should be canonically different blocks

@nperez0111
nperez0111 merged commit fd12914 into main Nov 26, 2026
35 of 37 checks passed
@nperez0111
nperez0111 deleted the fix/y-doc-diff-by-block-id branch November 26, 2026 14:00
YousefED added a commit that referenced this pull request Dec 3, 2026
main has 91 new commits. Most core conflicts come from core fixes of this
branch that went to main as separate pull requests, often in a changed form.
For these files, the form of main is the reviewed one.

Resolutions:
- y/extensions/blockMatchNodes.ts and its browser test, y/utils.ts and its
  test: the files of main (#3380: blocks pair by ID only).
- api/nodeConversions/blockToNode.ts: main (#3383).
- api/parsers/html/util/nestedLists.ts and its snapshot: main (#3393, #3404).
- blocks/File/block.ts, createLinkWithCaption.ts: main (#3401, #3387).
- blocks/Image, Video, Audio: main (#3400).
- CheckListItem/block.ts: main (#3388). NumberedListItem/block.ts: main
  (#3403).
- api/exporters/markdown/htmlToMarkdown.ts and
  api/parsers/markdown/markdownToHtml.ts: main (#3385, #3392, #3394, #3391
  and later commits). The parts of the branch that main does not have and
  that the lens needs come back in separate commits after this merge.
- markdownExporter.ts, parseMarkdown.ts, ExportManager.ts, copyExtension.ts,
  SideMenu/dragging.ts merged without a conflict, but they used the API of
  the old markdown files of the branch (`escapes`, `target`): main.
- serializeBlocksExternalHTML.ts merged without a conflict, but the merge
  had the toggle child order fix two times and did not compile: the form of
  main (#3388), with the white space placeholder of the branch.
- exportParseEqualityTestInstances.ts: main (the toggle case of #3388).
- vite.config.ts: main, with the lint rules of the lens.
- tests/vite.config.browser.ts: main, with the lens files.
- pnpm-lock.yaml: main, then `vp install`.

main moved the shared test document to tests/src/fixtures/exporters (#3420).
The lens test helper imports it from there, and the xl-ai build does not
include the lens test helpers and benchmarks (a package build cannot read a
file outside the package).

This branch had an error being deployed

2 failed deployments
Preview – blocknote — 1b106478 Deployed Nov 26, 2026 by vercel[bot]
Preview – blocknote-website — 1b106478 Deployed Nov 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants