From 849012b9fd8fdd5cf719126e730ff47633c3a249 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sun, 25 Dec 2022 17:43:43 -0800 Subject: [PATCH] cleanup --- d2graph/d2graph.go | 18 +++++------------- d2layouts/d2near/layout.go | 6 +++--- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index 4e3721dbf..ebaf0274c 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -1191,19 +1191,6 @@ var StyleKeywords = map[string]struct{}{ // TODO maybe autofmt should allow other values, and transform them to conform // e.g. left-center becomes center-left -var NearConstants = map[string]struct{}{ - "top-left": {}, - "top-center": {}, - "top-right": {}, - - "center-left": {}, - "center-right": {}, - - "bottom-left": {}, - "bottom-center": {}, - "bottom-right": {}, -} - var NearConstantsArray = []string{ "top-left", "top-center", @@ -1216,6 +1203,7 @@ var NearConstantsArray = []string{ "bottom-center", "bottom-right", } +var NearConstants map[string]struct{} func init() { for k, v := range StyleKeywords { @@ -1224,4 +1212,8 @@ func init() { for k, v := range ReservedKeywordHolders { ReservedKeywords[k] = v } + NearConstants = make(map[string]struct{}, len(NearConstantsArray)) + for _, k := range NearConstantsArray { + NearConstants[k] = struct{}{} + } } diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index 954e6af30..99e8a10e1 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -55,7 +55,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNears []*d2graph.Obje return nil } -// position returns the position of obj, taking into consideration its near value and the diagram +// place returns the position of obj, taking into consideration its near value and the diagram func place(obj *d2graph.Object) (float64, float64) { tl, br := boundingBox(obj.Graph) w := br.X - tl.X @@ -79,10 +79,10 @@ func place(obj *d2graph.Object) (float64, float64) { return br.X + pad, br.Y + pad } return 0, 0 - } -// WithoutConstantNears removes +// 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) { for i := 0; i < len(g.Objects); i++ { obj := g.Objects[i]