(sharedRoot: Y.XmlText, editorRoot: Node, op: MoveNodeOperation)
| 13 | } from '@editablejs/yjs-transform' |
| 14 | |
| 15 | export function moveNode(sharedRoot: Y.XmlText, editorRoot: Node, op: MoveNodeOperation): void { |
| 16 | const newParentPath = Path.parent(op.newPath) |
| 17 | const newPathOffset = op.newPath[op.newPath.length - 1] |
| 18 | const parent = Node.get(editorRoot, newParentPath) |
| 19 | if (Text.isText(parent)) { |
| 20 | throw new Error('Cannot move editor node into text element') |
| 21 | } |
| 22 | const normalizedNewPath = [...newParentPath, Math.min(newPathOffset, parent.children.length)] |
| 23 | |
| 24 | const origin = getYTarget(sharedRoot, editorRoot, op.path) |
| 25 | const target = getYTarget(sharedRoot, editorRoot, normalizedNewPath) |
| 26 | const insertDelta = cloneInsertDeltaDeep(origin.targetDelta) |
| 27 | |
| 28 | const storedPositions = getStoredPositionsInDeltaAbsolute( |
| 29 | sharedRoot, |
| 30 | origin.yParent, |
| 31 | origin.targetDelta, |
| 32 | ) |
| 33 | |
| 34 | origin.yParent.delete(origin.textRange.start, origin.textRange.end - origin.textRange.start) |
| 35 | |
| 36 | const targetLength = getInsertDeltaLength(yTextToInsertDelta(target.yParent)) |
| 37 | const deltaApplyYOffset = Math.min(target.textRange.start, targetLength) |
| 38 | const applyDelta: Delta = [{ retain: deltaApplyYOffset }, ...insertDelta] |
| 39 | |
| 40 | target.yParent.applyDelta(applyDelta, { sanitize: false }) |
| 41 | |
| 42 | restoreStoredPositionsWithDeltaAbsolute( |
| 43 | sharedRoot, |
| 44 | target.yParent, |
| 45 | storedPositions, |
| 46 | insertDelta, |
| 47 | deltaApplyYOffset, |
| 48 | origin.textRange.start, |
| 49 | ) |
| 50 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…