fix: cr, delete redundant variable

This commit is contained in:
donglixiaoche 2023-03-25 02:38:05 +08:00
parent 4e4959f39b
commit ebab91b043
No known key found for this signature in database
GPG key ID: 3190E65EBAD6D6E2
2 changed files with 9 additions and 13 deletions

View file

@ -15,7 +15,7 @@ import (
const pad = 20 const pad = 20
// Layout finds the shapes which are assigned constant near keywords and places them. // 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 { if len(constantNearGraphs) == 0 {
return nil 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. // 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 // So place the center ones first, then the later ones will consider them for bounding box
for _, processCenters := range []bool{true, false} { 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") { if processCenters == strings.Contains(d2graph.Key(obj.Attributes.NearKey)[0], "-center") {
preX, preY := obj.TopLeft.X, obj.TopLeft.Y preX, preY := obj.TopLeft.X, obj.TopLeft.Y
obj.TopLeft = geo.NewPoint(place(obj)) obj.TopLeft = geo.NewPoint(place(obj))
dx, dy := obj.TopLeft.X-preX, obj.TopLeft.Y-preY dx, dy := obj.TopLeft.X-preX, obj.TopLeft.Y-preY
tempGraph := constantNearGraphs[obj]
if tempGraph == nil {
continue
}
subObjects, subEdges := tempGraph.Objects, tempGraph.Edges subObjects, subEdges := tempGraph.Objects, tempGraph.Edges
for _, subObject := range subObjects { for _, subObject := range subObjects {
// `obj` already been replaced above by `place(obj)` // `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...) 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") { 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 // The z-index for constant nears does not matter, as it will not collide
g.Objects = append(g.Objects, obj) 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 // 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 // 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) { func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (constantNearGraphs []*d2graph.Graph) {
constantNearGraphs = make(map[*d2graph.Object]*d2graph.Graph)
for i := 0; i < len(g.Objects); i++ { for i := 0; i < len(g.Objects); i++ {
obj := g.Objects[i] obj := g.Objects[i]
if obj.Attributes.NearKey == nil { if obj.Attributes.NearKey == nil {
@ -166,7 +161,7 @@ func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (constantNearGr
tempGraph.Objects = descendantObjects tempGraph.Objects = descendantObjects
tempGraph.Edges = edges tempGraph.Edges = edges
constantNearGraphs[obj] = tempGraph constantNearGraphs = append(constantNearGraphs, tempGraph)
i-- i--
delete(obj.Parent.Children, strings.ToLower(obj.ID)) delete(obj.Parent.Children, strings.ToLower(obj.ID))

View file

@ -71,11 +71,12 @@ func compile(ctx context.Context, g *d2graph.Graph, opts *CompileOptions) (*d2ta
constantNearGraphs := d2near.WithoutConstantNears(ctx, g) constantNearGraphs := d2near.WithoutConstantNears(ctx, g)
// run core layout for constantNears // run core layout for constantNears
for nearObject, tempGraph := range constantNearGraphs { for _, tempGraph := range constantNearGraphs {
if tempGraph == nil { if tempGraph == nil {
continue continue
} }
nearObject := tempGraph.Root.ChildrenArray[0]
nearObject.Parent = tempGraph.Root nearObject.Parent = tempGraph.Root
if err = coreLayout(ctx, tempGraph); err != nil { if err = coreLayout(ctx, tempGraph); err != nil {
return nil, err return nil, err