@diagrammo/dgmo 0.26.0 → 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 (136) hide show
  1. package/README.md +3 -3
  2. package/dist/advanced.cjs +4182 -2704
  3. package/dist/advanced.d.cts +266 -58
  4. package/dist/advanced.d.ts +266 -58
  5. package/dist/advanced.js +4182 -2698
  6. package/dist/auto.cjs +4042 -2581
  7. package/dist/auto.js +124 -122
  8. package/dist/auto.mjs +4042 -2581
  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 +4067 -2583
  15. package/dist/index.d.cts +33 -8
  16. package/dist/index.d.ts +33 -8
  17. package/dist/index.js +4067 -2583
  18. package/dist/internal.cjs +4182 -2704
  19. package/dist/internal.d.cts +266 -58
  20. package/dist/internal.d.ts +266 -58
  21. package/dist/internal.js +4182 -2698
  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 +1 -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 -0
  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.ts +26 -12
  48. package/src/cycle/layout.ts +55 -72
  49. package/src/cycle/renderer.ts +11 -6
  50. package/src/d3.ts +78 -117
  51. package/src/diagnostics.ts +16 -0
  52. package/src/echarts.ts +46 -33
  53. package/src/editor/keywords.ts +4 -0
  54. package/src/er/layout.ts +114 -22
  55. package/src/er/parser.ts +28 -0
  56. package/src/er/renderer.ts +55 -2
  57. package/src/er/types.ts +3 -0
  58. package/src/gantt/renderer.ts +46 -38
  59. package/src/gantt/resolver.ts +9 -2
  60. package/src/graph/edge-spline.ts +29 -0
  61. package/src/graph/flowchart-parser.ts +34 -1
  62. package/src/graph/flowchart-renderer.ts +78 -64
  63. package/src/graph/layout.ts +206 -23
  64. package/src/graph/notes.ts +21 -0
  65. package/src/graph/state-parser.ts +26 -1
  66. package/src/graph/state-renderer.ts +78 -64
  67. package/src/graph/types.ts +13 -0
  68. package/src/index.ts +1 -1
  69. package/src/infra/layout.ts +46 -26
  70. package/src/infra/renderer.ts +16 -7
  71. package/src/journey-map/layout.ts +38 -49
  72. package/src/journey-map/renderer.ts +22 -45
  73. package/src/kanban/renderer.ts +15 -6
  74. package/src/label-layout.ts +3 -3
  75. package/src/map/completion.ts +77 -22
  76. package/src/map/context-labels.ts +57 -12
  77. package/src/map/data/PROVENANCE.json +1 -1
  78. package/src/map/data/airport-collisions.json +1 -0
  79. package/src/map/data/airports.json +1 -0
  80. package/src/map/data/types.ts +19 -0
  81. package/src/map/layout.ts +1196 -90
  82. package/src/map/legend-band.ts +2 -2
  83. package/src/map/load-data.ts +10 -1
  84. package/src/map/parser.ts +61 -32
  85. package/src/map/renderer.ts +284 -12
  86. package/src/map/resolved-types.ts +15 -1
  87. package/src/map/resolver.ts +132 -12
  88. package/src/map/types.ts +28 -8
  89. package/src/migrate/embedded.ts +9 -7
  90. package/src/mindmap/text-wrap.ts +13 -14
  91. package/src/org/layout.ts +19 -17
  92. package/src/org/renderer.ts +11 -4
  93. package/src/palettes/color-utils.ts +82 -21
  94. package/src/palettes/index.ts +0 -19
  95. package/src/palettes/registry.ts +1 -1
  96. package/src/palettes/types.ts +2 -2
  97. package/src/pert/layout.ts +48 -40
  98. package/src/pert/renderer.ts +30 -43
  99. package/src/pyramid/renderer.ts +4 -5
  100. package/src/raci/renderer.ts +34 -68
  101. package/src/render.ts +1 -1
  102. package/src/ring/renderer.ts +1 -2
  103. package/src/sequence/parser.ts +100 -22
  104. package/src/sequence/renderer.ts +75 -50
  105. package/src/sitemap/layout.ts +27 -19
  106. package/src/sitemap/renderer.ts +12 -5
  107. package/src/tech-radar/renderer.ts +11 -35
  108. package/src/utils/arrow-markers.ts +51 -0
  109. package/src/utils/fit-canvas.ts +64 -0
  110. package/src/utils/legend-constants.ts +8 -54
  111. package/src/utils/legend-d3.ts +10 -7
  112. package/src/utils/legend-layout.ts +7 -4
  113. package/src/utils/legend-types.ts +10 -4
  114. package/src/utils/note-box/constants.ts +25 -0
  115. package/src/utils/note-box/index.ts +11 -0
  116. package/src/utils/note-box/metrics.ts +90 -0
  117. package/src/utils/note-box/svg.ts +331 -0
  118. package/src/utils/notes/bounds.ts +30 -0
  119. package/src/utils/notes/build.ts +131 -0
  120. package/src/utils/notes/index.ts +18 -0
  121. package/src/utils/notes/model.ts +19 -0
  122. package/src/utils/notes/parse.ts +131 -0
  123. package/src/utils/notes/place.ts +177 -0
  124. package/src/utils/notes/resolve.ts +88 -0
  125. package/src/utils/number-format.ts +36 -0
  126. package/src/utils/parsing.ts +41 -0
  127. package/src/utils/reserved-key-registry.ts +4 -0
  128. package/src/utils/text-measure.ts +122 -0
  129. package/src/wireframe/layout.ts +4 -2
  130. package/src/wireframe/renderer.ts +8 -6
  131. package/src/palettes/dracula.ts +0 -68
  132. package/src/palettes/gruvbox.ts +0 -85
  133. package/src/palettes/monokai.ts +0 -68
  134. package/src/palettes/one-dark.ts +0 -70
  135. package/src/palettes/rose-pine.ts +0 -84
  136. package/src/palettes/solarized.ts +0 -77
