From 385a2564c78a4c9b6ce193ed7f7afb81bbce4226 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Wed, 28 Dec 2022 16:33:46 -0800 Subject: [PATCH] appendix --- d2renderers/d2svg/appendix/appendix.go | 31 ++++++++----- d2renderers/d2svg/appendix/appendix_test.go | 9 +++- .../diagram_wider_than_tooltip/sketch.exp.svg | 8 ++-- .../appendix/testdata/links/sketch.exp.svg | 46 +++++++++++++++++++ .../tooltip_wider_than_diagram/sketch.exp.svg | 8 ++-- d2renderers/d2svg/link.svg | 4 +- 6 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 d2renderers/d2svg/appendix/testdata/links/sketch.exp.svg diff --git a/d2renderers/d2svg/appendix/appendix.go b/d2renderers/d2svg/appendix/appendix.go index 3aeb5ab94..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, 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!