Skip to content

Commit ea72582

Browse files
niloysikdarmeganindya
authored andcommitted
feat(playground): recursive function to get below brick IDs
1 parent da683cd commit ea72582

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

modules/code-builder/playground/pages/WorkSpace/BrickFactory.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { useState } from 'react';
22
import { useMove } from 'react-aria';
33
import { BrickBlock, BrickData, BrickExpression, BrickStatement } from '@/brick';
44
import { useBricksCoords } from './BricksCoordsStore';
5+
import { WORKSPACES_DATA } from './data';
56
import type { Brick } from './data';
7+
import { getBelowBricksIds } from './utils';
68

79
const BrickFactory = ({ brickData }: { brickData: Brick }) => {
810
const CONTAINER_SIZE_X = 800;
@@ -35,14 +37,14 @@ const BrickFactory = ({ brickData }: { brickData: Brick }) => {
3537
});
3638
});
3739

38-
const belowBrickId = brickData.surroundingBricks.below;
39-
if (belowBrickId) {
40+
const belowBrickIds = getBelowBricksIds(WORKSPACES_DATA[0].data, brickData.id);
41+
belowBrickIds.forEach((belowBrickId) => {
4042
const belowBrickCoords = getCoords(belowBrickId)!;
4143
setCoords(belowBrickId, {
4244
x: belowBrickCoords.x + e.deltaX,
4345
y: belowBrickCoords.y + e.deltaY,
4446
});
45-
}
47+
});
4648

4749
// Normally, we want to allow the user to continue
4850
// dragging outside the box such that they need to
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Brick } from './data';
2+
3+
export function getBelowBricksIds(arr: Brick[], item: string): string[] {
4+
let result: string[] = [];
5+
6+
function recursiveSearch(arr: Brick[], item: string) {
7+
arr.forEach((element, index) => {
8+
if (element.id === item) {
9+
arr.slice(index + 1, arr.length).map((el) => {
10+
result = result.concat(el.childBricks);
11+
result = result.concat(el.id);
12+
});
13+
return;
14+
}
15+
if (Array.isArray(element.children)) {
16+
recursiveSearch(element.children, item);
17+
}
18+
});
19+
}
20+
21+
recursiveSearch(arr, item);
22+
return result;
23+
}

0 commit comments

Comments
 (0)