@diagrammo/dgmo 0.25.5 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/README.md +3 -3
  2. package/dist/advanced.cjs +4255 -2756
  3. package/dist/advanced.d.cts +285 -59
  4. package/dist/advanced.d.ts +285 -59
  5. package/dist/advanced.js +4253 -2750
  6. package/dist/auto.cjs +4051 -2589
  7. package/dist/auto.js +124 -122
  8. package/dist/auto.mjs +4051 -2589
  9. package/dist/cli.cjs +172 -170
  10. package/dist/editor.cjs +4 -0
  11. package/dist/editor.js +4 -0
  12. package/dist/highlight.cjs +4 -0
  13. package/dist/highlight.js +4 -0
  14. package/dist/index.cjs +4076 -2591
  15. package/dist/index.d.cts +33 -8
  16. package/dist/index.d.ts +33 -8
  17. package/dist/index.js +4076 -2591
  18. package/dist/internal.cjs +4255 -2756
  19. package/dist/internal.d.cts +285 -59
  20. package/dist/internal.d.ts +285 -59
  21. package/dist/internal.js +4253 -2750
  22. package/dist/map-data/PROVENANCE.json +1 -1
  23. package/dist/map-data/airport-collisions.json +1 -0
  24. package/dist/map-data/airports.json +1 -0
  25. package/docs/language-reference.md +68 -18
  26. package/gallery/fixtures/boxes-and-lines-diverging.dgmo +15 -0
  27. package/gallery/fixtures/map-choropleth-diverging.dgmo +9 -0
  28. package/gallery/fixtures/map-region-values.dgmo +13 -0
  29. package/gallery/fixtures/map-subnational-zoom.dgmo +12 -0
  30. package/gallery/fixtures/map-tagged-legs.dgmo +16 -0
  31. package/gallery/fixtures/map-undirected-edges.dgmo +12 -0
  32. package/package.json +1 -1
  33. package/src/advanced.ts +3 -6
  34. package/src/auto/index.ts +1 -1
  35. package/src/boxes-and-lines/layout.ts +146 -26
  36. package/src/boxes-and-lines/parser.ts +43 -8
  37. package/src/boxes-and-lines/renderer.ts +223 -96
  38. package/src/boxes-and-lines/types.ts +9 -2
  39. package/src/c4/layout.ts +14 -32
  40. package/src/c4/parser.ts +9 -5
  41. package/src/c4/renderer.ts +34 -39
  42. package/src/class/layout.ts +118 -18
  43. package/src/class/parser.ts +35 -1
  44. package/src/class/renderer.ts +58 -2
  45. package/src/class/types.ts +3 -0
  46. package/src/cli.ts +4 -4
  47. package/src/completion-types.ts +0 -1
  48. package/src/completion.ts +106 -51
  49. package/src/cycle/layout.ts +55 -72
  50. package/src/cycle/renderer.ts +11 -6
  51. package/src/d3.ts +78 -117
  52. package/src/diagnostics.ts +16 -0
  53. package/src/echarts.ts +46 -33
  54. package/src/editor/keywords.ts +4 -0
  55. package/src/er/layout.ts +114 -22
  56. package/src/er/parser.ts +28 -1
  57. package/src/er/renderer.ts +55 -2
  58. package/src/er/types.ts +3 -0
  59. package/src/gantt/renderer.ts +46 -38
  60. package/src/gantt/resolver.ts +9 -2
  61. package/src/graph/edge-spline.ts +29 -0
  62. package/src/graph/flowchart-parser.ts +35 -2
  63. package/src/graph/flowchart-renderer.ts +80 -52
  64. package/src/graph/layout.ts +206 -23
  65. package/src/graph/notes.ts +21 -0
  66. package/src/graph/state-parser.ts +26 -1
  67. package/src/graph/state-renderer.ts +80 -52
  68. package/src/graph/types.ts +13 -0
  69. package/src/index.ts +1 -1
  70. package/src/infra/layout.ts +46 -26
  71. package/src/infra/parser.ts +1 -1
  72. package/src/infra/renderer.ts +16 -7
  73. package/src/journey-map/layout.ts +38 -49
  74. package/src/journey-map/renderer.ts +22 -45
  75. package/src/kanban/renderer.ts +15 -6
  76. package/src/label-layout.ts +3 -3
  77. package/src/map/completion.ts +77 -22
  78. package/src/map/context-labels.ts +57 -12
  79. package/src/map/data/PROVENANCE.json +1 -1
  80. package/src/map/data/airport-collisions.json +1 -0
  81. package/src/map/data/airports.json +1 -0
  82. package/src/map/data/types.ts +19 -0
  83. package/src/map/layout.ts +1196 -90
  84. package/src/map/legend-band.ts +2 -2
  85. package/src/map/load-data.ts +10 -1
  86. package/src/map/parser.ts +61 -32
  87. package/src/map/renderer.ts +284 -12
  88. package/src/map/resolved-types.ts +15 -1
  89. package/src/map/resolver.ts +132 -12
  90. package/src/map/types.ts +28 -8
  91. package/src/migrate/embedded.ts +9 -7
  92. package/src/mindmap/text-wrap.ts +13 -14
  93. package/src/org/layout.ts +19 -17
  94. package/src/org/renderer.ts +11 -4
  95. package/src/palettes/color-utils.ts +82 -21
  96. package/src/palettes/index.ts +0 -19
  97. package/src/palettes/registry.ts +1 -1
  98. package/src/palettes/types.ts +2 -2
  99. package/src/pert/layout.ts +48 -40
  100. package/src/pert/parser.ts +0 -14
  101. package/src/pert/renderer.ts +30 -43
  102. package/src/pyramid/renderer.ts +4 -5
  103. package/src/raci/renderer.ts +42 -70
  104. package/src/render.ts +1 -1
  105. package/src/ring/renderer.ts +1 -2
  106. package/src/sequence/parser.ts +100 -22
  107. package/src/sequence/renderer.ts +75 -50
  108. package/src/sitemap/layout.ts +27 -19
  109. package/src/sitemap/renderer.ts +12 -5
  110. package/src/tech-radar/renderer.ts +11 -35
  111. package/src/utils/arrow-markers.ts +51 -0
  112. package/src/utils/fit-canvas.ts +64 -0
  113. package/src/utils/legend-constants.ts +8 -54
  114. package/src/utils/legend-d3.ts +10 -7
  115. package/src/utils/legend-layout.ts +7 -4
  116. package/src/utils/legend-types.ts +10 -4
  117. package/src/utils/note-box/constants.ts +25 -0
  118. package/src/utils/note-box/index.ts +11 -0
  119. package/src/utils/note-box/metrics.ts +90 -0
  120. package/src/utils/note-box/svg.ts +331 -0
  121. package/src/utils/notes/bounds.ts +30 -0
  122. package/src/utils/notes/build.ts +131 -0
  123. package/src/utils/notes/index.ts +18 -0
  124. package/src/utils/notes/model.ts +19 -0
  125. package/src/utils/notes/parse.ts +131 -0
  126. package/src/utils/notes/place.ts +177 -0
  127. package/src/utils/notes/resolve.ts +88 -0
  128. package/src/utils/number-format.ts +36 -0
  129. package/src/utils/parsing.ts +41 -0
  130. package/src/utils/reserved-key-registry.ts +4 -0
  131. package/src/utils/text-measure.ts +122 -0
  132. package/src/wireframe/layout.ts +4 -2
  133. package/src/wireframe/renderer.ts +8 -6
  134. package/src/palettes/dracula.ts +0 -68
  135. package/src/palettes/gruvbox.ts +0 -85
  136. package/src/palettes/monokai.ts +0 -68
  137. package/src/palettes/one-dark.ts +0 -70
  138. package/src/palettes/rose-pine.ts +0 -84
  139. package/src/palettes/solarized.ts +0 -77
