layout nested grids

This commit is contained in:
Gavin Nishizawa 2023-05-08 13:22:48 -07:00
parent c6820c89cc
commit 939eb500da
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
3 changed files with 70 additions and 46 deletions

View file

@ -1815,3 +1815,11 @@ func (g *Graph) ApplyTheme(themeID int64) error {
g.Theme = &theme g.Theme = &theme
return nil return nil
} }
func (obj *Object) MoveWithDescendants(dx, dy float64) {
obj.TopLeft.X += dx
obj.TopLeft.Y += dy
for _, child := range obj.ChildrenArray {
child.MoveWithDescendants(dx, dy)
}
}

View file

@ -100,8 +100,7 @@ func newGridDiagram(root *d2graph.Object) *gridDiagram {
func (gd *gridDiagram) shift(dx, dy float64) { func (gd *gridDiagram) shift(dx, dy float64) {
for _, obj := range gd.objects { for _, obj := range gd.objects {
obj.TopLeft.X += dx obj.MoveWithDescendants(dx, dy)
obj.TopLeft.Y += dy
} }
} }

View file

@ -53,23 +53,20 @@ func withoutGridDiagrams(ctx context.Context, g *d2graph.Graph) (gridDiagrams ma
toRemove := make(map[*d2graph.Object]struct{}) toRemove := make(map[*d2graph.Object]struct{})
gridDiagrams = make(map[string]*gridDiagram) gridDiagrams = make(map[string]*gridDiagram)
if len(g.Objects) > 0 { var processGrid func(obj *d2graph.Object) error
queue := make([]*d2graph.Object, 1, len(g.Objects)) processGrid = func(obj *d2graph.Object) error {
queue[0] = g.Root // layout any nested grids first
for len(queue) > 0 { for _, child := range obj.ChildrenArray {
obj := queue[0] if child.IsGridDiagram() {
queue = queue[1:] if err := processGrid(child); err != nil {
if len(obj.ChildrenArray) == 0 { return err
continue }
} }
if !obj.IsGridDiagram() {
queue = append(queue, obj.ChildrenArray...)
continue
} }
gd, err := layoutGrid(g, obj) gd, err := layoutGrid(g, obj)
if err != nil { if err != nil {
return nil, nil, err return err
} }
obj.Children = make(map[string]*d2graph.Object) obj.Children = make(map[string]*d2graph.Object)
obj.ChildrenArray = nil obj.ChildrenArray = nil
@ -112,6 +109,26 @@ func withoutGridDiagrams(ctx context.Context, g *d2graph.Graph) (gridDiagrams ma
for _, o := range gd.objects { for _, o := range gd.objects {
toRemove[o] = struct{}{} toRemove[o] = struct{}{}
} }
return nil
}
if len(g.Objects) > 0 {
queue := make([]*d2graph.Object, 1, len(g.Objects))
queue[0] = g.Root
for len(queue) > 0 {
obj := queue[0]
queue = queue[1:]
if len(obj.ChildrenArray) == 0 {
continue
}
if !obj.IsGridDiagram() {
queue = append(queue, obj.ChildrenArray...)
continue
}
if err := processGrid(obj); err != nil {
return nil, nil, err
}
} }
} }
@ -821,7 +838,7 @@ func cleanup(graph *d2graph.Graph, gridDiagrams map[string]*gridDiagram, objects
gd, exists := gridDiagrams[graph.Root.AbsID()] gd, exists := gridDiagrams[graph.Root.AbsID()]
if exists { if exists {
gd.cleanup(graph.Root, graph) gd.cleanup(graph.Root, graph)
return // return
} }
} }