@fengwk/kchart 0.0.0-stage → 0.1.1

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 (201) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE.md +11 -0
  4. package/README.md +68 -2
  5. package/README.zh-CN.md +69 -0
  6. package/dist/LICENSE +21 -0
  7. package/dist/NOTICE.md +11 -0
  8. package/dist/assets/ICONS-LICENSE.txt +53 -0
  9. package/dist/assets/maps/LICENSE.md +75 -0
  10. package/dist/assets/maps/METADATA.md +9 -0
  11. package/dist/assets/maps/REGIONS.md +28 -0
  12. package/dist/assets/maps/us-states.geojson +1 -0
  13. package/dist/assets/maps/world.geojson +1 -0
  14. package/dist/esm/_virtual/_kchart-families.js +48 -0
  15. package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
  16. package/dist/esm/assets/icons.js +11 -0
  17. package/dist/esm/assets/maps/us-states.js +3 -0
  18. package/dist/esm/assets/maps/world.js +3 -0
  19. package/dist/esm/assets/themes/claude.js +69 -0
  20. package/dist/esm/assets/themes/cloudmusic.js +62 -0
  21. package/dist/esm/assets/themes/kanagawa.js +67 -0
  22. package/dist/esm/assets/themes/obsidian.js +67 -0
  23. package/dist/esm/assets/themes/paper.js +61 -0
  24. package/dist/esm/assets/themes/terminal.js +60 -0
  25. package/dist/esm/assets/themes/tokyo.js +62 -0
  26. package/dist/esm/chart/basic.js +1313 -0
  27. package/dist/esm/chart/catalog.js +476 -0
  28. package/dist/esm/chart/composition.js +844 -0
  29. package/dist/esm/chart/context.js +189 -0
  30. package/dist/esm/chart/distribution.js +442 -0
  31. package/dist/esm/chart/fields.js +67 -0
  32. package/dist/esm/chart/maps.js +563 -0
  33. package/dist/esm/chart/mindmap.js +629 -0
  34. package/dist/esm/chart/multivariate.js +1229 -0
  35. package/dist/esm/chart/registry.js +108 -0
  36. package/dist/esm/chart/relations.js +949 -0
  37. package/dist/esm/chart/spec.js +1415 -0
  38. package/dist/esm/chart/stat-compare.js +302 -0
  39. package/dist/esm/chart/stat-trend.js +181 -0
  40. package/dist/esm/chart/statistics.js +1007 -0
  41. package/dist/esm/chart/unit-variants.js +645 -0
  42. package/dist/esm/chart/variants.js +552 -0
  43. package/dist/esm/core/color.js +197 -0
  44. package/dist/esm/core/context.js +256 -0
  45. package/dist/esm/core/diagnostics.js +23 -0
  46. package/dist/esm/core/format.js +167 -0
  47. package/dist/esm/core/geometry.js +130 -0
  48. package/dist/esm/core/icon-data.js +5 -0
  49. package/dist/esm/core/icons.js +173 -0
  50. package/dist/esm/core/scale.js +112 -0
  51. package/dist/esm/core/scene.js +234 -0
  52. package/dist/esm/core/svg.js +89 -0
  53. package/dist/esm/core/text.js +184 -0
  54. package/dist/esm/core/theme.js +318 -0
  55. package/dist/esm/diagram/agentflow.js +596 -0
  56. package/dist/esm/diagram/architecture.js +729 -0
  57. package/dist/esm/diagram/block.js +988 -0
  58. package/dist/esm/diagram/blocks.js +687 -0
  59. package/dist/esm/diagram/c4.js +1002 -0
  60. package/dist/esm/diagram/class.js +1158 -0
  61. package/dist/esm/diagram/cynefin.js +749 -0
  62. package/dist/esm/diagram/emphasis.js +7 -0
  63. package/dist/esm/diagram/er.js +657 -0
  64. package/dist/esm/diagram/eventmodeling.js +948 -0
  65. package/dist/esm/diagram/gantt-date.js +323 -0
  66. package/dist/esm/diagram/gantt.js +661 -0
  67. package/dist/esm/diagram/gitgraph.js +472 -0
  68. package/dist/esm/diagram/graph-layout.js +4046 -0
  69. package/dist/esm/diagram/graph-parse.js +1366 -0
  70. package/dist/esm/diagram/graph-route.js +490 -0
  71. package/dist/esm/diagram/graph.js +553 -0
  72. package/dist/esm/diagram/ishikawa.js +496 -0
  73. package/dist/esm/diagram/journey.js +512 -0
  74. package/dist/esm/diagram/kanban.js +280 -0
  75. package/dist/esm/diagram/label.js +56 -0
  76. package/dist/esm/diagram/layers.js +385 -0
  77. package/dist/esm/diagram/map-label.js +44 -0
  78. package/dist/esm/diagram/mindmap.js +277 -0
  79. package/dist/esm/diagram/numeric-composition.js +78 -0
  80. package/dist/esm/diagram/numeric-model.js +86 -0
  81. package/dist/esm/diagram/numeric-parse.js +205 -0
  82. package/dist/esm/diagram/numeric-radar.js +355 -0
  83. package/dist/esm/diagram/numeric-sankey.js +201 -0
  84. package/dist/esm/diagram/numeric-xy.js +422 -0
  85. package/dist/esm/diagram/packet.js +175 -0
  86. package/dist/esm/diagram/presentation.js +252 -0
  87. package/dist/esm/diagram/quadrant.js +297 -0
  88. package/dist/esm/diagram/railroad-parse.js +465 -0
  89. package/dist/esm/diagram/railroad.js +294 -0
  90. package/dist/esm/diagram/registry.js +296 -0
  91. package/dist/esm/diagram/relations.js +966 -0
  92. package/dist/esm/diagram/requirement.js +341 -0
  93. package/dist/esm/diagram/sequence.js +1228 -0
  94. package/dist/esm/diagram/shapes.js +366 -0
  95. package/dist/esm/diagram/source.js +538 -0
  96. package/dist/esm/diagram/support.js +153 -0
  97. package/dist/esm/diagram/swimlane-mermaid.js +140 -0
  98. package/dist/esm/diagram/swimlane.js +1186 -0
  99. package/dist/esm/diagram/time.js +748 -0
  100. package/dist/esm/diagram/timeline.js +376 -0
  101. package/dist/esm/diagram/tree-family-icon.js +22 -0
  102. package/dist/esm/diagram/treeview.js +293 -0
  103. package/dist/esm/diagram/usecase.js +852 -0
  104. package/dist/esm/diagram/venn.js +1038 -0
  105. package/dist/esm/diagram/wardley.js +838 -0
  106. package/dist/esm/index.d.ts +133 -0
  107. package/dist/esm/index.js +92 -0
  108. package/dist/esm/interaction.js +561 -0
  109. package/dist/esm/mermaid.d.ts +28 -0
  110. package/dist/esm/mermaid.js +70 -0
  111. package/dist/esm/node.d.ts +68 -0
  112. package/dist/esm/node.js +373 -0
  113. package/dist/esm/package.json +1 -0
  114. package/dist/esm/page-api.js +65 -0
  115. package/dist/esm/runtime.js +352 -0
  116. package/dist/esm/src/families.js +209 -0
  117. package/dist/page-api.js +1 -0
  118. package/dist/units/chart/basic.js +1 -0
  119. package/dist/units/chart/catalog.js +1 -0
  120. package/dist/units/chart/composition.js +1 -0
  121. package/dist/units/chart/context.js +1 -0
  122. package/dist/units/chart/distribution.js +1 -0
  123. package/dist/units/chart/fields.js +1 -0
  124. package/dist/units/chart/maps.js +1 -0
  125. package/dist/units/chart/mindmap.js +1 -0
  126. package/dist/units/chart/multivariate.js +1 -0
  127. package/dist/units/chart/registry.js +1 -0
  128. package/dist/units/chart/relations.js +1 -0
  129. package/dist/units/chart/spec.js +3 -0
  130. package/dist/units/chart/stat-compare.js +1 -0
  131. package/dist/units/chart/stat-trend.js +1 -0
  132. package/dist/units/chart/statistics.js +1 -0
  133. package/dist/units/chart/unit-variants.js +1 -0
  134. package/dist/units/chart/variants.js +1 -0
  135. package/dist/units/core/color.js +1 -0
  136. package/dist/units/core/context.js +1 -0
  137. package/dist/units/core/diagnostics.js +1 -0
  138. package/dist/units/core/format.js +1 -0
  139. package/dist/units/core/geometry.js +1 -0
  140. package/dist/units/core/icon-data.js +1 -0
  141. package/dist/units/core/icons.js +1 -0
  142. package/dist/units/core/scale.js +1 -0
  143. package/dist/units/core/scene.js +1 -0
  144. package/dist/units/core/svg.js +1 -0
  145. package/dist/units/core/text.js +2 -0
  146. package/dist/units/core/theme.js +1 -0
  147. package/dist/units/diagram/agentflow.js +12 -0
  148. package/dist/units/diagram/architecture.js +4 -0
  149. package/dist/units/diagram/block.js +2 -0
  150. package/dist/units/diagram/blocks.js +2 -0
  151. package/dist/units/diagram/c4.js +6 -0
  152. package/dist/units/diagram/class.js +5 -0
  153. package/dist/units/diagram/cynefin.js +4 -0
  154. package/dist/units/diagram/emphasis.js +1 -0
  155. package/dist/units/diagram/er.js +9 -0
  156. package/dist/units/diagram/eventmodeling.js +5 -0
  157. package/dist/units/diagram/gantt-date.js +1 -0
  158. package/dist/units/diagram/gantt.js +3 -0
  159. package/dist/units/diagram/gitgraph.js +2 -0
  160. package/dist/units/diagram/graph-layout.js +4 -0
  161. package/dist/units/diagram/graph-parse.js +9 -0
  162. package/dist/units/diagram/graph-route.js +1 -0
  163. package/dist/units/diagram/graph.js +1 -0
  164. package/dist/units/diagram/ishikawa.js +2 -0
  165. package/dist/units/diagram/journey.js +5 -0
  166. package/dist/units/diagram/kanban.js +2 -0
  167. package/dist/units/diagram/label.js +4 -0
  168. package/dist/units/diagram/layers.js +1 -0
  169. package/dist/units/diagram/map-label.js +1 -0
  170. package/dist/units/diagram/mindmap.js +2 -0
  171. package/dist/units/diagram/numeric-composition.js +1 -0
  172. package/dist/units/diagram/numeric-model.js +1 -0
  173. package/dist/units/diagram/numeric-parse.js +2 -0
  174. package/dist/units/diagram/numeric-radar.js +3 -0
  175. package/dist/units/diagram/numeric-sankey.js +2 -0
  176. package/dist/units/diagram/numeric-xy.js +2 -0
  177. package/dist/units/diagram/packet.js +2 -0
  178. package/dist/units/diagram/presentation.js +2 -0
  179. package/dist/units/diagram/quadrant.js +2 -0
  180. package/dist/units/diagram/railroad-parse.js +4 -0
  181. package/dist/units/diagram/railroad.js +1 -0
  182. package/dist/units/diagram/registry.js +2 -0
  183. package/dist/units/diagram/relations.js +3 -0
  184. package/dist/units/diagram/requirement.js +3 -0
  185. package/dist/units/diagram/sequence.js +2 -0
  186. package/dist/units/diagram/shapes.js +60 -0
  187. package/dist/units/diagram/source.js +20 -0
  188. package/dist/units/diagram/support.js +3 -0
  189. package/dist/units/diagram/swimlane-mermaid.js +3 -0
  190. package/dist/units/diagram/swimlane.js +2 -0
  191. package/dist/units/diagram/time.js +3 -0
  192. package/dist/units/diagram/timeline.js +2 -0
  193. package/dist/units/diagram/tree-family-icon.js +1 -0
  194. package/dist/units/diagram/treeview.js +2 -0
  195. package/dist/units/diagram/usecase.js +3 -0
  196. package/dist/units/diagram/venn.js +2 -0
  197. package/dist/units/diagram/wardley.js +4 -0
  198. package/dist/units/interaction.js +1 -0
  199. package/dist/units/runtime.js +1 -0
  200. package/dist/units.json +712 -0
  201. package/package.json +30 -4