@@ -19,61 +19,15 @@ export const LEGEND_ICON_W = 20;
19
19
  export const LEGEND_MAX_ENTRY_ROWS = 3;
20
20
 
21
21
  // ── Proportional text measurement ────────────────────────────
22
- // Helvetica character width ratios (fraction of fontSize).
23
- // Replaces the naive `chars * 0.6 * fontSize` estimate with
24
- // per-character proportional widths for accurate legend sizing.
25
- // prettier-ignore
26
- const CHAR_W: Record<string, number> = {
27
- ' ':.28,'!': .28,'"': .36,'#': .56,'$': .56,'%': .89,'&': .67,"'":.19,
28
- '(':.33,')':.33,'*': .39,'+':.58,',':.28,'-':.33,'.':.28,'/':.28,
29
- '0':.56,'1':.56,'2':.56,'3':.56,'4':.56,'5':.56,'6':.56,'7':.56,'8':.56,'9':.56,
30
- ':':.28,';':.28,'<':.58,'=':.58,'>':.58,'?':.56,'@':1.02,
31
- A:.67,B:.67,C:.72,D:.72,E:.67,F:.61,G:.78,H:.72,I:.28,J:.50,K:.67,L:.56,M:.83,
32
- N:.72,O:.78,P:.67,Q:.78,R:.72,S:.67,T:.61,U:.72,V:.67,W:.94,X:.67,Y:.67,Z:.61,
33
- a:.56,b:.56,c:.50,d:.56,e:.56,f:.28,g:.56,h:.56,i:.22,j:.22,k:.50,l:.22,m:.83,
34
- n:.56,o:.56,p:.56,q:.56,r:.33,s:.50,t:.28,u:.56,v:.50,w:.72,x:.50,y:.50,z:.50,
35
- };
36
- const DEFAULT_W = 0.56;
22
+ // The canonical glyph-table measurer now lives in `./text-measure`.
23
+ // Re-exported here under the legacy names so existing legend call
24
+ // sites keep working; new code should import from `./text-measure`.
25
+ import { measureText, truncateText } from './text-measure';
37
26
 
