Skip to content

Commit

Permalink
fix: make parentId param optional in actions.add()
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong committed May 13, 2024
1 parent 438f48f commit b16aa5f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/sixty-rice-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@craftjs/core': patch
---

Make parentId param optional in actions.add()
15 changes: 3 additions & 12 deletions packages/core/src/editor/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const Methods = (
/** Helper functions */
const addNodeTreeToParent = (
tree: NodeTree,
parentId?: NodeId,
parentId: NodeId,
addNodeType?:
| {
type: 'child';
Expand Down Expand Up @@ -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') {
Expand Down Expand Up @@ -180,7 +171,7 @@ const Methods = (
* @param parentId
* @param index
*/
add(nodeToAdd: Node | Node[], parentId: NodeId, index?: number) {
add(nodeToAdd: Node | Node[], parentId?: NodeId, index?: number) {
// TODO: Deprecate adding array of Nodes to keep implementation simpler
let nodes = [nodeToAdd];
if (Array.isArray(nodeToAdd)) {
Expand All @@ -197,7 +188,7 @@ const Methods = (
},
rootNodeId: node.id,
},
parentId,
parentId ?? ROOT_NODE,
{ type: 'child', index }
);
});
Expand Down

0 comments on commit b16aa5f

Please sign in to comment.