Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix appendix icon numbering/order #1704

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
- Adds a compiler error when accidentally using an arrowhead on a shape [#1686](https://github.com/terrastruct/d2/pull/1686)
- Correctly reports errors from invalid values set by globs. [#1691](https://github.com/terrastruct/d2/pull/1691)
- Fixes panic when spread substitution referenced a nonexistant var. [#1695](https://github.com/terrastruct/d2/pull/1695)
- Fixes incorrect appendix icon numbering. [#1704](https://github.com/terrastruct/d2/pull/1704)
38 changes: 34 additions & 4 deletions d2renderers/d2svg/appendix/appendix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package appendix
import (
"fmt"
"regexp"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -146,19 +147,48 @@ func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []by
closingIndex := strings.LastIndex(svg, "</svg></svg>")
svg = svg[:closingIndex] + appendix + svg[closingIndex:]

// icons are numbered according to diagram.Shapes which is based on their order of definition,
// but they appear in the svg according to renderOrder so we have to replace in that order
type appendixIcon struct {
number int
isTooltip bool
shape d2target.Shape
}
var renderOrder []appendixIcon

i := 1
for _, s := range diagram.Shapes {
if s.Tooltip != "" {
// The clip-path has a unique ID, so this won't replace any user icons
// In the existing SVG, the transform places it top-left, so we adjust
svg = strings.Replace(svg, d2svg.TooltipIcon, generateNumberedIcon(i, 0, ICON_RADIUS), 1)
renderOrder = append(renderOrder, appendixIcon{i, true, s})
i++
}
if s.Link != "" {
svg = strings.Replace(svg, d2svg.LinkIcon, generateNumberedIcon(i, 0, ICON_RADIUS), 1)
renderOrder = append(renderOrder, appendixIcon{i, false, s})
i++
}
}
// sort to match render order
sort.SliceStable(renderOrder, func(i, j int) bool {
iZIndex := renderOrder[i].shape.GetZIndex()
jZIndex := renderOrder[j].shape.GetZIndex()
if iZIndex != jZIndex {
return iZIndex < jZIndex
}
return renderOrder[i].shape.Level < renderOrder[j].shape.Level
})

// replace each rendered svg icon
for _, icon := range renderOrder {
// The clip-path has a unique ID, so this won't replace any user icons
// In the existing SVG, the transform places it top-left, so we adjust
var iconStr string
if icon.isTooltip {
iconStr = d2svg.TooltipIcon
} else {
iconStr = d2svg.LinkIcon
}
svg = strings.Replace(svg, iconStr, generateNumberedIcon(icon.number, 0, ICON_RADIUS), 1)
}

return []byte(svg)
}
Expand Down