38
- /** Estimate rendered text width using Helvetica proportional character widths. */
39
- export function measureLegendText(text: string, fontSize: number): number {
40
- let w = 0;
41
- for (let i = 0; i < text.length; i++) {
42
- // charAt returns '' for out-of-bounds, never undefined.
43
- w += (CHAR_W[text.charAt(i)] ?? DEFAULT_W) * fontSize;
44
- }
45
- return w;
46
- }
47
-
48
- /**
49
- * Truncate text with a trailing ellipsis to fit within maxWidth.
50
- * Returns the original text if it already fits, or '' if even the
51
- * ellipsis alone won't fit.
52
- */
53
- export function truncateLegendText(
54
- text: string,
55
- fontSize: number,
56
- maxWidth: number
57
- ): string {
58
- if (measureLegendText(text, fontSize) <= maxWidth) return text;
59
- const ellipsis = '…';
60
- const ellipsisW = measureLegendText(ellipsis, fontSize);
61
- if (ellipsisW > maxWidth) return '';
62
- let lo = 0;
63
- let hi = text.length;
64
- while (lo < hi) {
65
- const mid = Math.ceil((lo + hi) / 2);
66
- if (
67
- measureLegendText(text.slice(0, mid), fontSize) + ellipsisW <=
68
- maxWidth
69
- ) {
70
- lo = mid;
71
- } else {
72
- hi = mid - 1;
73
- }
74
- }
75
- return lo === 0 ? ellipsis : text.slice(0, lo) + ellipsis;
76
- }
27
+ /** @deprecated import `measureText` from `./text-measure`. */
28
+ export const measureLegendText = measureText;
29
+ /** @deprecated import `truncateText` from `./text-measure`. */
30
+ export const truncateLegendText = truncateText;
77
31
 
78
32
  // Eye icon SVG paths (14×14 viewBox)
79
33
  // Present only in org and sitemap legends (metadata visibility toggle)
@@ -15,7 +15,7 @@ import {
15
15
  measureLegendText,
16
16
  } from './legend-constants';
17
17
  import { computeLegendLayout } from './legend-layout';
18
- import { mix } from '../palettes/color-utils';
18
+ import { mix, valueRampStops } from '../palettes/color-utils';
19
19
  import { FONT_FAMILY } from '../fonts';
