-
Notifications
You must be signed in to change notification settings - Fork 2
/
filedata.js
168 lines (145 loc) · 4.08 KB
/
filedata.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
function GetTreeRepresentation(rootNode)
{
let nodeChildren = {}
let nodeData = {}
let nodeArrows = {}
let register = (node) =>
{
// add my personal data
nodeData[node.id] = {
text : node.text,
color : node.color,
isRoot : !node.parent,
}
// add my arrows
nodeArrows[node.id] = []
for (let i=0; i<node.arrows.length; i++)
{
let arrow = node.arrows[i]
if (arrow && arrow.from && arrow.to)
{
nodeArrows[node.id].push({
from : arrow.from.id,
to : arrow.to.id,
anchorPoints : [arrow.xoff1, arrow.yoff1, arrow.xoff2, arrow.yoff2],
})
}
}
// add my children, recursively
nodeChildren[node.id] = []
for (const child in node.children)
{
register(node.children[child])
nodeChildren[node.id].push(node.children[child].id)
}
}
register(rootNode)
return {
nodeChildren : nodeChildren,
nodeData : nodeData,
nodeArrows : nodeArrows,
maxIndex : CurrentNodeID,
version : 3.1,
}
}
function LoadTreeRepresentation(treeRep)
{
SpatialHash = {}
let idToNode = {}
let root = null
for (const id in treeRep.nodeChildren)
{
let data = treeRep.nodeData[id]
let node = idToNode[id]
if (!node)
{
node = new TreeNode(0,0)
idToNode[id] = node
AddToSpatialHash(node.x,node.y, node)
}
node.text = data.text
node.color = data.color
node.id = id
node.recalculate()
node.countSpaces()
if (data.isRoot)
root = node
for (let i=0; i<treeRep.nodeChildren[id].length; i++)
{
let childNode = idToNode[treeRep.nodeChildren[id][i]]
if (!childNode)
{
childNode = new TreeNode(0,0)
AddToSpatialHash(childNode.x,childNode.y, childNode)
idToNode[treeRep.nodeChildren[id][i]] = childNode
}
childNode.parent = node
node.children.push(childNode)
childNode.recalculate()
}
}
root.recalculate()
Trees[0] = root
CurrentNodeID = treeRep.maxIndex+1
for (const id in treeRep.nodeArrows)
{
for (let i=0; i<treeRep.nodeArrows[id].length; i++)
{
let arrowData = treeRep.nodeArrows[id][i]
let nodeFrom = idToNode[arrowData.from]
let nodeTo = idToNode[arrowData.to]
let arrow = new Arrow(nodeFrom)
arrow.to = nodeTo
if (nodeTo)
nodeFrom.arrows.push(arrow)
arrow.xoff1 = arrowData.anchorPoints[0]
arrow.yoff1 = arrowData.anchorPoints[1]
arrow.xoff2 = arrowData.anchorPoints[2]
arrow.yoff2 = arrowData.anchorPoints[3]
}
}
}
const UndoMax = 20
function AddChange(dontStore)
{
let treeRep = GetTreeRepresentation(Trees[0])
// return if there is no difference between this state and last state
//print(UndoIndex, UndoList[UndoIndex], UndoList[UndoIndex] && JSON.stringify(treeRep) === JSON.stringify(UndoList[UndoIndex]))
if (UndoList[UndoIndex] && JSON.stringify(treeRep) === JSON.stringify(UndoList[UndoIndex])) { return }
UndoIndex += 1
UndoList[UndoIndex] = treeRep
if (!dontStore)
storeItem("treeBackup", treeRep)
while (UndoIndex > UndoMax)
{
UndoList.splice(0,1)
UndoIndex -= 1
}
MostCurrentUndoIndex = UndoIndex
}
function AttemptAddChange()
{
if (TreeHasChanged)
{
TreeHasChanged = false
AddChange()
}
}
function UndoChange()
{
SelectionList = {}
if (UndoIndex > 1)
{
UndoIndex -= 1
LoadTreeRepresentation(UndoList[UndoIndex])
}
}
function RedoChange()
{
SelectionList = {}
if (UndoIndex < MostCurrentUndoIndex)
{
UndoIndex += 1
LoadTreeRepresentation(UndoList[UndoIndex])
}
}