2023-09-14 20:38:59 +00:00
|
|
|
package d2layouts
|
|
|
|
|
|
2023-09-14 21:17:18 +00:00
|
|
|
import (
|
2023-09-15 00:00:44 +00:00
|
|
|
"math"
|
2023-09-14 22:38:52 +00:00
|
|
|
"strings"
|
|
|
|
|
|
2023-09-14 21:17:18 +00:00
|
|
|
"oss.terrastruct.com/d2/d2graph"
|
2023-09-15 00:00:44 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/geo"
|
2023-09-14 21:17:18 +00:00
|
|
|
)
|
2023-09-14 20:38:59 +00:00
|
|
|
|
2023-09-14 21:17:18 +00:00
|
|
|
type GraphType string
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
DefaultGraphType GraphType = ""
|
|
|
|
|
ConstantNearGraph GraphType = "constant-near"
|
|
|
|
|
GridDiagram GraphType = "grid-diagram"
|
|
|
|
|
SequenceDiagram GraphType = "sequence-diagram"
|
|
|
|
|
)
|
|
|
|
|
|
2023-09-15 00:00:44 +00:00
|
|
|
func LayoutNested(g *d2graph.Graph, graphType GraphType, coreLayout d2graph.LayoutGraph) geo.Spacing {
|
2023-09-14 20:38:59 +00:00
|
|
|
// Before we can layout these nodes, we need to handle all nested diagrams first.
|
|
|
|
|
extracted := make(map[*d2graph.Object]*d2graph.Graph)
|
|
|
|
|
|
|
|
|
|
// Iterate top-down from Root so all nested diagrams can process their own contents
|
|
|
|
|
queue := make([]*d2graph.Object, 0, len(g.Root.ChildrenArray))
|
|
|
|
|
queue = append(queue, g.Root.ChildrenArray...)
|
|
|
|
|
|
|
|
|
|
for _, child := range queue {
|
2023-09-14 21:55:52 +00:00
|
|
|
if graphType := NestedGraphType(child); graphType != DefaultGraphType {
|
2023-09-14 20:38:59 +00:00
|
|
|
// There is a nested diagram here, so extract its contents and process in the same way
|
|
|
|
|
nestedGraph := ExtractNested(child)
|
|
|
|
|
|
|
|
|
|
// Layout of nestedGraph is completed
|
2023-09-15 00:00:44 +00:00
|
|
|
spacing := LayoutNested(nestedGraph, graphType, coreLayout)
|
2023-09-14 20:38:59 +00:00
|
|
|
|
|
|
|
|
// Fit child to size of nested layout
|
2023-09-15 00:00:44 +00:00
|
|
|
FitToGraph(child, nestedGraph, spacing)
|
2023-09-14 20:38:59 +00:00
|
|
|
|
|
|
|
|
// We will restore the contents after running layout with child as the placeholder
|
|
|
|
|
extracted[child] = nestedGraph
|
|
|
|
|
} else if len(child.Children) > 0 {
|
|
|
|
|
queue = append(queue, child.ChildrenArray...)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We can now run layout with accurate sizes of nested layout containers
|
|
|
|
|
// Layout according to the type of diagram
|
2023-09-15 00:00:44 +00:00
|
|
|
spacing := LayoutDiagram(g, graphType, coreLayout)
|
2023-09-14 20:38:59 +00:00
|
|
|
|
|
|
|
|
// With the layout set, inject all the extracted graphs
|
|
|
|
|
for n, nestedGraph := range extracted {
|
|
|
|
|
InjectNested(n, nestedGraph)
|
|
|
|
|
}
|
2023-09-15 00:00:44 +00:00
|
|
|
|
|
|
|
|
return spacing
|
2023-09-14 20:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-14 21:55:52 +00:00
|
|
|
func NestedGraphType(obj *d2graph.Object) GraphType {
|
|
|
|
|
if obj.Graph.RootLevel == 0 && obj.IsConstantNear() {
|
2023-09-14 21:17:18 +00:00
|
|
|
return ConstantNearGraph
|
|
|
|
|
}
|
2023-09-14 21:55:52 +00:00
|
|
|
if obj.IsGridDiagram() {
|
2023-09-14 21:17:18 +00:00
|
|
|
return GridDiagram
|
|
|
|
|
}
|
2023-09-14 21:55:52 +00:00
|
|
|
if obj.IsSequenceDiagram() {
|
2023-09-14 21:17:18 +00:00
|
|
|
return SequenceDiagram
|
|
|
|
|
}
|
|
|
|
|
return DefaultGraphType
|
2023-09-14 20:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ExtractNested(container *d2graph.Object) *d2graph.Graph {
|
2023-09-15 00:00:44 +00:00
|
|
|
nestedGraph := d2graph.NewGraph()
|
|
|
|
|
nestedGraph.RootLevel = int(container.Level())
|
2023-09-14 22:38:52 +00:00
|
|
|
|
|
|
|
|
// separate out nested edges
|
|
|
|
|
g := container.Graph
|
|
|
|
|
remainingEdges := make([]*d2graph.Edge, 0, len(g.Edges))
|
|
|
|
|
for _, edge := range g.Edges {
|
|
|
|
|
if edge.Src.Parent.IsDescendantOf(container) && edge.Dst.Parent.IsDescendantOf(container) {
|
2023-09-15 00:00:44 +00:00
|
|
|
nestedGraph.Edges = append(nestedGraph.Edges, edge)
|
2023-09-14 22:38:52 +00:00
|
|
|
} else {
|
|
|
|
|
remainingEdges = append(remainingEdges, edge)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g.Edges = remainingEdges
|
|
|
|
|
|
|
|
|
|
// separate out nested objects
|
|
|
|
|
remainingObjects := make([]*d2graph.Object, 0, len(g.Objects))
|
|
|
|
|
for _, obj := range g.Objects {
|
|
|
|
|
if obj.IsDescendantOf(container) {
|
2023-09-15 00:00:44 +00:00
|
|
|
nestedGraph.Objects = append(nestedGraph.Objects, obj)
|
2023-09-14 22:38:52 +00:00
|
|
|
} else {
|
|
|
|
|
remainingObjects = append(remainingObjects, obj)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
g.Objects = remainingObjects
|
|
|
|
|
|
|
|
|
|
// update object and new root references
|
2023-09-15 00:00:44 +00:00
|
|
|
for _, o := range nestedGraph.Objects {
|
|
|
|
|
o.Graph = nestedGraph
|
2023-09-14 22:38:52 +00:00
|
|
|
}
|
|
|
|
|
// set root references
|
2023-09-15 00:00:44 +00:00
|
|
|
nestedGraph.Root.ChildrenArray = append(nestedGraph.Root.ChildrenArray, container.ChildrenArray...)
|
2023-09-14 22:38:52 +00:00
|
|
|
for _, child := range container.ChildrenArray {
|
2023-09-15 00:00:44 +00:00
|
|
|
child.Parent = nestedGraph.Root
|
|
|
|
|
nestedGraph.Root.Children[strings.ToLower(child.ID)] = child
|
2023-09-14 22:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// remove container's references
|
|
|
|
|
for k := range container.Children {
|
|
|
|
|
delete(container.Children, k)
|
|
|
|
|
}
|
|
|
|
|
container.ChildrenArray = nil
|
|
|
|
|
|
2023-09-15 00:00:44 +00:00
|
|
|
// position contents relative to 0,0
|
|
|
|
|
dx := -container.TopLeft.X
|
|
|
|
|
dy := -container.TopLeft.Y
|
|
|
|
|
for _, o := range nestedGraph.Objects {
|
|
|
|
|
o.TopLeft.X += dx
|
|
|
|
|
o.TopLeft.Y += dy
|
|
|
|
|
}
|
|
|
|
|
for _, e := range nestedGraph.Edges {
|
|
|
|
|
e.Move(dx, dy)
|
|
|
|
|
}
|
|
|
|
|
return nestedGraph
|
2023-09-14 20:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 00:00:44 +00:00
|
|
|
func InjectNested(container *d2graph.Object, nestedGraph *d2graph.Graph) {
|
|
|
|
|
g := container.Graph
|
|
|
|
|
for _, obj := range nestedGraph.Root.ChildrenArray {
|
|
|
|
|
obj.Parent = container
|
|
|
|
|
container.Children[strings.ToLower(obj.ID)] = obj
|
|
|
|
|
container.ChildrenArray = append(container.ChildrenArray, obj)
|
|
|
|
|
}
|
|
|
|
|
for _, obj := range nestedGraph.Objects {
|
|
|
|
|
obj.Graph = g
|
|
|
|
|
}
|
|
|
|
|
g.Objects = append(g.Objects, nestedGraph.Objects...)
|
|
|
|
|
g.Edges = append(g.Edges, nestedGraph.Edges...)
|
|
|
|
|
|
|
|
|
|
// Note: assumes nestedGraph's layout has contents positioned relative to 0,0
|
|
|
|
|
dx := container.TopLeft.X
|
|
|
|
|
dy := container.TopLeft.Y
|
|
|
|
|
for _, o := range nestedGraph.Objects {
|
|
|
|
|
o.TopLeft.X += dx
|
|
|
|
|
o.TopLeft.Y += dy
|
|
|
|
|
}
|
|
|
|
|
for _, e := range nestedGraph.Edges {
|
|
|
|
|
e.Move(dx, dy)
|
|
|
|
|
}
|
2023-09-14 20:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 00:00:44 +00:00
|
|
|
func boundingBox(g *d2graph.Graph) (tl, br *geo.Point) {
|
|
|
|
|
if len(g.Objects) == 0 {
|
|
|
|
|
return geo.NewPoint(0, 0), geo.NewPoint(0, 0)
|
|
|
|
|
}
|
|
|
|
|
tl = geo.NewPoint(math.Inf(1), math.Inf(1))
|
|
|
|
|
br = geo.NewPoint(math.Inf(-1), math.Inf(-1))
|
|
|
|
|
|
|
|
|
|
for _, obj := range g.Objects {
|
|
|
|
|
tl.X = math.Min(tl.X, obj.TopLeft.X)
|
|
|
|
|
tl.Y = math.Min(tl.Y, obj.TopLeft.Y)
|
|
|
|
|
br.X = math.Max(br.X, obj.TopLeft.X+obj.Width)
|
|
|
|
|
br.Y = math.Max(br.Y, obj.TopLeft.Y+obj.Height)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tl, br
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FitToGraph(container *d2graph.Object, nestedGraph *d2graph.Graph, padding geo.Spacing) {
|
|
|
|
|
tl, br := boundingBox(nestedGraph)
|
|
|
|
|
container.Width = padding.Left + br.X - tl.X + padding.Right
|
|
|
|
|
container.Height = padding.Top + br.Y - tl.Y + padding.Bottom
|
2023-09-14 20:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
2023-09-15 00:00:44 +00:00
|
|
|
func LayoutDiagram(graph *d2graph.Graph, graphType GraphType, coreLayout d2graph.LayoutGraph) geo.Spacing {
|
2023-09-14 20:38:59 +00:00
|
|
|
// TODO
|
2023-09-15 00:00:44 +00:00
|
|
|
return geo.Spacing{}
|
2023-09-14 20:38:59 +00:00
|
|
|
}
|