From ebab91b043e35c135716ace72a77e4727a42d53f Mon Sep 17 00:00:00 2001 From: donglixiaoche Date: Sat, 25 Mar 2023 02:38:05 +0800 Subject: [PATCH] fix: cr, delete redundant variable --- d2layouts/d2near/layout.go | 19 +++++++------------ d2lib/d2.go | 3 ++- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index fee36c395..ad7fba3bd 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -15,7 +15,7 @@ import ( const pad = 20 // Layout finds the shapes which are assigned constant near keywords and places them. -func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs map[*d2graph.Object]*d2graph.Graph) error { +func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs []*d2graph.Graph) error { if len(constantNearGraphs) == 0 { return nil } @@ -24,17 +24,13 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs map[*d2gra // Top left should go left enough to not collide with center. // So place the center ones first, then the later ones will consider them for bounding box for _, processCenters := range []bool{true, false} { - for obj := range constantNearGraphs { + for _, tempGraph := range constantNearGraphs { + obj := tempGraph.Root.ChildrenArray[0] if processCenters == strings.Contains(d2graph.Key(obj.Attributes.NearKey)[0], "-center") { preX, preY := obj.TopLeft.X, obj.TopLeft.Y obj.TopLeft = geo.NewPoint(place(obj)) dx, dy := obj.TopLeft.X-preX, obj.TopLeft.Y-preY - tempGraph := constantNearGraphs[obj] - if tempGraph == nil { - continue - } - subObjects, subEdges := tempGraph.Objects, tempGraph.Edges for _, subObject := range subObjects { // `obj` already been replaced above by `place(obj)` @@ -54,7 +50,8 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs map[*d2gra g.Edges = append(g.Edges, subEdges...) } } - for obj := range constantNearGraphs { + for _, tempGraph := range constantNearGraphs { + obj := tempGraph.Root.ChildrenArray[0] if processCenters == strings.Contains(d2graph.Key(obj.Attributes.NearKey)[0], "-center") { // The z-index for constant nears does not matter, as it will not collide g.Objects = append(g.Objects, obj) @@ -144,9 +141,7 @@ func calcLabelDimension(obj *d2graph.Object, x float64, y float64) (float64, flo // WithoutConstantNears plucks out the graph objects which have "near" set to a constant value // This is to be called before layout engines so they don't take part in regular positioning -func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (constantNearGraphs map[*d2graph.Object]*d2graph.Graph) { - constantNearGraphs = make(map[*d2graph.Object]*d2graph.Graph) - +func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (constantNearGraphs []*d2graph.Graph) { for i := 0; i < len(g.Objects); i++ { obj := g.Objects[i] if obj.Attributes.NearKey == nil { @@ -166,7 +161,7 @@ func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (constantNearGr tempGraph.Objects = descendantObjects tempGraph.Edges = edges - constantNearGraphs[obj] = tempGraph + constantNearGraphs = append(constantNearGraphs, tempGraph) i-- delete(obj.Parent.Children, strings.ToLower(obj.ID)) diff --git a/d2lib/d2.go b/d2lib/d2.go index c43bb789e..13dccad3f 100644 --- a/d2lib/d2.go +++ b/d2lib/d2.go @@ -71,11 +71,12 @@ func compile(ctx context.Context, g *d2graph.Graph, opts *CompileOptions) (*d2ta constantNearGraphs := d2near.WithoutConstantNears(ctx, g) // run core layout for constantNears - for nearObject, tempGraph := range constantNearGraphs { + for _, tempGraph := range constantNearGraphs { if tempGraph == nil { continue } + nearObject := tempGraph.Root.ChildrenArray[0] nearObject.Parent = tempGraph.Root if err = coreLayout(ctx, tempGraph); err != nil { return nil, err