Skip to main content

Function: moveNode()

moveNode(version, nodeId, toParentId, toSlot, index?): DocumentVersion

Defined in: packages/core/src/moveNode.ts:44

Moves a node into a slot: the reference is taken out of whatever held it — a slot, or the document's roots — and placed in the target slot.

A move rewrites references, never the node. Reordering within one slot is a move to the same parent and slot with a new index.

Parameters

version

DocumentVersion

The document to edit. Read, never written.

nodeId

string

Id of the node to move. The document must hold it.

toParentId

string

Id of the node whose slot receives it. The document must hold it, and it may be the parent the node already sits under.

toSlot

string

Slot name on that parent. A slot the parent has never filled is opened.

index?

number

Position in the target slot as it stands after the node is taken out, so the last position is that slot's length. Omit to append.

Returns

DocumentVersion

A new DocumentVersion with the reference moved. The argument is not mutated, the moved node is carried over by reference along with every node the edit did not touch, and version.version is not bumped.

Throws

no-such-node when nodeId or toParentId names a node the document does not hold.

Example

// sections: [a, b, c] → aside: [a]
const next = moveNode(version, "a", "stack", "aside");

// Reorder inside one slot: [a, b, c] → [b, c, a]
const last = moveNode(version, "a", "stack", "sections", 2);

// A root becomes a child, and drops out of `roots`.
const nested = moveNode(version, "loose", "stack", "sections");