From 243df4b8e199442278b4118eff6156ee46ec5d23 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Tue, 20 Dec 2022 14:03:40 -0800 Subject: [PATCH] use elk layout's edge label position --- d2exporter/export.go | 3 +++ d2graph/d2graph.go | 1 + d2layouts/d2elklayout/layout.go | 3 ++- d2target/d2target.go | 8 ++++++-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/d2exporter/export.go b/d2exporter/export.go index 952a85662..6724283e8 100644 --- a/d2exporter/export.go +++ b/d2exporter/export.go @@ -242,6 +242,9 @@ func toConnection(edge *d2graph.Edge, theme *d2themes.Theme) d2target.Connection if edge.LabelPercentage != nil { connection.LabelPercentage = *edge.LabelPercentage } + if edge.LabelTL != nil { + connection.LabelTL = edge.LabelTL.Copy() + } connection.Route = edge.Route connection.IsCurve = edge.IsCurve diff --git a/d2graph/d2graph.go b/d2graph/d2graph.go index a6ec263f0..9ef90ed4c 100644 --- a/d2graph/d2graph.go +++ b/d2graph/d2graph.go @@ -664,6 +664,7 @@ type Edge struct { LabelDimensions d2target.TextDimensions `json:"label_dimensions"` LabelPosition *string `json:"labelPosition,omitempty"` LabelPercentage *float64 `json:"labelPercentage,omitempty"` + LabelTL *geo.Point `json:"labelTL,omitempty"` IsCurve bool `json:"isCurve"` Route []*geo.Point `json:"route,omitempty"` diff --git a/d2layouts/d2elklayout/layout.go b/d2layouts/d2elklayout/layout.go index fb364ceba..75ada9428 100644 --- a/d2layouts/d2elklayout/layout.go +++ b/d2layouts/d2elklayout/layout.go @@ -308,7 +308,8 @@ func Layout(ctx context.Context, g *d2graph.Graph) (err error) { points[endIndex] = shape.TraceToShapeBorder(dstShape, points[endIndex], points[endIndex-1]) if edge.Attributes.Label.Value != "" { - edge.LabelPosition = go2.Pointer(string(label.InsideMiddleCenter)) + // using the edge label position from elk layout + edge.LabelTL = geo.NewPoint(e.Labels[0].X+parentX, e.Labels[0].Y+parentY) } edge.Route = points diff --git a/d2target/d2target.go b/d2target/d2target.go index ae098fd28..053a11687 100644 --- a/d2target/d2target.go +++ b/d2target/d2target.go @@ -210,8 +210,9 @@ type Connection struct { Fill string `json:"fill,omitempty"` Text - LabelPosition string `json:"labelPosition"` - LabelPercentage float64 `json:"labelPercentage"` + LabelPosition string `json:"labelPosition"` + LabelPercentage float64 `json:"labelPercentage"` + LabelTL *geo.Point `json:"labelTL,omitempty"` Route []*geo.Point `json:"route"` IsCurve bool `json:"isCurve,omitempty"` @@ -239,6 +240,9 @@ func BaseConnection() *Connection { } func (c *Connection) GetLabelTopLeft() *geo.Point { + if c.LabelTL != nil { + return c.LabelTL + } return label.Position(c.LabelPosition).GetPointOnRoute( c.Route, float64(c.StrokeWidth),