Skip to content

Commit

Permalink
retain empty text objects in some cases - fixes #108
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed May 13, 2022
1 parent f2f8365 commit a20c9d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/plugins/sync-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,11 @@ export const updateYFragment = (y, yDomFragment, pNode, mapping) => {
}
}
const yDelLen = yChildCnt - left - right
if (yDelLen > 0) {
if (yChildCnt === 1 && pChildCnt === 0 && yChildren[0] instanceof Y.XmlText) {
// Edge case handling https://github.com/yjs/y-prosemirror/issues/108
// Only delete the content of the Y.Text to retain remote changes on the same Y.Text object
yChildren[0].delete(0, yChildren[0].length)
} else if (yDelLen > 0) {
yDomFragment.delete(left, yDelLen)
}
if (left + right < pChildCnt) {
Expand Down
13 changes: 13 additions & 0 deletions test/y-prosemirror.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,19 @@ export const testEmptyNotSync = tc => {
t.compareStrings(type.toString(), '<custom checked="true"></custom><paragraph></paragraph>')
}

/**
* @param {t.TestCase} tc
*/
export const testEmptyParagraph = tc => {
const ydoc = new Y.Doc()
const view = createNewProsemirrorView(ydoc)
view.dispatch(view.state.tr.insert(0, /** @type {any} */ (schema.node('paragraph', undefined, schema.text('123')))))
const yxml = ydoc.get('prosemirror')
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'contains one paragraph containing a ytext')
view.dispatch(view.state.tr.delete(1, 4)) // delete characters 123
t.assert(yxml.length === 2 && yxml.get(0).length === 1, 'doesn\'t delete the ytext')
}

const createNewComplexProsemirrorView = y => {
const view = new EditorView(null, {
// @ts-ignore
Expand Down

0 comments on commit a20c9d8

Please sign in to comment.