From 801d307807c98a35acd6f34bb44ed6825376baef Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Sat, 17 Dec 2022 20:40:56 -0800 Subject: [PATCH] trace elk edges to shape border --- d2layouts/d2elklayout/layout.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/d2layouts/d2elklayout/layout.go b/d2layouts/d2elklayout/layout.go index 63b26c95f..dce31bc8a 100644 --- a/d2layouts/d2elklayout/layout.go +++ b/d2layouts/d2elklayout/layout.go @@ -10,6 +10,7 @@ import ( "encoding/json" "fmt" "math" + "strings" "github.com/dop251/goja" @@ -21,6 +22,7 @@ import ( "oss.terrastruct.com/d2/d2target" "oss.terrastruct.com/d2/lib/geo" "oss.terrastruct.com/d2/lib/label" + "oss.terrastruct.com/d2/lib/shape" ) //go:embed elk.js @@ -289,6 +291,14 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) { }) } + startIndex, endIndex := 0, len(points)-1 + srcShape := shape.NewShape(d2target.DSL_SHAPE_TO_SHAPE_TYPE[strings.ToLower(edge.Src.Attributes.Shape.Value)], edge.Src.Box) + dstShape := shape.NewShape(d2target.DSL_SHAPE_TO_SHAPE_TYPE[strings.ToLower(edge.Dst.Attributes.Shape.Value)], edge.Dst.Box) + + // trace the edge to the specific shape's border + points[startIndex] = shape.TraceToShapeBorder(srcShape, points[startIndex], points[startIndex+1]) + points[endIndex] = shape.TraceToShapeBorder(dstShape, points[endIndex], points[endIndex-1]) + if edge.Attributes.Label.Value != "" { edge.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter)) }