(
blocks: Record<string, BlockState>,
edges: Edge[],
options: LayoutOptions = {}
)
| 22 | * Updates both child positions and container dimensions. |
| 23 | */ |
| 24 | export function layoutContainers( |
| 25 | blocks: Record<string, BlockState>, |
| 26 | edges: Edge[], |
| 27 | options: LayoutOptions = {} |
| 28 | ): void { |
| 29 | const { children } = getBlocksByParent(blocks) |
| 30 | |
| 31 | const containerOptions: LayoutOptions = { |
| 32 | horizontalSpacing: options.horizontalSpacing |
| 33 | ? options.horizontalSpacing * 0.85 |
| 34 | : DEFAULT_CONTAINER_HORIZONTAL_SPACING, |
| 35 | verticalSpacing: options.verticalSpacing ?? DEFAULT_VERTICAL_SPACING, |
| 36 | padding: { x: CONTAINER_PADDING_X, y: CONTAINER_PADDING_Y }, |
| 37 | gridSize: options.gridSize, |
| 38 | } |
| 39 | |
| 40 | for (const [parentId, childIds] of children.entries()) { |
| 41 | const parentBlock = blocks[parentId] |
| 42 | if (!parentBlock) continue |
| 43 | |
| 44 | logger.debug('Processing container', { parentId, childCount: childIds.length }) |
| 45 | |
| 46 | const layoutChildIds = filterLayoutEligibleBlockIds(childIds, blocks) |
| 47 | const childBlocks: Record<string, BlockState> = {} |
| 48 | for (const childId of layoutChildIds) { |
| 49 | childBlocks[childId] = blocks[childId] |
| 50 | } |
| 51 | |
| 52 | const childEdges = edges.filter( |
| 53 | (edge) => layoutChildIds.includes(edge.source) && layoutChildIds.includes(edge.target) |
| 54 | ) |
| 55 | |
| 56 | if (Object.keys(childBlocks).length === 0) { |
| 57 | continue |
| 58 | } |
| 59 | |
| 60 | const { nodes, dimensions } = layoutBlocksCore(childBlocks, childEdges, { |
| 61 | isContainer: true, |
| 62 | layoutOptions: containerOptions, |
| 63 | }) |
| 64 | |
| 65 | for (const node of nodes.values()) { |
| 66 | blocks[node.id].position = node.position |
| 67 | } |
| 68 | |
| 69 | const calculatedWidth = dimensions.width |
| 70 | const calculatedHeight = dimensions.height |
| 71 | |
| 72 | const containerWidth = Math.max(calculatedWidth, CONTAINER_DIMENSIONS.DEFAULT_WIDTH) |
| 73 | const containerHeight = Math.max(calculatedHeight, CONTAINER_DIMENSIONS.DEFAULT_HEIGHT) |
| 74 | |
| 75 | if (!parentBlock.data) { |
| 76 | parentBlock.data = {} |
| 77 | } |
| 78 | |
| 79 | parentBlock.data.width = containerWidth |
| 80 | parentBlock.data.height = containerHeight |
| 81 |
no test coverage detected