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.
Two clients each delete one half of a bold word at the same time, so together they delete the whole word. After sync, both open editors show the paragraph correctly, with no bold. But the shared Y.Doc now makes the next text bold: a client that loads the document (a reload) shows " and the list" in bold. The open editors and the saved state disagree, and the wrong format appears at the next reload.
Steps To Reproduce
- Two headless editors, each with a v1 binding and its own
Y.Doc, start from one paragraph: "Bring the ", "drawings" (bold), " and the list". - Client A deletes the first 4 characters of the bold text node; client B deletes the last 4. Then they exchange updates.
- A third client loads the merged state from scratch.
Link to code example (runs with tsx, depends only on Lexical and Yjs):
import {createHeadlessEditor} from '@lexical/headless'
import {createYjsBinding, syncLexicalUpdateToYjs, syncYjsChangesToLexical, type Provider} from '@lexical/yjs'
import {$createParagraphNode, $createTextNode, $getRoot} 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({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().getAllTextNodes().map(n => JSON.stringify(n.getTextContent()) + (n.hasFormat('bold') ? ' (bold)' : '')).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()
}
const a = client(), b = client()
a.update(() => $getRoot().append($createParagraphNode().append(
$createTextNode('Bring the '), $createTextNode('drawings').toggleFormat('bold'), $createTextNode(' and the list'))))
exchange(a, b)
// Each client deletes one half of the bold word, concurrently. Together they delete all of it.
a.update(() => { $getRoot().getAllTextNodes()[1].spliceText(0, 4, '') })
b.update(() => { $getRoot().getAllTextNodes()[1].spliceText(4, 4, '') })
exchange(a, b)
// A third client loads the same document state from scratch (a reload).
const c = client()
Y.applyUpdate(c.doc, Y.encodeStateAsUpdate(a.doc))
c.flush()
console.log('A:', a.text())
console.log('B:', b.text())
console.log('C (reload):', c.text())The current behavior
A: "Bring the and the list"
B: "Bring the and the list"
C (reload): "Bring the ", " and the list" (bold)
The expected behavior
All three clients show the same text, with no bold: "Bring the and the list".
Impact of fix
Open editors diverge from the shared document without any error, and a format moves onto words nobody formatted. It reproduces for both client-id orders. In our seeded random tests of many clients it appears in about one run in a thousand. We have not checked whether #9583 (text lost inside nested marks in v1) has the same cause.
Activity