@standarx/graph 0.0.0-stage → 1.0.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 (127) hide show
  1. package/LICENSE +21 -0
  2. package/README.fr.md +436 -0
  3. package/README.md +435 -2
  4. package/THIRD_PARTY_NOTICES.md +22 -0
  5. package/assets/fonts/Inter-Regular.ttf +0 -0
  6. package/assets/fonts/Inter-SemiBold.ttf +0 -0
  7. package/assets/fonts/OFL.txt +92 -0
  8. package/dist/adapter-0bggwh8x.js +393 -0
  9. package/dist/cli-4m00785g.js +20 -0
  10. package/dist/cli-5v9761s9.js +330 -0
  11. package/dist/cli-9f1pdw5q.js +462 -0
  12. package/dist/cli-a4ax1vkc.js +172 -0
  13. package/dist/cli-b4n941t8.js +1439 -0
  14. package/dist/cli-eqq7a3wz.js +1144 -0
  15. package/dist/cli-gh5166k6.js +1565 -0
  16. package/dist/cli-kzp16pt5.js +207 -0
  17. package/dist/cli-mzmjkk5v.js +11 -0
  18. package/dist/cli-njgyf690.js +1060 -0
  19. package/dist/cli-nkm2j980.js +141 -0
  20. package/dist/cli-nyckgf1t.js +4 -0
  21. package/dist/cli-sfdqkbd1.js +433 -0
  22. package/dist/cli-txrzzr2q.js +0 -0
  23. package/dist/cli-vecew2na.js +709 -0
  24. package/dist/cli-wt9dmbhm.js +560 -0
  25. package/dist/cli-xhdxp3cj.js +387 -0
  26. package/dist/cli-ywgtv1cg.js +39 -0
  27. package/dist/cli.d.ts +2 -0
  28. package/dist/cli.js +415 -0
  29. package/dist/core/index.d.ts +6 -0
  30. package/dist/core/index.js +93 -0
  31. package/dist/core/spatial.d.ts +16 -0
  32. package/dist/core/threshold.d.ts +1 -0
  33. package/dist/core/view.d.ts +80 -0
  34. package/dist/export/index.d.ts +18 -0
  35. package/dist/export/index.js +172 -0
  36. package/dist/export/pdf.d.ts +5 -0
  37. package/dist/export/png.d.ts +4 -0
  38. package/dist/format/index.d.ts +4 -0
  39. package/dist/format/index.js +71 -0
  40. package/dist/format/json-schema.d.ts +2 -0
  41. package/dist/format/schema.d.ts +787 -0
  42. package/dist/format/semantic/er.d.ts +3 -0
  43. package/dist/format/semantic/graph.d.ts +3 -0
  44. package/dist/format/semantic/sequence.d.ts +3 -0
  45. package/dist/format/semantic/shared.d.ts +14 -0
  46. package/dist/format/semantic/state.d.ts +3 -0
  47. package/dist/format/semantic/tree.d.ts +3 -0
  48. package/dist/format/transition-label.d.ts +2 -0
  49. package/dist/format/validate.d.ts +11 -0
  50. package/dist/html/index.d.ts +3 -0
  51. package/dist/html/index.js +127 -0
  52. package/dist/html/layout-worker.d.ts +1 -0
  53. package/dist/html/layout-worker.generated.d.ts +1 -0
  54. package/dist/html/mount.d.ts +12 -0
  55. package/dist/html/page.d.ts +14 -0
  56. package/dist/html/render.d.ts +7 -0
  57. package/dist/html/runtime.d.ts +1 -0
  58. package/dist/html/runtime.generated.d.ts +1 -0
  59. package/dist/html/shell.d.ts +10 -0
  60. package/dist/html/svg-adapter.d.ts +8 -0
  61. package/dist/html/ui.d.ts +34 -0
  62. package/dist/language/analysis.d.ts +38 -0
  63. package/dist/language/complete.d.ts +3 -0
  64. package/dist/language/docs.d.ts +11 -0
  65. package/dist/language/format.d.ts +17 -0
  66. package/dist/language/hover.d.ts +3 -0
  67. package/dist/language/index.d.ts +6 -0
  68. package/dist/language/index.js +1385 -0
  69. package/dist/language/navigation.d.ts +9 -0
  70. package/dist/language/positions.d.ts +7 -0
  71. package/dist/language/symbols.d.ts +3 -0
  72. package/dist/language/types.d.ts +34 -0
  73. package/dist/layout/elk.d.ts +35 -0
  74. package/dist/layout/er.d.ts +3 -0
  75. package/dist/layout/font-metrics.d.ts +9 -0
  76. package/dist/layout/graph.d.ts +3 -0
  77. package/dist/layout/index.d.ts +5 -0
  78. package/dist/layout/index.js +17 -0
  79. package/dist/layout/measure.d.ts +10 -0
  80. package/dist/layout/sequence.d.ts +3 -0
  81. package/dist/layout/shapes.d.ts +16 -0
  82. package/dist/layout/shared.d.ts +31 -0
  83. package/dist/layout/state.d.ts +3 -0
  84. package/dist/layout/tree.d.ts +3 -0
  85. package/dist/layout/types.d.ts +198 -0
  86. package/dist/layout-worker.generated-emmvgfb1.js +34 -0
  87. package/dist/mermaid/common.d.ts +36 -0
  88. package/dist/mermaid/er.d.ts +3 -0
  89. package/dist/mermaid/graph.d.ts +4 -0
  90. package/dist/mermaid/index.d.ts +6 -0
  91. package/dist/mermaid/index.js +6 -0
  92. package/dist/mermaid/mindmap.d.ts +3 -0
  93. package/dist/mermaid/sequence.d.ts +3 -0
  94. package/dist/mermaid/state.d.ts +3 -0
  95. package/dist/remark/index.d.ts +9 -0
  96. package/dist/remark/index.js +104 -0
  97. package/dist/shared/errors.d.ts +6 -0
  98. package/dist/shared/package-root.d.ts +5 -0
  99. package/dist/svg/escape.d.ts +1 -0
  100. package/dist/svg/index.d.ts +3 -0
  101. package/dist/svg/index.js +25 -0
  102. package/dist/svg/render.d.ts +9 -0
  103. package/dist/svg/theme.d.ts +62 -0
  104. package/dist/svg-adapter-gq5vmp6q.js +227 -0
  105. package/dist/text/build.d.ts +10 -0
  106. package/dist/text/canonical.d.ts +10 -0
  107. package/dist/text/errors.d.ts +33 -0
  108. package/dist/text/highlight.d.ts +13 -0
  109. package/dist/text/index.d.ts +33 -0
  110. package/dist/text/index.js +27 -0
  111. package/dist/text/lexer.d.ts +29 -0
  112. package/dist/text/parser.d.ts +160 -0
  113. package/dist/text/print.d.ts +6 -0
  114. package/dist/text/slug.d.ts +1 -0
  115. package/dist/text/syntax.d.ts +8 -0
  116. package/dist/version.d.ts +1 -0
  117. package/dist/webgl/adapter.d.ts +4 -0
  118. package/dist/webgl/index.d.ts +7 -0
  119. package/dist/webgl/index.js +15 -0
  120. package/dist/webgl/runtime.d.ts +1 -0
  121. package/dist/webgl/runtime.generated.d.ts +1 -0
  122. package/grammars/standargraph.tmLanguage.json +599 -0
  123. package/language-configuration.json +35 -0
  124. package/licenses/EPL-2.0.txt +277 -0
  125. package/licenses/MPL-2.0.txt +373 -0
  126. package/package.json +133 -4
  127. package/schema/graph-document.v1.json +1737 -0
