@@ -16,6 +16,8 @@ import type { TConnectionPoints as TCP } from '../../tree/model/model';
1616import { generateBrickData } from '../utils/path' ;
1717import type { TInputUnion } from '../utils/path' ;
1818import { getLabelWidth } from '../utils/textMeasurement' ;
19+ import type { ExtendedTowerNode } from '../../tower/view/components/TowerView' ;
20+ import { calculateCompleteSubtreeDimensions } from '../../tower/utils/towerUtils' ;
1921
2022export abstract class BrickModel implements IBrick {
2123 protected _uuid : string ;
@@ -386,34 +388,54 @@ export default class CompoundBrick extends BrickModel implements IBrickCompound
386388 public setBoundingBoxNest ( extents : TExtent [ ] ) : void {
387389 this . _bboxNest = extents ;
388390 }
389-
391+
390392 /**
391393 * Recursively update bounding box and connection points to fit nested children.
392394 * Call this after all children are attached, before rendering.
393395 */
394- public updateLayoutWithChildren ( nestedChildren : BrickModel [ ] ) : void {
395- // If there are nested children, calculate the total bounding box
396+ public updateLayoutWithChildren ( nestedChildren : BrickModel [ ] , allNodes : Map < string , ExtendedTowerNode > ) : void {
396397 if ( nestedChildren && nestedChildren . length > 0 ) {
397- // Calculate the bounding box that fits all nested children
398- let _minX = 0 ,
399- _minY = 0 ,
400- maxX = 0 ,
401- maxY = 0 ;
402- nestedChildren . forEach ( ( child ) => {
403- const bbox = child . boundingBox ;
404- // For simplicity, assume children are stacked vertically for now
405- maxY += bbox . h ;
406- maxX = Math . max ( maxX , bbox . w ) ;
407- } ) ;
408- // Expand this brick's bboxNest to fit the children
409- this . _bboxNest = [ { w : maxX , h : maxY } ] ;
398+ let totalHeight = 0 ;
399+ let maxWidth = 0 ;
400+
401+ // Recursively calculate the total height and max width of all descendants
402+ const calculateSubtreeDimensions = ( children : BrickModel [ ] ) : { h : number ; w : number } => {
403+ let height = 0 ;
404+ let width = 0 ;
405+ children . forEach ( child => {
406+ const childNode = Array . from ( allNodes . values ( ) ) . find ( n => n . brick . uuid === child . uuid ) ;
407+ if ( childNode ) {
408+ const { w, h } = calculateCompleteSubtreeDimensions ( childNode . brick . uuid , allNodes , new Map < string , { w : number ; h : number } > ( ) ) ;
409+ height += h ;
410+ width = Math . max ( width , w ) ;
411+ }
412+ } ) ;
413+ return { h : height , w : width } ;
414+ } ;
415+
416+ const { h, w } = calculateSubtreeDimensions ( nestedChildren ) ;
417+ totalHeight = h ;
418+ maxWidth = w ;
419+
420+ // Update bboxNest to reflect the full subtree dimensions
421+ this . _bboxNest = [ { w : maxWidth , h : totalHeight } ] ;
410422 } else {
411423 this . _bboxNest = [ ] ;
412424 }
413- // Update geometry with new bboxNest
414425 this . updateGeometry ( ) ;
415426 }
416427
428+ // Helper method to get nested children based on the tower structure
429+ public getNestedChildren ( allNodes : Map < string , ExtendedTowerNode > ) : BrickModel [ ] {
430+ const children : BrickModel [ ] = [ ] ;
431+ allNodes . forEach ( node => {
432+ if ( node . parent ?. brick . uuid === this . uuid && node . isNested ) {
433+ children . push ( node . brick as BrickModel ) ; // Cast IBrick to BrickModel
434+ }
435+ } ) ;
436+ return children ;
437+ }
438+
417439 public override get renderProps ( ) : TBrickRenderPropsCompound {
418440 return {
419441 ...this . getCommonRenderProps ( ) ,
0 commit comments