Repository navigation
Description
Lexical version: 0.53.0 (@lexical/yjs v1 binding). Also reproduces on 0.53.1-nightly.20261117.0. Yjs 13.6.31.
Changing a block's type with $setBlocksType (which calls replace(newNode, true)) is written to Yjs as "delete the old block, insert a new block with a copy of its children". Because the new block is a copy, two concurrent changes give wrong results:
- Two clients change the same paragraph into a heading at the same time: after sync, the document has two headings with the same words.
- One client types in a paragraph while another client changes it into a heading: after sync, the typed words are lost on every client. They went into the paragraph that the other client deleted.
Both results are in the shared Y.Doc, so they persist and every client converges on them.
Steps To Reproduce
- Two headless editors, each with a v1 binding (
createYjsBinding) and its ownY.Doc. Both start from the same paragraph "Hello world". - Case 1: both clients call
paragraph.replace($createHeadingNode('h1'), true), then exchange updates. - Case 2: client A inserts ", again" at the end of the text; client B calls
paragraph.replace($createHeadingNode('h1'), true); then they exchange updates.
Link to code example (runs with tsx, depends only on Lexical and Yjs):
import {createHeadlessEditor} from '@lexical/headless'
import {$createHeadingNode, HeadingNode} from '@lexical/rich-text'
import {createYjsBinding, syncLexicalUpdateToYjs, syncYjsChangesToLexical, type Provider} from '@lexical/yjs'
import {$createParagraphNode, $createTextNode, $getRoot, $isTextNode} from 'lexical'
import * as Y from 'yjs'
const noop = () => {}
const provider = {
awareness: {getLocalState: () => null, getStates: () => new Map(), setLocalState: noop, setLocalStateField: noop, on: noop, off: noop},
connect: noop, disconnect: noop, on: noop, off: noop,
} as unknown as Provider
function client() {
const doc = new Y.Doc()
const editor = createHeadlessEditor({nodes: [HeadingNode], onError: e => { throw e }})
const binding = createYjsBinding({editor, id: 'root', doc, docMap: new Map([['root', doc]])})
editor.registerUpdateListener(({prevEditorState, editorState, dirtyElements, dirtyLeaves, normalizedNodes, tags}) =>
syncLexicalUpdateToYjs(binding, provider, prevEditorState, editorState, dirtyElements, dirtyLeaves, normalizedNodes, tags))
binding.root.getSharedType().observeDeep((events, tr) => {
if (tr.origin !== binding) syncYjsChangesToLexical(binding, provider, events, false)
})
const update = (fn: () => void) => editor.update(fn, {discrete: true})
const text = () => editor.getEditorState().read(() =>
$getRoot().getChildren().map(n => `${n.getType()}: ${n.getTextContent()}`).join(' | '))
// Remote changes reach the editor in a scheduled update; commit it before reading.
const flush = () => editor.update(() => {}, {discrete: true})
return {doc, update, text, flush}
}
type Client = ReturnType<typeof client>
function exchange(a: Client, b: Client) {
const toB = Y.encodeStateAsUpdate(a.doc, Y.encodeStateVector(b.doc))
const toA = Y.encodeStateAsUpdate(b.doc, Y.encodeStateVector(a.doc))
Y.applyUpdate(b.doc, toB)
Y.applyUpdate(a.doc, toA)
a.flush()
b.flush()
}
function twoClients() {
const a = client(), b = client()
a.update(() => $getRoot().append($createParagraphNode().append($createTextNode('Hello world'))))
exchange(a, b)
return {a, b}
}
// What $setBlocksType does: replace the block, keeping its children.
const toHeading = () => $getRoot().getFirstChildOrThrow().replace($createHeadingNode('h1'), true)
// 1. Both clients turn the same paragraph into a heading, concurrently.
{
const {a, b} = twoClients()
a.update(toHeading)
b.update(toHeading)
exchange(a, b)
console.log('1.', a.text(), '||', b.text())
}
// 2. Client A types in the paragraph while client B turns it into a heading, concurrently.
{
const {a, b} = twoClients()
a.update(() => { const t = $getRoot().getFirstDescendant(); if ($isTextNode(t)) t.spliceText(11, 0, ', again') })
b.update(toHeading)
exchange(a, b)
console.log('2.', a.text(), '||', b.text())
}The current behavior
1. heading: Hello world | heading: Hello world || heading: Hello world | heading: Hello world
2. heading: Hello world || heading: Hello world
The expected behavior
1. heading: Hello world || heading: Hello world
2. heading: Hello world, again || heading: Hello world, again
A change of block type should keep the block's identity in the shared document (for example, change the block's type in place), so concurrent edits inside the block are kept and two identical changes do not copy the words.
Impact of fix
This is silent data loss in collaborative editing. Changing a block's type (paragraph, heading, quote) is a very common action, and an offline client that comes back is the usual case where it collides with typing. The binding's own comment says any moved block is re-created at its new position, so other moves may lose text the same way; we have tested only the type change. We found it with a seeded random test of many clients and are working on a fix; we can send a pull request.
Activity