Skip to content

Commit

Permalink
feat: nodes can be cloned and deep cloned
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobrosenberg committed Dec 19, 2024
1 parent 25bbb07 commit c2015be
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/common/RNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ export class RNode {
return node
}

clone() {
const node = /** @type {InstanceType['NodeConstructor']['prototype']} */ (
this.instance.createNode(this.name, this.module)
)
this.parent?.appendChild(node)

Object.assign(node, this)
node.meta = { ...this.meta }
return node
}

deepClone() {
const clone = this.clone()
this.children.forEach(child => clone.appendChild(child.deepClone()))
return clone
}

/** @type {InstanceType['NodeConstructor']['prototype'][]} */
get descendants() {
return this.instance.nodeIndex.filter(node =>
Expand Down

0 comments on commit c2015be

Please sign in to comment.