Repository navigation
fix(core): the sync binding and the document diff pair blocks by their ID - #3380
Merged
Merged
Conversation
`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.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
|
Caution Review failedThe pull request is closed.
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. |
@blocknote/ariakit
@blocknote/code-block
@blocknote/core
@blocknote/diagram-block
@blocknote/mantine
@blocknote/math-block
@blocknote/react
@blocknote/server-util
@blocknote/shadcn
@blocknote/xl-ai
@blocknote/xl-docx-exporter
@blocknote/xl-email-exporter
@blocknote/xl-multi-column
@blocknote/xl-odt-exporter
@blocknote/xl-pdf-exporter
@blocknote/xl-typst-exporter
commit: |
…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).
|
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
approved these changes
Nov 26, 2026
nperez0111
marked this pull request as ready for review
November 26, 2026 14:00
Contributor
|
Makes sense to me block IDs should be canonically different blocks |
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
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.
Summary
blockMatchNodes(@blocknote/core/y) now pairs two block containers only if they have the same block ID. The sync binding,docDiffToDeltaand 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
blockMatchNodescompares theidattribute of two block containers. A container with a different ID is a different block, also if its content is the same.main.docDiffToDeltausesblockMatchNodes.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
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.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 onmain. 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.
mainhas 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
mainit is a change of theidattribute in place. With this pull request a block with a new ID is a new block, so it is a removal and an insert: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 ofdocDiffToDelta. A block with a different ID is a removed block and an inserted block, also with the same text.versionDiffAttribution.browser.test.tsand the snapshot "versioning diff: Heading vs list item": the two copies show without their authors (the known limit).Additional Notes
getTableDimensionsinblockMatchNodes.tsreadstd.attrs.colspanas 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.docDiffToDeltato write a change to a Y type.