From df19311f9bd0ccc1c1efd987861bdc633a6bf022 Mon Sep 17 00:00:00 2001 From: donglixiaoche Date: Fri, 24 Mar 2023 12:08:19 +0800 Subject: [PATCH] fix: ignore objects inside near container when calc boundingBox --- d2graph/d2graph.go | 11 ++++++----- d2layouts/d2near/layout.go | 7 ++++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 54c658a21..74d238b9c 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -90,11 +90,12 @@ type Object struct { LabelDimensions d2target.TextDimensions `json:"label_dimensions"` References []Reference `json:"references,omitempty"` - *geo.Box `json:"box,omitempty"` - LabelPosition *string `json:"labelPosition,omitempty"` - LabelWidth *int `json:"labelWidth,omitempty"` - LabelHeight *int `json:"labelHeight,omitempty"` - IconPosition *string `json:"iconPosition,omitempty"` + *geo.Box `json:"box,omitempty"` + LabelPosition *string `json:"labelPosition,omitempty"` + LabelWidth *int `json:"labelWidth,omitempty"` + LabelHeight *int `json:"labelHeight,omitempty"` + IconPosition *string `json:"iconPosition,omitempty"` + IsInsideNearContainer bool `json:"isInsideNearContainer,omitempty"` Class *d2target.Class `json:"class,omitempty"` SQLTable *d2target.SQLTable `json:"sql_table,omitempty"` diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index 747a0b1be..42e60c1c2 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -128,9 +128,6 @@ func place(obj *d2graph.Object) (float64, float64) { func calcLabelDimension(obj *d2graph.Object, x float64, y float64) (float64, float64) { var position string if obj.LabelPosition != nil { - if strings.Contains(*obj.LabelPosition, "INSIDE") { - return x, y - } if strings.Contains(*obj.LabelPosition, "_TOP_") { position = "TOP" } else if strings.Contains(*obj.LabelPosition, "_LEFT_") { @@ -211,6 +208,7 @@ func pluckOutNearObjectAndEdges(g *d2graph.Graph, obj *d2graph.Object) (descenda if temp.AbsID() == obj.AbsID() { descendantsObjects = append(descendantsObjects, obj) g.Objects = append(g.Objects[:i], g.Objects[i+1:]...) + obj.IsInsideNearContainer = true for _, child := range obj.ChildrenArray { subObjects, subEdges := pluckOutNearObjectAndEdges(g, child) descendantsObjects = append(descendantsObjects, subObjects...) @@ -248,6 +246,9 @@ func boundingBox(g *d2graph.Graph) (tl, br *geo.Point) { y2 = math.Max(y2, obj.TopLeft.Y+obj.Height) } } else { + if obj.IsInsideNearContainer { + continue + } x1 = math.Min(x1, obj.TopLeft.X) y1 = math.Min(y1, obj.TopLeft.Y) x2 = math.Max(x2, obj.TopLeft.X+obj.Width)