From 9eaa4e98c4839c49c4d9aea1a04a8f9948646970 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sun, 25 Dec 2022 16:49:19 -0800 Subject: [PATCH] cleanup --- d2layouts/d2near/layout.go | 41 ++++++++-------- .../stable/constant_near/elk/board.exp.json | 48 +++++++++---------- .../stable/constant_near/elk/sketch.exp.svg | 18 +++---- 3 files changed, 53 insertions(+), 54 deletions(-) diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index 8a735c4c0..954e6af30 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -22,30 +22,22 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNears []*d2graph.Obje return nil } - // Imagine the grpah has two long texts, one at top center and one at top left. + // 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 _, obj := range constantNears { - if strings.Contains(d2graph.Key(obj.Attributes.NearKey)[0], "center") { - obj.TopLeft = geo.NewPoint(place(obj)) + for _, processCenters := range []bool{true, false} { + for _, obj := range constantNears { + if processCenters == strings.Contains(d2graph.Key(obj.Attributes.NearKey)[0], "center") { + obj.TopLeft = geo.NewPoint(place(obj)) + } } - } - for _, obj := range constantNears { - if 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) - } - } - - for _, obj := range constantNears { - if !strings.Contains(d2graph.Key(obj.Attributes.NearKey)[0], "center") { - obj.TopLeft = geo.NewPoint(place(obj)) - } - } - for _, obj := range constantNears { - if !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) + for _, obj := range constantNears { + 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) + obj.Parent.Children[obj.ID] = obj + obj.Parent.ChildrenArray = append(obj.Parent.ChildrenArray, obj) + } } } @@ -106,6 +98,13 @@ func WithoutConstantNears(ctx context.Context, g *d2graph.Graph) (nears []*d2gra nears = append(nears, obj) g.Objects = append(g.Objects[:i], g.Objects[i+1:]...) i-- + delete(obj.Parent.Children, obj.ID) + for i := 0; i < len(obj.Parent.ChildrenArray); i++ { + if obj.Parent.ChildrenArray[i] == obj { + obj.Parent.ChildrenArray = append(obj.Parent.ChildrenArray[:i], obj.Parent.ChildrenArray[i+1:]...) + break + } + } } } return nears diff --git a/e2etests/testdata/stable/constant_near/elk/board.exp.json b/e2etests/testdata/stable/constant_near/elk/board.exp.json index 2b2e89b8e..3bde4523c 100644 --- a/e2etests/testdata/stable/constant_near/elk/board.exp.json +++ b/e2etests/testdata/stable/constant_near/elk/board.exp.json @@ -6,8 +6,8 @@ "id": "x", "type": "", "pos": { - "x": 1961, - "y": 16 + "x": 12, + "y": 12 }, "width": 113, "height": 126, @@ -46,8 +46,8 @@ "id": "y", "type": "", "pos": { - "x": 1960, - "y": 242 + "x": 12, + "y": 238 }, "width": 114, "height": 126, @@ -86,8 +86,8 @@ "id": "The top of the mountain", "type": "text", "pos": { - "x": 1936, - "y": -28 + "x": -12, + "y": -32 }, "width": 162, "height": 24, @@ -126,8 +126,8 @@ "id": "Joe", "type": "person", "pos": { - "x": 1809, - "y": 129 + "x": -139, + "y": 125 }, "width": 131, "height": 126, @@ -166,8 +166,8 @@ "id": "Donald", "type": "person", "pos": { - "x": 2094, - "y": 129 + "x": 146, + "y": 125 }, "width": 155, "height": 126, @@ -206,8 +206,8 @@ "id": "bottom", "type": "text", "pos": { - "x": 1546, - "y": 388 + "x": -402, + "y": 384 }, "width": 943, "height": 130, @@ -246,8 +246,8 @@ "id": "i am top left", "type": "text", "pos": { - "x": 1445, - "y": -28 + "x": -503, + "y": -32 }, "width": 81, "height": 24, @@ -286,8 +286,8 @@ "id": "i am top right", "type": "text", "pos": { - "x": 2509, - "y": -28 + "x": 560, + "y": -32 }, "width": 91, "height": 24, @@ -326,8 +326,8 @@ "id": "i am bottom left", "type": "text", "pos": { - "x": 1418, - "y": 388 + "x": -530, + "y": 384 }, "width": 108, "height": 24, @@ -366,8 +366,8 @@ "id": "i am bottom right", "type": "text", "pos": { - "x": 2509, - "y": 388 + "x": 560, + "y": 384 }, "width": 118, "height": 24, @@ -430,12 +430,12 @@ "labelPercentage": 0, "route": [ { - "x": 2017.5, - "y": 142 + "x": 69, + "y": 138 }, { - "x": 2017.5, - "y": 242 + "x": 69, + "y": 238 } ], "animated": false, diff --git a/e2etests/testdata/stable/constant_near/elk/sketch.exp.svg b/e2etests/testdata/stable/constant_near/elk/sketch.exp.svg index a9a1c7387..c2022876a 100644 --- a/e2etests/testdata/stable/constant_near/elk/sketch.exp.svg +++ b/e2etests/testdata/stable/constant_near/elk/sketch.exp.svg @@ -2,7 +2,7 @@