Skip to content

Commit f54d809

Browse files
committed
feat(Masonry): [brick] fixed iteration through a tower and added utility to traverse children
1 parent f9fd562 commit f54d809

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

modules/masonry/playground/pages/workspace/WorkspaceCanvas.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ export default function WorkspaceCanvas(): JSX.Element {
155155
const rootNode = towerModel.nodesArray().find(n => n.parent === null);
156156
// Type assertion to BrickModel to access getTotalBounds
157157
if (rootNode && rootNode.brick.uuid === draggedBrickId && typeof (rootNode.brick as any).getTotalBounds === 'function') {
158-
// Optionally, you could use getTotalBounds for snap/visual feedback here
159-
// For now, just move the root and all children will follow
158+
// we can use getTotalBounds for snap/visual feedback here
159+
// For now, move the root and all children will follow
160160
towerModel.setBrickPosition(draggedBrickId, { x, y });
161161
} else {
162162
// Otherwise, move just the dragged brick
@@ -243,7 +243,7 @@ export default function WorkspaceCanvas(): JSX.Element {
243243
bboxArgs: [{ w: 60, h: 20 }, { w: 60, h: 20 }],
244244
});
245245

246-
const tower = new TowerModel('interactive_tower', root, { x: 100, y: 100 });
246+
const tower = new TowerModel('dummy_tower', root, { x: 100, y: 100 });
247247

248248
// Add 2 argument bricks (expressions)
249249
const expr1 = createExpressionBrick({ label: 'Expr 1' });
@@ -315,4 +315,4 @@ export default function WorkspaceCanvas(): JSX.Element {
315315
</svg>
316316
</div>
317317
);
318-
}
318+
}

modules/masonry/src/tower/model/model.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ export default class TowerModel {
202202
// Create a new tower for the detached subtree
203203
const newTower = new TowerModel(uuid(), node.brick, { ...node.position });
204204

205-
// Helper to recursively gather all descendants
206-
const gatherDescendants = (n: ITowerNode, collection: Map<string, ITowerNode>) => {
205+
// Helper to recursively gather all descendants, respecting isNested
206+
const gatherDescendants = (n: ITowerNode, collection: Map<string, ITowerNode>): void => {
207207
collection.set(n.brick.uuid, cloneDeep(n));
208+
// Find all children (nested, args, or stacked) based on parent reference
208209
this.nodes.forEach(childNode => {
209-
if (childNode.parent && childNode.parent.brick.uuid === n.brick.uuid) {
210+
if (childNode.parent?.brick.uuid === n.brick.uuid) {
210211
gatherDescendants(childNode, collection);
211212
}
212213
});
@@ -219,8 +220,7 @@ export default class TowerModel {
219220
if (node.parent) {
220221
// Remove connections involving the detached subtree
221222
this.connections = this.connections.filter(
222-
(conn) => {
223-
// Keep connections that don't involve any node in the detached subtree
223+
conn => {
224224
const fromInSubtree = newNodes.has(conn.from);
225225
const toInSubtree = newNodes.has(conn.to);
226226
return !(fromInSubtree || toInSubtree);
@@ -233,14 +233,23 @@ export default class TowerModel {
233233
this.nodes.delete(key);
234234
}
235235

236-
// The new tower's nodes are the collected descendants
236+
// Set up the new tower's nodes, adjusting parents
237237
newTower.nodes.clear(); // Clear the root created by the constructor
238238
newNodes.forEach((n, id) => {
239+
const newNode = cloneDeep(n);
239240
// Reset parent for the root of the new tower
240241
if (id === brickId) {
241-
n.parent = null;
242+
newNode.parent = null;
243+
} else {
244+
// Re-link parent if it exists in the new tower
245+
if (n.parent) {
246+
const parentNode = newNodes.get(n.parent.brick.uuid);
247+
newNode.parent = parentNode || null; // Safe assignment with type guard
248+
} else {
249+
newNode.parent = null; // Explicitly handle null parent
250+
}
242251
}
243-
newTower.nodes.set(id, n);
252+
newTower.nodes.set(id, newNode);
244253
});
245254

246255
// Copy relevant connections to the new tower

0 commit comments

Comments
 (0)