Skip to content

Commit

Permalink
fix: Use addChild function instead on directly pushing child
Browse files Browse the repository at this point in the history
- To make sure parentBlock is set
- This will also make sure all the blocks pasted from figma has parent set
  • Loading branch information
surajshetty3416 committed Dec 18, 2024
1 parent 2d13250 commit f20e376
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
3 changes: 1 addition & 2 deletions frontend/src/components/BuilderCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ const { isOverDropZone } = useDropZone(canvasContainer, {
if (!parentBlock) return;
const parentParentBlock = parentBlock.getParentBlock();
if (!parentParentBlock) return;
const index = parentParentBlock.children.indexOf(parentBlock);
parentParentBlock.children.splice(index, 1, newBlock);
parentParentBlock.replaceChild(parentBlock, newBlock);
} else {
while (parentBlock && !parentBlock.canHaveChildren()) {
parentBlock = parentBlock.getParentBlock();
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ const useStore = defineStore("store", {
this.activeCanvas.setRootBlock(firstBlock);
} else {
for (let block of blocks) {
parent?.children.push(getBlockInstance(block));
parent?.addChild(block);
}
}
},
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/utils/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
addPxToNumber,
dataURLtoFile,
getBlockCopy,
getBlockInstance,
getNumberFromPx,
getTextContent,
kebabToCamelCase,
Expand Down Expand Up @@ -71,7 +72,7 @@ class Block implements BlockOptions {
}
this.children = (options.children || []).map((child: BlockOptions) => {
child.parentBlock = this;
return reactive(new Block(child));
return getBlockInstance(child);
});

this.baseStyles = reactive(options.styles || options.baseStyles || {});
Expand Down Expand Up @@ -420,13 +421,13 @@ class Block implements BlockOptions {
}
}
addChild(child: BlockOptions, index?: number | null, select: boolean = true) {
child.parentBlock = this;
if (index === undefined || index === null) {
index = this.children.length;
}
index = clamp(index, 0, this.children.length);

const childBlock = reactive(new Block(child));
const childBlock = getBlockInstance(child);
childBlock.parentBlock = this;
this.children.splice(index, 0, childBlock);
if (select) {
childBlock.selectBlock();
Expand All @@ -453,7 +454,7 @@ class Block implements BlockOptions {
newChild.parentBlock = this;
const index = this.getChildIndex(child);
if (index > -1) {
this.children.splice(index, 1, newChild);
this.children.splice(index, 1, reactive(newChild));
}
}
getChildIndex(child: Block) {
Expand Down

0 comments on commit f20e376

Please sign in to comment.