20
20
  import type {
21
21
  LegendConfig,
@@ -158,7 +158,7 @@ function renderCapsule(
158
158
  palette: LegendPalette,
159
159
  groupBg: string,
160
160
  pillBorder: string,
161
- _isDark: boolean,
161
+ isDark: boolean,
162
162
  callbacks?: LegendCallbacks
163
163
  ): void {
164
164
  const g = parent
@@ -213,11 +213,14 @@ function renderCapsule(
213
213
  const gr = capsule.gradient;
214
214
  const gradId = `dgmo-legend-ramp-${capsule.groupName.toLowerCase().replace(/[^a-z0-9]+/g, '-')}`;
215
215
  const def = g.append('defs').append('linearGradient').attr('id', gradId);
216
- def
217
- .append('stop')
218
- .attr('offset', '0%')
219
- .attr('stop-color', mix(gr.hue, gr.base, 15));
220
- def.append('stop').attr('offset', '100%').attr('stop-color', gr.hue);
216
+ // Sample the SAME ramp the fills use (direct = 2 endpoints; diverging =
217
+ // stops through the neutral midpoint), so the legend matches the basemap.
218
+ for (const stop of valueRampStops(gr.low, gr.high, { isDark })) {
219
+ def
220
+ .append('stop')
221
+ .attr('offset', `${stop.offset * 100}%`)
222
+ .attr('stop-color', stop.color);
223
+ }
221
224
  g.append('text')
222
225
  .attr('x', gr.minX)
223
226
  .attr('y', gr.textY)
@@ -20,6 +20,7 @@ import {
20
20
  truncateLegendText,
21
21
  } from './legend-constants';
22
22
 
23
+ import { compactNumber } from './number-format';
23
24
  import type { LegendGroupData } from './legend-types';
24
25
  import type {
25
26
  LegendConfig,
@@ -47,9 +48,11 @@ const RAMP_LEGEND_W = 80;
47
48
  const RAMP_LEGEND_H = 8;
48
49
  const RAMP_LABEL_GAP = 6;
49
50
 
50
- /** Compact numeric label for a ramp end (integers bare; else 1 decimal). */
51
+ /** Compact numeric label for a ramp end shared with the map's on-region value
52
+ * labels (`compactNumber`) so the gradient ends and region values read the same
53
+ * (`40M` legend end ↔ a `39.5M` region). */
51
54
  function fmtRamp(n: number): string {
52
- return Number.isInteger(n) ? String(n) : String(Math.round(n * 10) / 10);
55
+ return compactNumber(n);
53
56
  }
54
57
 
55
58
  /** Width of a gradient group's capsule: pill + min label + ramp + max label. */
@@ -114,8 +117,8 @@ function buildGradientCapsuleLayout(
114
117
  maxText,
115
118
  maxX,
116
119
  textY: LEGEND_HEIGHT / 2,
117
- hue: gradient.hue,
118
- base: gradient.base,
120
+ low: gradient.low,
121
+ high: gradient.high,
119
122
  },
120
123
  };
121
124
  }
@@ -88,8 +88,12 @@ export interface LegendGroupData {
88
88
  readonly gradient?: {
89
89
  readonly min: number;
90
90
  readonly max: number;
91
- readonly hue: string;
92
- readonly base: string;
91
+ /** Resolved hex of the LOW (t=0) endpoint. For a single-colour ramp this is
92
+ * the floored neutral (`mix(hue, base, RAMP_FLOOR)`); for an explicit
93
+ * two-colour ramp it is the user's low colour. */
94
+ readonly low: string;
95
+ /** Resolved hex of the HIGH (t=1) endpoint (the named hue). */
96
+ readonly high: string;
93
97
  };
94
98
  }
95
99
 
@@ -179,8 +183,10 @@ export interface LegendCapsuleLayout {
179
183
  maxText: string;
180
184
  maxX: number;
181
185
  textY: number;
182
- hue: string;
183
- base: string;
186
+ /** Resolved hex endpoints (low = t0, high = t1); the renderer samples the
187
+ * ramp between them via `valueRampStops`. */
188
+ low: string;
189
+ high: string;
184
190
  };
185
191
  }
186
192
 
@@ -0,0 +1,25 @@
1
+ // ============================================================
2
+ // Shared note-box primitive — constants
3
+ // ============================================================
4
+ //
5
+ // Lifted verbatim from the sequence renderer's note constants
6
+ // (sequence/renderer.ts) so any chart type can draw the same
7
+ // folded-corner annotation box. Pure values — no chart coupling.
8
+
9
+ /** Hard ceiling on a note box's width (px) before text wraps. */
10
+ export const NOTE_MAX_W = 200;
11
+ /** Size of the folded top-right corner (px). */
12
+ export const NOTE_FOLD = 10;
13
+ /** Horizontal padding inside the box (px). */
14
+ export const NOTE_PAD_H = 8;
15
+ /** Vertical padding inside the box (px). */
16
+ export const NOTE_PAD_V = 6;
17
+ /** Note body font size (px). */
18
+ export const NOTE_FONT_SIZE = 10;
19
+ /** Line height for wrapped body lines (px). */
20
+ export const NOTE_LINE_H = 14;
21
+ /** Gap between an anchor shape's edge and its floated note box (px). The
22
+ * note is tethered to its node with a solid connector across this gap. */
23
+ export const NOTE_GAP = 22;
24
+ /** Hanging-indent width for bullet body text past the "•" glyph (px). */
25
+ export const NOTE_BULLET_INDENT = 10;
@@ -0,0 +1,11 @@
1
+ // ============================================================
2
+ // Shared note-box primitive — barrel
3
+ // ============================================================
4
+ //
5
+ // Generic folded-corner annotation box, extracted from the sequence
6
+ // renderer so any node/edge chart type can adopt notes by writing only
7
+ // an anchor resolver + placement policy — never re-implementing the box.
8
+
9
+ export * from './constants';
10
+ export * from './metrics';
11
+ export * from './svg';
@@ -0,0 +1,90 @@
1
+ // ============================================================
2
+ // Shared note-box primitive — geometry / wrapping
3
+ // ============================================================
4
+ //
5
+ // Pure measurement: turns a note body string into wrapped lines and
6
+ // a box width/height. Layout-agnostic — used both to reserve space
7
+ // pre-layout and to draw the box. Reuses the canonical text measurer
8
+ // and the bullet-aware wrapper so note text wraps identically to
9
+ // every other rich-text field in the library.
10
+
11
+ import { wrapDescriptionLines, type WrappedDescLine } from '../wrapped-desc';
12
+ import { measureText } from '../text-measure';
13
+ import {
14
+ NOTE_MAX_W,
15
+ NOTE_PAD_H,
16
+ NOTE_PAD_V,
17
+ NOTE_FOLD,
18
+ NOTE_FONT_SIZE,
19
+ NOTE_LINE_H,
20
+ NOTE_BULLET_INDENT,
21
+ } from './constants';
22
+
23
+ export interface NoteBoxSize {
24
+ /** Box width in px (clamped to `maxW`). */
25
+ readonly width: number;
26
+ /** Box height in px. */
27
+ readonly height: number;
28
+ /** Wrapped, bullet-classified body lines ready for drawing. */
29
+ readonly lines: WrappedDescLine[];
30
+ }
31
+
32
+ export interface NoteBoxSizeOptions {
33
+ readonly fontSize?: number;
34
+ readonly maxW?: number;
35
+ }
36
+
37
+ /**
38
+ * Normalize a source body line's leading bullet marker (`- ` / `* `) to
39
+ * the canonical `• ` that {@link wrapDescriptionLines} recognizes for
40
+ * hanging-indent rendering.
41
+ */
42
+ function normalizeBulletLine(line: string): string {
43
+ return line.replace(/^\s*[-*]\s+/, '• ');
44
+ }
45
+
46
+ /**
47
+ * Wrap a note body (lines joined by `\n`) into bullet-classified display
48
+ * lines. Wrapping happens in pixel space via {@link measureText} so the
49
+ * boundary matches the rendered glyph widths.
50
+ */
51
+ export function wrapNoteBody(
52
+ body: string,
53
+ textMaxWidth: number,
54
+ fontSize: number = NOTE_FONT_SIZE
55
+ ): WrappedDescLine[] {
56
+ const sourceLines = body.split('\n').map(normalizeBulletLine);
57
+ return wrapDescriptionLines(sourceLines, textMaxWidth, (s) =>
58
+ measureText(s, fontSize)
59
+ );
60
+ }
61
+
62
+ /**
63
+ * Compute a note box's wrapped lines and outer dimensions.
64
+ *
65
+ * width = min(maxW, max(80, longestLine + padH*2 + fold))
66
+ * height = lineCount * lineH + padV*2
67
+ */
68
+ export function noteBoxSize(
69
+ body: string,
70
+ opts: NoteBoxSizeOptions = {}
71
+ ): NoteBoxSize {
72
+ const fontSize = opts.fontSize ?? NOTE_FONT_SIZE;
73
+ const maxW = opts.maxW ?? NOTE_MAX_W;
74
+ const textMaxWidth = maxW - NOTE_PAD_H * 2;
75
+ const lines = wrapNoteBody(body, textMaxWidth, fontSize);
76
+
77
+ let maxLineW = 0;
78
+ for (const line of lines) {
79
+ const indent = line.kind === 'plain' ? 0 : NOTE_BULLET_INDENT;
80
+ const w = measureText(line.text, fontSize) + indent;
81
+ if (w > maxLineW) maxLineW = w;
82
+ }
83
+
84
+ const width = Math.min(
85
+ maxW,
86
+ Math.max(80, maxLineW + NOTE_PAD_H * 2 + NOTE_FOLD)
87
+ );
88
+ const height = Math.max(1, lines.length) * NOTE_LINE_H + NOTE_PAD_V * 2;
89
+ return { width, height, lines };
90
+ }
@@ -0,0 +1,331 @@
1
+ // ============================================================
2
+ // Shared note-box primitive — SVG drawing
3
+ // ============================================================
4
+ //
5
+ // A pure `(parent, rect, lines) → <g class="note">` drawer. Takes a
6
+ // final position and knows nothing about charts, layout, or anchoring.
7
+ // Folded-corner box (palette-themed via `mix`, resvg-safe — never CSS
8
+ // color-mix) + inline-markdown body. Carries the `data-note-toggle`
9
+ // hook + line-number attrs for a future collapse enhancement; decorative
10
+ // sub-paths are `pointer-events:none` so they never steal interactivity.
11
+
12
+ import type * as d3Selection from 'd3-selection';
13
+ import type { PaletteColors } from '../../palettes';
14
+ import { mix } from '../../palettes/color-utils';
15
+ import { renderInlineText } from '../inline-markdown';
16
+ import type { WrappedDescLine } from '../wrapped-desc';
17
+ import {
18
+ NOTE_FOLD,
19
+ NOTE_PAD_H,
20
+ NOTE_PAD_V,
21
+ NOTE_FONT_SIZE,
22
+ NOTE_LINE_H,
23
+ NOTE_BULLET_INDENT,
24
+ } from './constants';
25
+
26
+ type GSelection = d3Selection.Selection<SVGGElement, unknown, null, undefined>;
27
+
28
+ // Subdued stroke styling for the note box, fold, and leader line — thin and
29
+ // a little lighter so the annotation stays quiet next to the diagram.
30
+ const NOTE_STROKE_WIDTH = 0.6;
31
+ const NOTE_STROKE_OPACITY = 0.6;
32
+
33
+ export interface NoteRect {
34
+ /** Left edge of the box, in the parent group's coordinate space. */
35
+ readonly x: number;
36
+ /** Top edge of the box. */
37
+ readonly y: number;
38
+ readonly width: number;
39
+ readonly height: number;
40
+ }
41
+
42
+ export interface RenderNoteBoxOptions {
43
+ readonly isDark: boolean;
44
+ readonly fontSize?: number;
45
+ /** Resolved hex accent (border + faded fill); default yellow if absent. */
46
+ readonly color?: string;
47
+ /** 1-based source line of the note (drives the toggle hook). */
48
+ readonly lineNumber?: number;
49
+ /** 1-based last source line of the note body. */
50
+ readonly endLineNumber?: number;
51
+ /**
52
+ * When true, the box is a click target that collapses the note (the app
53
+ * wires `data-note-toggle`): the group gets `cursor:pointer` + button
54
+ * a11y and the box fill catches pointer events. When false (the default,
55
+ * e.g. static export contexts), the box is inert.
56
+ */
57
+ readonly interactive?: boolean;
58
+ }
59
+
60
+ /**
61
+ * Draw the solid "tether" connecting a node to its note box so the
62
+ * annotation reads as belonging to that node. The note floats beside the
63
+ * shape WITHOUT moving it, so the tether spans the gap between them.
64
+ * Coordinates are in the parent group's space. Decorative —
65
+ * `pointer-events:none`.
66
+ */
67
+ export function renderNoteConnector(
68
+ parent: GSelection,
69
+ x1: number,
70
+ y1: number,
71
+ x2: number,
72
+ y2: number,
73
+ palette: PaletteColors
74
+ ): void {
75
+ parent
76
+ .append('line')
77
+ .attr('x1', x1)
78
+ .attr('y1', y1)
79
+ .attr('x2', x2)
80
+ .attr('y2', y2)
81
+ .attr('stroke', palette.textMuted)
82
+ .attr('stroke-width', NOTE_STROKE_WIDTH)
83
+ .attr('stroke-opacity', NOTE_STROKE_OPACITY)
84
+ .attr('class', 'note-connector')
85
+ .style('pointer-events', 'none');
86
+ }
87
+
88
+ /**
89
+ * Connector endpoints `[x1, y1, x2, y2]` (node-center-local) from the
90
+ * shape edge to the note's near edge, for the side the note sits on.
91
+ */
92
+ export function noteConnectorPoints(
93
+ node: { width: number; height: number },
94
+ note: {
95
+ x: number;
96
+ y: number;
97
+ width: number;
98
+ height: number;
99
+ side: 'above' | 'below' | 'left' | 'right';
100
+ }
101
+ ): [number, number, number, number] {
102
+ const clampX = Math.max(note.x, Math.min(0, note.x + note.width));
103
+ switch (note.side) {
104
+ case 'right':
105
+ return [node.width / 2, 0, note.x, note.y + note.height / 2];
106
+ case 'left':
107
+ return [
108
+ -node.width / 2,
109
+ 0,
110
+ note.x + note.width,
111
+ note.y + note.height / 2,
112
+ ];
113
+ case 'below':
114
+ return [clampX, node.height / 2, clampX, note.y];
115
+ case 'above':
116
+ default:
117
+ return [clampX, -node.height / 2, clampX, note.y + note.height];
118
+ }
119
+ }
120
+
121
+ /** Note accent (border) colour — the note's color, else the palette yellow. */
122
+ export function noteAccent(palette: PaletteColors, color?: string): string {
123
+ return color ?? palette.colors.yellow;
124
+ }
125
+
126
+ /** Faded note fill (resvg-safe — `mix`, never CSS color-mix). */
127
+ export function noteBoxFill(
128
+ palette: PaletteColors,
129
+ isDark: boolean,
130
+ color?: string
131
+ ): string {
132
+ const accent = noteAccent(palette, color);
133
+ return isDark ? mix(accent, palette.bg, 24) : mix(accent, palette.bg, 16);
134
+ }
135
+
136
+ /**
137
+ * Append a folded-corner note box to `parent` at `rect`, with `lines`
138
+ * (from {@link noteBoxSize}) as the body. Returns the created group.
139
+ */
140
+ export function renderNoteBox(
141
+ parent: GSelection,
142
+ rect: NoteRect,
143
+ lines: readonly WrappedDescLine[],
144
+ palette: PaletteColors,
145
+ opts: RenderNoteBoxOptions
146
+ ): GSelection {
147
+ const fontSize = opts.fontSize ?? NOTE_FONT_SIZE;
148
+ const interactive = opts.interactive ?? false;
149
+ const { x, y, width, height } = rect;
150
+ const fill = noteBoxFill(palette, opts.isDark, opts.color);
151
+ const accent = noteAccent(palette, opts.color);
152
+
153
+ const noteG = parent
154
+ .append('g')
155
+ .attr('class', 'note')
156
+ .attr('data-note-toggle', '');
157
+ if (opts.lineNumber !== undefined) {
158
+ noteG.attr('data-line-number', String(opts.lineNumber));
159
+ }
160
+ if (opts.endLineNumber !== undefined) {
161
+ noteG.attr('data-line-end', String(opts.endLineNumber));
162
+ }
163
+ if (interactive) {
164
+ noteG
165
+ .style('cursor', 'pointer')
166
+ .attr('role', 'button')
167
+ .attr('tabindex', '0')
168
+ .attr('aria-expanded', 'true')
169
+ .attr('aria-label', 'Collapse note');
170
+ }
171
+
172
+ // Folded-corner body path. When interactive it doubles as the click
173
+ // target (default pointer-events); otherwise it's inert.
174
+ const boxPath = noteG
175
+ .append('path')
176
+ .attr(
177
+ 'd',
178
+ [
179
+ `M ${x} ${y}`,
180
+ `L ${x + width - NOTE_FOLD} ${y}`,
181
+ `L ${x + width} ${y + NOTE_FOLD}`,
182
+ `L ${x + width} ${y + height}`,
183
+ `L ${x} ${y + height}`,
184
+ 'Z',
185
+ ].join(' ')
186
+ )
187
+ .attr('fill', fill)
188
+ .attr('stroke', accent)
189
+ .attr('stroke-width', NOTE_STROKE_WIDTH)
190
+ .attr('stroke-opacity', NOTE_STROKE_OPACITY)
191
+ .attr('class', 'note-box');
192
+ if (!interactive) boxPath.style('pointer-events', 'none');
193
+
194
+ // Fold triangle.
195
+ noteG
196
+ .append('path')
197
+ .attr(
198
+ 'd',
199
+ [
200
+ `M ${x + width - NOTE_FOLD} ${y}`,
201
+ `L ${x + width - NOTE_FOLD} ${y + NOTE_FOLD}`,
202
+ `L ${x + width} ${y + NOTE_FOLD}`,
203
+ ].join(' ')
204
+ )
205
+ .attr('fill', 'none')
206
+ .attr('stroke', accent)
207
+ .attr('stroke-width', NOTE_STROKE_WIDTH)
208
+ .attr('stroke-opacity', NOTE_STROKE_OPACITY)
209
+ .attr('class', 'note-fold')
210
+ .style('pointer-events', 'none');
211
+
212
+ // Body text — bullet first-lines get a "•" glyph at the left edge with
213
+ // the body hanging-indented; continuation lines align under the body.
214
+ lines.forEach((line, li) => {
215
+ const textY = y + NOTE_PAD_V + (li + 1) * NOTE_LINE_H - 3;
216
+ const indent = line.kind === 'plain' ? 0 : NOTE_BULLET_INDENT;
217
+ if (line.kind === 'bullet-first') {
218
+ noteG
219
+ .append('text')
220
+ .attr('x', x + NOTE_PAD_H)
221
+ .attr('y', textY)
222
+ .attr('fill', palette.text)
223
+ .attr('font-size', fontSize)
224
+ .text('•');
225
+ }
226
+ const textEl = noteG
227
+ .append('text')
228
+ .attr('x', x + NOTE_PAD_H + indent)
229
+ .attr('y', textY)
230
+ .attr('fill', palette.text)
231
+ .attr('font-size', fontSize)
232
+ .attr('class', 'note-text');
233
+ renderInlineText(textEl, line.text, palette, fontSize);
234
+ });
235
+
236
+ return noteG;
237
+ }
238
+
239
+ export interface RenderNoteBadgeOptions {
240
+ readonly isDark: boolean;
241
+ /** Resolved hex accent; default yellow if absent. */
242
+ readonly color?: string;
243
+ /** 1-based source line of the note (drives the toggle hook). */
244
+ readonly lineNumber?: number;
245
+ readonly endLineNumber?: number;
246
+ }
247
+
248
+ /** Half the badge's footprint (px) — for callers reserving corner space. */
249
+ export const NOTE_BADGE_RADIUS = 7;
250
+
251
+ /** Overall opacity of the collapsed badge — kept quiet so it reads as a
252
+ * subtle affordance, not a loud icon competing with the node. */
253
+ const NOTE_BADGE_OPACITY = 0.6;
254
+
255
+ /**
256
+ * Draw the collapsed-note badge: a small comment bubble pinned at `center`
257
+ * (parent-group coords). Carries the same `data-note-toggle` + line attrs
258
+ * as the expanded box, so clicking it re-expands the note. Interactive by
259
+ * design (button a11y + `cursor:pointer`).
260
+ */
261
+ export function renderNoteBadge(
262
+ parent: GSelection,
263
+ center: { readonly x: number; readonly y: number },
264
+ palette: PaletteColors,
265
+ opts: RenderNoteBadgeOptions
266
+ ): GSelection {
267
+ const fill = noteBoxFill(palette, opts.isDark, opts.color);
268
+ const accent = noteAccent(palette, opts.color);
269
+ const g = parent
270
+ .append('g')
271
+ .attr('class', 'note note-badge')
272
+ .attr('data-note-toggle', '')
273
+ .attr('transform', `translate(${center.x}, ${center.y})`)
274
+ .attr('opacity', NOTE_BADGE_OPACITY)
275
+ .style('cursor', 'pointer')
276
+ .attr('role', 'button')
277
+ .attr('tabindex', '0')
278
+ .attr('aria-expanded', 'false')
279
+ .attr('aria-label', 'Expand note');
280
+ if (opts.lineNumber !== undefined) {
281
+ g.attr('data-line-number', String(opts.lineNumber));
282
+ }
283
+ if (opts.endLineNumber !== undefined) {
284
+ g.attr('data-line-end', String(opts.endLineNumber));
285
+ }
286
+
287
+ // Comment bubble: small rounded body (13×9) with a tail at the
288
+ // bottom-left. Single path so body + tail share one seamless outline.
289
+ g.append('path')
290
+ .attr(
291
+ 'd',
292
+ [
293
+ 'M -6.5 -4',
294
+ 'Q -6.5 -6 -4.5 -6',
295
+ 'L 4.5 -6',
296
+ 'Q 6.5 -6 6.5 -4',
297
+ 'L 6.5 1',
298
+ 'Q 6.5 3 4.5 3',
299
+ 'L -1 3',
300
+ 'L -4 6',
301
+ 'L -2.5 3',
302
+ 'L -4.5 3',
303
+ 'Q -6.5 3 -6.5 1',
304
+ 'Z',
305
+ ].join(' ')
306
+ )
307
+ .attr('fill', fill)
308
+ .attr('stroke', accent)
309
+ .attr('stroke-width', 0.55)
310
+ .attr('class', 'note-badge-bubble');
311
+
312
+ // Two short "text" strokes inside the bubble (group opacity tones them).
313
+ g.append('line')
314
+ .attr('x1', -3.5)
315
+ .attr('y1', -3)
316
+ .attr('x2', 3.5)
317
+ .attr('y2', -3)
318
+ .attr('stroke', palette.textMuted)
319
+ .attr('stroke-width', 0.75)
320
+ .style('pointer-events', 'none');
321
+ g.append('line')
322
+ .attr('x1', -3.5)
323
+ .attr('y1', -0.5)
324
+ .attr('x2', 1.5)
325
+ .attr('y2', -0.5)
326
+ .attr('stroke', palette.textMuted)
327
+ .attr('stroke-width', 0.75)
328
+ .style('pointer-events', 'none');
329
+
330
+ return g;
331
+ }
@@ -0,0 +1,30 @@
1
+ // ============================================================
2
+ // Generic diagram note — canvas shift
3
+ // ============================================================
4
+ //
5
+ // A note floated above/left of its node can land at negative coordinates.
6
+ // After a chart computes its content bbox (INCLUDING note rects), it passes
7
+ // the bbox min corner here to learn how much to translate every node, edge,
8
+ // and group so nothing clips on export. Charts with no off-canvas note get
9
+ // `{shiftX:0, shiftY:0}` and stay byte-for-byte unchanged.
10
+
11
+ export interface NoteCanvasShift {
12
+ readonly shiftX: number;
13
+ readonly shiftY: number;
14
+ }
15
+
16
+ /**
17
+ * Translation needed to bring content that ran off the top/left back into
18
+ * the canvas with a `margin` gutter. Only shifts when a min coord is
19
+ * negative; otherwise returns 0 on that axis.
20
+ */
21
+ export function noteCanvasShift(
22
+ minX: number,
23
+ minY: number,
24
+ margin = 20
25
+ ): NoteCanvasShift {
26
+ return {
27
+ shiftX: minX < 0 ? margin - minX : 0,
28
+ shiftY: minY < 0 ? margin - minY : 0,
29
+ };
30
+ }