Skip to content

Commit

Permalink
fix: cr, delete redundant return value
Browse files Browse the repository at this point in the history
  • Loading branch information
ShupingHe committed Mar 24, 2023
1 parent 040be1d commit 849b117
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 6 additions & 7 deletions d2layouts/d2near/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ 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, constantNears []*d2graph.Object, constantNearGraphs map[*d2graph.Object]*d2graph.Graph) error {
if len(constantNears) == 0 {
func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs map[*d2graph.Object]*d2graph.Graph) error {
if len(constantNearGraphs) == 0 {
return nil
}

// Imagine the graph has two long texts, one at top center and one at top left.
// 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 constantNears {
for obj := range constantNearGraphs {
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))
Expand Down Expand Up @@ -54,7 +54,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNears []*d2graph.Obje
g.Edges = append(g.Edges, subEdges...)
}
}
for _, obj := range constantNears {
for obj := range constantNearGraphs {
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)
Expand Down Expand Up @@ -144,7 +144,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) (nears []*d2graph.Object, constantNearGraphs map[*d2graph.Object]*d2graph.Graph) {
func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (constantNearGraphs map[*d2graph.Object]*d2graph.Graph) {
constantNearGraphs = make(map[*d2graph.Object]*d2graph.Graph)

for i := 0; i < len(g.Objects); i++ {
Expand All @@ -168,7 +168,6 @@ func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (nears []*d2gra

constantNearGraphs[obj] = tempGraph

nears = append(nears, obj)
i--
delete(obj.Parent.Children, strings.ToLower(obj.ID))
for i := 0; i < len(obj.Parent.ChildrenArray); i++ {
Expand All @@ -179,7 +178,7 @@ func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (nears []*d2gra
}
}
}
return nears, constantNearGraphs
return constantNearGraphs
}

func pluckOutNearObjectAndEdges(g *d2graph.Graph, obj *d2graph.Object) (descendantsObjects []*d2graph.Object, edges []*d2graph.Edge) {
Expand Down
4 changes: 2 additions & 2 deletions d2lib/d2.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func compile(ctx context.Context, g *d2graph.Graph, opts *CompileOptions) (*d2ta
return nil, err
}

constantNears, constantNearGraphs := d2near.WithoutConstantNears(ctx, g)
constantNearGraphs := d2near.WithoutConstantNears(ctx, g)

// run core layout for constantNears
for nearObject, tempGraph := range constantNearGraphs {
Expand All @@ -88,7 +88,7 @@ func compile(ctx context.Context, g *d2graph.Graph, opts *CompileOptions) (*d2ta
return nil, err
}

err = d2near.Layout(ctx, g, constantNears, constantNearGraphs)
err = d2near.Layout(ctx, g, constantNearGraphs)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 849b117

Please sign in to comment.