@@ -0,0 +1,294 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { structureRoles } from "../core/context.js";
3
+ import { FONT, snap, textBlock, warning } from "./support.js";
4
+ import { childrenOf, parseAbnf, parseEbnf, parseIr, parsePeg } from "./railroad-parse.js";
5
+ // src/diagram/railroad.js
6
+ var railroad_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
7
+ var honoredConfig = [
8
+ "padding",
9
+ "verticalSeparation",
10
+ "horizontalSeparation",
11
+ "arcRadius",
12
+ "fontSize",
13
+ "showMarkers",
14
+ "markerRadius",
15
+ "compactMode"
16
+ ].map((key) => `railroad.${key}`);
17
+ function metrics(config) {
18
+ const section = Object.entries(config ?? {}).find(([key]) => key.toLowerCase() === "railroad")?.[1];
19
+ const options = new Map(Object.entries(section ?? {}).map(([key, value]) => [key.toLowerCase(), value]));
20
+ const value = (key, fallback, low, high) => {
21
+ const n = options.get(key.toLowerCase());
22
+ return typeof n === "number" && Number.isFinite(n) ? Math.max(low, Math.min(high, n)) : fallback;
23
+ };
24
+ const compact = options.get("compactmode") === true;
25
+ return {
26
+ pad: value("padding", 16, 8, 80),
27
+ rail: compact ? 6 : value("horizontalSeparation", 12, 6, 80),
28
+ gap: compact ? 6 : value("verticalSeparation", 10, 6, 80),
29
+ radius: value("arcRadius", 9, 6, 16),
30
+ marker: options.get("showmarkers") === false ? 0 : value("markerRadius", 4, 2, 7),
31
+ font: {
32
+ ...FONT.body,
33
+ size: value("fontSize", FONT.body.size, 12, 32)
34
+ }
35
+ };
36
+ }
37
+ var nameFont = {
38
+ ...FONT.body,
39
+ weight: 600
40
+ };
41
+ var caption = ({ min, max }) => max === Infinity ? `${min}+` : min === max ? `×${min}` : `${min}…${max}`;
42
+ var literal = (value) => value.replace(/[\n\r\t]/g, (ch) => ({
43
+ "\n": "\\n",
44
+ "\r": "\\r",
45
+ " ": "\\t"
46
+ })[ch]);
47
+ function measureTrack(root, measure, prefix, { rail, gap, radius, font }) {
48
+ const sizes = /* @__PURE__ */ new Map(), stack = [[root, false]];
49
+ while (stack.length) {
50
+ const [node, visited] = stack.pop(), descendants = childrenOf(node);
51
+ if (!visited && descendants.length) {
52
+ stack.push([node, true], ...descendants.map((child) => [child, false]).reverse());
53
+ continue;
54
+ }
55
+ const id = `${prefix}-${sizes.size}`, parts = descendants.map((child) => sizes.get(child));
56
+ const track = {
57
+ id,
58
+ entry: `${id}-in`,
59
+ exit: `${id}-out`,
60
+ node,
61
+ parts
62
+ };
63
+ if (!parts.length) {
64
+ const label = literal(node.label ?? node.text ?? node.value ?? node.name);
65
+ track.label = node.type === "special" ? `? ${label} ?` : label;
66
+ track.labelWidth = Math.ceil(measure(track.label, font));
67
+ track.w = Math.max(24, track.labelWidth + 16 + (node.icon ? track.label ? 20 : 14 : 0));
68
+ track.up = track.down = snap(font.size + 12) / 2;
69
+ track.entry = track.exit = id;
70
+ } else if (node.type === "sequence") {
71
+ track.w = parts.reduce((sum, child) => sum + child.w, (parts.length - 1) * rail);
72
+ track.up = Math.max(...parts.map((child) => child.up));
73
+ track.down = Math.max(...parts.map((child) => child.down));
74
+ track.entry = parts[0].entry;
75
+ track.exit = parts.at(-1).exit;
76
+ } else if (node.type === "choice") {
77
+ const spacing = gap + Math.min(14, Math.max(0, 2 * (parts.length - 3)));
78
+ let cursor = 0;
79
+ const lanes = parts.map((child) => {
80
+ const baseline = cursor + child.up;
81
+ cursor = baseline + child.down + spacing;
82
+ return baseline;
83
+ });
84
+ const baseline = lanes[Math.floor((lanes.length - 1) / 2)];
85
+ track.lanes = lanes.map((y) => y - baseline);
86
+ track.w = Math.max(...parts.map((child) => child.w)) + 4 * radius;
87
+ track.up = baseline;
88
+ track.down = cursor - spacing - baseline;
89
+ } else {
90
+ const [inner] = parts;
91
+ track.bypass = node.type === "optional" || node.min === 0;
92
+ track.caption = node.type === "repetition" ? caption(node) : null;
93
+ track.w = 4 * radius + Math.max(inner.w, track.caption ? Math.ceil(measure(track.caption, FONT.label)) + 16 : 0);
94
+ track.up = inner.up + (track.bypass ? 2 * radius : 0);
95
+ track.down = inner.down + (track.caption ? 2 * radius + 8 : 0);
96
+ }
97
+ sizes.set(node, track);
98
+ }
99
+ return sizes.get(root);
100
+ }
101
+ function paintTrack(track, left, baseline, draw, { rail, radius }) {
102
+ const stack = [[
103
+ track,
104
+ left,
105
+ baseline
106
+ ]];
107
+ const { edge, chip, count } = draw;
108
+ while (stack.length) {
109
+ const [t, x, y] = stack.pop(), right = x + t.w;
110
+ const r = radius, { parts, entry, exit } = t;
111
+ if (!parts.length) {
112
+ chip(t, x, y);
113
+ continue;
114
+ }
115
+ const children = [];
116
+ if (t.node.type === "sequence") {
117
+ let cursor = x;
118
+ parts.forEach((child, i) => {
119
+ if (i) edge(`M ${cursor - rail} ${y} H ${cursor}`, parts[i - 1].exit, child.entry);
120
+ children.push([
121
+ child,
122
+ cursor,
123
+ y
124
+ ]);
125
+ cursor += child.w + rail;
126
+ });
127
+ } else if (t.node.type === "choice") {
128
+ const a = x + r, b = right - r;
129
+ edge(`M ${x} ${y} H ${a}`, entry, `${entry}-fan`);
130
+ edge(`M ${b} ${y} H ${right}`, `${exit}-fan`, exit);
131
+ const top = y + Math.min(0, t.lanes[0] + r), bottom = y + Math.max(0, t.lanes.at(-1) - r);
132
+ edge(`M ${a} ${top} V ${bottom}`, `${entry}-fan`, `${entry}-fan`);
133
+ edge(`M ${b} ${top} V ${bottom}`, `${exit}-fan`, `${exit}-fan`);
134
+ parts.forEach((child, i) => {
135
+ const lane = y + t.lanes[i], childX = x + (t.w - child.w) / 2;
136
+ const dy = Math.sign(t.lanes[i]) * r;
137
+ const start = dy ? `M ${a} ${lane - dy} A ${r} ${r} 0 0 ${dy > 0 ? 0 : 1} ${a + r} ${lane}` : `M ${a} ${y}`;
138
+ const end = dy ? `H ${b - r} A ${r} ${r} 0 0 ${dy > 0 ? 0 : 1} ${b} ${lane - dy}` : `H ${b}`;
139
+ edge(`${start} H ${childX}`, `${entry}-fan`, child.entry);
140
+ edge(`M ${childX + child.w} ${lane} ${end}`, child.exit, `${exit}-fan`);
141
+ children.push([
142
+ child,
143
+ childX,
144
+ lane
145
+ ]);
146
+ });
147
+ } else {
148
+ const [inner] = parts, childX = x + (t.w - inner.w) / 2;
149
+ const a = x + 2 * r, b = right - 2 * r;
150
+ edge(`M ${x} ${y} H ${childX}`, entry, inner.entry);
151
+ edge(`M ${childX + inner.w} ${y} H ${right}`, inner.exit, exit);
152
+ if (t.bypass) {
153
+ const above = y - t.up + 2;
154
+ edge(`M ${x} ${y} Q ${x + r} ${y} ${x + r} ${y - r} V ${above + r} Q ${x + r} ${above} ${a} ${above} H ${b} Q ${right - r} ${above} ${right - r} ${above + r} V ${y - r} Q ${right - r} ${y} ${right} ${y}`, entry, exit);
155
+ }
156
+ if (t.caption) {
157
+ const below = y + inner.down + 2 * r, mid = (a + b) / 2;
158
+ const half = count(t.caption, mid, below);
159
+ edge(`M ${b} ${y} Q ${b + r} ${y} ${b + r} ${y + r} V ${below - r} Q ${b + r} ${below} ${b} ${below} H ${mid + half}`, inner.exit, `${t.id}-count`);
160
+ edge(`M ${mid - half} ${below} H ${a} Q ${a - r} ${below} ${a - r} ${below - r} V ${y + r} Q ${a - r} ${y} ${a} ${y}`, `${t.id}-count`, inner.entry);
161
+ }
162
+ children.push([
163
+ inner,
164
+ childX,
165
+ y
166
+ ]);
167
+ }
168
+ stack.push(...children.reverse());
169
+ }
170
+ }
171
+ function layout(model, { width, ctx, measure, lang, renderOptions }, info) {
172
+ const colors = structureRoles(renderOptions).colors;
173
+ const style = metrics(info.config), { pad, gap, marker, font } = style;
174
+ const edges = [], chips = [], text = [];
175
+ const edgeInk = colors["edge-strong"];
176
+ const edge = (d, from, to) => edges.push(ctx.path(d, {
177
+ fill: "none",
178
+ stroke: ctx.design[model.edgeStatusOf(edges.length)] ?? edgeInk,
179
+ "stroke-width": 1.5,
180
+ "stroke-linecap": "round",
181
+ meta: {
182
+ kind: "edge",
183
+ from,
184
+ to
185
+ }
186
+ }));
187
+ const label = (value, x, y, font, anchor, fill) => textBlock(ctx, {
188
+ lines: [value],
189
+ x,
190
+ y,
191
+ font,
192
+ anchor,
193
+ fill
194
+ });
195
+ const linked = (link, children) => link ? ctx.link(link.href, children, link) : ctx.el("g", {}, children);
196
+ function iconAt(icon, x, y) {
197
+ const [left, top, w, h] = icon.viewBox.split(/\s+/).map(Number), scale = 14 / Math.max(w, h);
198
+ const fill = ctx.design.fg1;
199
+ return ctx.el("g", { transform: `translate(${x - (left + w / 2) * scale} ${y - (top + h / 2) * scale}) scale(${scale})` }, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, {
200
+ ...attrs,
201
+ ...icon.set === "simple" ? { fill } : {
202
+ fill: "none",
203
+ stroke: fill,
204
+ "stroke-width": 1.6,
205
+ "stroke-linecap": "round",
206
+ "stroke-linejoin": "round"
207
+ }
208
+ })));
209
+ }
210
+ const chip = (t, x, y) => {
211
+ const special = t.node.type === "special";
212
+ const { icon, status, link } = t.node;
213
+ const stroke = ctx.design[status] ?? (t.node.undefined || t.node.exception ? ctx.design.warn : special ? colors["em-line"] : colors["node-line"]);
214
+ const center = x + t.w / 2;
215
+ const content = [ctx.rect(x, y - t.up, t.w, t.up + t.down, {
216
+ rx: t.node.type === "terminal" ? t.up : 2,
217
+ fill: special ? colors["em-fill"] : colors.node,
218
+ stroke,
219
+ ...special ? { "stroke-dasharray": "4 3" } : {},
220
+ meta: {
221
+ kind: "node",
222
+ id: t.id
223
+ }
224
+ })];
225
+ if (icon) content.push(iconAt(icon, center - (t.label ? t.labelWidth / 2 + 3 : 0), y));
226
+ if (t.label) content.push(label(t.label, center + (icon ? 10 : 0), y, font, "middle", ctx.design.fg1));
227
+ chips.push(linked(link, content));
228
+ };
229
+ const count = (value, x, y) => {
230
+ text.push(ctx.text(x, y, value, {
231
+ ...FONT.label,
232
+ anchor: "middle",
233
+ fill: edgeInk,
234
+ "dominant-baseline": "central",
235
+ meta: { kind: "edge-label" }
236
+ }));
237
+ return Math.ceil(measure(value, FONT.label)) / 2 + 5;
238
+ };
239
+ const start = pad + Math.ceil(Math.max(0, ...model.rules.map((rule) => measure(`${rule.name} =`, nameFont) + (rule.icon ? 20 : 0)))) + 16, left = start + 16;
240
+ let y = pad, natural = 200;
241
+ for (const [index, rule] of model.rules.entries()) {
242
+ const id = `rule-${index}`, t = measureTrack(rule.definition, measure, id, style), baseline = y + t.up;
243
+ const end = left + t.w + 16;
244
+ const markerInk = ctx.design[rule.status] ?? edgeInk;
245
+ const heading = label(`${rule.name} =`, pad + (rule.icon ? 20 : 0), baseline, nameFont, "start", ctx.textColor(ctx.design[rule.status] ?? ctx.design.fg1));
246
+ text.push(linked(rule.link, rule.icon ? [iconAt(rule.icon, pad + 7, baseline), heading] : [heading]));
247
+ if (marker) for (const [x, suffix] of [[start, "start"], [end, "end"]]) chips.push(ctx.circle(x, baseline, marker, {
248
+ fill: markerInk,
249
+ meta: {
250
+ kind: "node",
251
+ id: `${id}-${suffix}`
252
+ }
253
+ }));
254
+ edge(`M ${start + marker} ${baseline} H ${left}`, `${id}-start`, t.entry);
255
+ paintTrack(t, left, baseline, {
256
+ chip,
257
+ count,
258
+ edge
259
+ }, style);
260
+ edge(`M ${left + t.w} ${baseline} H ${end - marker}`, t.exit, `${id}-end`);
261
+ natural = Math.max(natural, end + marker + pad);
262
+ y = baseline + t.down + gap + 14;
263
+ }
264
+ const errors = natural > 2 * width || model.count > 60 ? [warning("LARGE_DIAGRAM", lang, info.headerLine, "语法图较大,可在图框中滚动", "Large grammar; scroll inside the frame", "考虑拆分规则", "Consider splitting the rules")] : [];
265
+ return {
266
+ scene: {
267
+ width: natural,
268
+ height: model.rules.length ? y - gap - 14 + pad : 100,
269
+ children: [
270
+ ...edges,
271
+ ...chips,
272
+ ...text
273
+ ],
274
+ title: info.accTitle || model.accTitle || model.title,
275
+ heading: model.title,
276
+ description: info.accDescr || model.accDescr
277
+ },
278
+ errors
279
+ };
280
+ }
281
+ var implementation = (parse) => ({
282
+ optionMode: "raw",
283
+ parse,
284
+ layout,
285
+ acceptsPresentation: true,
286
+ honoredConfig
287
+ });
288
+ var implementations = {
289
+ "railroad-beta": implementation(parseIr),
290
+ "railroad-ebnf-beta": implementation(parseEbnf),
291
+ "railroad-abnf-beta": implementation(parseAbnf),
292
+ "railroad-peg-beta": implementation(parsePeg)
293
+ };
294
+ export { implementations, railroad_exports };
@@ -0,0 +1,296 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { ignoredConfigWarning, inferLang, parseOptions, prepareSource, siteConfig } from "./source.js";
3
+ import { blocking, error, measureOf } from "./support.js";
4
+ import { createContext } from "../core/context.js";
5
+ import { containText, el } from "../core/scene.js";
6
+ import { renderScene } from "../core/svg.js";
7
+ import { ExpectedDiagnostic } from "../core/diagnostics.js";
8
+ // src/diagram/registry.js
9
+ var registry_exports = /* @__PURE__ */ __exportAll({ createDiagramApi: () => createDiagramApi });
10
+ var SILENT_CONFIG = /* @__PURE__ */ new Set([
11
+ "securitylevel",
12
+ "startonload",
13
+ "loglevel",
14
+ "deterministicids",
15
+ "deterministicidseed",
16
+ "maxtextsize",
17
+ "maxedges",
18
+ "suppresserrorrendering",
19
+ "htmllabels",
20
+ "markdownautowrap",
21
+ "arrowmarkerabsolute",
22
+ "secure",
23
+ "dompurifyconfig",
24
+ "fontfamily",
25
+ "fontsize"
26
+ ]);
27
+ var DEFAULT_CONFIG = /* @__PURE__ */ new Map([
28
+ ["look", "classic"],
29
+ ["layout", "dagre"],
30
+ ["theme", "default"]
31
+ ]);
32
+ function configWarning(info, implementation) {
33
+ const honored = (implementation?.honoredConfig ?? []).map((key) => key.toLowerCase());
34
+ const ignored = [];
35
+ function visit(config, prefix = "") {
36
+ for (const [name, value] of Object.entries(config)) {
37
+ const key = prefix + name, path = key.toLowerCase();
38
+ if (SILENT_CONFIG.has(path) || honored.some((entry) => path === entry || path.startsWith(`${entry}.`))) continue;
39
+ if (value && typeof value === "object" && !Array.isArray(value)) visit(value, `${key}.`);
40
+ else if (typeof value !== "string" || DEFAULT_CONFIG.get(path) !== value.toLowerCase()) ignored.push([key, info.configLines[path] ?? 1]);
41
+ }
42
+ }
43
+ visit(info.config);
44
+ const previous = info.errors.find((item) => item.code === "IGNORED_CONFIG");
45
+ if (!ignored.length) return previous;
46
+ ignored.sort((a, b) => a[1] - b[1]);
47
+ const subjects = ignored.map(([key]) => key).join(", ");
48
+ if (previous) return {
49
+ ...previous,
50
+ line: Math.min(previous.line, ignored[0][1]),
51
+ message: `${previous.message}, ${subjects}`
52
+ };
53
+ return ignoredConfigWarning(info.lang, ignored[0][1], subjects);
54
+ }
55
+ function presentationErrors(info) {
56
+ if (info.syntax !== "mermaid") return [];
57
+ const errors = [];
58
+ for (const [i, raw] of info.body.split("\n").entries()) {
59
+ const code = raw.replace(/"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'/g, "");
60
+ const forbidden = /^\s*(classDef|style|linkStyle|click|href|callback)\s/i.exec(code) || /::icon\s*\(/i.exec(code);
61
+ const line = info.firstLine + i;
62
+ if (forbidden) {
63
+ const word = forbidden[1] || "::icon";
64
+ errors.push(error(info.lang, line, `不支持 Mermaid 写法「${word}」`, `Unsupported Mermaid syntax "${word}"`, "移除样式、链接或回调写法;颜色由主题决定", "Remove styles, links, or callbacks; colors are controlled by the theme", raw.toLowerCase().indexOf(word.toLowerCase()) + 1));
65
+ } else if (!info.kind.toLowerCase().startsWith("classdiagram") && /^\s*class\s+\S+\s+\S+/i.test(code)) errors.push(error(info.lang, line, "不支持 Mermaid 样式类声明", "Mermaid style-class declarations are not supported", "使用 ::: 状态分类;不要声明通用样式类", "Use ::: status classes; generic style classes are unsupported", raw.search(/\S/) + 1));
66
+ }
67
+ return errors;
68
+ }
69
+ function createDiagramApi(implementations, types) {
70
+ if (!implementations || typeof implementations !== "object" || !Array.isArray(types)) throw new TypeError("Invalid diagram registry configuration");
71
+ const byKind = /* @__PURE__ */ new Map(), byAlias = /* @__PURE__ */ new Map();
72
+ for (const type of types) {
73
+ if (!type || typeof type.kind !== "string" || !type.kind || typeof type.syntax !== "string" || !type.syntax || !Array.isArray(type.aliases) || typeof type.topic !== "string") throw new TypeError("Invalid diagram type descriptor");
74
+ const kind = type.kind.toLowerCase();
75
+ if (byKind.has(kind)) throw new TypeError(`Duplicate diagram type: ${type.kind}`);
76
+ byKind.set(kind, type);
77
+ for (const alias of [type.kind, ...type.aliases]) {
78
+ if (typeof alias !== "string" || !alias.trim()) throw new TypeError(`Invalid diagram alias: ${alias}`);
79
+ const key = alias.toLowerCase();
80
+ if (byAlias.has(key)) throw new TypeError(`Duplicate diagram alias: ${alias}`);
81
+ byAlias.set(key, type);
82
+ }
83
+ }
84
+ const diagnostic = (error, type, defaultCode = "BAD_DIAGRAM") => ({
85
+ code: error.code || defaultCode,
86
+ line: error.line ?? 1,
87
+ column: error.column ?? 1,
88
+ message: error.message || "无效的结构图",
89
+ fix: error.fix || "修正结构图声明后重试",
90
+ topic: error.topic || type?.topic || "diagram",
91
+ severity: error.severity === "warning" ? "warning" : "error"
92
+ });
93
+ const normalize = (items, type) => (items || []).map((item) => diagnostic(item, type));
94
+ const merge = (...groups) => {
95
+ const seen = /* @__PURE__ */ new Set();
96
+ return groups.flat().filter((item) => {
97
+ const key = JSON.stringify([
98
+ item.code,
99
+ item.line,
100
+ item.column,
101
+ item.message,
102
+ item.fix,
103
+ item.topic,
104
+ item.severity
105
+ ]);
106
+ if (seen.has(key)) return false;
107
+ seen.add(key);
108
+ return true;
109
+ });
110
+ };
111
+ const headerLineOf = (prepared) => prepared.headerLine || prepared.firstLine || 1;
112
+ function parseUnchecked(input, options = {}) {
113
+ options = {
114
+ ...options,
115
+ lang: options.lang || inferLang(input)
116
+ };
117
+ const syntax = options.syntax || "mermaid";
118
+ if (/^mermaid$/i.test(syntax) && String(input ?? "").length > 5e4) return {
119
+ info: null,
120
+ type: null,
121
+ model: null,
122
+ diagnostics: normalize([error(options.lang, 1, "Mermaid 源码最多 50000 个字符", "Mermaid source is limited to 50,000 characters", "请拆分图表", "Split the diagram")], null)
123
+ };
124
+ const keepsTrailingComments = (kind) => {
125
+ return implementations[byAlias.get(kind.toLowerCase())?.kind]?.trailingComments === false;
126
+ };
127
+ const prepared = prepareSource(input, {
128
+ syntax,
129
+ lang: options.lang,
130
+ keepsTrailingComments,
131
+ site: options[siteConfig]
132
+ });
133
+ const rawKind = String(prepared.kind || "").toLowerCase();
134
+ const type = byAlias.get(rawKind) || byKind.get(rawKind);
135
+ const implementation = type && implementations[type.kind];
136
+ const optionResult = implementation?.optionMode === "raw" ? {
137
+ options: prepared.options || Object.create(null),
138
+ errors: []
139
+ } : parseOptions(prepared.params, {
140
+ lang: options.lang,
141
+ line: prepared.headerLine || prepared.firstLine
142
+ });
143
+ const info = {
144
+ ...prepared,
145
+ ...optionResult.options ? { options: optionResult.options } : {},
146
+ kind: type?.kind || prepared.kind,
147
+ syntax,
148
+ lang: options.lang
149
+ };
150
+ const config = configWarning(info, implementation);
151
+ const sourceErrors = prepared.errors.filter((item) => item.code !== "IGNORED_CONFIG");
152
+ const presentation = implementation && implementation.acceptsPresentation !== true ? presentationErrors(info) : [];
153
+ const preparedDiagnostics = normalize(merge(sourceErrors, config ? [config] : [], presentation, optionResult.errors || []), type);
154
+ if (!type) {
155
+ if (preparedDiagnostics.some(blocking)) return {
156
+ info,
157
+ type: null,
158
+ model: null,
159
+ diagnostics: preparedDiagnostics
160
+ };
161
+ const shown = prepared.kind || prepared.body.trim().split(/\s+/)[0].slice(0, 40) || syntax;
162
+ const unknown = error(options.lang, headerLineOf(prepared), `未知结构图类型「${shown}」`, `Unknown diagram type "${shown}"`, "使用已声明的结构图类型", "Use a supported diagram type");
163
+ return {
164
+ info,
165
+ type: null,
166
+ model: null,
167
+ diagnostics: [...preparedDiagnostics, diagnostic(unknown)]
168
+ };
169
+ }
170
+ if (String(type.syntax).toLowerCase() !== String(syntax).toLowerCase()) {
171
+ const syntaxError = diagnostic(error(options.lang, headerLineOf(prepared), `图类型「${prepared.kind}」不支持语法「${syntax}」`, `Diagram type "${prepared.kind}" does not use ${syntax} syntax`, `使用 ${type.syntax} 语法`, `Use ${type.syntax} syntax`), type);
172
+ return {
173
+ info,
174
+ type,
175
+ model: null,
176
+ diagnostics: merge(preparedDiagnostics, [syntaxError])
177
+ };
178
+ }
179
+ if (preparedDiagnostics.some(blocking)) return {
180
+ info,
181
+ type,
182
+ model: null,
183
+ diagnostics: preparedDiagnostics
184
+ };
185
+ if (!implementation) {
186
+ const unloaded = error(options.lang, headerLineOf(prepared), `已知图类型「${type.kind}」尚未装入`, `Diagram type "${type.kind}" is not loaded`, "重新组装并装入该图类型", "Load this diagram type before rendering");
187
+ return {
188
+ info,
189
+ type,
190
+ model: null,
191
+ diagnostics: [...preparedDiagnostics, diagnostic(unloaded, type)]
192
+ };
193
+ }
194
+ if (typeof implementation.parse !== "function") throw new TypeError(`Missing parser for diagram: ${type.kind}`);
195
+ const parsed = implementation.parse(prepared.body, info);
196
+ if (!parsed || !Array.isArray(parsed.errors)) throw new TypeError(`Invalid parser result for diagram: ${type.kind}`);
197
+ const validation = !parsed.errors.some(blocking) && typeof implementation.validate === "function" ? implementation.validate(parsed.model, info) || [] : [];
198
+ if (!Array.isArray(validation)) throw new TypeError(`Invalid validation result for diagram: ${type.kind}`);
199
+ return {
200
+ info,
201
+ type,
202
+ model: parsed.model,
203
+ diagnostics: merge(preparedDiagnostics, normalize(parsed.errors, type), normalize(validation, type))
204
+ };
205
+ }
206
+ function parse(input, options = {}) {
207
+ try {
208
+ return parseUnchecked(input, options);
209
+ } catch (error) {
210
+ if (!(error instanceof ExpectedDiagnostic)) throw error;
211
+ return {
212
+ info: null,
213
+ type: null,
214
+ model: null,
215
+ diagnostics: normalize(error.errors, null)
216
+ };
217
+ }
218
+ }
219
+ function check(input, options = {}) {
220
+ return parse(input, options).diagnostics;
221
+ }
222
+ function diagram(input, options = {}) {
223
+ const parsed = parse(input, options);
224
+ if (parsed.diagnostics.some(blocking)) return {
225
+ svg: null,
226
+ scene: null,
227
+ diagnostics: parsed.diagnostics
228
+ };
229
+ try {
230
+ const implementation = implementations[parsed.type.kind];
231
+ if (typeof implementation.layout !== "function") throw new TypeError(`Missing layout for diagram: ${parsed.type.kind}`);
232
+ const width = Math.max(1, Number(options.width) || 800);
233
+ const ctx = createContext({
234
+ ...options,
235
+ width,
236
+ lang: parsed.info.lang
237
+ });
238
+ const measure = measureOf(ctx);
239
+ const renderOptions = {
240
+ ...options,
241
+ frame: options.frame ?? true
242
+ };
243
+ const geometry = implementation.layout(parsed.model, {
244
+ width,
245
+ lang: ctx.lang,
246
+ measure,
247
+ ctx,
248
+ renderOptions
249
+ }, parsed.info);
250
+ const diagnostics = merge(parsed.diagnostics, normalize(geometry?.errors, parsed.type));
251
+ if (diagnostics.some(blocking)) return {
252
+ svg: null,
253
+ scene: null,
254
+ diagnostics
255
+ };
256
+ const laid = geometry?.scene || (typeof implementation.scene === "function" ? implementation.scene(geometry, ctx, parsed.info) : null);
257
+ if (!laid || !Number.isFinite(laid.width) || !Number.isFinite(laid.height) || !Array.isArray(laid.children)) throw new TypeError(`Invalid scene for diagram: ${parsed.type.kind}`);
258
+ const contained = containText(laid, ctx.measure, { blockPadding: 4 });
259
+ const sceneWidth = Math.max(width, contained.width);
260
+ const offset = Math.max(0, (sceneWidth - contained.width) / 2);
261
+ const children = offset ? [el("g", { transform: `translate(${offset} 0)` }, contained.children)] : contained.children;
262
+ const anchor = contained.anchor ? {
263
+ ...contained.anchor,
264
+ x: contained.anchor.x + offset
265
+ } : contained.anchor;
266
+ const title = parsed.info.accTitle || parsed.info.title || contained.title || parsed.type.kind;
267
+ const scene = {
268
+ ...contained,
269
+ width: sceneWidth,
270
+ children,
271
+ ...anchor ? { anchor } : {},
272
+ title,
273
+ ...parsed.info.accDescr !== void 0 ? { description: parsed.info.accDescr } : {},
274
+ heading: parsed.info.title || contained.heading
275
+ };
276
+ return {
277
+ svg: renderScene(scene, renderOptions),
278
+ scene,
279
+ diagnostics
280
+ };
281
+ } catch (error) {
282
+ if (!(error instanceof ExpectedDiagnostic)) throw error;
283
+ return {
284
+ svg: null,
285
+ scene: null,
286
+ diagnostics: merge(parsed.diagnostics, normalize(error.errors, parsed.type))
287
+ };
288
+ }
289
+ }
290
+ return Object.freeze({
291
+ parse,
292
+ check,
293
+ diagram
294
+ });
295
+ }
296
+ export { createDiagramApi, registry_exports };