Skip to content

Commit 6cfd18f

Browse files
committed
fix: centered layout regularity
Signed-off-by: Andres Correa Casablanca <castarco@coderspirit.xyz>
1 parent 4029be4 commit 6cfd18f

File tree

3 files changed

+25
-41
lines changed

3 files changed

+25
-41
lines changed

src/BeautifulTree.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { edgesIterator, postOrderIterator } from './traversal'
22
import type { Tree } from './types'
33
import type { WrappedTreeWithLayout } from './layouts'
4-
export { computeLeftShiftLayout, computeCenter1Layout } from './layouts'
4+
export { computeLeftShiftLayout, computeCenter2Layout } from './layouts'
55

66
export interface BeautifulTreeProps {
77
readonly id: string

src/layouts.ts

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,76 +61,60 @@ const _addMods = (
6161
}
6262
}
6363

64-
const _computeCenter1Layout = (
64+
const _computeCenter2Layout = (
6565
tree: Tree,
6666
depth = 0,
6767
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
6868
offsets: number[],
69-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
70-
nexts: number[],
7169
): DeepWriteable<TreeWithLayout> => {
7270
const children = tree.children?.map((child) => ({
7371
edgeData: child.edgeData,
74-
node: _computeCenter1Layout(child.node, depth + 1, offsets, nexts),
72+
node: _computeCenter2Layout(child.node, depth + 1, offsets),
7573
}))
7674

77-
let place: number
7875
let x: number
79-
76+
let m = 0
8077
const numChildren = tree.children?.length ?? 0
8178
if (numChildren === 0) {
82-
place = nexts[depth] ?? 0
83-
x = place
79+
x = offsets[depth] ?? 0
8480
} else if (numChildren === 1) {
8581
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
86-
place = children![0]!.node.meta.pos.x
82+
x = children![0]!.node.meta.pos.x
8783
} else {
88-
place =
89-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
84+
const c = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
9085
(children![0]!.node.meta.pos.x +
9186
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
9287
children![numChildren - 1]!.node.meta.pos.x) *
9388
0.5
89+
x = Math.max(offsets[depth] ?? 0, c)
90+
m = x - c
9491
}
95-
96-
offsets[depth] = Math.max(offsets[depth] ?? 0, nexts[depth] ?? 0 - place)
97-
if (numChildren > 0) {
98-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
99-
x = place + offsets[depth]!
100-
}
101-
nexts[depth] = (nexts[depth] ?? 0) + 1
92+
offsets[depth] = 1 + x
10293

10394
return {
10495
data: tree.data,
10596
children,
10697
meta: {
10798
isRoot: depth === 0,
10899
isLeaf: tree.children === undefined || tree.children.length === 0,
109-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
110-
pos: { x: x!, y: depth },
111-
m: offsets[depth],
100+
pos: { x, y: depth },
101+
m,
112102
},
113103
} satisfies Readonly<TreeWithLayout> as DeepWriteable<TreeWithLayout>
114104
}
115105

116-
export const computeCenter1Layout = (
106+
export const computeCenter2Layout = (
117107
tree: Readonly<Tree>,
118108
): Readonly<WrappedTreeWithLayout> => {
119-
const nexts: number[] = []
120-
const t = _computeCenter1Layout(tree, 0, [], nexts)
121-
122-
console.log('without mods')
123-
console.log(t)
109+
const offsets: number[] = []
110+
const t = _computeCenter2Layout(tree, 0, offsets)
124111

125112
const tracer = { maxX: 0 }
126113
_addMods(t, 0, tracer)
127114

128-
console.log('with mods')
129-
console.log(t)
130-
131115
return {
132116
tree: t,
133-
maxY: nexts.length - 1,
117+
maxY: offsets.length - 1,
134118
maxX: tracer.maxX,
135119
}
136120
}

src/stories/BeautifulTree.stories.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
BeautifulTree,
3-
computeCenter1Layout,
3+
computeCenter2Layout,
44
computeLeftShiftLayout,
55
} from '../BeautifulTree'
66
import type { Meta, StoryObj } from '@storybook/react'
@@ -97,7 +97,7 @@ const bigTree = {
9797

9898
export const LeftShifted_Tree: Story = {
9999
args: {
100-
id: 'leftshifted-beautiful-tree',
100+
id: 'leftshifted-small-tree',
101101
svgProps: {
102102
width: 100,
103103
height: 100,
@@ -109,7 +109,7 @@ export const LeftShifted_Tree: Story = {
109109

110110
export const LeftShifted_Big_Tree: Story = {
111111
args: {
112-
id: 'leftshifted-beautiful-tree',
112+
id: 'leftshifted-big-tree',
113113
svgProps: {
114114
width: 100,
115115
height: 100,
@@ -119,26 +119,26 @@ export const LeftShifted_Big_Tree: Story = {
119119
},
120120
}
121121

122-
export const Centered1_Tree: Story = {
122+
export const Centered2_Tree: Story = {
123123
args: {
124-
id: 'centered1-beautiful-tree',
124+
id: 'centered2-small-tree',
125125
svgProps: {
126126
width: 100,
127127
height: 100,
128128
},
129129
tree: smallTree,
130-
computeLayout: computeCenter1Layout,
130+
computeLayout: computeCenter2Layout,
131131
},
132132
}
133133

134-
export const Centered1_Big_Tree: Story = {
134+
export const Centered2_Big_Tree: Story = {
135135
args: {
136-
id: 'centered1-beautiful-tree',
136+
id: 'centered2-big-tree',
137137
svgProps: {
138138
width: 100,
139139
height: 100,
140140
},
141141
tree: bigTree,
142-
computeLayout: computeCenter1Layout,
142+
computeLayout: computeCenter2Layout,
143143
},
144144
}

0 commit comments

Comments
 (0)