From e2ec1756c387af9bf0116242ebf2a82231d2b667 Mon Sep 17 00:00:00 2001 From: Prev Wong Date: Mon, 13 May 2024 17:52:45 +0800 Subject: [PATCH] fix: make parentId param optional in actions.add() --- .changeset/sixty-rice-build.md | 5 +++++ packages/core/src/editor/actions.ts | 17 ++++++----------- 2 files changed, 11 insertions(+), 11 deletions(-) create mode 100644 .changeset/sixty-rice-build.md diff --git a/.changeset/sixty-rice-build.md b/.changeset/sixty-rice-build.md new file mode 100644 index 000000000..d1a0eca63 --- /dev/null +++ b/.changeset/sixty-rice-build.md @@ -0,0 +1,5 @@ +--- +'@craftjs/core': patch +--- + +Make parentId param optional in actions.add() diff --git a/packages/core/src/editor/actions.ts b/packages/core/src/editor/actions.ts index 3295d6de4..afd64c8fc 100644 --- a/packages/core/src/editor/actions.ts +++ b/packages/core/src/editor/actions.ts @@ -38,7 +38,7 @@ const Methods = ( /** Helper functions */ const addNodeTreeToParent = ( tree: NodeTree, - parentId?: NodeId, + parentId: NodeId, addNodeType?: | { type: 'child'; @@ -84,15 +84,6 @@ const Methods = ( iterateChildren(tree.rootNodeId, parentId); - if (!parentId) { - invariant( - tree.rootNodeId === ROOT_NODE, - 'Cannot add non-root Node without a parent' - ); - - return; - } - const parent = getParentAndValidate(parentId); if (addNodeType.type === 'child') { @@ -180,7 +171,11 @@ const Methods = ( * @param parentId * @param index */ - add(nodeToAdd: Node | Node[], parentId: NodeId, index?: number) { + add( + nodeToAdd: Node | Node[], + parentId: NodeId = ROOT_NODE, + index?: number + ) { // TODO: Deprecate adding array of Nodes to keep implementation simpler let nodes = [nodeToAdd]; if (Array.isArray(nodeToAdd)) {