Skip to content

Commit

Permalink
fix: make parentId param optional in actions.add() (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong authored May 13, 2024
1 parent fcc71d8 commit 553d827
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 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()
17 changes: 6 additions & 11 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,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)) {
Expand Down

0 comments on commit 553d827

Please sign in to comment.