This commit is contained in:
Gavin Nishizawa 2023-09-14 14:55:52 -07:00
parent 37adab4803
commit 799311877a
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
3 changed files with 22 additions and 23 deletions

View file

@ -1090,6 +1090,21 @@ func (obj *Object) OuterNearContainer() *Object {
return nil 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 { type Edge struct {
Index int `json:"index"` Index int `json:"index"`

View file

@ -2,7 +2,6 @@ package d2layouts
import ( import (
"oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2near"
) )
type GraphType string type GraphType string
@ -14,7 +13,7 @@ const (
SequenceDiagram GraphType = "sequence-diagram" 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. // Before we can layout these nodes, we need to handle all nested diagrams first.
extracted := make(map[*d2graph.Object]*d2graph.Graph) 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...) queue = append(queue, g.Root.ChildrenArray...)
for _, child := range queue { 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 // There is a nested diagram here, so extract its contents and process in the same way
nestedGraph := ExtractNested(child) nestedGraph := ExtractNested(child)
// Layout of nestedGraph is completed // Layout of nestedGraph is completed
LayoutNested(nestedGraph, graphType, coreLayout, false) LayoutNested(nestedGraph, graphType, coreLayout)
// Fit child to size of nested layout // Fit child to size of nested layout
FitToGraph(child, nestedGraph) 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 { func NestedGraphType(obj *d2graph.Object) GraphType {
if isRoot && d2near.IsConstantNear(container) { if obj.Graph.RootLevel == 0 && obj.IsConstantNear() {
return ConstantNearGraph return ConstantNearGraph
} }
if container.IsGridDiagram() { if obj.IsGridDiagram() {
return GridDiagram return GridDiagram
} }
if container.IsSequenceDiagram() { if obj.IsSequenceDiagram() {
return SequenceDiagram return SequenceDiagram
} }
return DefaultGraphType return DefaultGraphType

View file

@ -223,18 +223,3 @@ func boundingBox(g *d2graph.Graph) (tl, br *geo.Point) {
return geo.NewPoint(x1, y1), geo.NewPoint(x2, y2) 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
}