Skip to main content

Function: addNode()

addNode(version, parentId, slot, node, index?): DocumentVersion

Defined in: packages/core/src/addNode.ts:46

Places a node in a parent's slot and registers it among the document's elements.

The caller mints node.id; crypto.randomUUID() in the calling code is the usual source.

Parameters

version

DocumentVersion

The document to edit. Read, never written.

parentId

string

Id of the node whose slot receives the child. The document must already hold it.

slot

string

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

node

Node

The node to place. Its id must be one the document does not already hold.

index?

number

Position in the slot's child list as it stands before the insert. Omit to append; an index past the end appends.

Returns

DocumentVersion

A new DocumentVersion with the node registered and the slot's order rewritten. The argument is not mutated, version.version is not bumped, and every node the edit did not touch is carried over by reference.

Throws

no-such-node when parentId names a node the document does not hold; duplicate-node-id when node.id is already in use, which would replace the node holding it and redirect every slot that named it.

Example

const next = addNode(version, "stack", "sections", {
id: crypto.randomUUID(),
block: "Card",
props: { title: "New" },
});

// Second in the slot rather than last.
const inserted = addNode(version, "stack", "sections", card, 1);