MCPcopy
hub / github.com/midrender/revideo / insert

Method insert

packages/2d/src/lib/components/Node.ts:722–743  ·  view source on GitHub ↗

* Insert the given node(s) at the specified index in the children list. * * @example * ```tsx * const node = ( * * * * * ); * * node.insert( , 1); * ``` * * Result: * ```mermaid * graph TD; *

(node: ComponentChildren, index = 0)

Source from the content-addressed store, hash-verified

720 * @param index - An index at which to insert the node(s).
721 */
722 public insert(node: ComponentChildren, index = 0): this {
723 const array: ComponentChild[] = Array.isArray(node) ? node : [node];
724 if (array.length === 0) {
725 return this;
726 }
727
728 const children = this.children();
729 const newChildren = children.slice(0, index);
730
731 for (const node of array) {
732 if (node instanceof Node) {
733 newChildren.push(node);
734 node.remove();
735 node.parent(this);
736 }
737 }
738
739 newChildren.push(...children.slice(index));
740 this.setParsedChildren(newChildren);
741
742 return this;
743 }
744
745 /**
746 * Remove this node from the tree.

Callers 1

addMethod · 0.95

Calls 2

setParsedChildrenMethod · 0.95
removeMethod · 0.45

Tested by

no test coverage detected