@@ -7,6 +7,7 @@ export interface WrappedTreeWithLayout {
7
7
}
8
8
9
9
const M = Math . max
10
+ const C = 'children'
10
11
11
12
const _computeNaiveLayout = (
12
13
tree : Tree ,
@@ -19,11 +20,11 @@ const _computeNaiveLayout = (
19
20
l [ depth ] = x
20
21
counters . mX = M ( counters . mX , x )
21
22
22
- const tc = tree . children
23
+ const tc = tree [ C ]
23
24
return {
24
25
data : tree . data ,
25
26
children : tc ?. map ( ( child : Readonly < TreeChild > ) => ( {
26
- edgeData : child . edgeData ,
27
+ eData : child . eData ,
27
28
node : _computeNaiveLayout ( child . node , depth + 1 , counters ) ,
28
29
} ) ) ,
29
30
meta : {
@@ -49,32 +50,36 @@ type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> }
49
50
50
51
const _addMods = (
51
52
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
52
- tree : DeepWriteable < TreeWithLayout > ,
53
+ { meta , children } : DeepWriteable < TreeWithLayout > ,
53
54
modsum = 0 ,
54
55
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
55
56
tracer : { mX : number } ,
56
57
) : void => {
57
- tree . meta . pos . x += modsum
58
- tracer . mX = M ( tracer . mX , tree . meta . pos . x )
58
+ meta . pos . x += modsum
59
+ tracer . mX = M ( tracer . mX , meta . pos . x )
59
60
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
60
- modsum += tree . meta . m ! // We know it's defined because we control when it's called
61
- for ( const child of tree . children ?? [ ] ) {
61
+ modsum += meta . m ! // We know it's defined because we control when it's called
62
+ for ( const child of children ?? [ ] ) {
62
63
_addMods ( child . node , modsum , tracer )
63
64
}
64
65
}
65
66
66
- const _computeCenter2Layout = (
67
+ const _getPosX = ( v : { readonly node : Readonly < TreeWithLayout > } ) : number => {
68
+ return v . node . meta . pos . x
69
+ }
70
+
71
+ const _computeSmartLayout = (
67
72
tree : Tree ,
68
73
depth = 0 ,
69
74
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
70
75
offsets : number [ ] ,
71
76
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
72
77
tracer : { mX : number } ,
73
78
) : DeepWriteable < TreeWithLayout > => {
74
- const tc = tree . children
79
+ const tc = tree [ C ]
75
80
const children = tc ?. map ( ( child ) => ( {
76
- edgeData : child . edgeData ,
77
- node : _computeCenter2Layout ( child . node , depth + 1 , offsets , tracer ) ,
81
+ eData : child . eData ,
82
+ node : _computeSmartLayout ( child . node , depth + 1 , offsets , tracer ) ,
78
83
} ) )
79
84
80
85
let x : number
@@ -84,12 +89,12 @@ const _computeCenter2Layout = (
84
89
x = offsets [ depth ] ?? 0
85
90
} else if ( numChildren === 1 ) {
86
91
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
87
- x = children ! [ 0 ] ! . node . meta . pos . x
92
+ x = _getPosX ( children ! [ 0 ] ! )
88
93
} else {
89
94
const c = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
90
- ( children ! [ 0 ] ! . node . meta . pos . x +
95
+ ( _getPosX ( children ! [ 0 ] ! ) +
91
96
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
92
- children ! [ numChildren - 1 ] ! . node . meta . pos . x ) *
97
+ _getPosX ( children ! [ numChildren - 1 ] ! ) ) *
93
98
0.5
94
99
x = M ( offsets [ depth ] ?? 0 , c )
95
100
m = x - c
@@ -126,12 +131,12 @@ const _inPlaceEvenSpacingUpdate = (
126
131
tmp . x += shift
127
132
} else if ( numChildren === 1 ) {
128
133
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
129
- tmp . x = tree . children ! [ 0 ] ! . node . meta . pos . x
134
+ tmp . x = _getPosX ( tree [ C ] ! [ 0 ] ! )
130
135
} else {
131
136
const c = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
132
- ( tree . children ! [ 0 ] ! . node . meta . pos . x +
137
+ ( _getPosX ( tree [ C ] ! [ 0 ] ! ) +
133
138
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
134
- tree . children ! [ numChildren - 1 ] ! . node . meta . pos . x ) *
139
+ _getPosX ( tree [ C ] ! [ numChildren - 1 ] ! ) ) *
135
140
0.5
136
141
tmp . x = M ( offsets [ depth ] ?? 0 , c )
137
142
}
@@ -151,18 +156,18 @@ const _siblingsEvenSpacing = (
151
156
shift = 0 ,
152
157
// eslint-disable-next-line sonarjs/cognitive-complexity
153
158
) : void => {
154
- const tc = tree . children
159
+ const tc = tree [ C ]
155
160
const numChildren = tc ?. length ?? 0
156
161
let lastFixedIdx : number | undefined
157
162
let maxSpacing = 1
158
163
for ( const [ idx , { node } ] of ( tc ?? [ ] ) . entries ( ) ) {
159
- const isFixed = ( node . children ?. length ?? 0 ) > 0
164
+ const isFixed = ( node [ C ] ?. length ?? 0 ) > 0
160
165
if ( isFixed ) {
161
166
if ( lastFixedIdx !== undefined ) {
162
167
const spacing =
163
168
( node . meta . pos . x -
164
169
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
165
- tc ! [ lastFixedIdx ] ! . node . meta . pos . x ) /
170
+ _getPosX ( tc ! [ lastFixedIdx ] ! ) ) /
166
171
( idx - lastFixedIdx )
167
172
maxSpacing = M ( maxSpacing , spacing )
168
173
}
@@ -178,7 +183,7 @@ const _siblingsEvenSpacing = (
178
183
0 ,
179
184
shift +
180
185
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
181
- tc ! [ 1 ] ! . node . meta . pos . x -
186
+ _getPosX ( tc ! [ 1 ] ! ) -
182
187
maxSpacing -
183
188
node . meta . pos . x ,
184
189
)
@@ -189,7 +194,7 @@ const _siblingsEvenSpacing = (
189
194
M (
190
195
0 ,
191
196
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
192
- tc ! [ idx - 1 ] ! . node . meta . pos . x + maxSpacing - node . meta . pos . x ,
197
+ _getPosX ( tc ! [ idx - 1 ] ! ) + maxSpacing - node . meta . pos . x ,
193
198
)
194
199
}
195
200
_siblingsEvenSpacing ( node , offsets , tracer , depth + 1 , accShift )
@@ -208,7 +213,7 @@ const _cousinsEvenSpacing = (
208
213
depth = 0 ,
209
214
shift = 0 ,
210
215
) : void => {
211
- const tc = tree . children
216
+ const tc = tree [ C ]
212
217
const numChildren = tc ?. length ?? 0
213
218
214
219
const nextOffset = offsets [ depth + 1 ]
@@ -217,12 +222,12 @@ const _cousinsEvenSpacing = (
217
222
numChildren >= 2 &&
218
223
nextOffset !== undefined &&
219
224
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
220
- ( tc ! [ 0 ] ! . node . children ?. length ?? 0 ) === 0
225
+ ( tc ! [ 0 ] ! . node [ C ] ?. length ?? 0 ) === 0
221
226
) {
222
227
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
223
- const mid = tc ! [ 1 ] ! . node . meta . pos . x - 1
228
+ const mid = _getPosX ( tc ! [ 1 ] ! ) - 1
224
229
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
225
- accShift = shift + M ( 0 , mid - tc ! [ 0 ] ! . node . meta . pos . x )
230
+ accShift = shift + M ( 0 , mid - _getPosX ( tc ! [ 0 ] ! ) )
226
231
}
227
232
228
233
for ( const [ idx , { node } ] of ( tc ?? [ ] ) . entries ( ) ) {
@@ -242,7 +247,7 @@ export const computeSmartLayout = (
242
247
const offsets : number [ ] = [ ]
243
248
const tracer = { mX : 0 }
244
249
245
- const t = _computeCenter2Layout ( tree , 0 , offsets , tracer )
250
+ const t = _computeSmartLayout ( tree , 0 , offsets , tracer )
246
251
_addMods ( t , 0 , tracer )
247
252
248
253
_cousinsEvenSpacing ( t , [ ] , tracer )
0 commit comments