diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 1452cf4bc..a3e0bf760 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -1090,6 +1090,21 @@ func (obj *Object) OuterNearContainer() *Object { return nil } +func (obj *Object) IsConstantNear() bool { + if obj.NearKey == nil { + return false + } + keyPath := Key(obj.NearKey) + + // interesting if there is a shape with id=top-left, then top-left isn't treated a constant near + _, isKey := obj.Graph.Root.HasChild(keyPath) + if isKey { + return false + } + _, isConst := NearConstants[keyPath[0]] + return isConst +} + type Edge struct { Index int `json:"index"` diff --git a/d2layouts/d2layouts.go b/d2layouts/d2layouts.go index 60da6c0c7..a92b9542b 100644 --- a/d2layouts/d2layouts.go +++ b/d2layouts/d2layouts.go @@ -2,7 +2,6 @@ package d2layouts import ( "oss.terrastruct.com/d2/d2graph" - "oss.terrastruct.com/d2/d2layouts/d2near" ) type GraphType string @@ -14,7 +13,7 @@ const ( SequenceDiagram GraphType = "sequence-diagram" ) -func LayoutNested(g *d2graph.Graph, graphType GraphType, coreLayout d2graph.LayoutGraph, isRoot bool) { +func LayoutNested(g *d2graph.Graph, graphType GraphType, coreLayout d2graph.LayoutGraph) { // Before we can layout these nodes, we need to handle all nested diagrams first. extracted := make(map[*d2graph.Object]*d2graph.Graph) @@ -23,12 +22,12 @@ func LayoutNested(g *d2graph.Graph, graphType GraphType, coreLayout d2graph.Layo queue = append(queue, g.Root.ChildrenArray...) for _, child := range queue { - if graphType := NestedGraphType(child, isRoot); graphType != DefaultGraphType { + if graphType := NestedGraphType(child); graphType != DefaultGraphType { // There is a nested diagram here, so extract its contents and process in the same way nestedGraph := ExtractNested(child) // Layout of nestedGraph is completed - LayoutNested(nestedGraph, graphType, coreLayout, false) + LayoutNested(nestedGraph, graphType, coreLayout) // Fit child to size of nested layout FitToGraph(child, nestedGraph) @@ -50,14 +49,14 @@ func LayoutNested(g *d2graph.Graph, graphType GraphType, coreLayout d2graph.Layo } } -func NestedGraphType(container *d2graph.Object, isRoot bool) GraphType { - if isRoot && d2near.IsConstantNear(container) { +func NestedGraphType(obj *d2graph.Object) GraphType { + if obj.Graph.RootLevel == 0 && obj.IsConstantNear() { return ConstantNearGraph } - if container.IsGridDiagram() { + if obj.IsGridDiagram() { return GridDiagram } - if container.IsSequenceDiagram() { + if obj.IsSequenceDiagram() { return SequenceDiagram } return DefaultGraphType diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index 2f1444f46..190317542 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -223,18 +223,3 @@ func boundingBox(g *d2graph.Graph) (tl, br *geo.Point) { return geo.NewPoint(x1, y1), geo.NewPoint(x2, y2) } - -func IsConstantNear(obj *d2graph.Object) bool { - if obj.NearKey == nil { - return false - } - keyPath := d2graph.Key(obj.NearKey) - - // interesting if there is a shape with id=top-left, then top-left isn't treated a constant near - _, isKey := obj.Graph.Root.HasChild(keyPath) - if isKey { - return false - } - _, isConst := d2graph.NearConstants[keyPath[0]] - return isConst -}