-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtree.js
131 lines (117 loc) · 2.74 KB
/
tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Konva from "Konva";
const stage = new Konva.Stage({
container: "container",
width: window.innerWidth,
height: window.innerHeight,
});
let layer = new Konva.Layer();
const r = 40;
const boxX = 80;
const boxY = 60;
const drawNode = function ({ x, y, id, isDelete, value }, preNode) {
const circle = new Konva.Circle({
radius: r / 2,
x,
y,
fill: isDelete ? "#D3D3D3" : "red",
stroke: "black",
strokeWidth: 5,
});
layer.add(circle);
};
const drawText = function ({ x, y, id, isDelete, value }, preNode) {
const t = JSON.stringify(value);
const text = new Konva.Text({
x: x - 8,
y: y - 15,
text: t.slice(1, t.length - 1),
fontSize: 30,
fontFamily: "Calibri",
fill: "white",
});
layer.add(text);
};
const drawArraw = function ({ x, y, id, isDelete, value }, preNode) {
if (id !== preNode.id) {
const arrow = new Konva.Arrow({
x: 0,
y: 0,
points: [x, y, preNode.x, preNode.y],
pointerLength: 8,
pointerWidth: 8,
fill: "black",
stroke: "black",
strokeWidth: 5,
});
layer.add(arrow);
}
};
const draw = function (tree) {
layer.clear();
layer.destroy();
layer = new Konva.Layer();
stage.add(layer);
console.log(tree);
const ns = new Map();
const setX = function (node, x) {
let info = ns.get(node.word.id.toString());
if (!info) {
info = {};
}
info.x = x;
info.id = node.word.id.toString();
info.value = node.word.value;
info.isDelete = node.word.isDelete;
info.preId = node.word.preId.toString();
ns.set(node.word.id.toString(), info);
};
const setY = function (node, y) {
let info = ns.get(node.word.id.toString());
if (!info) {
info = {};
}
info.y = y;
ns.set(node.word.id.toString(), info);
};
const getWidth = function (tree, left) {
if (tree.nextNode.length <= 0) {
setX(tree, left + boxX / 2);
return boxX;
}
let w = 0;
for (let n of tree.nextNode) {
w = w + getWidth(n, left + w);
}
setX(tree, left + w / 2);
return w;
};
const getHight = function (tree, h) {
setY(tree, h);
if (tree.nextNode.length <= 0) return h;
for (let n of tree.nextNode) {
getHight(n, h + boxY);
}
};
getWidth(tree, 0);
getHight(tree, r);
console.log(ns);
for (const node of ns.values()) {
const preNode = ns.get(node.preId);
drawArraw(node, preNode);
}
for (const node of ns.values()) {
const preNode = ns.get(node.preId);
drawNode(node, preNode);
}
for (const node of ns.values()) {
const preNode = ns.get(node.preId);
drawText(node, preNode);
}
layer.draw();
window.l = layer;
};
const clear = function () {
layer.clear();
layer.destroy();
};
export { draw, clear };