@@ -0,0 +1,560 @@
1
+ import {
2
+ FONT_METRICS,
3
+ LINE_HEIGHT,
4
+ measureText2,
5
+ LABEL_FONT,
6
+ CAPTION_FONT,
7
+ EDGE_LABEL_FONT,
8
+ GROUP_FONT,
9
+ PAD_X,
10
+ SELF_LOOP_WIDTH,
11
+ SELF_LOOP_HEIGHT,
12
+ round,
13
+ boxSize,
14
+ ACTIONS_GAP,
15
+ keysText,
16
+ attributeTypeText,
17
+ CYLINDER_RY,
18
+ DOCUMENT_WAVE,
19
+ SUBROUTINE_BAR,
20
+ textOffsetY,
21
+ outline
22
+ } from "./cli-gh5166k6.js";
23
+ import {
24
+ esc,
25
+ resolveTheme2,
26
+ withPalette,
27
+ typesPresent,
28
+ escapedTheme,
29
+ themeVariables2,
30
+ typeRules
31
+ } from "./cli-5v9761s9.js";
32
+
33
+ // src/svg/render.ts
34
+ function typeStyleOf(types, type) {
35
+ return type !== undefined && Object.hasOwn(types, type) ? types[type] : {};
36
+ }
37
+ var DASH = { inferred: "6 4", unknown: "2 3", reply: "5 3" };
38
+ var UNSAFE_HREF = /^[\s\u0000-\u001f]*(javascript|data|vbscript):/i;
39
+ function n(value) {
40
+ return String(round(value));
41
+ }
42
+ function baseline(top, size, weight) {
43
+ const metrics = FONT_METRICS[weight];
44
+ const ascent = metrics.ascender / metrics.unitsPerEm * size;
45
+ const descent = -metrics.descender / metrics.unitsPerEm * size;
46
+ return top + (LINE_HEIGHT * size - (ascent + descent)) / 2 + ascent;
47
+ }
48
+ function textLines(text, x, top, style, cls, fill, anchor = "middle") {
49
+ return text.split(`
50
+ `).map((line, index) => {
51
+ const y = baseline(top + index * LINE_HEIGHT * style.size, style.size, style.weight);
52
+ const weight = style.weight === "semibold" ? ' font-weight="600"' : "";
53
+ return `<text class="${cls}" x="${n(x)}" y="${n(y)}" text-anchor="${anchor}" font-size="${style.size}"${weight} fill="${fill}">${esc(line)}</text>`;
54
+ }).join("");
55
+ }
56
+ function metaTitle(meta) {
57
+ if (meta === undefined)
58
+ return "";
59
+ const lines = [];
60
+ const provenance = meta.provenance;
61
+ if (provenance !== undefined) {
62
+ const confidence = provenance.confidence === undefined ? "" : ` (${provenance.confidence})`;
63
+ lines.push(`Source: ${provenance.source}${confidence}`);
64
+ if (provenance.tool !== undefined)
65
+ lines.push(`Tool: ${provenance.tool}`);
66
+ }
67
+ if (meta.coverage !== undefined) {
68
+ lines.push(`Coverage: ${round(meta.coverage * 100)}%`);
69
+ }
70
+ if (meta.freshness !== undefined)
71
+ lines.push(`Freshness: ${meta.freshness}`);
72
+ if (lines.length === 0)
73
+ return "";
74
+ return `<title>${esc(lines.join(`
75
+ `))}</title>`;
76
+ }
77
+ function confidenceOf(meta) {
78
+ return meta?.provenance?.confidence;
79
+ }
80
+ function dashAttr(dash) {
81
+ return dash === undefined ? "" : ` stroke-dasharray="${dash}"`;
82
+ }
83
+ function confidenceDash(meta) {
84
+ const confidence = confidenceOf(meta);
85
+ if (confidence === "inferred")
86
+ return DASH.inferred;
87
+ if (confidence === "unknown")
88
+ return DASH.unknown;
89
+ return;
90
+ }
91
+ function css(tokens, types) {
92
+ return [
93
+ `.sg-root{${themeVariables2({ tokens })}}`,
94
+ ".sg-bg{fill:var(--sg-bg)}",
95
+ ".sg-text{fill:var(--sg-fg)}",
96
+ ".sg-muted{fill:var(--sg-muted)}",
97
+ ".sg-node{fill:var(--sg-node-fill);stroke:var(--sg-node-stroke)}",
98
+ ".sg-node.sg-unknown{fill:var(--sg-unknown-fill)}",
99
+ ".sg-node-detail{fill:none;stroke:var(--sg-node-stroke)}",
100
+ ".sg-divider{stroke:var(--sg-node-stroke)}",
101
+ ".sg-header{fill:var(--sg-header-fill)}",
102
+ ".sg-pseudo{fill:var(--sg-fg)}",
103
+ ".sg-group{fill:var(--sg-group-fill);stroke:var(--sg-group-stroke)}",
104
+ ".sg-edge{stroke:var(--sg-edge)}",
105
+ ".sg-arrow-filled{fill:var(--sg-edge);stroke:var(--sg-edge)}",
106
+ ".sg-arrow-open{stroke:var(--sg-edge)}",
107
+ ".sg-label-bg{fill:var(--sg-label-bg)}",
108
+ ".sg-accent{fill:var(--sg-accent)}",
109
+ ".sg-lifeline{stroke:var(--sg-lifeline)}",
110
+ typeRules(types, ".sg-root")
111
+ ].join("");
112
+ }
113
+ function defs(theme, resolved, styles) {
114
+ return [
115
+ "<defs>",
116
+ styles ? `<style>${css(resolved.tokens, resolved.types)}</style>` : "",
117
+ `<marker id="sg-arrow-filled" viewBox="0 0 10 10" refX="10" refY="5" markerUnits="userSpaceOnUse" markerWidth="10" markerHeight="10" orient="auto"><path class="sg-arrow-filled" d="M0,0 L10,5 L0,10 z" fill="${theme.edge}" stroke="${theme.edge}" stroke-width="1" stroke-linejoin="round"/></marker>`,
118
+ `<marker id="sg-arrow-open" viewBox="0 0 10 10" refX="9" refY="5" markerUnits="userSpaceOnUse" markerWidth="10" markerHeight="10" orient="auto"><path class="sg-arrow-open" d="M1,1 L9,5 L1,9" fill="none" stroke="${theme.edge}" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/></marker>`,
119
+ "</defs>"
120
+ ].join("");
121
+ }
122
+ function wrapLink(content, link) {
123
+ if (link === undefined || link === "" || UNSAFE_HREF.test(link)) {
124
+ return content;
125
+ }
126
+ return `<a href="${esc(link)}">${content}</a>`;
127
+ }
128
+ function data(entries) {
129
+ return entries.filter((entry) => entry[1] !== undefined).map(([name, value]) => ` data-${name}="${esc(String(value))}"`).join("");
130
+ }
131
+ function group(kind, id, meta, body, attributes = "") {
132
+ return `<g data-kind="${kind}" data-id="${esc(id)}"${attributes}>${metaTitle(meta)}${body}</g>`;
133
+ }
134
+ function labelBoxSvg(box, text, theme, withBackground) {
135
+ const lines = text.split(`
136
+ `).length;
137
+ const textHeight = lines * LINE_HEIGHT * EDGE_LABEL_FONT.size;
138
+ const top = box.y + (box.height - textHeight) / 2;
139
+ const background = withBackground ? `<rect class="sg-label-bg sg-edge-label" x="${n(box.x)}" y="${n(box.y)}" width="${n(box.width)}" height="${n(box.height)}" rx="3" fill="${theme.labelBg}"/>` : "";
140
+ return background + textLines(text, box.x + box.width / 2, top, EDGE_LABEL_FONT, "sg-text sg-edge-label", theme.fg);
141
+ }
142
+ function renderGroup(item, theme) {
143
+ const dash = confidenceDash(item.meta);
144
+ const rect = `<rect class="sg-group" x="${n(item.x)}" y="${n(item.y)}" width="${n(item.width)}" height="${n(item.height)}" rx="10" fill="${theme.groupFill}" stroke="${theme.groupStroke}" stroke-width="1"${dashAttr(dash)}/>`;
145
+ const label = item.label === undefined || item.label === "" ? "" : textLines(item.label, item.x + PAD_X, item.y + 8, GROUP_FONT, "sg-muted sg-label", theme.muted, "start");
146
+ return group("group", item.id, item.meta, rect + label, data([
147
+ ["confidence", confidenceOf(item.meta)],
148
+ ["depth", item.depth]
149
+ ]));
150
+ }
151
+ function pathOf(points) {
152
+ return points.map((point, index) => `${index === 0 ? "M" : "L"}${n(point.x)},${n(point.y)}`).join(" ");
153
+ }
154
+ function renderEdge(item, theme) {
155
+ const dash = confidenceDash(item.meta);
156
+ const marker = item.directed ? ' marker-end="url(#sg-arrow-filled)"' : "";
157
+ const path = `<path class="sg-edge" d="${pathOf(item.points)}" fill="none" stroke="${theme.edge}" stroke-width="1.5" stroke-linejoin="round"${dashAttr(dash)}${marker}/>`;
158
+ const label = item.labelBox === undefined || item.label === undefined ? "" : labelBoxSvg(item.labelBox, item.label, theme, true);
159
+ return group("edge", item.id, item.meta, path + label, data([
160
+ ["type", item.type],
161
+ ["confidence", confidenceOf(item.meta)],
162
+ ["from", item.from],
163
+ ["to", item.to],
164
+ ["directed", item.directed ? undefined : "false"]
165
+ ]));
166
+ }
167
+ function shapeGeometry(shape, box) {
168
+ const { x, y, width: w, height: h } = box;
169
+ const rect = (rx) => ({
170
+ tag: "rect",
171
+ attrs: ` x="${n(x)}" y="${n(y)}" width="${n(w)}" height="${n(h)}" rx="${n(rx)}"`
172
+ });
173
+ const polygon = () => ({
174
+ tag: "polygon",
175
+ attrs: ` points="${outline(shape, box).map((point) => `${n(point.x)},${n(point.y)}`).join(" ")}"`
176
+ });
177
+ switch (shape) {
178
+ case "box":
179
+ return rect(8);
180
+ case "rect":
181
+ case "subroutine":
182
+ return rect(0);
183
+ case "pill":
184
+ return rect(h / 2);
185
+ case "diamond":
186
+ case "hexagon":
187
+ case "parallelogram":
188
+ return polygon();
189
+ case "circle":
190
+ return {
191
+ tag: "circle",
192
+ attrs: ` cx="${n(x + w / 2)}" cy="${n(y + h / 2)}" r="${n(w / 2)}"`
193
+ };
194
+ case "ellipse":
195
+ return {
196
+ tag: "ellipse",
197
+ attrs: ` cx="${n(x + w / 2)}" cy="${n(y + h / 2)}" rx="${n(w / 2)}" ry="${n(h / 2)}"`
198
+ };
199
+ case "cylinder": {
200
+ const arc = `A${n(w / 2)},${CYLINDER_RY} 0 0 1`;
201
+ return {
202
+ tag: "path",
203
+ attrs: ` d="M${n(x)},${n(y + CYLINDER_RY)} ${arc} ${n(x + w)},${n(y + CYLINDER_RY)} V${n(y + h - CYLINDER_RY)} ${arc} ${n(x)},${n(y + h - CYLINDER_RY)} Z"`
204
+ };
205
+ }
206
+ case "document": {
207
+ const a = DOCUMENT_WAVE;
208
+ return {
209
+ tag: "path",
210
+ attrs: ` d="M${n(x)},${n(y)} H${n(x + w)} V${n(y + h - a)} C${n(x + 0.75 * w)},${n(y + h - 3 * a)} ${n(x + 0.25 * w)},${n(y + h + a)} ${n(x)},${n(y + h - a)} Z"`
211
+ };
212
+ }
213
+ }
214
+ }
215
+ function shapeDetails(shape, box, theme) {
216
+ const { x, y, width: w, height: h } = box;
217
+ const style = `fill="none" stroke="${theme.nodeStroke}" stroke-width="1.5"`;
218
+ if (shape === "cylinder") {
219
+ return `<path class="sg-node-detail" d="M${n(x)},${n(y + CYLINDER_RY)} A${n(w / 2)},${CYLINDER_RY} 0 0 0 ${n(x + w)},${n(y + CYLINDER_RY)}" ${style}/>`;
220
+ }
221
+ if (shape === "subroutine") {
222
+ return [x + SUBROUTINE_BAR, x + w - SUBROUTINE_BAR].map((lineX) => `<line class="sg-node-detail" x1="${n(lineX)}" y1="${n(y)}" x2="${n(lineX)}" y2="${n(y + h)}" ${style}/>`).join("");
223
+ }
224
+ return "";
225
+ }
226
+ function renderNode(item, index, theme, types, extra = []) {
227
+ const confidence = confidenceOf(item.meta);
228
+ const dash = confidenceDash(item.meta);
229
+ const unknown = confidence === "unknown";
230
+ const typed = typeStyleOf(types, item.type);
231
+ const fill = unknown ? theme.unknownFill : typed.fill ?? theme.nodeFill;
232
+ const stroke = typed.stroke ?? theme.nodeStroke;
233
+ const cls = unknown ? "sg-node sg-unknown" : "sg-node";
234
+ const shape = item.shape ?? "box";
235
+ const geometry = shapeGeometry(shape, item);
236
+ let body = `<${geometry.tag} class="${cls}"${geometry.attrs} fill="${fill}" stroke="${stroke}" stroke-width="1.5"${dashAttr(dash)}/>${shapeDetails(shape, item, theme)}`;
237
+ const label = measureText2(item.label, LABEL_FONT);
238
+ const caption = item.type === undefined ? undefined : measureText2(item.type, CAPTION_FONT);
239
+ const block = label.height + (caption?.height ?? 0);
240
+ const top = item.y + (item.height - block) / 2 + textOffsetY(shape);
241
+ const cx = item.x + item.width / 2;
242
+ body += textLines(item.label, cx, top, LABEL_FONT, "sg-text sg-label", typed.text ?? theme.fg);
243
+ if (item.type !== undefined) {
244
+ body += textLines(item.type, cx, top + label.height, CAPTION_FONT, "sg-muted sg-caption", theme.muted);
245
+ }
246
+ const coverage = item.meta?.coverage;
247
+ if (coverage !== undefined) {
248
+ const clip = `sg-clip-${index}`;
249
+ body = `<clipPath id="${clip}"><${geometry.tag}${geometry.attrs}/></clipPath>` + body + `<rect class="sg-accent" x="${n(item.x)}" y="${n(item.y + item.height - 4)}" width="${n(coverage * item.width)}" height="4" fill="${theme.accent}" clip-path="url(#${clip})"/>`;
250
+ }
251
+ return wrapLink(group("node", item.id, item.meta, body, data([
252
+ ["type", item.type],
253
+ ["confidence", confidence],
254
+ ["shape", shape],
255
+ ...extra
256
+ ])), item.link);
257
+ }
258
+ function renderGraph(laid, theme, types) {
259
+ return [
260
+ ...laid.groups.map((item) => renderGroup(item, theme)),
261
+ ...laid.edges.map((item) => renderEdge(item, theme)),
262
+ ...laid.nodes.map((item, index) => renderNode(item, index, theme, types))
263
+ ].join("");
264
+ }
265
+ function renderParticipant(item, lifelineBottom, theme, types) {
266
+ const lifeline = `<line class="sg-lifeline" x1="${n(item.lifelineX)}" y1="${n(item.y + item.height)}" x2="${n(item.lifelineX)}" y2="${n(lifelineBottom)}" stroke="${theme.lifeline}" stroke-width="1" stroke-dasharray="4 4"/>`;
267
+ const confidence = confidenceOf(item.meta);
268
+ const unknown = confidence === "unknown";
269
+ const typed = typeStyleOf(types, item.type);
270
+ const fill = unknown ? theme.unknownFill : typed.fill ?? theme.nodeFill;
271
+ const cls = unknown ? "sg-node sg-unknown" : "sg-node";
272
+ let body = `<rect class="${cls}" x="${n(item.x)}" y="${n(item.y)}" width="${n(item.width)}" height="${n(item.height)}" rx="8" fill="${fill}" stroke="${typed.stroke ?? theme.nodeStroke}" stroke-width="1.5"${dashAttr(confidenceDash(item.meta))}/>`;
273
+ const label = measureText2(item.label, LABEL_FONT);
274
+ const caption = item.type === undefined ? undefined : measureText2(item.type, CAPTION_FONT);
275
+ const top = item.y + (item.height - label.height - (caption?.height ?? 0)) / 2;
276
+ const cx = item.x + item.width / 2;
277
+ body += textLines(item.label, cx, top, LABEL_FONT, "sg-text sg-label", typed.text ?? theme.fg);
278
+ if (item.type !== undefined) {
279
+ body += textLines(item.type, cx, top + label.height, CAPTION_FONT, "sg-muted sg-caption", theme.muted);
280
+ }
281
+ return group("participant", item.id, item.meta, lifeline + body, data([
282
+ ["type", item.type],
283
+ ["confidence", confidence]
284
+ ]));
285
+ }
286
+ function renderMessage(item, theme) {
287
+ const dash = confidenceDash(item.meta) ?? (item.style === "reply" ? DASH.reply : undefined);
288
+ const marker = item.style === "sync" ? "url(#sg-arrow-filled)" : "url(#sg-arrow-open)";
289
+ const d = item.self ? `M${n(item.x1)},${n(item.y)} H${n(item.x1 + SELF_LOOP_WIDTH)} V${n(item.y + SELF_LOOP_HEIGHT)} H${n(item.x1)}` : `M${n(item.x1)},${n(item.y)} L${n(item.x2)},${n(item.y)}`;
290
+ const path = `<path class="sg-edge" d="${d}" fill="none" stroke="${theme.edge}" stroke-width="1.5" stroke-linejoin="round"${dashAttr(dash)} marker-end="${marker}"/>`;
291
+ const label = item.labelBox === undefined || item.label === undefined ? "" : labelBoxSvg(item.labelBox, item.label, theme, false);
292
+ return group("message", String(item.index), item.meta, path + label, data([
293
+ ["confidence", confidenceOf(item.meta)],
294
+ ["from", item.from],
295
+ ["to", item.to],
296
+ ["style", item.style]
297
+ ]));
298
+ }
299
+ function renderSequence(laid, theme, types) {
300
+ return [
301
+ ...laid.participants.map((item) => renderParticipant(item, laid.lifelineBottom, theme, types)),
302
+ ...laid.messages.map((item) => renderMessage(item, theme))
303
+ ].join("");
304
+ }
305
+ function nodeLook(meta, type, theme, types) {
306
+ const unknown = confidenceOf(meta) === "unknown";
307
+ const typed = typeStyleOf(types, type);
308
+ const fill = unknown ? theme.unknownFill : typed.fill ?? theme.nodeFill;
309
+ const stroke = typed.stroke ?? theme.nodeStroke;
310
+ return {
311
+ cls: unknown ? "sg-node sg-unknown" : "sg-node",
312
+ paint: `fill="${fill}" stroke="${stroke}" stroke-width="1.5"${dashAttr(confidenceDash(meta))}`,
313
+ typed
314
+ };
315
+ }
316
+ function actionLines(actions, x, top, theme) {
317
+ return actions.map((action, index) => textLines(action, x, top + index * LINE_HEIGHT * CAPTION_FONT.size, CAPTION_FONT, "sg-muted sg-caption", theme.muted, "start")).join("");
318
+ }
319
+ function pseudoBody(item, theme, types) {
320
+ const cx = item.x + item.width / 2;
321
+ const cy = item.y + item.height / 2;
322
+ const look = nodeLook(item.meta, item.type, theme, types);
323
+ const circle = (r, cls, paint) => `<circle class="${cls}" cx="${n(cx)}" cy="${n(cy)}" r="${r}" ${paint}/>`;
324
+ const solid = `fill="${theme.fg}"`;
325
+ switch (item.pseudo) {
326
+ case "initial":
327
+ return circle(10, "sg-pseudo", solid);
328
+ case "final":
329
+ return circle(12, look.cls, look.paint) + circle(7, "sg-pseudo", solid);
330
+ case "history": {
331
+ const glyph = measureText2("H", LABEL_FONT);
332
+ return circle(12, look.cls, look.paint) + textLines("H", cx, cy - glyph.height / 2, LABEL_FONT, "sg-text", theme.fg);
333
+ }
334
+ case "choice": {
335
+ const points = outline("diamond", item).map((point) => `${n(point.x)},${n(point.y)}`).join(" ");
336
+ return `<polygon class="${look.cls}" points="${points}" ${look.paint}/>`;
337
+ }
338
+ default:
339
+ return `<rect class="sg-pseudo" x="${n(item.x)}" y="${n(item.y)}" width="${n(item.width)}" height="${n(item.height)}" ${solid}/>`;
340
+ }
341
+ }
342
+ function compositeBody(item, theme) {
343
+ const rect = `<rect class="sg-group" x="${n(item.x)}" y="${n(item.y)}" width="${n(item.width)}" height="${n(item.height)}" rx="10" fill="${theme.groupFill}" stroke="${theme.groupStroke}" stroke-width="1"${dashAttr(confidenceDash(item.meta))}/>`;
344
+ const title = measureText2(item.label, GROUP_FONT);
345
+ const label = item.label === "" ? "" : textLines(item.label, item.x + PAD_X, item.y + 8, GROUP_FONT, "sg-muted sg-label", theme.muted, "start");
346
+ const actions = item.actions ?? [];
347
+ return rect + label + actionLines(actions, item.x + PAD_X, item.y + 8 + title.height + ACTIONS_GAP / 2, theme);
348
+ }
349
+ function simpleBody(item, index, theme, types) {
350
+ const look = nodeLook(item.meta, item.type, theme, types);
351
+ const actions = item.actions ?? [];
352
+ const geometry = {
353
+ tag: "rect",
354
+ attrs: ` x="${n(item.x)}" y="${n(item.y)}" width="${n(item.width)}" height="${n(item.height)}" rx="10"`
355
+ };
356
+ let body = `<rect class="${look.cls}"${geometry.attrs} ${look.paint}/>`;
357
+ const header = actions.length === 0 ? item.height : boxSize(item.label, item.type, 80).height;
358
+ const label = measureText2(item.label, LABEL_FONT);
359
+ const caption = item.type === undefined ? undefined : measureText2(item.type, CAPTION_FONT);
360
+ const top = item.y + (header - label.height - (caption?.height ?? 0)) / 2;
361
+ const cx = item.x + item.width / 2;
362
+ body += textLines(item.label, cx, top, LABEL_FONT, "sg-text sg-label", look.typed.text ?? theme.fg);
363
+ if (item.type !== undefined) {
364
+ body += textLines(item.type, cx, top + label.height, CAPTION_FONT, "sg-muted sg-caption", theme.muted);
365
+ }
366
+ if (actions.length > 0) {
367
+ body += `<line class="sg-divider" x1="${n(item.x)}" y1="${n(item.y + header)}" x2="${n(item.x + item.width)}" y2="${n(item.y + header)}" stroke="${theme.nodeStroke}" stroke-width="1"/>`;
368
+ body += actionLines(actions, item.x + PAD_X, item.y + header + ACTIONS_GAP, theme);
369
+ }
370
+ const coverage = item.meta?.coverage;
371
+ if (coverage !== undefined) {
372
+ const clip = `sg-clip-${index}`;
373
+ body = `<clipPath id="${clip}"><rect${geometry.attrs}/></clipPath>` + body + `<rect class="sg-accent" x="${n(item.x)}" y="${n(item.y + item.height - 4)}" width="${n(coverage * item.width)}" height="4" fill="${theme.accent}" clip-path="url(#${clip})"/>`;
374
+ }
375
+ return body;
376
+ }
377
+ function renderStateItem(item, index, theme, types) {
378
+ const body = item.composite ? compositeBody(item, theme) : item.pseudo !== undefined ? pseudoBody(item, theme, types) : simpleBody(item, index, theme, types);
379
+ return wrapLink(group("state", item.id, item.meta, body, data([
380
+ ["type", item.type],
381
+ ["confidence", confidenceOf(item.meta)],
382
+ ["pseudo", item.pseudo],
383
+ ["composite", item.composite ? "true" : undefined]
384
+ ])), item.link);
385
+ }
386
+ function renderTransition(item, theme) {
387
+ const path = `<path class="sg-edge" d="${pathOf(item.points)}" fill="none" stroke="${theme.edge}" stroke-width="1.5" stroke-linejoin="round"${dashAttr(confidenceDash(item.meta))} marker-end="url(#sg-arrow-filled)"/>`;
388
+ const label = item.labelBox === undefined || item.label === undefined ? "" : labelBoxSvg(item.labelBox, item.label, theme, true);
389
+ return group("transition", item.id, item.meta, path + label, data([
390
+ ["type", item.type],
391
+ ["confidence", confidenceOf(item.meta)],
392
+ ["from", item.from],
393
+ ["to", item.to]
394
+ ]));
395
+ }
396
+ function renderState(laid, theme, types) {
397
+ const composites = laid.states.map((item, index) => ({ item, index })).filter(({ item }) => item.composite).sort((a, b) => a.item.depth - b.item.depth);
398
+ const simple = laid.states.map((item, index) => ({ item, index })).filter(({ item }) => !item.composite);
399
+ return [
400
+ ...composites.map(({ item, index }) => renderStateItem(item, index, theme, types)),
401
+ ...laid.transitions.map((item) => renderTransition(item, theme)),
402
+ ...simple.map(({ item, index }) => renderStateItem(item, index, theme, types))
403
+ ].join("");
404
+ }
405
+ function attributeRow(attribute, entity, theme) {
406
+ const keys = keysText(attribute.keys);
407
+ const typeText = attributeTypeText(attribute);
408
+ const top = attribute.y + (attribute.height - LINE_HEIGHT * EDGE_LABEL_FONT.size) / 2;
409
+ const keyTop = attribute.y + (attribute.height - LINE_HEIGHT * CAPTION_FONT.size) / 2;
410
+ let body = attribute.note === undefined ? "" : `<title>${esc(attribute.note)}</title>`;
411
+ if (keys !== "") {
412
+ body += textLines(keys, entity.x + entity.columns.key, keyTop, CAPTION_FONT, "sg-muted sg-attr-key", theme.muted, "start");
413
+ }
414
+ body += textLines(attribute.name, entity.x + entity.columns.name, top, EDGE_LABEL_FONT, "sg-text", theme.fg, "start");
415
+ if (typeText !== "") {
416
+ body += textLines(typeText, entity.x + entity.columns.type, top, EDGE_LABEL_FONT, "sg-muted sg-attr-type", theme.muted, "start");
417
+ }
418
+ return `<g class="sg-attr"${data([
419
+ ["attr", attribute.name],
420
+ ["keys", attribute.keys === undefined ? undefined : attribute.keys.join(" ")]
421
+ ])}>${body}</g>`;
422
+ }
423
+ function renderEntity(item, index, theme, types) {
424
+ const look = nodeLook(item.meta, item.type, theme, types);
425
+ const frame = ` x="${n(item.x)}" y="${n(item.y)}" width="${n(item.width)}" height="${n(item.height)}" rx="4"`;
426
+ const clip = `sg-clip-${index}`;
427
+ let body = `<clipPath id="${clip}"><rect${frame}/></clipPath>` + `<rect class="${look.cls}"${frame} ${look.paint}/>` + `<rect class="sg-header" x="${n(item.x)}" y="${n(item.y)}" width="${n(item.width)}" height="${n(item.headerHeight)}" fill="${theme.headerFill}" clip-path="url(#${clip})"/>`;
428
+ const label = measureText2(item.label, LABEL_FONT);
429
+ const caption = item.type === undefined ? undefined : measureText2(item.type, CAPTION_FONT);
430
+ const top = item.y + (item.headerHeight - label.height - (caption?.height ?? 0)) / 2;
431
+ const cx = item.x + item.width / 2;
432
+ body += textLines(item.label, cx, top, LABEL_FONT, "sg-text sg-label", look.typed.text ?? theme.fg);
433
+ if (item.type !== undefined) {
434
+ body += textLines(item.type, cx, top + label.height, CAPTION_FONT, "sg-muted sg-caption", theme.muted);
435
+ }
436
+ body += item.attributes.map((attribute) => attributeRow(attribute, item, theme)).join("");
437
+ const coverage = item.meta?.coverage;
438
+ if (coverage !== undefined) {
439
+ body += `<rect class="sg-accent" x="${n(item.x)}" y="${n(item.y + item.height - 4)}" width="${n(coverage * item.width)}" height="4" fill="${theme.accent}" clip-path="url(#${clip})"/>`;
440
+ }
441
+ return wrapLink(group("entity", item.id, item.meta, body, data([
442
+ ["type", item.type],
443
+ ["confidence", confidenceOf(item.meta)]
444
+ ])), item.link);
445
+ }
446
+ function cardinalityMarks(cardinality, from, next, theme) {
447
+ if (cardinality === undefined || from === undefined || next === undefined)
448
+ return "";
449
+ const length = Math.hypot(next.x - from.x, next.y - from.y);
450
+ if (length === 0)
451
+ return "";
452
+ const ux = (next.x - from.x) / length;
453
+ const uy = (next.y - from.y) / length;
454
+ const vx = -uy;
455
+ const vy = ux;
456
+ const at = (distance, side = 0) => `${n(from.x + distance * ux + side * vx)},${n(from.y + distance * uy + side * vy)}`;
457
+ const stroke = `fill="none" stroke="${theme.edge}" stroke-width="1.5" stroke-linecap="round"`;
458
+ const line = (d) => `<path class="sg-edge" d="${d}" ${stroke}/>`;
459
+ const bar = (distance) => line(`M${at(distance, 6)} L${at(distance, -6)}`);
460
+ const circle = (distance) => `<circle class="sg-bg sg-edge" cx="${n(from.x + distance * ux)}" cy="${n(from.y + distance * uy)}" r="4" fill="${theme.bg}" stroke="${theme.edge}" stroke-width="1.5"/>`;
461
+ const foot = () => line(`M${at(12)} L${at(0, 7)} M${at(12)} L${at(0)} M${at(12)} L${at(0, -7)}`);
462
+ switch (cardinality) {
463
+ case "one":
464
+ return bar(6) + bar(11);
465
+ case "zero-or-one":
466
+ return bar(6) + circle(15);
467
+ case "one-or-more":
468
+ return foot() + bar(15);
469
+ case "zero-or-more":
470
+ return foot() + circle(17);
471
+ }
472
+ }
473
+ function renderRelation(item, theme) {
474
+ const dash = confidenceDash(item.meta) ?? (item.identifying ? undefined : "4 3");
475
+ const path = `<path class="sg-edge" d="${pathOf(item.points)}" fill="none" stroke="${theme.edge}" stroke-width="1.5" stroke-linejoin="round"${dashAttr(dash)}/>`;
476
+ const points = item.points;
477
+ const marks = cardinalityMarks(item.fromCardinality, points[0], points[1], theme) + cardinalityMarks(item.toCardinality, points[points.length - 1], points[points.length - 2], theme);
478
+ const label = item.labelBox === undefined || item.label === undefined ? "" : labelBoxSvg(item.labelBox, item.label, theme, true);
479
+ return group("relation", item.id, item.meta, path + marks + label, data([
480
+ ["type", item.type],
481
+ ["confidence", confidenceOf(item.meta)],
482
+ ["from", item.from],
483
+ ["to", item.to],
484
+ ["from-cardinality", item.fromCardinality],
485
+ ["to-cardinality", item.toCardinality],
486
+ ["identifying", String(item.identifying)]
487
+ ]));
488
+ }
489
+ function renderEr(laid, theme, types) {
490
+ return [
491
+ ...laid.relations.map((item) => renderRelation(item, theme)),
492
+ ...laid.entities.map((item, index) => renderEntity(item, index, theme, types))
493
+ ].join("");
494
+ }
495
+ function renderTreeEdge(item, theme) {
496
+ const [first, ...rest] = item.points;
497
+ const d = first === undefined ? "" : item.curve === "bezier" ? `M${n(first.x)},${n(first.y)} C${rest.map((point) => `${n(point.x)},${n(point.y)}`).join(" ")}` : pathOf(item.points);
498
+ return group("edge", item.id, undefined, `<path class="sg-edge" d="${d}" fill="none" stroke="${theme.edge}" stroke-width="1.5" stroke-linejoin="round"/>`, data([
499
+ ["from", item.from],
500
+ ["to", item.to]
501
+ ]));
502
+ }
503
+ function renderTreeNode(item, index, theme, types) {
504
+ return renderNode(item, index, theme, types, [
505
+ ["depth", item.depth],
506
+ ["side", item.side]
507
+ ]);
508
+ }
509
+ function renderTree(laid, theme, types) {
510
+ return [
511
+ ...laid.edges.map((item) => renderTreeEdge(item, theme)),
512
+ ...laid.nodes.map((item, index) => renderTreeNode(item, index, theme, types))
513
+ ].join("");
514
+ }
515
+ function renderScene(laidOut, theme, types) {
516
+ switch (laidOut.kind) {
517
+ case "graph":
518
+ return renderGraph(laidOut, theme, types);
519
+ case "sequence":
520
+ return renderSequence(laidOut, theme, types);
521
+ case "state":
522
+ return renderState(laidOut, theme, types);
523
+ case "er":
524
+ return renderEr(laidOut, theme, types);
525
+ case "tree":
526
+ return renderTree(laidOut, theme, types);
527
+ default:
528
+ return unreachable(laidOut);
529
+ }
530
+ }
531
+ function unreachable(value) {
532
+ throw new Error(`Unsupported scene: ${JSON.stringify(value)}`);
533
+ }
534
+ function renderSvg2(laidOut, options = {}) {
535
+ const resolved = withPalette(resolveTheme2(options.theme), typesPresent(laidOut));
536
+ const { tokens: theme, types } = escapedTheme(resolved);
537
+ const standalone = options.standalone ?? true;
538
+ const width = n(laidOut.width);
539
+ const height = n(laidOut.height);
540
+ const head = standalone ? `<?xml version="1.0" encoding="UTF-8"?>
541
+ <svg xmlns="http://www.w3.org/2000/svg"` : "<svg";
542
+ const title = laidOut.title === undefined ? "" : `<title>${esc(laidOut.title)}</title>`;
543
+ const body = renderScene(laidOut, theme, types);
544
+ const svg = [
545
+ `${head} class="sg-root" data-diagram="${laidOut.kind}"${laidOut.kind === "tree" ? ` data-layout="${laidOut.layout}"` : ""} version="1.1" viewBox="0 0 ${width} ${height}" width="${width}" height="${height}" font-family="Inter, system-ui, sans-serif">`,
546
+ title,
547
+ defs(theme, resolved, options.styles ?? true),
548
+ `<rect class="sg-bg" x="0" y="0" width="${width}" height="${height}" fill="${theme.bg}"/>`,
549
+ body,
550
+ "</svg>",
551
+ ""
552
+ ].filter((part, index, all) => part !== "" || index === all.length - 1).join(`
553
+ `);
554
+ const prefix = options.idPrefix;
555
+ if (prefix === undefined || prefix === "sg")
556
+ return svg;
557
+ return svg.replaceAll('id="sg-', `id="${esc(prefix)}-`).replaceAll("url(#sg-", `url(#${esc(prefix)}-`);
558
+ }
559
+
560
+ export { renderSvg2 };