This commit is contained in:
Gavin Nishizawa 2023-09-21 13:54:41 -07:00
parent cbf81c23f3
commit 2cb2b406e6
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD

View file

@ -34,6 +34,7 @@ func (gi GraphInfo) isDefault() bool {
} }
func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, coreLayout d2graph.LayoutGraph) geo.Spacing { func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, coreLayout d2graph.LayoutGraph) geo.Spacing {
g.Root.Box = &geo.Box{}
log.Warn(ctx, "ln info", slog.F("gi", graphInfo)) log.Warn(ctx, "ln info", slog.F("gi", graphInfo))
// Before we can layout these nodes, we need to handle all nested diagrams first. // Before we can layout these nodes, we need to handle all nested diagrams first.
@ -49,7 +50,6 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
for _, child := range queue { for _, child := range queue {
if gi := NestedGraphInfo(child); !gi.isDefault() { if gi := NestedGraphInfo(child); !gi.isDefault() {
extractedInfo[child] = gi extractedInfo[child] = gi
// log.Warn(ctx, "nested", slog.F("child", child.AbsID()), slog.F("gi", gi))
// There is a nested diagram here, so extract its contents and process in the same way // There is a nested diagram here, so extract its contents and process in the same way
nestedGraph := ExtractDescendants(child) nestedGraph := ExtractDescendants(child)
@ -61,24 +61,15 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
// Fit child to size of nested layout // Fit child to size of nested layout
FitToGraph(child, nestedGraph, spacing) FitToGraph(child, nestedGraph, spacing)
var nearGraph *d2graph.Graph // for constant nears, we also extract the child after extracting descendants
// main layout is run, then near positions child, then descendants are injected with all others
if gi.IsConstantNear { if gi.IsConstantNear {
nearGraph = ExtractSelf(child) nearGraph := ExtractSelf(child)
child.TopLeft = geo.NewPoint(0, 0) child.TopLeft = geo.NewPoint(0, 0)
}
// if gi.IsConstantNear {
// // FitToGraph(child, nestedGraph, spacing)
// if nestedGraph.Root.Box != nil {
// child.Width = nestedGraph.Root.Width
// child.Height = nestedGraph.Root.Height
// }
// }
// We will restore the contents after running layout with child as the placeholder
if gi.IsConstantNear {
constantNears = append(constantNears, nearGraph) constantNears = append(constantNears, nearGraph)
} }
// We will restore the contents after running layout with child as the placeholder
extracted[child] = nestedGraph extracted[child] = nestedGraph
} else if len(child.Children) > 0 { } else if len(child.Children) > 0 {
queue = append(queue, child.ChildrenArray...) queue = append(queue, child.ChildrenArray...)
@ -90,13 +81,9 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
LayoutDiagram := func(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, coreLayout d2graph.LayoutGraph) geo.Spacing { LayoutDiagram := func(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, coreLayout d2graph.LayoutGraph) geo.Spacing {
spacing := geo.Spacing{} spacing := geo.Spacing{}
var err error var err error
// TODO
switch graphInfo.DiagramType { switch graphInfo.DiagramType {
case GridDiagram: case GridDiagram:
log.Warn(ctx, "layout grid", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString())) log.Warn(ctx, "layout grid", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString()))
// layoutWithGrids := d2grid.Layout2(ctx, g, coreLayout)
// layoutWithGrids(ctx, g)
if err = d2grid.Layout2(ctx, g); err != nil { if err = d2grid.Layout2(ctx, g); err != nil {
panic(err) panic(err)
} }
@ -118,7 +105,6 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
} }
spacing := LayoutDiagram(ctx, g, graphInfo, coreLayout) spacing := LayoutDiagram(ctx, g, graphInfo, coreLayout)
// if there are
if len(constantNears) > 0 { if len(constantNears) > 0 {
err := d2near.Layout(ctx, g, constantNears) err := d2near.Layout(ctx, g, constantNears)
if err != nil { if err != nil {
@ -128,10 +114,8 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
// With the layout set, inject all the extracted graphs // With the layout set, inject all the extracted graphs
for n, nestedGraph := range extracted { for n, nestedGraph := range extracted {
// if !extractedInfo[n].IsConstantNear {
InjectNested(n, nestedGraph) InjectNested(n, nestedGraph)
PositionNested(n, nestedGraph) PositionNested(n, nestedGraph)
// }
} }
log.Warn(ctx, "done", slog.F("rootlevel", g.RootLevel)) log.Warn(ctx, "done", slog.F("rootlevel", g.RootLevel))