Skip to content

Commit 99386e8

Browse files
committed
fix(autolayout): correct dimension calculation and propagate gridSize
1 parent 80afdd8 commit 99386e8

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

apps/sim/lib/workflows/autolayout/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ export function applyAutoLayout(
4141
edges,
4242
layoutBlocksCore,
4343
horizontalSpacing,
44-
verticalSpacing
44+
verticalSpacing,
45+
options.gridSize
4546
)
4647

4748
const { root: rootBlockIds } = getBlocksByParent(blocksCopy)

apps/sim/lib/workflows/autolayout/targeted.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export function applyTargetedLayout(
5252
edges,
5353
layoutBlocksCore,
5454
horizontalSpacing,
55-
verticalSpacing
55+
verticalSpacing,
56+
gridSize
5657
)
5758

5859
const groups = getBlocksByParent(blocksCopy)

apps/sim/lib/workflows/autolayout/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export interface LayoutOptions {
77
horizontalSpacing?: number
88
verticalSpacing?: number
99
padding?: { x: number; y: number }
10-
/** Grid size for snap-to-grid. When > 0, positions are snapped to grid multiples. */
1110
gridSize?: number
1211
}
1312

apps/sim/lib/workflows/autolayout/utils.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,22 @@ export function snapNodesToGrid(
5454
return null
5555
}
5656

57+
let minX = Number.POSITIVE_INFINITY
58+
let minY = Number.POSITIVE_INFINITY
5759
let maxX = Number.NEGATIVE_INFINITY
5860
let maxY = Number.NEGATIVE_INFINITY
5961

6062
for (const node of nodes.values()) {
6163
node.position = snapPositionToGrid(node.position, gridSize)
64+
minX = Math.min(minX, node.position.x)
65+
minY = Math.min(minY, node.position.y)
6266
maxX = Math.max(maxX, node.position.x + node.metrics.width)
6367
maxY = Math.max(maxY, node.position.y + node.metrics.height)
6468
}
6569

6670
return {
67-
width: maxX + CONTAINER_PADDING,
68-
height: maxY + CONTAINER_PADDING,
71+
width: maxX - minX + CONTAINER_PADDING * 2,
72+
height: maxY - minY + CONTAINER_PADDING * 2,
6973
}
7074
}
7175

@@ -365,6 +369,7 @@ export type LayoutFunction = (
365369
horizontalSpacing?: number
366370
verticalSpacing?: number
367371
padding?: { x: number; y: number }
372+
gridSize?: number
368373
}
369374
subflowDepths?: Map<string, number>
370375
}
@@ -380,13 +385,15 @@ export type LayoutFunction = (
380385
* @param layoutFn - The layout function to use for calculating dimensions
381386
* @param horizontalSpacing - Horizontal spacing between blocks
382387
* @param verticalSpacing - Vertical spacing between blocks
388+
* @param gridSize - Optional grid size for snap-to-grid
383389
*/
384390
export function prepareContainerDimensions(
385391
blocks: Record<string, BlockState>,
386392
edges: Edge[],
387393
layoutFn: LayoutFunction,
388394
horizontalSpacing: number,
389-
verticalSpacing: number
395+
verticalSpacing: number,
396+
gridSize?: number
390397
): void {
391398
const { children } = getBlocksByParent(blocks)
392399

@@ -453,6 +460,7 @@ export function prepareContainerDimensions(
453460
layoutOptions: {
454461
horizontalSpacing: horizontalSpacing * 0.85,
455462
verticalSpacing,
463+
gridSize,
456464
},
457465
})
458466

0 commit comments

Comments
 (0)