Skip to content

Commit a1a1ff1

Browse files
committed
fix: use post-order for siblings even spacing
Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
1 parent 4c88346 commit a1a1ff1

File tree

2 files changed

+32
-34
lines changed

2 files changed

+32
-34
lines changed

src/layouts.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,15 @@ const _siblingsEvenSpacing = (
155155
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
156156
tracer: { mX: number },
157157
depth = 0,
158-
shift = 0,
159158
// eslint-disable-next-line sonarjs/cognitive-complexity
160159
): void => {
161160
const tc = tree[C]
162161
const numChildren = tc?.length ?? 0
162+
163+
for (const { node } of tc ?? []) {
164+
_siblingsEvenSpacing(node, offsets, tracer, depth + 1)
165+
}
166+
163167
let lastFixedIdx: number | undefined
164168
let maxSpacing = 1
165169
for (const [idx, { node }] of (tc ?? []).entries()) {
@@ -177,32 +181,26 @@ const _siblingsEvenSpacing = (
177181
}
178182
}
179183

180-
let accShift = shift
184+
let accShift = 0
181185
for (const [idx, { node }] of (tc ?? []).entries()) {
182186
if (idx === 0) {
183187
if (numChildren > 1) {
184188
accShift = M(
185189
0,
186-
shift +
187-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
188-
_getPosX(tc![1]!) -
189-
maxSpacing -
190-
node.meta.pos.x,
190+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
191+
_getPosX(tc![1]!) - maxSpacing - node.meta.pos.x,
191192
)
192193
}
193194
} else {
194-
accShift =
195-
shift +
196-
M(
197-
0,
198-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
199-
_getPosX(tc![idx - 1]!) + maxSpacing - node.meta.pos.x,
200-
)
195+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
196+
accShift = _getPosX(tc![idx - 1]!) + maxSpacing - node.meta.pos.x
201197
}
202-
_siblingsEvenSpacing(node, offsets, tracer, depth + 1, accShift)
198+
199+
node.meta.pos.x += accShift
200+
node.meta.m = (node.meta.m ?? 0) + accShift
203201
}
204202

205-
_inPlaceEvenSpacingUpdate(numChildren, tree, shift, offsets, depth, tracer)
203+
_inPlaceEvenSpacingUpdate(numChildren, tree, 0, offsets, depth, tracer)
206204
}
207205

208206
const _cousinsEvenSpacing = (

src/stories/BeautifulTree.stories.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ export const LeftShifted_Tree: Story = {
3939
args: {
4040
id: 'leftshifted-small-tree',
4141
svgProps: {
42-
width: 100,
43-
height: 100,
42+
width: 400,
43+
height: 400,
4444
},
4545
tree: smallTree,
4646
computeLayout: computeNaiveLayout,
@@ -51,8 +51,8 @@ export const LeftShifted_Big_Tree: Story = {
5151
args: {
5252
id: 'leftshifted-big-tree',
5353
svgProps: {
54-
width: 100,
55-
height: 100,
54+
width: 400,
55+
height: 400,
5656
},
5757
tree: bigTree,
5858
computeLayout: computeNaiveLayout,
@@ -63,8 +63,8 @@ export const Centered3_Tree: Story = {
6363
args: {
6464
id: 'centered3-small-tree',
6565
svgProps: {
66-
width: 100,
67-
height: 100,
66+
width: 400,
67+
height: 400,
6868
},
6969
tree: smallTree,
7070
computeLayout: computeSmartLayout,
@@ -77,8 +77,8 @@ export const Centered3_Big_Tree: Story = {
7777
args: {
7878
id: 'centered3-big-tree',
7979
svgProps: {
80-
width: 100,
81-
height: 100,
80+
width: 400,
81+
height: 400,
8282
},
8383
tree: bigTree,
8484
computeLayout: computeSmartLayout,
@@ -91,8 +91,8 @@ export const Centered3_Wide_Tree_A: Story = {
9191
args: {
9292
id: 'centered3-wide-tree-a',
9393
svgProps: {
94-
width: 100,
95-
height: 100,
94+
width: 400,
95+
height: 400,
9696
},
9797
tree: wideTree_A,
9898
computeLayout: computeSmartLayout,
@@ -106,8 +106,8 @@ export const Centered3_Wide_Tree_B: Story = {
106106
args: {
107107
id: 'centered3-wide-tree-b',
108108
svgProps: {
109-
width: 100,
110-
height: 100,
109+
width: 400,
110+
height: 400,
111111
},
112112
tree: wideTree_B,
113113
computeLayout: computeSmartLayout,
@@ -120,8 +120,8 @@ export const Centered3_Wide_Tree_Bm: Story = {
120120
args: {
121121
id: 'centered3-wide-tree-bm',
122122
svgProps: {
123-
width: 100,
124-
height: 100,
123+
width: 400,
124+
height: 400,
125125
},
126126
tree: wideTree_Bm,
127127
computeLayout: computeSmartLayout,
@@ -134,8 +134,8 @@ export const Centered3_Wide_Tree_C: Story = {
134134
args: {
135135
id: 'centered3-wide-tree-c',
136136
svgProps: {
137-
width: 100,
138-
height: 100,
137+
width: 400,
138+
height: 400,
139139
},
140140
tree: wideTree_C,
141141
computeLayout: computeSmartLayout,
@@ -148,8 +148,8 @@ export const Centered3_Wide_Tree_D: Story = {
148148
args: {
149149
id: 'centered3-wide-tree-d',
150150
svgProps: {
151-
width: 100,
152-
height: 100,
151+
width: 400,
152+
height: 400,
153153
},
154154
tree: wideTree_D,
155155
computeLayout: computeSmartLayout,

0 commit comments

Comments
 (0)