@@ -0,0 +1,131 @@
1
+ // ============================================================
2
+ // Generic diagram note — layout assembly
3
+ // ============================================================
4
+ //
5
+ // Turns resolved notes + positioned anchor rects into render-ready
6
+ // {@link PlacedNote}s (box geometry LOCAL to each node's center, or a
7
+ // collapsed flag). A chart's layout calls this once, attaches each
8
+ // PlacedNote to its layout node, then extends its content bbox with the
9
+ // note rects and applies `noteCanvasShift`. Keeps every adopting chart's
10
+ // note wiring to a few lines.
11
+
12
+ import { noteBoxSize } from '../note-box';
13
+ import type { WrappedDescLine } from '../wrapped-desc';
14
+ import { placeNotes, type NoteSide, type PlaceableNode } from './place';
15
+ import type { DiagramNote } from './model';
16
+
17
+ /** A resolved, placed note ready for the note-box drawer. */
18
+ export interface PlacedNote {
19
+ /** Box left, LOCAL to the node center (add node.x). Unused if collapsed. */
20
+ readonly x: number;
21
+ /** Box top, LOCAL to the node center (add node.y). Unused if collapsed. */
22
+ readonly y: number;
23
+ readonly width: number;
24
+ readonly height: number;
25
+ readonly side: NoteSide;
26
+ /** Resolved hex accent (border + faded fill); default yellow if absent. */
27
+ readonly color?: string;
28
+ readonly lines: readonly WrappedDescLine[];
29
+ readonly lineNumber: number;
30
+ readonly endLineNumber: number;
31
+ /** Collapsed → renderer draws a corner badge; box geometry is unused. */
32
+ readonly collapsed?: boolean;
33
+ }
34
+
35
+ /** A positioned anchor node: its id plus its center rect. */
36
+ export interface NoteAnchor extends PlaceableNode {
37
+ readonly id: string;
38
+ }
39
+
40
+ /**
41
+ * Build a map of node id → {@link PlacedNote} for every anchor that has a
42
+ * resolved note. Measures each note, runs the shared collision placer, and
43
+ * assembles render-ready geometry. Collapsed notes (lineNumber ∈
44
+ * `collapsedNotes`) reserve no space.
45
+ */
46
+ export function buildPlacedNotes(
47
+ anchors: readonly NoteAnchor[],
48
+ noteByNode: ReadonlyMap<string, DiagramNote>,
49
+ direction: 'TB' | 'LR',
50
+ collapsedNotes?: ReadonlySet<number>
51
+ ): Map<string, PlacedNote> {
52
+ const placed = new Map<string, PlacedNote>();
53
+ if (noteByNode.size === 0) return placed;
54
+
55
+ interface Geom {
56
+ readonly noteW: number;
57
+ readonly noteH: number;
58
+ readonly color?: string;
59
+ readonly lines: readonly WrappedDescLine[];
60
+ readonly lineNumber: number;
61
+ readonly endLineNumber: number;
62
+ }
63
+ const geoms = new Map<string, Geom>();
64
+ const requests: {
65
+ key: string;
66
+ node: PlaceableNode;
67
+ noteW: number;
68
+ noteH: number;
69
+ collapsed: boolean;
70
+ }[] = [];
71
+
72
+ for (const a of anchors) {
73
+ const note = noteByNode.get(a.id);
74
+ if (!note) continue;
75
+ const size = noteBoxSize(note.body);
76
+ geoms.set(a.id, {
77
+ noteW: size.width,
78
+ noteH: size.height,
79
+ ...(note.color && { color: note.color }),
80
+ lines: size.lines,
81
+ lineNumber: note.lineNumber,
82
+ endLineNumber: note.endLineNumber,
83
+ });
84
+ requests.push({
85
+ key: a.id,
86
+ node: { x: a.x, y: a.y, width: a.width, height: a.height },
87
+ noteW: size.width,
88
+ noteH: size.height,
89
+ collapsed: collapsedNotes?.has(note.lineNumber) ?? false,
90
+ });
91
+ }
92
+
93
+ const placements = placeNotes(
94
+ anchors.map((a) => ({ x: a.x, y: a.y, width: a.width, height: a.height })),
95
+ requests,
96
+ direction
97
+ );
98
+
99
+ for (const [id, g] of geoms) {
100
+ const p = placements.get(id)!;
101
+ placed.set(
102
+ id,
103
+ p.collapsed
104
+ ? {
105
+ x: 0,
106
+ y: 0,
107
+ width: 0,
108
+ height: 0,
109
+ side: 'right',
110
+ ...(g.color && { color: g.color }),
111
+ lines: [],
112
+ lineNumber: g.lineNumber,
113
+ endLineNumber: g.endLineNumber,
114
+ collapsed: true,
115
+ }
116
+ : {
117
+ x: p.x,
118
+ y: p.y,
119
+ width: g.noteW,
120
+ height: g.noteH,
121
+ side: p.side,
122
+ ...(g.color && { color: g.color }),
123
+ lines: g.lines,
124
+ lineNumber: g.lineNumber,
125
+ endLineNumber: g.endLineNumber,
126
+ }
127
+ );
128
+ }
129
+
130
+ return placed;
131
+ }
@@ -0,0 +1,18 @@
1
+ // ============================================================
2
+ // Generic diagram notes — barrel
3
+ // ============================================================
4
+ //
5
+ // One chart-neutral note pipeline: model → parse → resolve → place →
6
+ // shift, plus the shared `note-box` drawer. A node-based chart adopts
7
+ // notes by importing from here and supplying only an anchor list and the
8
+ // per-chart layout wiring. See the cross-chart rollout tech spec.
9
+
10
+ export * from './model';
11
+ export * from './parse';
12
+ export * from './resolve';
13
+ export * from './place';
14
+ export * from './build';
15
+ export * from './bounds';
16
+
17
+ // Re-export the drawer so charts have one import site for the whole feature.
18
+ export * from '../note-box';
@@ -0,0 +1,19 @@
1
+ // ============================================================
2
+ // Generic diagram note — chart-neutral model
3
+ // ============================================================
4
+ //
5
+ // One annotation attachable to any node-based chart. The parser collects
6
+ // raw `{ref, body, color?}`; a shared resolver binds `ref` to a concrete
7
+ // node, and a shared placer + the `utils/note-box` drawer render it. Each
8
+ // chart adds only a note-line handler + an anchor (id/label) list.
9
+
10
+ export interface DiagramNote {
11
+ /** Author-typed node id/label the note attaches to. */
12
+ readonly ref: string;
13
+ /** Body text (inline + indented lines, joined with `\n`). */
14
+ readonly body: string;
15
+ /** Resolved hex accent (border + faded fill); default yellow if absent. */
16
+ readonly color?: string;
17
+ readonly lineNumber: number;
18
+ readonly endLineNumber: number;
19
+ }
@@ -0,0 +1,131 @@
1
+ // ============================================================
2
+ // Generic diagram note — shared parsing
3
+ // ============================================================
4
+
5
+ import { makeDgmoError, type DgmoError } from '../../diagnostics';
6
+ import type { PaletteColors } from '../../palettes';
7
+ import { measureIndent, extractColor } from '../parsing';
8
+ import type { DiagramNote } from './model';
9
+
10
+ /**
11
+ * Split a `note` heading's remainder into a `ref` token and inline body.
12
+ * Quote the ref for multi-word labels:
13
+ * `note Foo a comment` → { ref: 'Foo', inlineBody: 'a comment' }
14
+ * `note "Order Received" done` → { ref: 'Order Received', inlineBody: 'done' }
15
+ */
16
+ export function parseNoteHeader(rest: string): {
17
+ ref: string;
18
+ inlineBody: string;
19
+ } {
20
+ const t = rest.trim();
21
+ const quoted = t.match(/^"([^"]+)"\s*(.*)$/);
22
+ if (quoted) {
23
+ return { ref: quoted[1]!.trim(), inlineBody: quoted[2]!.trim() };
24
+ }
25
+ const m = t.match(/^(\S+)\s*(.*)$/);
26
+ if (m) {
27
+ return { ref: m[1]!, inlineBody: m[2]!.trim() };
28
+ }
29
+ return { ref: t, inlineBody: '' };
30
+ }
31
+
32
+ export interface CollectedNoteBody {
33
+ readonly body: string;
34
+ readonly endLineNumber: number;
35
+ /** 0-based index of the last source line consumed. */
36
+ readonly lastIndex: number;
37
+ }
38
+
39
+ /**
40
+ * Collect a note's multi-line body: following lines indented MORE than the
41
+ * note line. A blank line or a dedent terminates. Lines are trimmed and
42
+ * joined with `\n`; the inline body (if any) leads.
43
+ */
44
+ export function collectNoteBody(
45
+ lines: readonly string[],
46
+ noteIndex: number,
47
+ noteIndent: number,
48
+ inlineBody: string
49
+ ): CollectedNoteBody {
50
+ const parts: string[] = [];
51
+ if (inlineBody) parts.push(inlineBody);
52
+
53
+ let lastIndex = noteIndex;
54
+ let endLineNumber = noteIndex + 1;
55
+
56
+ for (let j = noteIndex + 1; j < lines.length; j++) {
57
+ const raw = lines[j]!;
58
+ const trimmed = raw.trim();
59
+ if (!trimmed) break;
60
+ if (measureIndent(raw) <= noteIndent) break;
61
+ parts.push(trimmed);
62
+ lastIndex = j;
63
+ endLineNumber = j + 1;
64
+ }
65
+
66
+ return { body: parts.join('\n'), endLineNumber, lastIndex };
67
+ }
68
+
69
+ export interface TryCollectNoteResult {
70
+ /** Absent when the body was empty (a warning was pushed, line consumed). */
71
+ readonly note?: DiagramNote;
72
+ /** 0-based index of the last consumed source line. */
73
+ readonly lastIndex: number;
74
+ }
75
+
76
+ /**
77
+ * Try to parse a `note` line at `lines[index]` into a {@link DiagramNote},
78
+ * collecting any indented body and peeling a trailing color word. Returns
79
+ * `null` when the line is not a note (incl. `note -> X`, which a chart with
80
+ * bare-name nodes parses as an edge). On an empty body, emits a warning and
81
+ * returns `{ lastIndex }` with no note (the line is still consumed).
82
+ *
83
+ * Each chart calls this in its main loop:
84
+ * const r = tryCollectNote(lines, i, indent, palette, diagnostics);
85
+ * if (r) { if (r.note) notes.push(r.note); i = r.lastIndex; continue; }
86
+ */
87
+ export function tryCollectNote(
88
+ lines: readonly string[],
89
+ index: number,
90
+ indent: number,
91
+ palette: PaletteColors | undefined,
92
+ diagnostics: DgmoError[]
93
+ ): TryCollectNoteResult | null {
94
+ const trimmed = lines[index]!.trim();
95
+ const m = trimmed.match(/^note\s+(.+)$/i);
96
+ if (!m || /^note\s+->/i.test(trimmed)) return null;
97
+
98
+ const lineNumber = index + 1;
99
+ // Trailing lowercase color word colors the note (§1.5); capitalize to
100
+ // keep it as literal body text.
101
+ const { label: headerNoColor, color } = extractColor(
102
+ m[1]!,
103
+ palette,
104
+ diagnostics,
105
+ lineNumber
106
+ );
107
+ const { ref, inlineBody } = parseNoteHeader(headerNoColor);
108
+ const collected = collectNoteBody(lines, index, indent, inlineBody);
109
+
110
+ if (!collected.body.trim()) {
111
+ diagnostics.push(
112
+ makeDgmoError(
113
+ lineNumber,
114
+ `Note on "${ref}" has no text — ignored.`,
115
+ 'warning'
116
+ )
117
+ );
118
+ return { lastIndex: collected.lastIndex };
119
+ }
120
+
121
+ return {
122
+ note: {
123
+ ref,
124
+ body: collected.body,
125
+ ...(color && { color }),
126
+ lineNumber,
127
+ endLineNumber: collected.endLineNumber,
128
+ },
129
+ lastIndex: collected.lastIndex,
130
+ };
131
+ }
@@ -0,0 +1,177 @@
1
+ // ============================================================
2
+ // Generic diagram note — collision-aware placement
3
+ // ============================================================
4
+ //
5
+ // Floats each note beside its anchor node WITHOUT moving the node, so the
6
+ // shape keeps its layout position and its edge connections. Ported verbatim
7
+ // from the graph layout's "Collision-aware note placement" block so every
8
+ // chart shares one placement policy. The caller supplies positioned node
9
+ // rects (`obstacles`) and per-note requests; this returns a box position
10
+ // LOCAL to each node's center (the renderer's translate origin).
11
+
12
+ import { NOTE_GAP } from '../note-box';
13
+
14
+ export type NoteSide = 'above' | 'below' | 'left' | 'right';
15
+
16
+ /** A positioned rect (center + size) that a note must avoid overlapping. */
17
+ export interface PlaceableNode {
18
+ readonly x: number;
19
+ readonly y: number;
20
+ readonly width: number;
21
+ readonly height: number;
22
+ }
23
+
24
+ export interface NotePlaceRequest {
25
+ /** Stable key (node id) the result map is keyed by. */
26
+ readonly key: string;
27
+ /** The anchor node the note floats beside. */
28
+ readonly node: PlaceableNode;
29
+ readonly noteW: number;
30
+ readonly noteH: number;
31
+ /** Collapsed notes reserve no space — drawn as a corner badge. */
32
+ readonly collapsed: boolean;
33
+ }
34
+
35
+ export interface NotePlacement {
36
+ readonly collapsed: boolean;
37
+ readonly side: NoteSide;
38
+ /** Box left/top, LOCAL to the node center (add node.x / node.y). */
39
+ readonly x: number;
40
+ readonly y: number;
41
+ }
42
+
43
+ /** Clearance kept between a note and any obstacle (node or earlier note). */
44
+ export const NOTE_CLEAR = 14;
45
+
46
+ type Rect = { left: number; top: number; right: number; bottom: number };
47
+
48
+ const intersects = (a: Rect, b: Rect, pad: number): boolean =>
49
+ !(
50
+ a.right + pad <= b.left ||
51
+ b.right + pad <= a.left ||
52
+ a.bottom + pad <= b.top ||
53
+ b.bottom + pad <= a.top
54
+ );
55
+
56
+ /**
57
+ * Place each note beside its node. Try the default side (right for TB,
58
+ * below for LR); if it would overlap an obstacle flip to the opposite
59
+ * side; if both collide, push the default side outward past the blockers.
60
+ * Each placed note then becomes an obstacle for later notes, so notes keep
61
+ * a comfortable distance from every shape and from each other.
62
+ *
63
+ * Returns a map keyed by request `key`. The `x/y` are the box left/top
64
+ * relative to the node center; collapsed requests return `{collapsed:true,
65
+ * side:'right', x:0, y:0}`.
66
+ */
67
+ export function placeNotes(
68
+ obstacles: readonly PlaceableNode[],
69
+ requests: readonly NotePlaceRequest[],
70
+ direction: 'TB' | 'LR'
71
+ ): Map<string, NotePlacement> {
72
+ const placements = new Map<string, NotePlacement>();
73
+
74
+ const occupied: Rect[] = obstacles.map((p) => ({
75
+ left: p.x - p.width / 2,
76
+ top: p.y - p.height / 2,
77
+ right: p.x + p.width / 2,
78
+ bottom: p.y + p.height / 2,
79
+ }));
80
+
81
+ for (const req of requests) {
82
+ if (req.collapsed) {
83
+ placements.set(req.key, {
84
+ collapsed: true,
85
+ side: 'right',
86
+ x: 0,
87
+ y: 0,
88
+ });
89
+ continue;
90
+ }
91
+
92
+ const p = req.node;
93
+ const cx = p.x;
94
+ const cy = p.y;
95
+ const nodeLeft = cx - p.width / 2;
96
+ const nodeRight = cx + p.width / 2;
97
+ const nodeTop = cy - p.height / 2;
98
+ const nodeBottom = cy + p.height / 2;
99
+ const { noteW, noteH } = req;
100
+
101
+ const rectFor = (side: NoteSide): Rect => {
102
+ switch (side) {
103
+ case 'right': {
104
+ const left = nodeRight + NOTE_GAP;
105
+ const top = cy - noteH / 2;
106
+ return { left, top, right: left + noteW, bottom: top + noteH };
107
+ }
108
+ case 'left': {
109
+ const right = nodeLeft - NOTE_GAP;
110
+ const top = cy - noteH / 2;
111
+ return { left: right - noteW, top, right, bottom: top + noteH };
112
+ }
113
+ case 'below': {
114
+ const left = cx - noteW / 2;
115
+ const top = nodeBottom + NOTE_GAP;
116
+ return { left, top, right: left + noteW, bottom: top + noteH };
117
+ }
118
+ case 'above':
119
+ default: {
120
+ const left = cx - noteW / 2;
121
+ const bottom = nodeTop - NOTE_GAP;
122
+ return { left, top: bottom - noteH, right: left + noteW, bottom };
123
+ }
124
+ }
125
+ };
126
+
127
+ const order: NoteSide[] =
128
+ direction === 'LR' ? ['below', 'above'] : ['right', 'left'];
129
+
130
+ let chosen: { rect: Rect; side: NoteSide } | null = null;
131
+ for (const side of order) {
132
+ const rect = rectFor(side);
133
+ if (!occupied.some((o) => intersects(rect, o, NOTE_CLEAR))) {
134
+ chosen = { rect, side };
135
+ break;
136
+ }
137
+ }
138
+
139
+ if (!chosen) {
140
+ // Both sides blocked — push the default side outward past blockers.
141
+ const side = order[0]!;
142
+ let rect = rectFor(side);
143
+ const axisIsY = side === 'above' || side === 'below';
144
+ const outward = side === 'below' || side === 'right' ? 1 : -1;
145
+ for (let guard = 0; guard < 50; guard++) {
146
+ const blockers = occupied.filter((o) =>
147
+ intersects(rect, o, NOTE_CLEAR)
148
+ );
149
+ if (blockers.length === 0) break;
150
+ if (axisIsY && outward > 0) {
151
+ const top = Math.max(...blockers.map((b) => b.bottom)) + NOTE_CLEAR;
152
+ rect = { ...rect, top, bottom: top + noteH };
153
+ } else if (axisIsY) {
154
+ const bottom = Math.min(...blockers.map((b) => b.top)) - NOTE_CLEAR;
155
+ rect = { ...rect, bottom, top: bottom - noteH };
156
+ } else if (outward > 0) {
157
+ const left = Math.max(...blockers.map((b) => b.right)) + NOTE_CLEAR;
158
+ rect = { ...rect, left, right: left + noteW };
159
+ } else {
160
+ const right = Math.min(...blockers.map((b) => b.left)) - NOTE_CLEAR;
161
+ rect = { ...rect, right, left: right - noteW };
162
+ }
163
+ }
164
+ chosen = { rect, side };
165
+ }
166
+
167
+ occupied.push(chosen.rect);
168
+ placements.set(req.key, {
169
+ collapsed: false,
170
+ side: chosen.side,
171
+ x: chosen.rect.left - p.x,
172
+ y: chosen.rect.top - p.y,
173
+ });
174
+ }
175
+
176
+ return placements;
177
+ }
@@ -0,0 +1,88 @@
1
+ // ============================================================
2
+ // Generic diagram note — shared resolver
3
+ // ============================================================
4
+
5
+ import { makeDgmoError, suggest, type DgmoError } from '../../diagnostics';
6
+ import { normalizeName } from '../name-normalize';
7
+ import type { DiagramNote } from './model';
8
+
9
+ /** Minimal node shape a chart must expose for note resolution. */
10
+ export interface NoteTarget {
11
+ readonly id: string;
12
+ readonly label: string;
13
+ }
14
+
15
+ /**
16
+ * Resolve each note's `ref` to a concrete node id (matched by normalized
17
+ * label). Returns a map keyed by node id — one note per node, first wins.
18
+ * When `diagnostics` is passed, emits an `error` for an unknown ref (with a
19
+ * suggest), a `warning` when a ref is ambiguous across nodes, and a
20
+ * `warning` when a node already has a note. Never silently drops.
21
+ */
22
+ export function resolveNotes<T extends NoteTarget>(
23
+ notes: readonly DiagramNote[],
24
+ targets: readonly T[],
25
+ diagnostics?: DgmoError[]
26
+ ): Map<string, DiagramNote> {
27
+ const byNodeId = new Map<string, DiagramNote>();
28
+ if (notes.length === 0) return byNodeId;
29
+
30
+ const byNormLabel = new Map<string, T[]>();
31
+ for (const t of targets) {
32
+ const key = normalizeName(t.label);
33
+ const arr = byNormLabel.get(key);
34
+ if (arr) arr.push(t);
35
+ else byNormLabel.set(key, [t]);
36
+ }
37
+
38
+ for (const note of notes) {
39
+ const matches = byNormLabel.get(normalizeName(note.ref));
40
+
41
+ if (!matches || matches.length === 0) {
42
+ if (diagnostics) {
43
+ const hint = suggest(
44
+ note.ref,
45
+ targets.map((t) => t.label)
46
+ );
47
+ diagnostics.push(
48
+ makeDgmoError(
49
+ note.lineNumber,
50
+ `Note references unknown node id "${note.ref}".${
51
+ hint ? ' ' + hint : ''
52
+ }`,
53
+ 'error'
54
+ )
55
+ );
56
+ }
57
+ continue;
58
+ }
59
+
60
+ const target = matches[0]!;
61
+ if (matches.length > 1 && diagnostics) {
62
+ diagnostics.push(
63
+ makeDgmoError(
64
+ note.lineNumber,
65
+ `Note ref "${note.ref}" matches ${matches.length} nodes; attaching to the first.`,
66
+ 'warning'
67
+ )
68
+ );
69
+ }
70
+
71
+ if (byNodeId.has(target.id)) {
72
+ if (diagnostics) {
73
+ diagnostics.push(
74
+ makeDgmoError(
75
+ note.lineNumber,
76
+ `Multiple notes on node "${target.label}"; keeping the first.`,
77
+ 'warning'
78
+ )
79
+ );
80
+ }
81
+ continue;
82
+ }
83
+
84
+ byNodeId.set(target.id, note);
85
+ }
86
+
87
+ return byNodeId;
88
+ }
@@ -0,0 +1,36 @@
1
+ // Shared compact number formatting. Used by the map region-value labels and the
2
+ // gradient legend's ramp ends so the two read identically (a region printing
3
+ // `39.5M` lines up with a legend that ends at `40M`).
4
+
5
+ /** Compact display of a numeric value:
6
+ * - integers and |n| < 1000 print bare (non-integers to 1 decimal, matching the
7
+ * legend's legacy ramp formatting): `0`, `3.2`, `999`, `42`.
8
+ * - |n| >= 1000 uses magnitude suffixes to 1 significant fraction digit:
9
+ * `1.1K`, `39.5M`, `2.3B`, `1.4T`.
10
+ * Negatives keep their sign (`-39.5M`). */
11
+ export function compactNumber(n: number): string {
12
+ if (!Number.isFinite(n)) return String(n);
13
+ const abs = Math.abs(n);
14
+ if (abs < 1000) {
15
+ return Number.isInteger(n) ? String(n) : String(Math.round(n * 10) / 10);
16
+ }
17
+ const sign = n < 0 ? '-' : '';
18
+ const units: ReadonlyArray<[number, string]> = [
19
+ [1e12, 'T'],
20
+ [1e9, 'B'],
21
+ [1e6, 'M'],
22
+ [1e3, 'K'],
23
+ ];
24
+ for (const [factor, suffix] of units) {
25
+ if (abs >= factor) {
26
+ // 1 fraction digit, but drop a trailing `.0` (39.0M → 39M).
27
+ const scaled = Math.round((abs / factor) * 10) / 10;
28
+ const body = Number.isInteger(scaled)
29
+ ? String(scaled)
30
+ : scaled.toFixed(1);
31
+ return `${sign}${body}${suffix}`;
32
+ }
33
+ }
34
+ // Unreachable (abs >= 1000 always matches the 1e3 unit), but keep TS happy.
35
+ return String(n);
36
+ }
@@ -146,6 +146,7 @@ export const OPTION_NOCOLON_RE = /^([a-z][a-z0-9-]*)\s+(.+)$/i;
146
146
  export const GLOBAL_BOOLEANS: ReadonlySet<string> = new Set([
147
147
  'solid-fill',
148
148
  'no-title',
149
+ 'no-notes',
149
150
  ]);
