From c647c6fb2e7837bc0d9470818f4a21e38fb22ef3 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 16 Oct 2023 16:04:12 -0700 Subject: [PATCH] update grid cell label/icon positioning --- d2layouts/d2grid/layout.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/d2layouts/d2grid/layout.go b/d2layouts/d2grid/layout.go index 80f183b56..9614e9fab 100644 --- a/d2layouts/d2grid/layout.go +++ b/d2layouts/d2grid/layout.go @@ -156,16 +156,27 @@ func layoutGrid(g *d2graph.Graph, obj *d2graph.Object) (*gridDiagram, error) { // position labels and icons for _, o := range gd.objects { - if o.Icon != nil { - // don't overwrite position if nested graph layout positioned label/icon - if o.LabelPosition == nil { - o.LabelPosition = go2.Pointer(string(label.InsideTopCenter)) - } - if o.IconPosition == nil { + positionedLabel := false + if o.Icon != nil && o.IconPosition == nil { + if len(o.ChildrenArray) > 0 { + o.IconPosition = go2.Pointer(string(label.OutsideTopLeft)) + // don't overwrite position if nested graph layout positioned label/icon + if o.LabelPosition == nil { + o.LabelPosition = go2.Pointer(string(label.OutsideTopRight)) + positionedLabel = true + } + } else { o.IconPosition = go2.Pointer(string(label.InsideMiddleCenter)) } - } else { - if o.LabelPosition == nil { + } + if !positionedLabel && o.HasLabel() && o.LabelPosition == nil { + if len(o.ChildrenArray) > 0 { + o.LabelPosition = go2.Pointer(string(label.OutsideTopCenter)) + } else if o.HasOutsideBottomLabel() { + o.LabelPosition = go2.Pointer(string(label.OutsideBottomCenter)) + } else if o.Icon != nil { + o.LabelPosition = go2.Pointer(string(label.InsideTopCenter)) + } else { o.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter)) } }