@@ -13,55 +13,22 @@ export class Arrangements {
1313 public autoArrange ( ) : Map < string , FlowOptions > {
1414 const newItems = new Map < string , FlowOptions > ( ) ;
1515 let currentX = 0 ;
16+ let currentY = 0 ;
1617
17- if ( this . direction === 'horizontal' ) {
18- // Start by positioning the base nodes
19- const baseNodes = this . list . filter (
20- ( node ) => node . position . deps . length === 0
21- ) ;
22-
23- let level = 0 ;
18+ // Handle both horizontal and vertical directions
19+ const baseNodes = this . list . filter (
20+ ( node ) => node . position . deps . length === 0
21+ ) ;
2422
25- for ( const baseNode of baseNodes ) {
26- const consumedHeight = this . positionDependents (
27- baseNode ,
28- 0 ,
29- 0 ,
30- newItems
31- ) ;
32- // const centerY = consumedHeight / 2;
33- // newItems.set(baseNode.position.id, {
34- // ...baseNode.position,
35- // x: currentX,
36- // y: centerY - baseNode.elRect.height / 2,
37- // });
23+ for ( const baseNode of baseNodes ) {
24+ if ( this . direction === 'horizontal' ) {
25+ this . positionDependents ( baseNode , currentX , 0 , newItems ) ;
3826 currentX += baseNode . elRect . width + this . horizontalPadding ;
27+ } else {
28+ // Vertical arrangement
29+ this . positionDependents ( baseNode , 0 , currentY , newItems ) ;
30+ currentY += baseNode . elRect . height + this . horizontalPadding ;
3931 }
40- } else {
41- // direction === 'vertical'
42- // let currentY = 0;
43- // for (const level in levelsMap) {
44- // const itemsInLevel = levelsMap[level];
45- // let currentX = 0;
46- // // Sort items within the level by their current x position
47- // itemsInLevel.sort((a, b) => a.position.x - b.position.x);
48- // for (const node of itemsInLevel) {
49- // const newNode: FlowOptions = {
50- // ...node.position,
51- // x: currentX,
52- // y: currentY,
53- // };
54- // currentX += node.elRect.width + this.horizontalPadding;
55- // newItems.set(node.position.id, newNode);
56- // }
57- // const maxHeightItem = itemsInLevel.reduce((max, item) => {
58- // return item.elRect.height > max.elRect.height ? item : max;
59- // }, itemsInLevel[0]);
60- // currentY +=
61- // maxHeightItem.elRect.height +
62- // this.verticalPadding +
63- // this.groupPadding;
64- // }
6532 }
6633
6734 return newItems ;
@@ -77,14 +44,17 @@ export class Arrangements {
7744 gp : - this . groupPadding * 2 ,
7845 maxDepLength : 0 ,
7946 }
80- ) : { consumedHeight : number ; dep : boolean } {
47+ ) : { consumedSpace : number ; dep : boolean } {
8148 const dependents = this . list . filter ( ( child ) =>
8249 child . position . deps . includes ( baseNode . position . id )
8350 ) ;
8451
52+ const isV = this . direction === 'vertical' ;
53+
8554 let startY = baseY ;
86- let newX = baseX + baseNode . elRect . width + this . horizontalPadding ;
87- const height = baseNode . elRect . height ;
55+ const { width : w , height : h } = baseNode . elRect ;
56+ let newX = baseX + ( isV ? h : w ) + this . horizontalPadding ;
57+ const height = isV ? w : h ;
8858
8959 const childC : { first : boolean ; gp : number ; maxDepLength : number } = {
9060 first : true ,
@@ -95,7 +65,7 @@ export class Arrangements {
9565 const depLast = i === dependents . length - 1 ;
9666 childC . first = i === 0 ;
9767 const dependent = dependents [ i ] ;
98- const { consumedHeight , dep } = this . positionDependents (
68+ const { consumedSpace , dep } = this . positionDependents (
9969 dependent ,
10070 newX ,
10171 startY ,
@@ -109,7 +79,7 @@ export class Arrangements {
10979 startY += this . groupPadding ;
11080 config . gp += this . groupPadding ;
11181 }
112- startY += consumedHeight + ( ! depLast ? this . verticalPadding : 0 ) ;
82+ startY += consumedSpace + ( ! depLast ? this . verticalPadding : 0 ) ;
11383 }
11484
11585 // baseY += childC.gp;
@@ -118,12 +88,12 @@ export class Arrangements {
11888 let y = 0 ;
11989 if ( dependents . length > 1 ) {
12090 // find the first and last dependent and there y position
121- const firstDep = dependents [ 0 ] ;
122- const lastDep = dependents [ dependents . length - 1 ] ;
123- const firstDepY = newItems . get ( firstDep . position . id ) ! . y ;
124- const lastDepY = newItems . get ( lastDep . position . id ) ! . y ;
91+ const firstDepId = dependents [ 0 ] . position . id ;
92+ const lastDepId = dependents [ dependents . length - 1 ] . position . id ;
93+ const firstDep = newItems . get ( firstDepId ) ! ;
94+ const lastDep = newItems . get ( lastDepId ) ! ;
12595 // find the center of the first and last dependent
126- y = ( firstDepY + lastDepY ) / 2 ;
96+ y = ( isV ? firstDep . x + lastDep . x : firstDep . y + lastDep . y ) / 2 ;
12797 } else {
12898 y = baseY + ( dependents . length ? ( startY - baseY ) / 2 - height / 2 : 0 ) ;
12999
@@ -136,13 +106,13 @@ export class Arrangements {
136106 }
137107 newItems . set ( baseNode . position . id , {
138108 ...baseNode . position ,
139- x : baseX ,
140- y : y ,
109+ x : isV ? y : baseX ,
110+ y : isV ? baseX : y ,
141111 } ) ;
142112 // add groupPadding if there are more than one dependency
143113 const groupPad =
144114 dependents . length > 1 ? this . groupPadding - this . verticalPadding : 0 ;
145- const consumedHeight = startY + ( dependents . length ? 0 : height ) + groupPad ;
146- return { consumedHeight , dep : dependents . length > 0 } ;
115+ const consumedSpace = startY + ( dependents . length ? 0 : height ) + groupPad ;
116+ return { consumedSpace , dep : dependents . length > 0 } ;
147117 }
148118}
0 commit comments