( blocks: Record<string, BlockState>, edges: Edge[], layoutFn: LayoutFunction, horizontalSpacing: number, verticalSpacing: number, gridSize?: number )
| 654 | * @param gridSize - Optional grid size for snap-to-grid |
| 655 | */ |
| 656 | export function prepareContainerDimensions( |
| 657 | blocks: Record<string, BlockState>, |
| 658 | edges: Edge[], |
| 659 | layoutFn: LayoutFunction, |
| 660 | horizontalSpacing: number, |
| 661 | verticalSpacing: number, |
| 662 | gridSize?: number |
| 663 | ): void { |
| 664 | const { children } = getBlocksByParent(blocks) |
| 665 | |
| 666 | // Build dependency graph to process nested containers bottom-up |
| 667 | const containerIds = Array.from(children.keys()) |
| 668 | const containerDepth = new Map<string, number>() |
| 669 | |
| 670 | // Calculate nesting depth for each container |
| 671 | for (const containerId of containerIds) { |
| 672 | let depth = 0 |
| 673 | let currentId: string | undefined = containerId |
| 674 | while (currentId) { |
| 675 | const block: BlockState | undefined = blocks[currentId] |
| 676 | const parentId: string | undefined = block?.data?.parentId |
| 677 | currentId = parentId |
| 678 | if (currentId) depth++ |
| 679 | } |
| 680 | containerDepth.set(containerId, depth) |
| 681 | } |
| 682 | |
| 683 | // Sort containers by depth (deepest first) for bottom-up processing |
| 684 | const sortedContainerIds = containerIds.sort((a, b) => { |
| 685 | const depthA = containerDepth.get(a) ?? 0 |
| 686 | const depthB = containerDepth.get(b) ?? 0 |
| 687 | return depthB - depthA |
| 688 | }) |
| 689 | |
| 690 | // Process each container, laying out its children to determine dimensions |
| 691 | for (const containerId of sortedContainerIds) { |
| 692 | const container = blocks[containerId] |
| 693 | if (!container) continue |
| 694 | |
| 695 | const childIds = children.get(containerId) ?? [] |
| 696 | const layoutChildIds = filterLayoutEligibleBlockIds(childIds, blocks) |
| 697 | |
| 698 | if (layoutChildIds.length === 0) { |
| 699 | // Empty container - use default dimensions |
| 700 | container.data = { |
| 701 | ...container.data, |
| 702 | width: CONTAINER_DIMENSIONS.DEFAULT_WIDTH, |
| 703 | height: CONTAINER_DIMENSIONS.DEFAULT_HEIGHT, |
| 704 | } |
| 705 | container.layout = { |
| 706 | ...container.layout, |
| 707 | measuredWidth: CONTAINER_DIMENSIONS.DEFAULT_WIDTH, |
| 708 | measuredHeight: CONTAINER_DIMENSIONS.DEFAULT_HEIGHT, |
| 709 | } |
| 710 | continue |
| 711 | } |
| 712 | |
| 713 | // Build subset of blocks and edges for this container's children |
no test coverage detected