@@ -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 */
384390export 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