File tree Expand file tree Collapse file tree 3 files changed +27
-15
lines changed
apps/sim/lib/workflows/autolayout Expand file tree Collapse file tree 3 files changed +27
-15
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 1- import { createLogger } from '@sim/logger'
21import {
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'
1818import { CONTAINER_DIMENSIONS } from '@/lib/workflows/blocks/block-dimensions'
1919import type { BlockState } from '@/stores/workflows/workflow/types'
2020
21- const logger = createLogger ( 'AutoLayout:Targeted' )
22-
2321export 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments