MCPcopy Create free account
hub / github.com/editablejs/editable / moveNode

Function moveNode

packages/plugins/yjs/src/apply-to-yjs/node/move-node.ts:15–50  ·  view source on GitHub ↗
(sharedRoot: Y.XmlText, editorRoot: Node, op: MoveNodeOperation)

Source from the content-addressed store, hash-verified

13} from '@editablejs/yjs-transform'
14
15export 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}

Callers

nothing calls this directly

Calls 7

getYTargetFunction · 0.90
cloneInsertDeltaDeepFunction · 0.90
getInsertDeltaLengthFunction · 0.90
yTextToInsertDeltaFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…