@@ -13,55 +13,22 @@ export class Arrangements {
13
13
public autoArrange ( ) : Map < string , FlowOptions > {
14
14
const newItems = new Map < string , FlowOptions > ( ) ;
15
15
let currentX = 0 ;
16
+ let currentY = 0 ;
16
17
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
+ ) ;
24
22
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 ) ;
38
26
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 ;
39
31
}
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
- // }
65
32
}
66
33
67
34
return newItems ;
@@ -77,14 +44,17 @@ export class Arrangements {
77
44
gp : - this . groupPadding * 2 ,
78
45
maxDepLength : 0 ,
79
46
}
80
- ) : { consumedHeight : number ; dep : boolean } {
47
+ ) : { consumedSpace : number ; dep : boolean } {
81
48
const dependents = this . list . filter ( ( child ) =>
82
49
child . position . deps . includes ( baseNode . position . id )
83
50
) ;
84
51
52
+ const isV = this . direction === 'vertical' ;
53
+
85
54
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 ;
88
58
89
59
const childC : { first : boolean ; gp : number ; maxDepLength : number } = {
90
60
first : true ,
@@ -95,7 +65,7 @@ export class Arrangements {
95
65
const depLast = i === dependents . length - 1 ;
96
66
childC . first = i === 0 ;
97
67
const dependent = dependents [ i ] ;
98
- const { consumedHeight , dep } = this . positionDependents (
68
+ const { consumedSpace , dep } = this . positionDependents (
99
69
dependent ,
100
70
newX ,
101
71
startY ,
@@ -109,7 +79,7 @@ export class Arrangements {
109
79
startY += this . groupPadding ;
110
80
config . gp += this . groupPadding ;
111
81
}
112
- startY += consumedHeight + ( ! depLast ? this . verticalPadding : 0 ) ;
82
+ startY += consumedSpace + ( ! depLast ? this . verticalPadding : 0 ) ;
113
83
}
114
84
115
85
// baseY += childC.gp;
@@ -118,12 +88,12 @@ export class Arrangements {
118
88
let y = 0 ;
119
89
if ( dependents . length > 1 ) {
120
90
// 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 ) ! ;
125
95
// 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 ;
127
97
} else {
128
98
y = baseY + ( dependents . length ? ( startY - baseY ) / 2 - height / 2 : 0 ) ;
129
99
@@ -136,13 +106,13 @@ export class Arrangements {
136
106
}
137
107
newItems . set ( baseNode . position . id , {
138
108
...baseNode . position ,
139
- x : baseX ,
140
- y : y ,
109
+ x : isV ? y : baseX ,
110
+ y : isV ? baseX : y ,
141
111
} ) ;
142
112
// add groupPadding if there are more than one dependency
143
113
const groupPad =
144
114
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 } ;
147
117
}
148
118
}
0 commit comments