Skip to content

Commit 80afdd8

Browse files
committed
fix(autolayout): recalculate dimensions after grid snapping
1 parent c3d2309 commit 80afdd8

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,13 @@ export function layoutBlocksCore(
420420
calculatePositions(layers, edges, layoutOptions)
421421

422422
// 5. Normalize positions
423-
const dimensions = normalizePositions(nodes, { isContainer: options.isContainer })
423+
let dimensions = normalizePositions(nodes, { isContainer: options.isContainer })
424424

425-
// 6. Snap to grid if gridSize is specified
426-
snapNodesToGrid(nodes, layoutOptions.gridSize)
425+
// 6. Snap to grid if gridSize is specified (recalculates dimensions)
426+
const snappedDimensions = snapNodesToGrid(nodes, layoutOptions.gridSize)
427+
if (snappedDimensions) {
428+
dimensions = snappedDimensions
429+
}
427430

428431
return { nodes, dimensions }
429432
}

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { createLogger } from '@sim/logger'
21
import {
32
CONTAINER_PADDING,
43
DEFAULT_HORIZONTAL_SPACING,
@@ -14,12 +13,11 @@ import {
1413
isContainerType,
1514
prepareContainerDimensions,
1615
shouldSkipAutoLayout,
16+
snapPositionToGrid,
1717
} from '@/lib/workflows/autolayout/utils'
1818
import { CONTAINER_DIMENSIONS } from '@/lib/workflows/blocks/block-dimensions'
1919
import type { BlockState } from '@/stores/workflows/workflow/types'
2020

21-
const logger = createLogger('AutoLayout:Targeted')
22-
2321
export interface TargetedLayoutOptions extends LayoutOptions {
2422
changedBlockIds: string[]
2523
verticalSpacing?: number
@@ -186,10 +184,7 @@ function layoutGroup(
186184
const block = blocks[id]
187185
const newPos = layoutPositions.get(id)
188186
if (!block || !newPos) continue
189-
block.position = {
190-
x: newPos.x + offsetX,
191-
y: newPos.y + offsetY,
192-
}
187+
block.position = snapPositionToGrid({ x: newPos.x + offsetX, y: newPos.y + offsetY }, gridSize)
193188
}
194189
}
195190

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,29 @@ export function snapPositionToGrid(
4343
}
4444

4545
/**
46-
* Snaps all node positions in a graph to grid positions.
47-
* Only applies if gridSize > 0.
46+
* Snaps all node positions in a graph to grid positions and returns updated dimensions.
47+
* Returns null if gridSize is not set or no snapping was needed.
4848
*/
49-
export function snapNodesToGrid(nodes: Map<string, GraphNode>, gridSize: number | undefined): void {
50-
if (!gridSize || gridSize <= 0) {
51-
return
49+
export function snapNodesToGrid(
50+
nodes: Map<string, GraphNode>,
51+
gridSize: number | undefined
52+
): { width: number; height: number } | null {
53+
if (!gridSize || gridSize <= 0 || nodes.size === 0) {
54+
return null
5255
}
56+
57+
let maxX = Number.NEGATIVE_INFINITY
58+
let maxY = Number.NEGATIVE_INFINITY
59+
5360
for (const node of nodes.values()) {
5461
node.position = snapPositionToGrid(node.position, gridSize)
62+
maxX = Math.max(maxX, node.position.x + node.metrics.width)
63+
maxY = Math.max(maxY, node.position.y + node.metrics.height)
64+
}
65+
66+
return {
67+
width: maxX + CONTAINER_PADDING,
68+
height: maxY + CONTAINER_PADDING,
5569
}
5670
}
5771

0 commit comments

Comments
 (0)