From 276f277a647cd269496da8bdb1c97ae6749d4bad Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 20:57:35 -0700 Subject: [PATCH 1/5] add multiple_constant_nears test --- e2etests/regression_test.go | 1 + .../testdata/files/multiple_constant_nears.d2 | 17 + .../dagre/board.exp.json | 518 ++++++++++++++++++ .../dagre/sketch.exp.svg | 109 ++++ .../elk/board.exp.json | 491 +++++++++++++++++ .../elk/sketch.exp.svg | 109 ++++ 6 files changed, 1245 insertions(+) create mode 100644 e2etests/testdata/files/multiple_constant_nears.d2 create mode 100644 e2etests/testdata/regression/multiple_constant_nears/dagre/board.exp.json create mode 100644 e2etests/testdata/regression/multiple_constant_nears/dagre/sketch.exp.svg create mode 100644 e2etests/testdata/regression/multiple_constant_nears/elk/board.exp.json create mode 100644 e2etests/testdata/regression/multiple_constant_nears/elk/sketch.exp.svg diff --git a/e2etests/regression_test.go b/e2etests/regression_test.go index 6377a70f5..9a1c29a9f 100644 --- a/e2etests/regression_test.go +++ b/e2etests/regression_test.go @@ -1039,6 +1039,7 @@ cf many required: { loadFromFile(t, "dagre_disconnected_edge"), loadFromFile(t, "outside_grid_label_position"), loadFromFile(t, "arrowhead_font_color"), + loadFromFile(t, "multiple_constant_nears"), } runa(t, tcs) diff --git a/e2etests/testdata/files/multiple_constant_nears.d2 b/e2etests/testdata/files/multiple_constant_nears.d2 new file mode 100644 index 000000000..f27104c43 --- /dev/null +++ b/e2etests/testdata/files/multiple_constant_nears.d2 @@ -0,0 +1,17 @@ +a +b +c + +a: { + 1 -> 2 -> 3 + near: center-right + style.fill: red + style.opacity: 0.5 +} + +b: { + 1111 -> 2222 + near: bottom-right + style.fill: green + style.opacity: 0.5 +} diff --git a/e2etests/testdata/regression/multiple_constant_nears/dagre/board.exp.json b/e2etests/testdata/regression/multiple_constant_nears/dagre/board.exp.json new file mode 100644 index 000000000..7f9abfb50 --- /dev/null +++ b/e2etests/testdata/regression/multiple_constant_nears/dagre/board.exp.json @@ -0,0 +1,518 @@ +{ + "name": "", + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "c", + "type": "rectangle", + "pos": { + "x": 0, + "y": 0 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "c", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "a", + "type": "rectangle", + "pos": { + "x": 73, + "y": -196 + }, + "width": 113, + "height": 458, + "opacity": 0.5, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "red", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "a", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 12, + "labelHeight": 36, + "labelPosition": "OUTSIDE_TOP_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "a.1", + "type": "rectangle", + "pos": { + "x": 104, + "y": -166 + }, + "width": 52, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "1", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 7, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "a.2", + "type": "rectangle", + "pos": { + "x": 103, + "y": 0 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "2", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "a.3", + "type": "rectangle", + "pos": { + "x": 103, + "y": 166 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "3", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "b", + "type": "rectangle", + "pos": { + "x": 73, + "y": 122 + }, + "width": 139, + "height": 292, + "opacity": 0.5, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "green", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "b", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 13, + "labelHeight": 36, + "labelPosition": "OUTSIDE_TOP_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "b.1111", + "type": "rectangle", + "pos": { + "x": 104, + "y": 152 + }, + "width": 78, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "1111", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 33, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "b.2222", + "type": "rectangle", + "pos": { + "x": 103, + "y": 318 + }, + "width": 79, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "2222", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 34, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + } + ], + "connections": [ + { + "id": "a.(1 -> 2)[0]", + "src": "a.1", + "srcArrow": "none", + "dst": "a.2", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "route": [ + { + "x": 129.5, + "y": -100 + }, + { + "x": 129.5, + "y": -60 + }, + { + "x": 129.5, + "y": -40 + }, + { + "x": 129.5, + "y": 0 + } + ], + "isCurve": true, + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + }, + { + "id": "a.(2 -> 3)[0]", + "src": "a.2", + "srcArrow": "none", + "dst": "a.3", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "route": [ + { + "x": 129.5, + "y": 66 + }, + { + "x": 129.5, + "y": 106 + }, + { + "x": 129.5, + "y": 126 + }, + { + "x": 129.5, + "y": 166 + } + ], + "isCurve": true, + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + }, + { + "id": "b.(1111 -> 2222)[0]", + "src": "b.1111", + "srcArrow": "none", + "dst": "b.2222", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "route": [ + { + "x": 142.5, + "y": 218 + }, + { + "x": 142.5, + "y": 258 + }, + { + "x": 142.5, + "y": 278 + }, + { + "x": 142.5, + "y": 318 + } + ], + "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": "", + "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 + } +} diff --git a/e2etests/testdata/regression/multiple_constant_nears/dagre/sketch.exp.svg b/e2etests/testdata/regression/multiple_constant_nears/dagre/sketch.exp.svg new file mode 100644 index 000000000..463a75b5f --- /dev/null +++ b/e2etests/testdata/regression/multiple_constant_nears/dagre/sketch.exp.svg @@ -0,0 +1,109 @@ +cab12311112222 + + + + + + + + + + \ No newline at end of file diff --git a/e2etests/testdata/regression/multiple_constant_nears/elk/board.exp.json b/e2etests/testdata/regression/multiple_constant_nears/elk/board.exp.json new file mode 100644 index 000000000..0842842e5 --- /dev/null +++ b/e2etests/testdata/regression/multiple_constant_nears/elk/board.exp.json @@ -0,0 +1,491 @@ +{ + "name": "", + "isFolderOnly": false, + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "c", + "type": "rectangle", + "pos": { + "x": 12, + "y": 12 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B6", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "c", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "a", + "type": "rectangle", + "pos": { + "x": 85, + "y": -174 + }, + "width": 153, + "height": 438, + "opacity": 0.5, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "red", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "a", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 12, + "labelHeight": 36, + "labelPosition": "INSIDE_TOP_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "a.1", + "type": "rectangle", + "pos": { + "x": 135, + "y": -124 + }, + "width": 52, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "1", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 7, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "a.2", + "type": "rectangle", + "pos": { + "x": 135, + "y": 12 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "2", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "a.3", + "type": "rectangle", + "pos": { + "x": 135, + "y": 148 + }, + "width": 53, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "3", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 8, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "b", + "type": "rectangle", + "pos": { + "x": 85, + "y": 98 + }, + "width": 179, + "height": 302, + "opacity": 0.5, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "green", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "b", + "fontSize": 28, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": false, + "underline": false, + "labelWidth": 13, + "labelHeight": 36, + "labelPosition": "INSIDE_TOP_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "b.1111", + "type": "rectangle", + "pos": { + "x": 135, + "y": 148 + }, + "width": 78, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "1111", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 33, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + }, + { + "id": "b.2222", + "type": "rectangle", + "pos": { + "x": 135, + "y": 284 + }, + "width": 79, + "height": 66, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "B5", + "stroke": "B1", + "shadow": false, + "3d": false, + "multiple": false, + "double-border": false, + "tooltip": "", + "link": "", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "2222", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N1", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 34, + "labelHeight": 21, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 2 + } + ], + "connections": [ + { + "id": "a.(1 -> 2)[0]", + "src": "a.1", + "srcArrow": "none", + "dst": "a.2", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "route": [ + { + "x": 161.5, + "y": -58 + }, + { + "x": 161.5, + "y": 12 + } + ], + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + }, + { + "id": "a.(2 -> 3)[0]", + "src": "a.2", + "srcArrow": "none", + "dst": "a.3", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "route": [ + { + "x": 161.5, + "y": 78 + }, + { + "x": 161.5, + "y": 148 + } + ], + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + }, + { + "id": "b.(1111 -> 2222)[0]", + "src": "b.1111", + "srcArrow": "none", + "dst": "b.2222", + "dstArrow": "triangle", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "B1", + "borderRadius": 10, + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "N2", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "route": [ + { + "x": 174.5, + "y": 214 + }, + { + "x": 174.5, + "y": 284 + } + ], + "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": "", + "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 + } +} diff --git a/e2etests/testdata/regression/multiple_constant_nears/elk/sketch.exp.svg b/e2etests/testdata/regression/multiple_constant_nears/elk/sketch.exp.svg new file mode 100644 index 000000000..719a0dd22 --- /dev/null +++ b/e2etests/testdata/regression/multiple_constant_nears/elk/sketch.exp.svg @@ -0,0 +1,109 @@ +cab12311112222 + + + + + + + + + + \ No newline at end of file From 99bae244358cc7bbdd95a05d210b5327630b7ed0 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 21:01:48 -0700 Subject: [PATCH 2/5] fix check for center near constants --- d2layouts/d2near/layout.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index 614c13652..12cabf793 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -14,6 +14,13 @@ import ( const pad = 20 +var CenterNearConstants = map[string]struct{}{ + "top-center": {}, + "center-left": {}, + "center-right": {}, + "bottom-center": {}, +} + // Layout finds the shapes which are assigned constant near keywords and places them. func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs []*d2graph.Graph) error { if len(constantNearGraphs) == 0 { @@ -33,7 +40,8 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs []*d2graph for _, processCenters := range []bool{true, false} { for _, tempGraph := range constantNearGraphs { obj := tempGraph.Root.ChildrenArray[0] - if processCenters == strings.Contains(d2graph.Key(obj.NearKey)[0], "-center") { + _, isCenterConstant := CenterNearConstants[d2graph.Key(obj.NearKey)[0]] + if processCenters == isCenterConstant { prevX, prevY := obj.TopLeft.X, obj.TopLeft.Y obj.TopLeft = geo.NewPoint(place(obj)) dx, dy := obj.TopLeft.X-prevX, obj.TopLeft.Y-prevY @@ -56,7 +64,8 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs []*d2graph } for _, tempGraph := range constantNearGraphs { obj := tempGraph.Root.ChildrenArray[0] - if processCenters == strings.Contains(d2graph.Key(obj.NearKey)[0], "-center") { + _, isCenterConstant := CenterNearConstants[d2graph.Key(obj.NearKey)[0]] + if processCenters == isCenterConstant { // The z-index for constant nears does not matter, as it will not collide g.Objects = append(g.Objects, tempGraph.Objects...) if obj.Parent.Children == nil { From c7f8c9a91efc3efe851e10de113d5b71a292917d Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 21:02:27 -0700 Subject: [PATCH 3/5] update test --- .../dagre/board.exp.json | 14 +- .../dagre/sketch.exp.svg | 166 +++++++++--------- .../elk/board.exp.json | 10 +- .../elk/sketch.exp.svg | 166 +++++++++--------- 4 files changed, 178 insertions(+), 178 deletions(-) diff --git a/e2etests/testdata/regression/multiple_constant_nears/dagre/board.exp.json b/e2etests/testdata/regression/multiple_constant_nears/dagre/board.exp.json index 7f9abfb50..d20cfdc39 100644 --- a/e2etests/testdata/regression/multiple_constant_nears/dagre/board.exp.json +++ b/e2etests/testdata/regression/multiple_constant_nears/dagre/board.exp.json @@ -213,7 +213,7 @@ "type": "rectangle", "pos": { "x": 73, - "y": 122 + "y": 318 }, "width": 139, "height": 292, @@ -254,7 +254,7 @@ "type": "rectangle", "pos": { "x": 104, - "y": 152 + "y": 348 }, "width": 78, "height": 66, @@ -295,7 +295,7 @@ "type": "rectangle", "pos": { "x": 103, - "y": 318 + "y": 514 }, "width": 79, "height": 66, @@ -453,19 +453,19 @@ "route": [ { "x": 142.5, - "y": 218 + "y": 414 }, { "x": 142.5, - "y": 258 + "y": 454 }, { "x": 142.5, - "y": 278 + "y": 474 }, { "x": 142.5, - "y": 318 + "y": 514 } ], "isCurve": true, diff --git a/e2etests/testdata/regression/multiple_constant_nears/dagre/sketch.exp.svg b/e2etests/testdata/regression/multiple_constant_nears/dagre/sketch.exp.svg index 463a75b5f..310f04aac 100644 --- a/e2etests/testdata/regression/multiple_constant_nears/dagre/sketch.exp.svg +++ b/e2etests/testdata/regression/multiple_constant_nears/dagre/sketch.exp.svg @@ -1,16 +1,16 @@ -cab12311112222 - + .d2-1681696268 .fill-N1{fill:#0A0F25;} + .d2-1681696268 .fill-N2{fill:#676C7E;} + .d2-1681696268 .fill-N3{fill:#9499AB;} + .d2-1681696268 .fill-N4{fill:#CFD2DD;} + .d2-1681696268 .fill-N5{fill:#DEE1EB;} + .d2-1681696268 .fill-N6{fill:#EEF1F8;} + .d2-1681696268 .fill-N7{fill:#FFFFFF;} + .d2-1681696268 .fill-B1{fill:#0D32B2;} + .d2-1681696268 .fill-B2{fill:#0D32B2;} + .d2-1681696268 .fill-B3{fill:#E3E9FD;} + .d2-1681696268 .fill-B4{fill:#E3E9FD;} + .d2-1681696268 .fill-B5{fill:#EDF0FD;} + .d2-1681696268 .fill-B6{fill:#F7F8FE;} + .d2-1681696268 .fill-AA2{fill:#4A6FF3;} + .d2-1681696268 .fill-AA4{fill:#EDF0FD;} + .d2-1681696268 .fill-AA5{fill:#F7F8FE;} + .d2-1681696268 .fill-AB4{fill:#EDF0FD;} + .d2-1681696268 .fill-AB5{fill:#F7F8FE;} + .d2-1681696268 .stroke-N1{stroke:#0A0F25;} + .d2-1681696268 .stroke-N2{stroke:#676C7E;} + .d2-1681696268 .stroke-N3{stroke:#9499AB;} + .d2-1681696268 .stroke-N4{stroke:#CFD2DD;} + .d2-1681696268 .stroke-N5{stroke:#DEE1EB;} + .d2-1681696268 .stroke-N6{stroke:#EEF1F8;} + .d2-1681696268 .stroke-N7{stroke:#FFFFFF;} + .d2-1681696268 .stroke-B1{stroke:#0D32B2;} + .d2-1681696268 .stroke-B2{stroke:#0D32B2;} + .d2-1681696268 .stroke-B3{stroke:#E3E9FD;} + .d2-1681696268 .stroke-B4{stroke:#E3E9FD;} + .d2-1681696268 .stroke-B5{stroke:#EDF0FD;} + .d2-1681696268 .stroke-B6{stroke:#F7F8FE;} + .d2-1681696268 .stroke-AA2{stroke:#4A6FF3;} + .d2-1681696268 .stroke-AA4{stroke:#EDF0FD;} + .d2-1681696268 .stroke-AA5{stroke:#F7F8FE;} + .d2-1681696268 .stroke-AB4{stroke:#EDF0FD;} + .d2-1681696268 .stroke-AB5{stroke:#F7F8FE;} + .d2-1681696268 .background-color-N1{background-color:#0A0F25;} + .d2-1681696268 .background-color-N2{background-color:#676C7E;} + .d2-1681696268 .background-color-N3{background-color:#9499AB;} + .d2-1681696268 .background-color-N4{background-color:#CFD2DD;} + .d2-1681696268 .background-color-N5{background-color:#DEE1EB;} + .d2-1681696268 .background-color-N6{background-color:#EEF1F8;} + .d2-1681696268 .background-color-N7{background-color:#FFFFFF;} + .d2-1681696268 .background-color-B1{background-color:#0D32B2;} + .d2-1681696268 .background-color-B2{background-color:#0D32B2;} + .d2-1681696268 .background-color-B3{background-color:#E3E9FD;} + .d2-1681696268 .background-color-B4{background-color:#E3E9FD;} + .d2-1681696268 .background-color-B5{background-color:#EDF0FD;} + .d2-1681696268 .background-color-B6{background-color:#F7F8FE;} + .d2-1681696268 .background-color-AA2{background-color:#4A6FF3;} + .d2-1681696268 .background-color-AA4{background-color:#EDF0FD;} + .d2-1681696268 .background-color-AA5{background-color:#F7F8FE;} + .d2-1681696268 .background-color-AB4{background-color:#EDF0FD;} + .d2-1681696268 .background-color-AB5{background-color:#F7F8FE;} + .d2-1681696268 .color-N1{color:#0A0F25;} + .d2-1681696268 .color-N2{color:#676C7E;} + .d2-1681696268 .color-N3{color:#9499AB;} + .d2-1681696268 .color-N4{color:#CFD2DD;} + .d2-1681696268 .color-N5{color:#DEE1EB;} + .d2-1681696268 .color-N6{color:#EEF1F8;} + .d2-1681696268 .color-N7{color:#FFFFFF;} + .d2-1681696268 .color-B1{color:#0D32B2;} + .d2-1681696268 .color-B2{color:#0D32B2;} + .d2-1681696268 .color-B3{color:#E3E9FD;} + .d2-1681696268 .color-B4{color:#E3E9FD;} + .d2-1681696268 .color-B5{color:#EDF0FD;} + .d2-1681696268 .color-B6{color:#F7F8FE;} + .d2-1681696268 .color-AA2{color:#4A6FF3;} + .d2-1681696268 .color-AA4{color:#EDF0FD;} + .d2-1681696268 .color-AA5{color:#F7F8FE;} + .d2-1681696268 .color-AB4{color:#EDF0FD;} + .d2-1681696268 .color-AB5{color:#F7F8FE;}.appendix text.text{fill:#0A0F25}.md{--color-fg-default:#0A0F25;--color-fg-muted:#676C7E;--color-fg-subtle:#9499AB;--color-canvas-default:#FFFFFF;--color-canvas-subtle:#EEF1F8;--color-border-default:#0D32B2;--color-border-muted:#0D32B2;--color-neutral-muted:#EEF1F8;--color-accent-fg:#0D32B2;--color-accent-emphasis:#0D32B2;--color-attention-subtle:#676C7E;--color-danger-fg:red;}.sketch-overlay-B1{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-B2{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-B3{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B6{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AA2{fill:url(#streaks-dark);mix-blend-mode:overlay}.sketch-overlay-AA4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AA5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AB4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AB5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N1{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-N2{fill:url(#streaks-dark);mix-blend-mode:overlay}.sketch-overlay-N3{fill:url(#streaks-normal);mix-blend-mode:color-burn}.sketch-overlay-N4{fill:url(#streaks-normal);mix-blend-mode:color-burn}.sketch-overlay-N5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N6{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N7{fill:url(#streaks-bright);mix-blend-mode:darken}.light-code{display: block}.dark-code{display: none}]]>cab12311112222 + - + - - + + \ No newline at end of file diff --git a/e2etests/testdata/regression/multiple_constant_nears/elk/board.exp.json b/e2etests/testdata/regression/multiple_constant_nears/elk/board.exp.json index 0842842e5..d80cd5430 100644 --- a/e2etests/testdata/regression/multiple_constant_nears/elk/board.exp.json +++ b/e2etests/testdata/regression/multiple_constant_nears/elk/board.exp.json @@ -213,7 +213,7 @@ "type": "rectangle", "pos": { "x": 85, - "y": 98 + "y": 284 }, "width": 179, "height": 302, @@ -254,7 +254,7 @@ "type": "rectangle", "pos": { "x": 135, - "y": 148 + "y": 334 }, "width": 78, "height": 66, @@ -295,7 +295,7 @@ "type": "rectangle", "pos": { "x": 135, - "y": 284 + "y": 470 }, "width": 79, "height": 66, @@ -435,11 +435,11 @@ "route": [ { "x": 174.5, - "y": 214 + "y": 400 }, { "x": 174.5, - "y": 284 + "y": 470 } ], "animated": false, diff --git a/e2etests/testdata/regression/multiple_constant_nears/elk/sketch.exp.svg b/e2etests/testdata/regression/multiple_constant_nears/elk/sketch.exp.svg index 719a0dd22..fa774803d 100644 --- a/e2etests/testdata/regression/multiple_constant_nears/elk/sketch.exp.svg +++ b/e2etests/testdata/regression/multiple_constant_nears/elk/sketch.exp.svg @@ -1,16 +1,16 @@ -cab12311112222 - + .d2-756368391 .fill-N1{fill:#0A0F25;} + .d2-756368391 .fill-N2{fill:#676C7E;} + .d2-756368391 .fill-N3{fill:#9499AB;} + .d2-756368391 .fill-N4{fill:#CFD2DD;} + .d2-756368391 .fill-N5{fill:#DEE1EB;} + .d2-756368391 .fill-N6{fill:#EEF1F8;} + .d2-756368391 .fill-N7{fill:#FFFFFF;} + .d2-756368391 .fill-B1{fill:#0D32B2;} + .d2-756368391 .fill-B2{fill:#0D32B2;} + .d2-756368391 .fill-B3{fill:#E3E9FD;} + .d2-756368391 .fill-B4{fill:#E3E9FD;} + .d2-756368391 .fill-B5{fill:#EDF0FD;} + .d2-756368391 .fill-B6{fill:#F7F8FE;} + .d2-756368391 .fill-AA2{fill:#4A6FF3;} + .d2-756368391 .fill-AA4{fill:#EDF0FD;} + .d2-756368391 .fill-AA5{fill:#F7F8FE;} + .d2-756368391 .fill-AB4{fill:#EDF0FD;} + .d2-756368391 .fill-AB5{fill:#F7F8FE;} + .d2-756368391 .stroke-N1{stroke:#0A0F25;} + .d2-756368391 .stroke-N2{stroke:#676C7E;} + .d2-756368391 .stroke-N3{stroke:#9499AB;} + .d2-756368391 .stroke-N4{stroke:#CFD2DD;} + .d2-756368391 .stroke-N5{stroke:#DEE1EB;} + .d2-756368391 .stroke-N6{stroke:#EEF1F8;} + .d2-756368391 .stroke-N7{stroke:#FFFFFF;} + .d2-756368391 .stroke-B1{stroke:#0D32B2;} + .d2-756368391 .stroke-B2{stroke:#0D32B2;} + .d2-756368391 .stroke-B3{stroke:#E3E9FD;} + .d2-756368391 .stroke-B4{stroke:#E3E9FD;} + .d2-756368391 .stroke-B5{stroke:#EDF0FD;} + .d2-756368391 .stroke-B6{stroke:#F7F8FE;} + .d2-756368391 .stroke-AA2{stroke:#4A6FF3;} + .d2-756368391 .stroke-AA4{stroke:#EDF0FD;} + .d2-756368391 .stroke-AA5{stroke:#F7F8FE;} + .d2-756368391 .stroke-AB4{stroke:#EDF0FD;} + .d2-756368391 .stroke-AB5{stroke:#F7F8FE;} + .d2-756368391 .background-color-N1{background-color:#0A0F25;} + .d2-756368391 .background-color-N2{background-color:#676C7E;} + .d2-756368391 .background-color-N3{background-color:#9499AB;} + .d2-756368391 .background-color-N4{background-color:#CFD2DD;} + .d2-756368391 .background-color-N5{background-color:#DEE1EB;} + .d2-756368391 .background-color-N6{background-color:#EEF1F8;} + .d2-756368391 .background-color-N7{background-color:#FFFFFF;} + .d2-756368391 .background-color-B1{background-color:#0D32B2;} + .d2-756368391 .background-color-B2{background-color:#0D32B2;} + .d2-756368391 .background-color-B3{background-color:#E3E9FD;} + .d2-756368391 .background-color-B4{background-color:#E3E9FD;} + .d2-756368391 .background-color-B5{background-color:#EDF0FD;} + .d2-756368391 .background-color-B6{background-color:#F7F8FE;} + .d2-756368391 .background-color-AA2{background-color:#4A6FF3;} + .d2-756368391 .background-color-AA4{background-color:#EDF0FD;} + .d2-756368391 .background-color-AA5{background-color:#F7F8FE;} + .d2-756368391 .background-color-AB4{background-color:#EDF0FD;} + .d2-756368391 .background-color-AB5{background-color:#F7F8FE;} + .d2-756368391 .color-N1{color:#0A0F25;} + .d2-756368391 .color-N2{color:#676C7E;} + .d2-756368391 .color-N3{color:#9499AB;} + .d2-756368391 .color-N4{color:#CFD2DD;} + .d2-756368391 .color-N5{color:#DEE1EB;} + .d2-756368391 .color-N6{color:#EEF1F8;} + .d2-756368391 .color-N7{color:#FFFFFF;} + .d2-756368391 .color-B1{color:#0D32B2;} + .d2-756368391 .color-B2{color:#0D32B2;} + .d2-756368391 .color-B3{color:#E3E9FD;} + .d2-756368391 .color-B4{color:#E3E9FD;} + .d2-756368391 .color-B5{color:#EDF0FD;} + .d2-756368391 .color-B6{color:#F7F8FE;} + .d2-756368391 .color-AA2{color:#4A6FF3;} + .d2-756368391 .color-AA4{color:#EDF0FD;} + .d2-756368391 .color-AA5{color:#F7F8FE;} + .d2-756368391 .color-AB4{color:#EDF0FD;} + .d2-756368391 .color-AB5{color:#F7F8FE;}.appendix text.text{fill:#0A0F25}.md{--color-fg-default:#0A0F25;--color-fg-muted:#676C7E;--color-fg-subtle:#9499AB;--color-canvas-default:#FFFFFF;--color-canvas-subtle:#EEF1F8;--color-border-default:#0D32B2;--color-border-muted:#0D32B2;--color-neutral-muted:#EEF1F8;--color-accent-fg:#0D32B2;--color-accent-emphasis:#0D32B2;--color-attention-subtle:#676C7E;--color-danger-fg:red;}.sketch-overlay-B1{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-B2{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-B3{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-B6{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AA2{fill:url(#streaks-dark);mix-blend-mode:overlay}.sketch-overlay-AA4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AA5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AB4{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-AB5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N1{fill:url(#streaks-darker);mix-blend-mode:lighten}.sketch-overlay-N2{fill:url(#streaks-dark);mix-blend-mode:overlay}.sketch-overlay-N3{fill:url(#streaks-normal);mix-blend-mode:color-burn}.sketch-overlay-N4{fill:url(#streaks-normal);mix-blend-mode:color-burn}.sketch-overlay-N5{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N6{fill:url(#streaks-bright);mix-blend-mode:darken}.sketch-overlay-N7{fill:url(#streaks-bright);mix-blend-mode:darken}.light-code{display: block}.dark-code{display: none}]]>cab12311112222 + - + - - + + \ No newline at end of file From 9ac8f3421668bc9540ecaec96fdc15497afe91e3 Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 21:18:56 -0700 Subject: [PATCH 4/5] fix scenario with only center nears --- d2layouts/d2near/layout.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/d2layouts/d2near/layout.go b/d2layouts/d2near/layout.go index 12cabf793..190317542 100644 --- a/d2layouts/d2near/layout.go +++ b/d2layouts/d2near/layout.go @@ -14,12 +14,22 @@ import ( const pad = 20 -var CenterNearConstants = map[string]struct{}{ +type set map[string]struct{} + +var HorizontalCenterNears = set{ + "center-left": {}, + "center-right": {}, +} +var VerticalCenterNears = set{ "top-center": {}, - "center-left": {}, - "center-right": {}, "bottom-center": {}, } +var NonCenterNears = set{ + "top-left": {}, + "top-right": {}, + "bottom-left": {}, + "bottom-right": {}, +} // Layout finds the shapes which are assigned constant near keywords and places them. func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs []*d2graph.Graph) error { @@ -37,11 +47,11 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs []*d2graph // Imagine the graph has two long texts, one at top center and one at top left. // Top left should go left enough to not collide with center. // So place the center ones first, then the later ones will consider them for bounding box - for _, processCenters := range []bool{true, false} { + for _, currentSet := range []set{VerticalCenterNears, HorizontalCenterNears, NonCenterNears} { for _, tempGraph := range constantNearGraphs { obj := tempGraph.Root.ChildrenArray[0] - _, isCenterConstant := CenterNearConstants[d2graph.Key(obj.NearKey)[0]] - if processCenters == isCenterConstant { + _, in := currentSet[d2graph.Key(obj.NearKey)[0]] + if in { prevX, prevY := obj.TopLeft.X, obj.TopLeft.Y obj.TopLeft = geo.NewPoint(place(obj)) dx, dy := obj.TopLeft.X-prevX, obj.TopLeft.Y-prevY @@ -64,8 +74,8 @@ func Layout(ctx context.Context, g *d2graph.Graph, constantNearGraphs []*d2graph } for _, tempGraph := range constantNearGraphs { obj := tempGraph.Root.ChildrenArray[0] - _, isCenterConstant := CenterNearConstants[d2graph.Key(obj.NearKey)[0]] - if processCenters == isCenterConstant { + _, in := currentSet[d2graph.Key(obj.NearKey)[0]] + if in { // The z-index for constant nears does not matter, as it will not collide g.Objects = append(g.Objects, tempGraph.Objects...) if obj.Parent.Children == nil { @@ -92,28 +102,20 @@ func place(obj *d2graph.Object) (float64, float64) { switch nearKeyStr { case "top-left": x, y = tl.X-obj.Width-pad, tl.Y-obj.Height-pad - break case "top-center": x, y = tl.X+w/2-obj.Width/2, tl.Y-obj.Height-pad - break case "top-right": x, y = br.X+pad, tl.Y-obj.Height-pad - break case "center-left": x, y = tl.X-obj.Width-pad, tl.Y+h/2-obj.Height/2 - break case "center-right": x, y = br.X+pad, tl.Y+h/2-obj.Height/2 - break case "bottom-left": x, y = tl.X-obj.Width-pad, br.Y+pad - break case "bottom-center": x, y = br.X-w/2-obj.Width/2, br.Y+pad - break case "bottom-right": x, y = br.X+pad, br.Y+pad - break } if obj.LabelPosition != nil && !strings.Contains(*obj.LabelPosition, "INSIDE") { From d5d7562bb0fb14d948bc46f3a49324ad1d2b92af Mon Sep 17 00:00:00 2001 From: Gavin Nishizawa Date: Mon, 18 Sep 2023 21:24:53 -0700 Subject: [PATCH 5/5] changelog --- ci/release/changelogs/next.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index cbc406562..8b1b3c4f6 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -14,3 +14,4 @@ - Fixes `d2 fmt` to format all files passed as arguments rather than first non-formatted only [#1523](https://github.com/terrastruct/d2/issues/1523) - Fixes Markdown cropping last element in mixed-element blocks (e.g. em and strong) [#1543](https://github.com/terrastruct/d2/issues/1543) - Fixes missing compile error for non-blockstring empty labels [#1590](https://github.com/terrastruct/d2/issues/1590) +- Fixes multiple constant nears overlapping in some cases [#1591](https://github.com/terrastruct/d2/issues/1591)