(sharedRoot: Y.XmlText, editorRoot: Node, op: SplitNodeOperation)
| 11 | import * as Y from 'yjs' |
| 12 | |
| 13 | export function splitNode(sharedRoot: Y.XmlText, editorRoot: Node, op: SplitNodeOperation): void { |
| 14 | const target = getYTarget(sharedRoot, editorRoot, op.path) |
| 15 | |
| 16 | if (!target.editorTarget) { |
| 17 | throw new Error('Y target without corresponding editor node') |
| 18 | } |
| 19 | |
| 20 | if (!target.yTarget) { |
| 21 | if (!Text.isText(target.editorTarget)) { |
| 22 | throw new Error('Mismatch node type between y target and editor node') |
| 23 | } |
| 24 | |
| 25 | const unset: Record<string, null> = {} |
| 26 | target.targetDelta.forEach(element => { |
| 27 | if (element.attributes) { |
| 28 | Object.keys(element.attributes).forEach(key => { |
| 29 | unset[key] = null |
| 30 | }) |
| 31 | } |
| 32 | }) |
| 33 | |
| 34 | return target.yParent.format( |
| 35 | target.textRange.start, |
| 36 | target.textRange.end - target.textRange.start, |
| 37 | { ...unset, ...op.properties }, |
| 38 | ) |
| 39 | } |
| 40 | |
| 41 | if (Text.isText(target.editorTarget)) { |
| 42 | throw new Error('Mismatch node type between y target and editor node') |
| 43 | } |
| 44 | |
| 45 | const splitTarget = getYTarget(target.yTarget, target.editorTarget, [op.position]) |
| 46 | |
| 47 | const ySplitOffset = target.editorTarget.children |
| 48 | .slice(0, op.position) |
| 49 | .reduce((length, child) => length + getEditorNodeYLength(child), 0) |
| 50 | |
| 51 | const length = target.editorTarget.children.reduce( |
| 52 | (current, child) => current + getEditorNodeYLength(child), |
| 53 | 0, |
| 54 | ) |
| 55 | |
| 56 | const splitDelta = sliceInsertDelta( |
| 57 | yTextToInsertDelta(target.yTarget), |
| 58 | ySplitOffset, |
| 59 | length - ySplitOffset, |
| 60 | ) |
| 61 | const clonedDelta = cloneInsertDeltaDeep(splitDelta) |
| 62 | |
| 63 | const storedPositions = getStoredPositionsInDeltaAbsolute( |
| 64 | sharedRoot, |
| 65 | target.yTarget, |
| 66 | splitDelta, |
| 67 | ySplitOffset, |
| 68 | ) |
| 69 | |
| 70 | const toInsert = new Y.XmlText() |
nothing calls this directly
no test coverage detected
searching dependent graphs…