From 4b911ffd15d9bca617d51cbe9631feeafc3aa926 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 4 Mar 2025 12:05:39 -0700 Subject: [PATCH 1/2] fix legend --- d2compiler/compile.go | 10 + d2exporter/export_test.go | 25 ++ .../TestExport/legend/basic_legend.exp.json | 331 ++++++++++++++++++ 3 files changed, 366 insertions(+) create mode 100644 testdata/d2exporter/TestExport/legend/basic_legend.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 0c119a1be..179797803 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -20,6 +20,7 @@ import ( "oss.terrastruct.com/d2/d2parser" "oss.terrastruct.com/d2/d2target" "oss.terrastruct.com/d2/lib/color" + "oss.terrastruct.com/d2/lib/geo" "oss.terrastruct.com/d2/lib/textmeasure" ) @@ -135,9 +136,18 @@ func (c *compiler) compileLegend(g *d2graph.Graph, m *d2ir.Map) { continue } } + obj.Box = &geo.Box{} + obj.TopLeft = geo.NewPoint(0, 0) objects = append(objects, obj) } + for _, edge := range legendGraph.Edges { + edge.Route = []*geo.Point{ + {X: 0, Y: 0}, + {X: 0, Y: 0}, + } + } + legend := &d2graph.Legend{ Objects: objects, Edges: legendGraph.Edges, diff --git a/d2exporter/export_test.go b/d2exporter/export_test.go index e0f72587b..94d3c8c4c 100644 --- a/d2exporter/export_test.go +++ b/d2exporter/export_test.go @@ -39,6 +39,7 @@ func TestExport(t *testing.T) { t.Run("connection", testConnection) t.Run("label", testLabel) t.Run("theme", testTheme) + t.Run("legend", testLegend) } func testShape(t *testing.T) { @@ -204,6 +205,30 @@ func testTheme(t *testing.T) { runa(t, tcs) } +func testLegend(t *testing.T) { + tcs := []testCase{ + { + name: "basic_legend", + dsl: `vars: { + d2-legend: { + legend: { + l1: Rectangles {shape: rectangle} + l2: Ovals {shape: oval} + l1 -> l2: Connection + } + } +} +x: {shape: rectangle} +y: {shape: oval} +x -> y: connects + +`, + }, + } + + runa(t, tcs) +} + func runa(t *testing.T, tcs []testCase) { for _, tc := range tcs { tc := tc diff --git a/testdata/d2exporter/TestExport/legend/basic_legend.exp.json b/testdata/d2exporter/TestExport/legend/basic_legend.exp.json new file mode 100644 index 000000000..720d46a31 --- /dev/null +++ b/testdata/d2exporter/TestExport/legend/basic_legend.exp.json @@ -0,0 +1,331 @@ +{ + "name": "", + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "x", + "type": "rectangle", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "x", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 1 + }, + { + "id": "y", + "type": "oval", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "y", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 1 + } + ], + "connections": [ + { + "id": "(x -> y)[0]", + "src": "x", + "srcArrow": "none", + "dst": "y", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "connects", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "link": "", + "route": [], + "isCurve": true, + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + } + ], + "root": { + "id": "", + "type": "", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 0, + "strokeDash": 0, + "strokeWidth": 0, + "borderRadius": 0, + "fill": "N7", + "stroke": "", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "", + "fontSize": 0, + "fontFamily": "", + "language": "", + "color": "", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 0 + }, + "legend": { + "shapes": [ + { + "id": "legend", + "type": "rectangle", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B4", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "legend", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 1 + }, + { + "id": "legend.l1", + "type": "rectangle", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "Rectangles", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 2 + }, + { + "id": "legend.l2", + "type": "oval", + "pos": { + "x": 0, + "y": 0 + }, + "width": 0, + "height": 0, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "animated": false, + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "Ovals", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "zIndex": 0, + "level": 2 + } + ], + "connections": [ + { + "id": "legend.(l1 -> l2)[0]", + "src": "legend.l1", + "srcArrow": "none", + "dst": "legend.l2", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "Connection", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "link": "", + "route": [ + { + "x": 0, + "y": 0 + }, + { + "x": 0, + "y": 0 + } + ], + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + } + ] + } +} From d1c05bc115201b37a4137dcc1ab9156fbc00d96c Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 4 Mar 2025 13:35:43 -0700 Subject: [PATCH 2/2] Ta --- .../d2compiler/TestCompile/legend.exp.json | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/testdata/d2compiler/TestCompile/legend.exp.json b/testdata/d2compiler/TestCompile/legend.exp.json index 7cff9897e..55e146b03 100644 --- a/testdata/d2compiler/TestCompile/legend.exp.json +++ b/testdata/d2compiler/TestCompile/legend.exp.json @@ -814,6 +814,14 @@ "map_key_edge_index": 0 } ], + "box": { + "TopLeft": { + "x": 0, + "y": 0 + }, + "Width": 0, + "Height": 0 + }, "attributes": { "label": { "value": "A person who interacts with the system" @@ -903,6 +911,14 @@ "map_key_edge_index": 0 } ], + "box": { + "TopLeft": { + "x": 0, + "y": 0 + }, + "Width": 0, + "Height": 0 + }, "attributes": { "label": { "value": "Stores application data" @@ -932,6 +948,16 @@ { "index": 0, "isCurve": false, + "route": [ + { + "x": 0, + "y": 0 + }, + { + "x": 0, + "y": 0 + } + ], "src_arrow": false, "dst_arrow": true, "references": [ @@ -966,6 +992,16 @@ { "index": 0, "isCurve": false, + "route": [ + { + "x": 0, + "y": 0 + }, + { + "x": 0, + "y": 0 + } + ], "src_arrow": false, "dst_arrow": true, "references": [