1
1
import { edgesIterator , postOrderIterator } from './traversal'
2
2
import type { Tree } from './types'
3
+ import type { WrappedTreeWithLayout } from './layouts'
3
4
import { computeLeftShiftLayout } from './layouts'
4
5
5
6
export interface BeautifulTreeProps {
@@ -10,14 +11,18 @@ export interface BeautifulTreeProps {
10
11
readonly sizeUnit ?: '%' | 'em' | 'px' | 'rem'
11
12
}
12
13
readonly tree : Tree
14
+ readonly computeLayout ?: (
15
+ tree : Readonly < Tree > ,
16
+ ) => Readonly < WrappedTreeWithLayout >
13
17
}
14
18
15
19
export function BeautifulTree ( {
16
20
id,
17
21
svgProps,
18
22
tree,
23
+ computeLayout = computeLeftShiftLayout ,
19
24
} : Readonly < BeautifulTreeProps > ) : JSX . Element {
20
- const { tree : treeWithLayout , maxX, maxY } = computeLeftShiftLayout ( tree )
25
+ const { tree : treeWithLayout , maxX, maxY } = computeLayout ( tree )
21
26
const { width, height, sizeUnit = 'px' } = svgProps
22
27
23
28
const xDivisor = maxX + 2
@@ -34,6 +39,10 @@ export function BeautifulTree({
34
39
} }
35
40
className = { 'beautiful-tree-react' }
36
41
>
42
+ < style > { `
43
+ line { stroke: black; }
44
+ circle { stroke: black; fill: white; }
45
+ ` } </ style >
37
46
{ /* TODO: introduce edge "styles" (straight, cornered, curved..., plus CSS styles) */ }
38
47
{ [ ...edgesIterator ( treeWithLayout ) ] . map ( ( edge , idx ) => {
39
48
return (
@@ -43,11 +52,9 @@ export function BeautifulTree({
43
52
y1 = { ( ( edge . start . y + 1 ) * height ) / yDivisor }
44
53
x2 = { ( ( edge . end . x + 1 ) * width ) / xDivisor }
45
54
y2 = { ( ( edge . end . y + 1 ) * height ) / yDivisor }
46
- stroke = "black"
47
55
/>
48
56
)
49
57
} ) }
50
-
51
58
{ [ ...postOrderIterator ( treeWithLayout ) ] . map ( ( node , idx ) => {
52
59
const aX = node . meta . abstractPosition . x
53
60
const aY = node . meta . abstractPosition . y
@@ -57,8 +64,6 @@ export function BeautifulTree({
57
64
className = { 'beautiful-tree-node' }
58
65
cx = { ( ( aX + 1 ) * width ) / xDivisor }
59
66
cy = { ( ( aY + 1 ) * height ) / yDivisor }
60
- stroke = "black"
61
- fill = "white"
62
67
r = "5"
63
68
/>
64
69
)
0 commit comments