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
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) {
for _, obj := range gd.objects {
obj.TopLeft.X += dx
obj.TopLeft.Y += dy
obj.MoveWithDescendants(dx, dy)
}
}

View file

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