diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 50ccdb876..1078069ca 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -1,6 +1,7 @@ #### Features ๐Ÿš€ -- Tooltips on shapes. See [https://d2lang.com/tour/tooltips](https://d2lang.com/tour/tooltips). [#545](https://github.com/terrastruct/d2/pull/545) +- Tooltips can be set on shapes. See [https://d2lang.com/tour/tooltips](https://d2lang.com/tour/interactive). [#548](https://github.com/terrastruct/d2/pull/548) +- Links can be set on shapes. See [https://d2lang.com/tour/tooltips](https://d2lang.com/tour/interactive). [#548](https://github.com/terrastruct/d2/pull/548) #### Improvements ๐Ÿงน diff --git a/d2renderers/d2svg/appendix/appendix.go b/d2renderers/d2svg/appendix/appendix.go index fa44931f7..f25dc1c02 100644 --- a/d2renderers/d2svg/appendix/appendix.go +++ b/d2renderers/d2svg/appendix/appendix.go @@ -51,10 +51,10 @@ var viewboxRegex = regexp.MustCompile(`viewBox=\"([0-9\- ]+)\"`) var widthRegex = regexp.MustCompile(`width=\"([0-9]+)\"`) var heightRegex = regexp.MustCompile(`height=\"([0-9]+)\"`) -func AppendTooltips(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []byte { +func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []byte { svg := string(in) - appendix, w, h := generateTooltipAppendix(diagram, ruler, svg) + appendix, w, h := generateAppendix(diagram, ruler, svg) if h == 0 { return in @@ -125,30 +125,37 @@ func AppendTooltips(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []by svg = strings.Replace(svg, d2svg.TooltipIcon, generateNumberedIcon(i, 0, ICON_RADIUS), 1) i++ } + if s.Link != "" { + svg = strings.Replace(svg, d2svg.LinkIcon, generateNumberedIcon(i, 0, ICON_RADIUS), 1) + i++ + } } return []byte(svg) } -func generateTooltipAppendix(diagram *d2target.Diagram, ruler *textmeasure.Ruler, svg string) (string, int, int) { +func generateAppendix(diagram *d2target.Diagram, ruler *textmeasure.Ruler, svg string) (string, int, int) { tl, br := diagram.BoundingBox() maxWidth, totalHeight := 0, 0 - var tooltipLines []string + var lines []string i := 1 + for _, s := range diagram.Shapes { - if s.Tooltip != "" { - line, w, h := generateTooltipLine(i, br.Y+(PAD_TOP*2)+totalHeight, s.Tooltip, ruler) - i++ - tooltipLines = append(tooltipLines, line) - maxWidth = go2.IntMax(maxWidth, w) - totalHeight += h + SPACER + for _, txt := range []string{s.Tooltip, s.Link} { + if txt != "" { + line, w, h := generateLine(i, br.Y+(PAD_TOP*2)+totalHeight, txt, ruler) + i++ + lines = append(lines, line) + maxWidth = go2.IntMax(maxWidth, w) + totalHeight += h + SPACER + } } } return fmt.Sprintf(`%s -`, tl.X, br.Y, (br.X - tl.X), strings.Join(tooltipLines, "\n")), maxWidth, totalHeight +`, tl.X, br.Y, (br.X - tl.X), strings.Join(lines, "\n")), maxWidth, totalHeight } func generateNumberedIcon(i, x, y int) string { @@ -161,7 +168,7 @@ func generateNumberedIcon(i, x, y int) string { return line } -func generateTooltipLine(i, y int, text string, ruler *textmeasure.Ruler) (string, int, int) { +func generateLine(i, y int, text string, ruler *textmeasure.Ruler) (string, int, int) { mtext := &d2target.MText{ Text: text, FontSize: FONT_SIZE, @@ -169,7 +176,7 @@ func generateTooltipLine(i, y int, text string, ruler *textmeasure.Ruler) (strin dims := d2graph.GetTextDimensions(nil, ruler, mtext, nil) - line := fmt.Sprintf(`%s`, + line := fmt.Sprintf(`%s`, 0, y, generateNumberedIcon(i, 0, 0)) line += fmt.Sprintf(`%s`, diff --git a/d2renderers/d2svg/appendix/appendix_test.go b/d2renderers/d2svg/appendix/appendix_test.go index a589498cf..a4105c38e 100644 --- a/d2renderers/d2svg/appendix/appendix_test.go +++ b/d2renderers/d2svg/appendix/appendix_test.go @@ -75,6 +75,13 @@ payment processor behind the scenes: { acquirer -> store bank: '$10 credit' } } +`, + }, + { + name: "links", + script: `x: { link: https://d2lang.com } + y: { link: https://terrastruct.com; tooltip: Gee, I feel kind of LIGHT in the head now,\nknowing I can't make my satellite dish PAYMENTS! } +x -> y `, }, } @@ -127,7 +134,7 @@ func run(t *testing.T, tc testCase) { Pad: d2svg.DEFAULT_PADDING, }) assert.Success(t, err) - svgBytes = appendix.AppendTooltips(diagram, ruler, svgBytes) + svgBytes = appendix.Append(diagram, ruler, svgBytes) err = os.MkdirAll(dataPath, 0755) assert.Success(t, err) diff --git a/d2renderers/d2svg/appendix/testdata/diagram_wider_than_tooltip/sketch.exp.svg b/d2renderers/d2svg/appendix/testdata/diagram_wider_than_tooltip/sketch.exp.svg index 268e98514..6c5e1e58b 100644 --- a/d2renderers/d2svg/appendix/testdata/diagram_wider_than_tooltip/sketch.exp.svg +++ b/d2renderers/d2svg/appendix/testdata/diagram_wider_than_tooltip/sketch.exp.svg @@ -18,7 +18,7 @@ width="1959" height="2297" viewBox="-175 -47 1959 2297">customerissuerstore1Like starbucks or somethingacquirer2I'm not sure what this isnetworkcustomer bankstore bankinitial transactionpayment processor behind the scenessimplified 1 banana please$10 dollarsthinking: wow, inflationchecks bank accountSavings: $11I can do that, here's my cardRun this cardProcess to card issuerProcess this payment$10 debit$10 creditAn error in judgement is about to occur +customerissuerstore1Like starbucks or somethingacquirer2I'm not sure what this isnetworkcustomer bankstore bankinitial transactionpayment processor behind the scenessimplified 1 banana please$10 dollarsthinking: wow, inflationchecks bank accountSavings: $11I can do that, here's my cardRun this cardProcess to card issuerProcess this payment$10 debit$10 creditAn error in judgement is about to occur @@ -42,7 +42,7 @@ width="1959" height="2297" viewBox="-175 -47 1959 2297">1Like starbucks or something -2I'm not sure what this is +}]]>1Like starbucks or something +2I'm not sure what this is x1y2Gee, I feel kind of LIGHT in the head now, +knowing I can't make my satellite dish PAYMENTS!3 + + +1https://d2lang.com +2Gee, I feel kind of LIGHT in the head now,knowing I can't make my satellite dish PAYMENTS! +3https://terrastruct.com + \ No newline at end of file diff --git a/d2renderers/d2svg/appendix/testdata/tooltip_wider_than_diagram/sketch.exp.svg b/d2renderers/d2svg/appendix/testdata/tooltip_wider_than_diagram/sketch.exp.svg index 8c6640ce4..b2b23e46f 100644 --- a/d2renderers/d2svg/appendix/testdata/tooltip_wider_than_diagram/sketch.exp.svg +++ b/d2renderers/d2svg/appendix/testdata/tooltip_wider_than_diagram/sketch.exp.svg @@ -18,12 +18,12 @@ width="564" height="700" viewBox="-100 -100 564 700">x1Total abstinence is easier than perfect moderationy2Gee, I feel kind of LIGHT in the head now, +</style><g id="x"><g class="shape" ><rect x="1" y="0" width="113" height="126" style="fill:#F7F8FE;stroke:#0D32B2;opacity:1.000000;stroke-width:2;" /></g><text class="text-bold" x="57.500000" y="66.000000" style="text-anchor:middle;font-size:16px;fill:#0A0F25">x</text><g transform="translate(98 -16)" class="appendix-icon"><circle cx="16" cy="16" r="16" fill="white" stroke="#DEE1EB" /><text class="text-bold" x="16" y="21" style="font-size: 16px;text-anchor:middle;">1</text></g><title>Total abstinence is easier than perfect moderationy2Gee, I feel kind of LIGHT in the head now, knowing I can't make my satellite dish PAYMENTS! 1Total abstinence is easier than perfect moderation -2Gee, I feel kind of LIGHT in the head now,knowing I can't make my satellite dish PAYMENTS! +}]]>1Total abstinence is easier than perfect moderation +2Gee, I feel kind of LIGHT in the head now,knowing I can't make my satellite dish PAYMENTS! x + + + + + + + + + + + +y + + + + + + + + + + + + +Gee, I feel kind of LIGHT in the head now, +knowing I can't make my satellite dish PAYMENTS! + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/e2etests/testdata/stable/links/elk/board.exp.json b/e2etests/testdata/stable/links/elk/board.exp.json new file mode 100644 index 000000000..69c78025b --- /dev/null +++ b/e2etests/testdata/stable/links/elk/board.exp.json @@ -0,0 +1,127 @@ +{ + "name": "", + "fontFamily": "SourceSansPro", + "shapes": [ + { + "id": "x", + "type": "", + "pos": { + "x": 12, + "y": 12 + }, + "width": 113, + "height": 126, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#F7F8FE", + "stroke": "#0D32B2", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "", + "link": "https://d2lang.com", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "x", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 13, + "labelHeight": 26, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + }, + { + "id": "y", + "type": "", + "pos": { + "x": 12, + "y": 238 + }, + "width": 114, + "height": 126, + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "borderRadius": 0, + "fill": "#F7F8FE", + "stroke": "#0D32B2", + "shadow": false, + "3d": false, + "multiple": false, + "tooltip": "Gee, I feel kind of LIGHT in the head now,\nknowing I can't make my satellite dish PAYMENTS!", + "link": "https://terrastruct.com", + "icon": null, + "iconPosition": "", + "blend": false, + "fields": null, + "methods": null, + "columns": null, + "label": "y", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "#0A0F25", + "italic": false, + "bold": true, + "underline": false, + "labelWidth": 14, + "labelHeight": 26, + "labelPosition": "INSIDE_MIDDLE_CENTER", + "zIndex": 0, + "level": 1 + } + ], + "connections": [ + { + "id": "(x -> y)[0]", + "src": "x", + "srcArrow": "none", + "srcLabel": "", + "dst": "y", + "dstArrow": "triangle", + "dstLabel": "", + "opacity": 1, + "strokeDash": 0, + "strokeWidth": 2, + "stroke": "#0D32B2", + "label": "", + "fontSize": 16, + "fontFamily": "DEFAULT", + "language": "", + "color": "#676C7E", + "italic": true, + "bold": false, + "underline": false, + "labelWidth": 0, + "labelHeight": 0, + "labelPosition": "", + "labelPercentage": 0, + "route": [ + { + "x": 69, + "y": 138 + }, + { + "x": 69, + "y": 238 + } + ], + "animated": false, + "tooltip": "", + "icon": null, + "zIndex": 0 + } + ] +} diff --git a/e2etests/testdata/stable/links/elk/sketch.exp.svg b/e2etests/testdata/stable/links/elk/sketch.exp.svg new file mode 100644 index 000000000..3a532dcdc --- /dev/null +++ b/e2etests/testdata/stable/links/elk/sketch.exp.svg @@ -0,0 +1,72 @@ + +x + + + + + + + + + + + +y + + + + + + + + + + + + +Gee, I feel kind of LIGHT in the head now, +knowing I can't make my satellite dish PAYMENTS! + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/e2etests/testdata/stable/tooltips/dagre/sketch.exp.svg b/e2etests/testdata/stable/tooltips/dagre/sketch.exp.svg index a18a668c0..083bb23f6 100644 --- a/e2etests/testdata/stable/tooltips/dagre/sketch.exp.svg +++ b/e2etests/testdata/stable/tooltips/dagre/sketch.exp.svg @@ -18,7 +18,7 @@ width="314" height="552" viewBox="-100 -100 314 552">x +x @@ -31,7 +31,7 @@ width="314" height="552" viewBox="-100 -100 314 552"> -Total abstinence is easier than perfect moderationy +Total abstinence is easier than perfect moderationy @@ -49,7 +49,7 @@ knowing I can't make my satellite dish PAYMENTS! x +x @@ -31,7 +31,7 @@ width="314" height="552" viewBox="-88 -88 314 552"> -Total abstinence is easier than perfect moderationy +Total abstinence is easier than perfect moderationy @@ -49,7 +49,7 @@ knowing I can't make my satellite dish PAYMENTS!