-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathlayout.go
216 lines (187 loc) · 5.34 KB
/
layout.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package d2graph
import (
"strings"
"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2parser"
"oss.terrastruct.com/d2/d2target"
"oss.terrastruct.com/d2/lib/geo"
"oss.terrastruct.com/d2/lib/label"
"oss.terrastruct.com/d2/lib/shape"
)
func (obj *Object) MoveWithDescendants(dx, dy float64) {
obj.TopLeft.X += dx
obj.TopLeft.Y += dy
for _, child := range obj.ChildrenArray {
child.MoveWithDescendants(dx, dy)
}
}
func (obj *Object) MoveWithDescendantsTo(x, y float64) {
dx := x - obj.TopLeft.X
dy := y - obj.TopLeft.Y
obj.MoveWithDescendants(dx, dy)
}
func (parent *Object) removeChild(child *Object) {
delete(parent.Children, strings.ToLower(child.ID))
for i := 0; i < len(parent.ChildrenArray); i++ {
if parent.ChildrenArray[i] == child {
parent.ChildrenArray = append(parent.ChildrenArray[:i], parent.ChildrenArray[i+1:]...)
break
}
}
}
// remove obj and all descendants from graph, as a new Graph
func (g *Graph) ExtractAsNestedGraph(obj *Object) *Graph {
descendantObjects, edges := pluckObjAndEdges(g, obj)
tempGraph := NewGraph()
tempGraph.Root.ChildrenArray = []*Object{obj}
tempGraph.Root.Children[strings.ToLower(obj.ID)] = obj
for _, descendantObj := range descendantObjects {
descendantObj.Graph = tempGraph
}
tempGraph.Objects = descendantObjects
tempGraph.Edges = edges
obj.Parent.removeChild(obj)
obj.Parent = tempGraph.Root
return tempGraph
}
func pluckObjAndEdges(g *Graph, obj *Object) (descendantsObjects []*Object, edges []*Edge) {
for i := 0; i < len(g.Edges); i++ {
edge := g.Edges[i]
if edge.Src == obj || edge.Dst == obj {
edges = append(edges, edge)
g.Edges = append(g.Edges[:i], g.Edges[i+1:]...)
i--
}
}
for i := 0; i < len(g.Objects); i++ {
temp := g.Objects[i]
if temp.AbsID() == obj.AbsID() {
descendantsObjects = append(descendantsObjects, obj)
g.Objects = append(g.Objects[:i], g.Objects[i+1:]...)
for _, child := range obj.ChildrenArray {
subObjects, subEdges := pluckObjAndEdges(g, child)
descendantsObjects = append(descendantsObjects, subObjects...)
edges = append(edges, subEdges...)
}
break
}
}
return descendantsObjects, edges
}
func (g *Graph) InjectNestedGraph(tempGraph *Graph, parent *Object) {
obj := tempGraph.Root.ChildrenArray[0]
obj.MoveWithDescendantsTo(0, 0)
obj.Parent = parent
for _, obj := range tempGraph.Objects {
obj.Graph = g
}
g.Objects = append(g.Objects, tempGraph.Objects...)
parent.Children[strings.ToLower(obj.ID)] = obj
parent.ChildrenArray = append(parent.ChildrenArray, obj)
g.Edges = append(g.Edges, tempGraph.Edges...)
}
// ShiftDescendants moves Object's descendants (not including itself)
// descendants' edges are also moved by the same dx and dy (the whole route is moved if both ends are a descendant)
func (obj *Object) ShiftDescendants(dx, dy float64) {
// also need to shift edges of descendants that are shifted
movedEdges := make(map[*Edge]struct{})
for _, e := range obj.Graph.Edges {
isSrcDesc := e.Src.IsDescendantOf(obj)
isDstDesc := e.Dst.IsDescendantOf(obj)
if isSrcDesc && isDstDesc {
movedEdges[e] = struct{}{}
for _, p := range e.Route {
p.X += dx
p.Y += dy
}
}
}
obj.IterDescendants(func(_, curr *Object) {
curr.TopLeft.X += dx
curr.TopLeft.Y += dy
for _, e := range obj.Graph.Edges {
if _, ok := movedEdges[e]; ok {
continue
}
isSrc := e.Src == curr
isDst := e.Dst == curr
if isSrc && isDst {
for _, p := range e.Route {
p.X += dx
p.Y += dy
}
} else if isSrc {
e.Route[0].X += dx
e.Route[0].Y += dy
} else if isDst {
e.Route[len(e.Route)-1].X += dx
e.Route[len(e.Route)-1].Y += dy
}
if isSrc || isDst {
movedEdges[e] = struct{}{}
}
}
})
}
// GetModifierElementAdjustments returns width/height adjustments to account for shapes with 3d or multiple
func (obj *Object) GetModifierElementAdjustments() (dx, dy float64) {
if obj.Is3D() {
if obj.Shape.Value == d2target.ShapeHexagon {
dy = d2target.THREE_DEE_OFFSET / 2
} else {
dy = d2target.THREE_DEE_OFFSET
}
dx = d2target.THREE_DEE_OFFSET
} else if obj.IsMultiple() {
dy = d2target.MULTIPLE_OFFSET
dx = d2target.MULTIPLE_OFFSET
}
return dx, dy
}
func (obj *Object) ToShape() shape.Shape {
tl := obj.TopLeft
if tl == nil {
tl = geo.NewPoint(0, 0)
}
dslShape := strings.ToLower(obj.Shape.Value)
shapeType := d2target.DSL_SHAPE_TO_SHAPE_TYPE[dslShape]
contentBox := geo.NewBox(tl, obj.Width, obj.Height)
return shape.NewShape(shapeType, contentBox)
}
func (obj *Object) GetLabelTopLeft() *geo.Point {
if obj.LabelPosition == nil {
return nil
}
s := obj.ToShape()
labelPosition := label.Position(*obj.LabelPosition)
var box *geo.Box
if labelPosition.IsOutside() {
box = s.GetBox()
} else {
box = s.GetInnerBox()
}
labelTL := labelPosition.GetPointOnBox(box, label.PADDING,
float64(obj.LabelDimensions.Width),
float64(obj.LabelDimensions.Height),
)
return labelTL
}
type LayoutError struct {
Errors []d2ast.Error `json:"errs"`
}
func (le LayoutError) Empty() bool {
return len(le.Errors) == 0
}
func (le LayoutError) Error() string {
var sb strings.Builder
for i, err := range le.Errors {
if i > 0 {
sb.WriteByte('\n')
}
sb.WriteString(err.Error())
}
return sb.String()
}
func (g *Graph) errorf(n d2ast.Node, f string, v ...interface{}) {
g.err.Errors = append(g.err.Errors, d2parser.Errorf(n, f, v...).(d2ast.Error))
}