Skip to content

Commit ef35266

Browse files
committed
Fix incorrect node updates by wrong right side match
1 parent 57ed09b commit ef35266

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/plugins/sync-plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ export const updateYFragment = (y, yDomFragment, pNode, meta) => {
11921192
}
11931193
}
11941194
// find number of matching elements from right
1195-
for (; right + left + 1 < minCnt; right++) {
1195+
for (; right + left < minCnt; right++) {
11961196
const rightY = yChildren[yChildCnt - right - 1]
11971197
const rightP = pChildren[pChildCnt - right - 1]
11981198
if (!mappedIdentity(meta.mapping.get(rightY), rightP)) {

tests/y-prosemirror.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,47 @@ export const testInsertDuplication = (_tc) => {
310310
t.assert(yxml1.toString() === '<paragraph>1122</paragraph><paragraph></paragraph>')
311311
}
312312

313+
export const testInsertRightMatch = (_tc) => {
314+
const ydoc = new Y.Doc()
315+
const yXmlFragment = ydoc.get('prosemirror', Y.XmlFragment)
316+
const view = createNewProsemirrorView(ydoc)
317+
view.dispatch(
318+
view.state.tr.insert(
319+
0,
320+
[
321+
schema.node(
322+
'heading',
323+
{ level: 1 },
324+
schema.text('Heading 1')
325+
),
326+
schema.node(
327+
'paragraph',
328+
undefined,
329+
schema.text('Paragraph 1')
330+
)
331+
]
332+
)
333+
)
334+
prosemirrorJSONToYXmlFragment(/** @type {any} */ (schema), view.state.doc.toJSON(), yXmlFragment)
335+
const lastP = yXmlFragment.get(yXmlFragment.length - 1)
336+
const tr = view.state.tr
337+
view.dispatch(
338+
tr.insert(
339+
tr.doc.child(0).nodeSize + tr.doc.child(1).nodeSize,
340+
schema.node(
341+
'paragraph',
342+
undefined,
343+
schema.text('Paragraph 2')
344+
)
345+
)
346+
)
347+
const newLastP = yXmlFragment.get(yXmlFragment.length - 1)
348+
const new2ndLastP = yXmlFragment.get(yXmlFragment.length - 2)
349+
t.assert(lastP === newLastP, 'last paragraph is the same as before')
350+
t.assert(new2ndLastP.toString() === '<paragraph>Paragraph 2</paragraph>', '2nd last paragraph is the inserted paragraph')
351+
t.assert(lastP.toString() === '<paragraph></paragraph>', 'last paragraph remains empty and is placed at the end')
352+
}
353+
313354
export const testAddToHistory = (_tc) => {
314355
const ydoc = new Y.Doc()
315356
const view = createNewProsemirrorViewWithUndoManager(ydoc)

0 commit comments

Comments
 (0)