150
151
 
151
152
  /**
@@ -511,6 +512,46 @@ export function peelTrailingColorName(label: string): {
511
512
  };
512
513
  }
513
514
 
515
+ /**
516
+ * Peel up to TWO trailing recognized color names from a label region — the
517
+ * shared value-ramp coloring convention (`<metric> <low?> <high?>`). Peels from
518
+ * the right, stops at the first non-color token, peels AT MOST 2, and NEVER
519
+ * reduces the label to empty (a 2nd peel that would empty the label is left as
520
+ * label text).
521
+ *
522
+ * Mapping (locked): one peeled color ⇒ `{ high }` only (today's neutral→hue
523
+ * meaning); two peeled ⇒ first(left) = `low`, second(right) = `high`.
524
+ * Order-respecting — NO sorting, NO "did-you-mean" intent detection.
525
+ *
526
+ * Shared by any value-ramp chart type (map `region-metric`, b&l `box-metric`,
527
+ * and future ramps). `peelTrailingColorName` (single-token) stays untouched for
528
+ * the non-ramp callers (d3, sitemap, cycle, …).
529
+ */
530
+ export function peelRampColors(label: string): {
531
+ label: string;
532
+ low?: string;
533
+ high?: string;
534
+ } {
535
+ // Peel a single trailing recognized color, but only if a token remains in
536
+ // front of it (a lone token is never peeled — that would empty the label).
537
+ const peelOne = (s: string): { rest: string; color?: string } => {
538
+ const idx = Math.max(s.lastIndexOf(' '), s.lastIndexOf('\t'));
539
+ if (idx < 0) return { rest: s };
540
+ const trailing = s.substring(idx + 1);
541
+ if (!RECOGNIZED_COLOR_SET.has(trailing)) return { rest: s };
542
+ return { rest: s.substring(0, idx).trimEnd(), color: trailing };
543
+ };
544
+ // High (rightmost) first.
545
+ const first = peelOne(label);
546
+ if (first.color === undefined) return { label };
547
+ // Low (second-from-right) — only if a non-empty label still remains after.
548
+ const second = peelOne(first.rest);
549
+ if (second.color === undefined) {
550
+ return { label: first.rest, high: first.color };
551
+ }
552
+ return { label: second.rest, low: second.color, high: first.color };
553
+ }
554
+
514
555
  /** Error message for multiple pipes on a single line. */
515
556
  export const MULTIPLE_PIPE_ERROR =
516
557
  'Use a single "|" to start metadata, then separate items with commas.';
@@ -63,6 +63,10 @@ export const SEQUENCE_REGISTRY: ReservedKeyRegistry = staticRegistry([
63
63
  'description',
64
64
  'role',
65
65
  'collapsed',
66
+ // Participant layout-order override (§2.2). Colon-keyed `position: N`
67
+ // replaced the legacy bare-keyword `position N` form — a stray bare
68
+ // `position N` now raises E_SEQUENCE_BARE_POSITION_REMOVED.
69
+ 'position',
66
70
  ]);
67
71
 
68
72
  export const INFRA_REGISTRY: ReservedKeyRegistry = staticRegistry([