@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,1158 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { FONT, appearance, error, warning } from "./support.js";
3
+ import { presentation } from "./presentation.js";
4
+ import { normalizeLabel } from "./label.js";
5
+ import { resolveIconRef } from "../core/icons.js";
6
+ import { layoutGraph } from "./graph-layout.js";
7
+ import { cubicPath } from "../core/geometry.js";
8
+ // src/diagram/class.js
9
+ var class_exports = /* @__PURE__ */ __exportAll({
10
+ arrowMarkers: () => arrowMarkers,
11
+ directions: () => directions,
12
+ finishCards: () => finishCards,
13
+ implementation: () => implementation,
14
+ implementations: () => implementations,
15
+ kind: () => kind,
16
+ kinds: () => kinds,
17
+ layoutCards: () => layoutCards,
18
+ limits: () => limits,
19
+ markers: () => markers,
20
+ parse: () => parseClass,
21
+ parseClass: () => parseClass
22
+ });
23
+ var kind = Object.freeze({
24
+ kind: "classDiagram",
25
+ syntax: "mermaid",
26
+ topic: "data-model",
27
+ aliases: Object.freeze(["classDiagram-v2"])
28
+ });
29
+ var kinds = Object.freeze([kind]);
30
+ var limits = Object.freeze({
31
+ cards: 2e3,
32
+ edges: 5e3,
33
+ large: 60
34
+ });
35
+ var directions = Object.freeze([
36
+ "TB",
37
+ "BT",
38
+ "RL",
39
+ "LR"
40
+ ]);
41
+ var markers = Object.freeze([
42
+ "triangle",
43
+ "filledDiamond",
44
+ "hollowDiamond",
45
+ "arrow",
46
+ "circle",
47
+ "cross"
48
+ ]);
49
+ var arrowMarkers = Object.freeze({
50
+ "<|": "triangle",
51
+ "|>": "triangle",
52
+ "*": "filledDiamond",
53
+ o: "hollowDiamond",
54
+ ">": "arrow",
55
+ "<": "arrow",
56
+ "()": "circle"
57
+ });
58
+ var VISIBILITY = "#+~-";
59
+ var issue = (lang, line, zh, en, fixZh, fixEn, column = 1) => error(lang, line, zh, en, fixZh, fixEn, column);
60
+ var unknownStatement = (lang, line, text) => issue(lang, line, `不支持或无效的类图语句:「${text}」`, `Unsupported or invalid class-diagram statement: "${text}"`, "使用 class、namespace、note、direction 或「A <|-- B : 标签」", "Use class, namespace, note, direction, or \"A <|-- B : label\"");
61
+ var unquote = (value) => {
62
+ const text = String(value ?? "").trim();
63
+ const quoted = /^"([^"]*)"$/u.exec(text);
64
+ if (quoted) return quoted[1];
65
+ return /^`[^`]*`$/u.test(text) ? text.slice(1, -1) : text;
66
+ };
67
+ var ATOM = String.raw`(?:\u0060[^\u0060]*\u0060|~[^~\s]*~|[\p{L}\p{N}\p{M}_$]|[-.](?![-.]))`;
68
+ var NAME = `${ATOM}+`;
69
+ var END = String.raw`(?:<\|?|\|>|\(\s*\)|o|\*|>|<)`;
70
+ var NAME_HEAD = new RegExp(`^(?:${ATOM})+`, "u");
71
+ var RELATION = new RegExp(`^(${NAME})\\s*(?:"([^"]*)")?\\s*((?:${END})?(?:--|\\.\\.)(?:${END})?)\\s*(?:"([^"]*)")?\\s*(${NAME})(?:\\s*:\\s*(.+))?$`, "u");
72
+ var ARROW = new RegExp(`^(?:(${END}))?(--|\\.\\.)(?:(${END}))?$`, "u");
73
+ var NOTE_FOR = new RegExp(`^note\\s+for\\s+(${NAME})\\s+"([^"]*)"$`, "iu");
74
+ var NOTE = /^note\s+"([^"]*)"$/iu;
75
+ var CLASS_DECL = /^class\s+(.+)$/iu;
76
+ var NAMESPACE_DECL = /^namespace\s+(.+)$/iu;
77
+ var ANNOTATION_LINE = new RegExp(`^<<\\s*([^<>]+?)\\s*>>\\s+(${NAME})$`, "u");
78
+ var ANNOTATION_HEAD = /^<<\s*([^<>]+?)\s*>>/u;
79
+ var COLON_MEMBER = new RegExp(`^(${NAME})\\s*:\\s*([\\s\\S]+)$`, "u");
80
+ var CLASS_ONLY = new RegExp(`^(${NAME})\\s*:::\\s*([\\w-]+(?:\\s*,\\s*[\\w-]+)*)$`, "u");
81
+ var BARE_NAME = new RegExp(`^(${NAME})$`, "u");
82
+ var LABEL_SUFFIX = /^\[\s*"([^"]*)"\s*\]/u;
83
+ var STYLE_SUFFIX = /^:::\s*([\w-]+(?:\s*,\s*[\w-]+)*)/u;
84
+ var DIRECTION = new RegExp(`\\bdirection\\s+(${directions.join("|")})\\b`, "iu");
85
+ var ICON_SUFFIX = /^::icon\s*\(\s*([^()]+?)\s*\)/u;
86
+ var ACCESSIBILITY = /^acc(?:Title|Descr)\b/iu;
87
+ var METHOD = /^([#+~-])?(.+)\((.*)\)([\s$*])?(.*)([$*])?$/u;
88
+ function stripComment(raw) {
89
+ let quote = "", backtick = false;
90
+ for (let i = 0; i < raw.length; i += 1) {
91
+ const ch = raw[i];
92
+ if (backtick) {
93
+ if (ch === "`") backtick = false;
94
+ continue;
95
+ }
96
+ if (ch === "`") {
97
+ backtick = true;
98
+ continue;
99
+ }
100
+ if (quote) {
101
+ if (ch === quote) quote = "";
102
+ continue;
103
+ }
104
+ if (ch === "\"" || ch === "'") {
105
+ quote = ch;
106
+ continue;
107
+ }
108
+ if (ch === "%" && raw[i + 1] === "%") return raw.slice(0, i);
109
+ }
110
+ return raw;
111
+ }
112
+ function indexOutside(raw, wanted) {
113
+ let quote = "", backtick = false;
114
+ for (let i = 0; i < raw.length; i += 1) {
115
+ const ch = raw[i];
116
+ if (backtick) {
117
+ if (ch === "`") backtick = false;
118
+ continue;
119
+ }
120
+ if (ch === "`") {
121
+ backtick = true;
122
+ continue;
123
+ }
124
+ if (quote) {
125
+ if (ch === quote) quote = "";
126
+ continue;
127
+ }
128
+ if (ch === "\"" || ch === "'") {
129
+ quote = ch;
130
+ continue;
131
+ }
132
+ if (wanted === "arrow" ? (ch === "-" || ch === ".") && raw[i + 1] === ch : ch === wanted) return i;
133
+ }
134
+ return -1;
135
+ }
136
+ function statementsOf(input, firstLine) {
137
+ if (Array.isArray(input)) return input.filter((item) => item && typeof item.text === "string").map((item) => ({
138
+ text: item.text,
139
+ line: Number.isFinite(item.line) ? item.line : firstLine
140
+ }));
141
+ return String(input ?? "").replace(/\r\n?/gu, "\n").split("\n").map((text, index) => ({
142
+ text,
143
+ line: firstLine + index
144
+ }));
145
+ }
146
+ function takeName(text, start = 0) {
147
+ const match = NAME_HEAD.exec(text.slice(start));
148
+ return match ? {
149
+ raw: match[0],
150
+ next: start + match[0].length
151
+ } : null;
152
+ }
153
+ function nameParts(raw) {
154
+ const parts = (String(raw).startsWith("`") ? String(raw).slice(1, -1) : String(raw)).split("~");
155
+ return {
156
+ id: parts[0],
157
+ generic: parts.length > 1 ? parts[1] : ""
158
+ };
159
+ }
160
+ var tildeCount = (text) => Math.max(0, String(text).split("~").length - 1);
161
+ function processSet(input) {
162
+ const count = tildeCount(input);
163
+ if (count <= 1) return input;
164
+ let text = input, prefixed = false;
165
+ if (count % 2 !== 0 && text.startsWith("~")) {
166
+ text = text.slice(1);
167
+ prefixed = true;
168
+ }
169
+ const chars = [...text];
170
+ let first = chars.indexOf("~"), last = chars.lastIndexOf("~");
171
+ while (first !== -1 && last !== -1 && first !== last) {
172
+ chars[first] = "&lt;";
173
+ chars[last] = "&gt;";
174
+ first = chars.indexOf("~");
175
+ last = chars.lastIndexOf("~");
176
+ }
177
+ if (prefixed) chars.unshift("~");
178
+ return chars.join("");
179
+ }
180
+ function parseGenericTypes(input) {
181
+ const sets = String(input ?? "").split(/(,)/u), output = [];
182
+ for (let i = 0; i < sets.length; i += 1) {
183
+ let set = sets[i];
184
+ if (set === "," && i > 0 && i + 1 < sets.length && tildeCount(sets[i - 1]) === 1 && tildeCount(sets[i + 1]) === 1) {
185
+ set = `${sets[i - 1]},${sets[i + 1]}`;
186
+ i += 1;
187
+ output.pop();
188
+ }
189
+ output.push(processSet(set));
190
+ }
191
+ return output.join("");
192
+ }
193
+ function decorate(row, classifier) {
194
+ if (classifier === "*") row.fontStyle = "italic";
195
+ else if (classifier === "$") row.decoration = "underline";
196
+ return row;
197
+ }
198
+ function parseMember(input) {
199
+ const text = String(input).trim();
200
+ const method = text.indexOf(")") > 0 ? METHOD.exec(text) : null;
201
+ if (method) {
202
+ const visibility = method[1] || "";
203
+ const id = String(method[2]).trim();
204
+ const parameters = String(method[3] || "").trim();
205
+ let classifier = String(method[4] || "").trim();
206
+ let returnType = String(method[5] || "").trim();
207
+ if (!classifier && /[$*]$/u.test(returnType)) {
208
+ classifier = returnType.slice(-1);
209
+ returnType = returnType.slice(0, -1);
210
+ }
211
+ const head = `${visibility}${parseGenericTypes(id)}(${parseGenericTypes(parameters)})`;
212
+ return {
213
+ method: true,
214
+ item: decorate({ text: (returnType ? `${head} : ${parseGenericTypes(returnType)}` : head).trim() }, classifier)
215
+ };
216
+ }
217
+ const length = text.length;
218
+ const visibility = VISIBILITY.includes(text[0] || "") ? text[0] : "";
219
+ const classifier = /[$*]$/u.test(text) ? text.slice(-1) : "";
220
+ let id = text.slice(visibility ? 1 : 0, classifier ? length - 1 : length);
221
+ id = id.startsWith(" ") ? ` ${id.trim()}` : id.trim();
222
+ return {
223
+ method: false,
224
+ item: decorate({ text: `${visibility}${parseGenericTypes(id)}`.trim() }, classifier)
225
+ };
226
+ }
227
+ function configKey(object, key) {
228
+ if (!object || typeof object !== "object" || Array.isArray(object)) return void 0;
229
+ const wanted = key.toLowerCase();
230
+ for (const [name, value] of Object.entries(object)) if (name.toLowerCase() === wanted) return value;
231
+ }
232
+ function configFlag(config, key, fallback) {
233
+ const value = configKey(configKey(config, "class"), key);
234
+ if (typeof value === "boolean") return value;
235
+ if (typeof value === "string") {
236
+ const text = value.trim().toLowerCase();
237
+ if (text === "true") return true;
238
+ if (text === "false") return false;
239
+ }
240
+ return fallback;
241
+ }
242
+ /**
243
+ * `parseClass(statements, info)` follows the registry contract: `statements` are the prepared body as
244
+ * `{ text, line }` pairs (a raw string body is accepted too) and `info` carries the language, the first body
245
+ * line, the header options and — once the shared config module lands — `info.config`.
246
+ * Returns `{ errors, warnings, model }`; warnings carry `severity: 'warning'` and never block rendering.
247
+ */
248
+ function parseClass(statements, info = {}) {
249
+ const lang = info.lang || "zh";
250
+ const firstLine = info.firstLine || 2;
251
+ const errors = [], warnings = [];
252
+ for (const key of Object.keys(info.options || {})) errors.push(issue(lang, firstLine - 1, `类图不接受选项「${key}」`, `Class diagrams take no option: ${key}`, "把方向写成正文的 direction TB|BT|RL|LR", "Write the direction as a body statement: direction TB|BT|RL|LR"));
253
+ const hierarchical = configFlag(info.config, "hierarchicalNamespaces", true);
254
+ const hideEmptyMembersBox = configFlag(info.config, "hideEmptyMembersBox", false);
255
+ const cards = [], byId = /* @__PURE__ */ new Map(), namespaces = /* @__PURE__ */ new Map(), edges = [], pendingNotes = [];
256
+ const look = presentation({
257
+ lang,
258
+ classKeyword: "cssClass"
259
+ });
260
+ const stack = [];
261
+ let direction = "TB", open = null, accessibilityBlock = false, interfaceCount = 0, noteCount = 0;
262
+ const create = (id, line) => {
263
+ const card = {
264
+ id,
265
+ label: id,
266
+ generic: "",
267
+ annotations: [],
268
+ members: [],
269
+ methods: [],
270
+ parent: null,
271
+ declared: false,
272
+ note: false,
273
+ hidden: false,
274
+ interface: false,
275
+ line
276
+ };
277
+ byId.set(id, card);
278
+ cards.push(card);
279
+ return card;
280
+ };
281
+ const reference = (name, line) => {
282
+ const card = byId.get(name.id) || create(name.id, line);
283
+ if (name.generic && !card.generic) card.generic = name.generic;
284
+ return card;
285
+ };
286
+ const declare = (name, line) => {
287
+ const card = reference(name, line);
288
+ card.declared = true;
289
+ if (card.interface) {
290
+ card.interface = false;
291
+ card.hidden = false;
292
+ }
293
+ if (stack.length) card.parent = stack.at(-1);
294
+ return card;
295
+ };
296
+ const addMember = (card, text) => {
297
+ const member = String(text).trim();
298
+ if (!member || !card) return;
299
+ if (member.startsWith("<<") && member.endsWith(">>")) {
300
+ card.annotations.push({ text: member.slice(2, -2).trim() });
301
+ return;
302
+ }
303
+ const parsed = parseMember(member);
304
+ (parsed.method ? card.methods : card.members).push(parsed.item);
305
+ };
306
+ const addNamespace = (name, label, line) => {
307
+ const qualified = stack.length ? `${stack.at(-1)}.${name}` : name;
308
+ stack.push(qualified);
309
+ const existing = namespaces.get(qualified);
310
+ if (existing) {
311
+ existing.explicit = true;
312
+ if (label) existing.label = label;
313
+ return;
314
+ }
315
+ const parts = qualified.split(".");
316
+ for (let i = 1; i <= parts.length; i += 1) {
317
+ const id = parts.slice(0, i).join(".");
318
+ const leaf = i === parts.length;
319
+ const parent = i > 1 ? parts.slice(0, i - 1).join(".") : null;
320
+ const known = namespaces.get(id);
321
+ if (!known) namespaces.set(id, {
322
+ id,
323
+ label: leaf && label ? label : parts[i - 1],
324
+ parent,
325
+ depth: i - 1,
326
+ explicit: leaf,
327
+ line
328
+ });
329
+ else if (leaf) {
330
+ known.explicit = true;
331
+ if (label) known.label = label;
332
+ }
333
+ }
334
+ };
335
+ const explicitAncestor = (id) => {
336
+ let current = id;
337
+ while (current) {
338
+ const namespace = namespaces.get(current);
339
+ if (!namespace) return null;
340
+ if (namespace.explicit) return current;
341
+ current = namespace.parent;
342
+ }
343
+ return null;
344
+ };
345
+ const addNote = (text, target, line) => {
346
+ while (byId.has(`note${noteCount}`)) noteCount += 1;
347
+ const card = create(`note${noteCount}`, line);
348
+ noteCount += 1;
349
+ Object.assign(card, {
350
+ note: true,
351
+ label: text,
352
+ parent: stack.at(-1) || null
353
+ });
354
+ if (target) pendingNotes.push({
355
+ card,
356
+ target,
357
+ line
358
+ });
359
+ return card;
360
+ };
361
+ const interfaceCard = (name, line) => {
362
+ if (!byId.get(name)) {
363
+ const card = create(name, line);
364
+ Object.assign(card, {
365
+ interface: true,
366
+ hidden: true,
367
+ parent: stack.at(-1) || null
368
+ });
369
+ return card;
370
+ }
371
+ while (byId.has(`interface${interfaceCount}`)) interfaceCount += 1;
372
+ const card = create(`interface${interfaceCount}`, line);
373
+ interfaceCount += 1;
374
+ Object.assign(card, {
375
+ interface: true,
376
+ hidden: true,
377
+ label: name,
378
+ parent: stack.at(-1) || null
379
+ });
380
+ return card;
381
+ };
382
+ for (const row of statementsOf(statements, firstLine)) {
383
+ const line = row.line;
384
+ const text = stripComment(row.text).trim();
385
+ if (!text) continue;
386
+ if (accessibilityBlock) {
387
+ if (text.includes("}")) accessibilityBlock = false;
388
+ continue;
389
+ }
390
+ if (/^accDescr\s*\{/iu.test(text)) {
391
+ accessibilityBlock = !text.slice(text.indexOf("{") + 1).includes("}");
392
+ continue;
393
+ }
394
+ if (open) {
395
+ const card = open;
396
+ let body = text;
397
+ if (text.endsWith("}")) {
398
+ body = text.slice(0, -1);
399
+ open = null;
400
+ }
401
+ if (body.includes("{")) {
402
+ errors.push(issue(lang, line, "类成员里不能再写 {", "A class body has no nested {", "把成员写在 class 块内的独立行上", "Write members on their own lines inside the class block"));
403
+ continue;
404
+ }
405
+ addMember(card, body);
406
+ continue;
407
+ }
408
+ const heading = DIRECTION.exec(text);
409
+ if (heading) {
410
+ direction = heading[1].toUpperCase();
411
+ continue;
412
+ }
413
+ if (text === "}") {
414
+ if (stack.length) stack.pop();
415
+ else errors.push(issue(lang, line, "多余的 }", "Unmatched }", "删掉这一行,或补上它对应的 namespace", "Remove this line, or add the namespace it closes"));
416
+ continue;
417
+ }
418
+ const namespace = NAMESPACE_DECL.exec(text);
419
+ if (namespace) {
420
+ let rest = namespace[1].trim();
421
+ const token = takeName(rest);
422
+ if (!token) {
423
+ errors.push(issue(lang, line, "namespace 后需要命名空间名称", "A namespace needs a name", "写成 namespace 名称 {", "Write namespace name {"));
424
+ continue;
425
+ }
426
+ const name = nameParts(token.raw).id;
427
+ rest = rest.slice(token.next).trim();
428
+ const label = LABEL_SUFFIX.exec(rest);
429
+ let display = null;
430
+ if (label) {
431
+ display = label[1];
432
+ rest = rest.slice(label[0].length).trim();
433
+ }
434
+ if (rest !== "{") {
435
+ errors.push(issue(lang, line, "命名空间后需要 {,块内容从下一行开始", "A namespace opens with { and its body starts on the next line", "写成 namespace 名称 { 再逐行写 class", "Write namespace name { and put each class on its own line"));
436
+ continue;
437
+ }
438
+ addNamespace(name, display, line);
439
+ continue;
440
+ }
441
+ const declaration = CLASS_DECL.exec(text);
442
+ if (declaration) {
443
+ let rest = declaration[1].trim();
444
+ const token = takeName(rest);
445
+ if (!token) {
446
+ errors.push(issue(lang, line, "class 后需要类名", "A class needs a name", "写成 class 类名、class 类名[\"标签\"] 或 class 类名 {", "Write class Name, class Name[\"label\"] or class Name {"));
447
+ continue;
448
+ }
449
+ const card = declare(nameParts(token.raw), line);
450
+ rest = rest.slice(token.next).trim();
451
+ const quoted = LABEL_SUFFIX.exec(rest);
452
+ if (quoted) {
453
+ card.label = quoted[1];
454
+ rest = rest.slice(quoted[0].length).trim();
455
+ } else {
456
+ const legacy = /^as\s+([\s\S]+)$/iu.exec(rest);
457
+ if (legacy) {
458
+ card.label = unquote(legacy[1]);
459
+ rest = "";
460
+ }
461
+ }
462
+ let body = null, bad = null;
463
+ for (;;) {
464
+ rest = rest.trim();
465
+ if (!rest) break;
466
+ const annotation = ANNOTATION_HEAD.exec(rest);
467
+ if (annotation) {
468
+ card.annotations.push({ text: annotation[1].trim() });
469
+ rest = rest.slice(annotation[0].length).trim();
470
+ continue;
471
+ }
472
+ const style = STYLE_SUFFIX.exec(rest);
473
+ if (style) {
474
+ look.addClasses(card.id, style[1].split(/\s*,\s*/u), line);
475
+ rest = rest.slice(style[0].length).trim();
476
+ continue;
477
+ }
478
+ if (rest.startsWith("{")) {
479
+ body = rest.slice(1);
480
+ break;
481
+ }
482
+ bad = rest;
483
+ break;
484
+ }
485
+ if (bad !== null) {
486
+ errors.push(unknownStatement(lang, line, text));
487
+ continue;
488
+ }
489
+ if (body === null) continue;
490
+ const inner = body.trim();
491
+ if (inner.endsWith("}")) {
492
+ const member = inner.slice(0, -1);
493
+ if (member.trim()) addMember(card, member);
494
+ } else {
495
+ if (inner) addMember(card, inner);
496
+ open = card;
497
+ }
498
+ continue;
499
+ }
500
+ const annotationLine = ANNOTATION_LINE.exec(text);
501
+ if (annotationLine) {
502
+ declare(nameParts(annotationLine[2]), line).annotations.push({ text: annotationLine[1].trim() });
503
+ continue;
504
+ }
505
+ const noteFor = NOTE_FOR.exec(text);
506
+ if (noteFor) {
507
+ addNote(noteFor[2], nameParts(noteFor[1]).id, line);
508
+ continue;
509
+ }
510
+ const note = NOTE.exec(text);
511
+ if (note) {
512
+ addNote(note[1], null, line);
513
+ continue;
514
+ }
515
+ if (/^note\b/iu.test(text)) {
516
+ errors.push(issue(lang, line, "note 的文本需要写在双引号里", "A note needs its text in double quotes", "写成 note \"文本\" 或 note for 类名 \"文本\"", "Write note \"text\" or note for Name \"text\""));
517
+ continue;
518
+ }
519
+ if (look.accept(text, line)) continue;
520
+ const iconAt = text.indexOf("::icon");
521
+ const iconSuffix = iconAt > 0 ? ICON_SUFFIX.exec(text.slice(iconAt)) : null;
522
+ if (iconSuffix && iconAt + iconSuffix[0].length === text.length && BARE_NAME.test(text.slice(0, iconAt).trimEnd())) {
523
+ declare(nameParts(text.slice(0, iconAt).trimEnd()), line).iconRef = iconSuffix[1];
524
+ continue;
525
+ }
526
+ if (ACCESSIBILITY.test(text)) continue;
527
+ const arrowAt = indexOutside(text, "arrow"), colonAt = indexOutside(text, ":");
528
+ if (arrowAt >= 0 && (colonAt < 0 || arrowAt < colonAt)) {
529
+ const relation = RELATION.exec(text);
530
+ const arrow = relation ? ARROW.exec(relation[3]) : null;
531
+ if (!arrow) {
532
+ errors.push(unknownStatement(lang, line, text));
533
+ continue;
534
+ }
535
+ const leftToken = arrow[1] ? String(arrow[1]).replace(/\s+/gu, "") : "";
536
+ const rightToken = arrow[3] ? String(arrow[3]).replace(/\s+/gu, "") : "";
537
+ const start = leftToken ? arrowMarkers[leftToken] : void 0;
538
+ const end = rightToken ? arrowMarkers[rightToken] : void 0;
539
+ if (leftToken && !start || rightToken && !end) {
540
+ errors.push(unknownStatement(lang, line, text));
541
+ continue;
542
+ }
543
+ const from = nameParts(relation[1]), to = nameParts(relation[5]);
544
+ const fromCard = start === "circle" && !end ? interfaceCard(from.id, line) : reference(from, line);
545
+ const toCard = end === "circle" && !start ? interfaceCard(to.id, line) : reference(to, line);
546
+ edges.push({
547
+ from: fromCard.id,
548
+ to: toCard.id,
549
+ label: relation[6] ? relation[6].trim() : "",
550
+ ...arrow[2] === ".." ? { dashed: true } : { dashed: false },
551
+ ...start ? { start } : {},
552
+ ...end ? { end } : {},
553
+ ...relation[2] ? { c1: relation[2].trim() } : {},
554
+ ...relation[4] ? { c2: relation[4].trim() } : {},
555
+ line
556
+ });
557
+ continue;
558
+ }
559
+ const classOnly = CLASS_ONLY.exec(text);
560
+ if (classOnly) {
561
+ look.addClasses(declare(nameParts(classOnly[1]), line).id, classOnly[2].split(/\s*,\s*/u), line);
562
+ continue;
563
+ }
564
+ const colonMember = COLON_MEMBER.exec(text);
565
+ if (colonMember) {
566
+ addMember(reference(nameParts(colonMember[1]), line), colonMember[2]);
567
+ continue;
568
+ }
569
+ const bare = BARE_NAME.exec(text);
570
+ if (bare) {
571
+ declare(nameParts(bare[1]), line);
572
+ continue;
573
+ }
574
+ errors.push(unknownStatement(lang, line, text));
575
+ }
576
+ if (open) errors.push(issue(lang, open.line, "class 块缺少结束的 }", "A class block is missing its closing }", "在块末尾补上 }", "Add } at the end of the block"));
577
+ if (stack.length) errors.push(issue(lang, namespaces.get(stack.at(-1))?.line ?? firstLine, "namespace 块缺少结束的 }", "A namespace block is missing its closing }", "在块末尾补上 }", "Add } at the end of the block"));
578
+ for (const entry of pendingNotes) {
579
+ const target = byId.get(entry.target);
580
+ if (!target) {
581
+ warnings.push(warning("DIAGRAM_HINT", lang, entry.line, `note for 指向的「${entry.target}」不是任何类,这张注释没有连线`, `"${entry.target}" is not a class, so this note stays unconnected`, "把 note for 后面的类名改成图中的类", "Point note for at a class in the diagram"));
582
+ continue;
583
+ }
584
+ edges.push({
585
+ from: entry.card.id,
586
+ to: target.id,
587
+ label: "",
588
+ dashed: true,
589
+ line: entry.line
590
+ });
591
+ }
592
+ edges.sort((a, b) => a.line - b.line);
593
+ if (cards.length > limits.large) warnings.push(warning("LARGE_DIAGRAM", lang, cards[0]?.line ?? firstLine, `${cards.length} 个类或注释,图会很大,建议拆分`, `${cards.length} classes and notes; consider splitting this diagram`, "按命名空间拆成多张图,并用文字说明它们的对应关系", "Split by namespace and describe the mapping in prose"));
594
+ if (cards.length > limits.cards) errors.push(issue(lang, cards[limits.cards].line, `类与注释最多 ${limits.cards} 个`, `At most ${limits.cards} classes and notes`, "拆成多张图", "Split into several diagrams"));
595
+ if (edges.length > limits.edges) errors.push(issue(lang, edges[limits.edges].line, `关系最多 ${limits.edges} 条`, `At most ${limits.edges} relations`, "删掉次要关系,或拆成多张图", "Remove secondary relations, or split into several diagrams"));
596
+ const sectionsOf = (card) => {
597
+ if (card.note || card.hidden) return [];
598
+ if (hideEmptyMembersBox && !card.members.length && !card.methods.length) return [];
599
+ return [card.members, card.methods];
600
+ };
601
+ const titleOf = (card) => {
602
+ const generic = card.generic ? parseGenericTypes(card.generic) : "";
603
+ return generic ? `${card.label}&lt;${generic}&gt;` : card.label;
604
+ };
605
+ const frames = [];
606
+ for (const namespace of namespaces.values()) {
607
+ if (!hierarchical && !namespace.explicit) continue;
608
+ frames.push({
609
+ id: namespace.id,
610
+ label: hierarchical ? namespace.label : namespace.id,
611
+ parent: hierarchical ? namespace.parent : null,
612
+ depth: hierarchical ? namespace.depth : 0,
613
+ line: namespace.line
614
+ });
615
+ }
616
+ const model = {
617
+ kind: "classDiagram",
618
+ direction,
619
+ cards: cards.map((card) => {
620
+ const parent = card.parent ? hierarchical ? card.parent : explicitAncestor(card.parent) : null;
621
+ return {
622
+ id: card.id,
623
+ title: card.note ? card.label : titleOf(card),
624
+ ...card.annotations.length ? { annotations: card.annotations.map((entry) => ({ text: entry.text })) } : {},
625
+ sections: sectionsOf(card),
626
+ ...parent ? { parent } : {},
627
+ ...card.note ? { note: true } : {},
628
+ ...card.hidden ? { hidden: true } : {},
629
+ ...card.iconRef ? { iconRef: card.iconRef } : {},
630
+ line: card.line
631
+ };
632
+ }),
633
+ frames,
634
+ edges
635
+ };
636
+ errors.push(...finishCards(model, look, lang));
637
+ return {
638
+ errors: [...errors, ...warnings],
639
+ warnings: [...warnings, ...errors.filter((item) => item.severity === "warning")],
640
+ model
641
+ };
642
+ }
643
+ function finishCards(model, look, lang) {
644
+ const issues = /* @__PURE__ */ new Map();
645
+ const remember = (issue) => {
646
+ if ((issues.get(issue.message)?.line ?? Infinity) > issue.line) issues.set(issue.message, issue);
647
+ };
648
+ const normalize = (item, key, owner = item) => {
649
+ if (item[key] == null) return;
650
+ const { text, icon, diagnostics } = normalizeLabel(item[key], {
651
+ lang,
652
+ line: owner.line
653
+ });
654
+ item[key] = text;
655
+ if (icon) owner.icon ??= icon;
656
+ diagnostics.forEach(remember);
657
+ };
658
+ for (const card of model.cards) {
659
+ const tone = look.statusOf(card.id), link = look.linkOf(card.id);
660
+ if (tone) card.tone = tone;
661
+ if (link) card.link = link;
662
+ if (card.iconRef) {
663
+ const ref = card.iconRef;
664
+ const icon = resolveIconRef(ref);
665
+ if (icon) card.icon = icon;
666
+ else remember(warning("IGNORED_ASSET", lang, card.line, `未知图标 ${ref}`, `Unknown icon ${ref}`, "使用内置图标", "Use a built-in icon"));
667
+ delete card.iconRef;
668
+ }
669
+ normalize(card, "title");
670
+ for (const row of [...card.annotations || [], ...card.sections.flat()]) normalize(row, "text", card);
671
+ }
672
+ for (const frame of model.frames || []) normalize(frame, "label");
673
+ for (const [index, edge] of model.edges.entries()) {
674
+ const tone = look.edgeStatusOf(index);
675
+ if (tone) edge.tone = tone;
676
+ for (const key of [
677
+ "label",
678
+ "c1",
679
+ "c2"
680
+ ]) normalize(edge, key);
681
+ }
682
+ return [...look.diagnostics(), ...issues.values()];
683
+ }
684
+ var overlaps = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
685
+ var iconSlot = "";
686
+ var withIconSlot = (text, icon) => icon ? `${iconSlot} ${text}` : text;
687
+ var markerId = (name, tone) => `uml-${name}${tone ? `-${tone}` : ""}`;
688
+ function drawIcon(ctx, icon, x, y, ink) {
689
+ const [left, top, w, h] = icon.viewBox.split(/\s+/).map(Number);
690
+ const scale = 14 / Math.max(w, h);
691
+ 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, {
692
+ ...attrs,
693
+ ...icon.set === "simple" ? { fill: ink } : {
694
+ fill: "none",
695
+ stroke: ink,
696
+ "stroke-width": 1.6,
697
+ "stroke-linecap": "round",
698
+ "stroke-linejoin": "round"
699
+ }
700
+ })));
701
+ }
702
+ function cardMarkers(ctx, a, edges) {
703
+ const surface = a.surface;
704
+ const shapes = {
705
+ triangle: ["M2 2 L13 7 L2 12 Z", surface],
706
+ hollowDiamond: ["M1 7 L7 2 L13 7 L7 12 Z", surface],
707
+ filledDiamond: ["M1 7 L7 2 L13 7 L7 12 Z", null],
708
+ arrow: ["M4 3 L13 7 L4 11", "none"],
709
+ cross: ["M5 3 L13 11 M5 11 L13 3", "none"]
710
+ };
711
+ const used = new Map(edges.flatMap((edge) => [edge.start, edge.end].filter(Boolean).map((name) => [markerId(name, edge.tone), {
712
+ name,
713
+ ink: a[edge.tone] || a.fg4
714
+ }])));
715
+ return ctx.el("defs", {}, [...used].map(([id, { name, ink }]) => {
716
+ const attrs = {
717
+ stroke: ink,
718
+ "stroke-width": 1.25
719
+ };
720
+ const children = name === "contains" ? [ctx.circle(8, 7, 4, {
721
+ ...attrs,
722
+ fill: surface
723
+ }), ctx.path("M4 7 H12 M8 3 V11", {
724
+ ...attrs,
725
+ fill: "none"
726
+ })] : name === "circle" ? [ctx.circle(7, 7, 5, {
727
+ ...attrs,
728
+ fill: surface
729
+ })] : [ctx.path(shapes[name][0], {
730
+ ...attrs,
731
+ fill: shapes[name][1] ?? ink
732
+ })];
733
+ return ctx.el("marker", {
734
+ id,
735
+ viewBox: "0 0 14 14",
736
+ refX: name === "circle" ? 7 : 13,
737
+ refY: 7,
738
+ markerWidth: 14,
739
+ markerHeight: 14,
740
+ markerUnits: "userSpaceOnUse",
741
+ orient: "auto-start-reverse"
742
+ }, children);
743
+ }));
744
+ }
745
+ function wrapAtSeparators(text, limit, measure, boundary) {
746
+ return text.split("\n").flatMap((paragraph) => {
747
+ const parts = paragraph.split(boundary);
748
+ const lines = [];
749
+ let row = parts.shift();
750
+ for (const part of parts) if (row && measure(row + part) > limit) {
751
+ lines.push(row.trimEnd());
752
+ row = part.trimStart();
753
+ } else row += part;
754
+ return [...lines, row];
755
+ });
756
+ }
757
+ function clearFrameBorders(graph) {
758
+ const clearance = 8;
759
+ const parentOf = new Map([...graph.nodes, ...graph.frames].map((item) => [item.id, item.parent]));
760
+ const contains = (frame, id) => {
761
+ for (let parent = parentOf.get(id); parent; parent = parentOf.get(parent)) if (parent === frame.id) return true;
762
+ return false;
763
+ };
764
+ for (const frame of graph.frames) {
765
+ const original = { ...frame };
766
+ for (const edge of graph.edges.filter((edge) => contains(frame, edge.from) || contains(frame, edge.to))) for (const { p0, p1, c1, c2 } of edge.segments) for (const [axis, cross, size, span] of [[
767
+ "x",
768
+ "y",
769
+ "w",
770
+ "h"
771
+ ], [
772
+ "y",
773
+ "x",
774
+ "h",
775
+ "w"
776
+ ]]) {
777
+ const track = p0[axis];
778
+ if (![
779
+ p1,
780
+ c1,
781
+ c2
782
+ ].every((p) => Math.abs(p[axis] - track) < .1)) continue;
783
+ if (Math.min(Math.max(p0[cross], p1[cross]), original[cross] + original[span]) - Math.max(Math.min(p0[cross], p1[cross]), original[cross]) < 16) continue;
784
+ const low = original[axis], high = low + original[size];
785
+ if (Math.abs(track - low) < clearance) {
786
+ const extra = Math.max(0, frame[axis] - track + clearance);
787
+ frame[axis] -= extra;
788
+ frame[size] += extra;
789
+ }
790
+ if (Math.abs(track - high) < clearance) frame[size] = Math.max(frame[size], track + clearance - frame[axis]);
791
+ }
792
+ }
793
+ }
794
+ function layoutCards(model, { ctx, width, lang, measure = (s, f) => ctx.measure(s, f.size, f.weight) }) {
795
+ const { cards, frames = [], edges, direction } = model;
796
+ const requirements = model.type === "requirementDiagram";
797
+ const a = appearance(ctx);
798
+ const cardById = new Map(cards.map((card) => [card.id, card]));
799
+ const rowsById = /* @__PURE__ */ new Map();
800
+ const frameById = new Map(frames.map((frame) => [frame.id, frame]));
801
+ const titleSlots = /* @__PURE__ */ new Map();
802
+ const frameHead = new Map(frames.map((frame, frameIndex) => {
803
+ const title = withIconSlot(frame.label, frame.icon);
804
+ const titleMeasure = (s) => s.startsWith(iconSlot) ? 18 + ctx.measure(s.slice(1), 13, 600) : ctx.measure(s, 13, 600);
805
+ const lines = wrapAtSeparators(title, 332, titleMeasure, /(?<=\.)/u);
806
+ const slots = lines.map((line, i) => {
807
+ if (!line) return "";
808
+ const token = `\ue000${frameIndex}.${i}\ue001`;
809
+ titleSlots.set(token, Math.min(199, titleMeasure(line)));
810
+ return token;
811
+ });
812
+ return [frame.id, {
813
+ lines,
814
+ slots,
815
+ w: Math.max(...lines.map(titleMeasure))
816
+ }];
817
+ }));
818
+ const frameMin = /* @__PURE__ */ new Map();
819
+ if (direction === "TB" || direction === "BT") {
820
+ const sized = /* @__PURE__ */ new Set();
821
+ for (const card of cards) if (!card.hidden) for (let id = card.parent; id && !sized.has(id); id = frameById.get(id)?.parent) {
822
+ sized.add(id);
823
+ const { w } = frameHead.get(id);
824
+ if (w > 199) frameMin.set(card.id, Math.max(frameMin.get(card.id) || 0, w + 2));
825
+ }
826
+ }
827
+ const nodes = cards.map((card, seq) => {
828
+ const wrap = (rows, members = false) => rows.flatMap((row) => (members ? wrapAtSeparators(row.text, 328, (s) => measure(s, FONT.label), /(?<=,)|(?<=[^\s:])(?=\s*:(?!:))/u) : ctx.wrap(row.text, 172, FONT.label.size, FONT.label.weight)).map((text) => ({
829
+ ...row,
830
+ text
831
+ })));
832
+ const [first, ...rest] = card.sections;
833
+ const typeBand = first?.[0]?.role === "stereotype";
834
+ const annotations = wrap(typeBand ? first : (card.annotations || []).map((row) => ({
835
+ ...row,
836
+ text: `«${row.text}»`
837
+ })));
838
+ const sections = (typeBand ? rest : card.sections).map((rows) => wrap(rows, !requirements));
839
+ const label = card.title || "\xA0";
840
+ const lines = ctx.wrap(label, 200, FONT.title.size, FONT.title.weight);
841
+ rowsById.set(card.id, {
842
+ annotations,
843
+ sections,
844
+ lines
845
+ });
846
+ const detail = [...annotations, ...sections.flat()];
847
+ const w = Math.max(64, frameMin.get(card.id) || 0, 28 + Math.max(0, ...lines.map((s) => measure(s, FONT.title)), ...detail.map((row) => measure(row.text, FONT.body))));
848
+ const rowCount = annotations.length + Number(Boolean(card.icon)) + sections.reduce((sum, rows) => sum + Math.max(1, rows.length), 0);
849
+ const h = Math.max(40, 20 + lines.length * 19 + (rowCount ? 5 + rowCount * 17 : 0));
850
+ return {
851
+ id: card.id,
852
+ seq,
853
+ label,
854
+ parent: card.parent,
855
+ ...card.hidden ? { kind: "start" } : { block: {
856
+ w,
857
+ h
858
+ } }
859
+ };
860
+ });
861
+ const graph = layoutGraph({
862
+ type: "flowchart",
863
+ direction,
864
+ nodes,
865
+ subgraphs: frames.map((frame, seq) => ({
866
+ ...frame,
867
+ seq,
868
+ title: frameHead.get(frame.id).slots.join("\n")
869
+ })),
870
+ edges: edges.map((edge, seq) => ({
871
+ ...edge,
872
+ seq,
873
+ label: withIconSlot(edge.label, edge.icon),
874
+ style: edge.dashed ? "dashed" : "solid",
875
+ startArrow: Boolean(edge.start),
876
+ endArrow: Boolean(edge.end)
877
+ }))
878
+ }, {
879
+ lang,
880
+ measure: (s, font) => titleSlots.get(s) ?? (s.startsWith(iconSlot) ? 18 + measure(s.slice(1), font) : measure(s, font))
881
+ });
882
+ for (const frame of graph.frames) {
883
+ const head = frameHead.get(frame.id), rect = frame.titleRect;
884
+ if (!head || !rect) continue;
885
+ frame.lines = head.lines;
886
+ rect.w = head.w;
887
+ frame.w = Math.max(frame.w, rect.x - frame.x + rect.w + 14);
888
+ }
889
+ clearFrameBorders(graph);
890
+ for (const frame of graph.frames) {
891
+ graph.width = Math.max(graph.width, frame.x + frame.w + 8);
892
+ graph.height = Math.max(graph.height, frame.y + frame.h + 8);
893
+ }
894
+ const text = (x, y, value, opts = {}) => ctx.text(x, y, value, {
895
+ "dominant-baseline": "central",
896
+ fill: a.fg3,
897
+ ...opts
898
+ });
899
+ const outlines = [], titles = [], paths = [], boxes = [], labels = [];
900
+ const obstacles = [
901
+ ...graph.nodes,
902
+ ...graph.frames.map((frame) => frame.titleRect).filter(Boolean),
903
+ ...graph.edges.map((edge) => edge.labelRect).filter(Boolean)
904
+ ];
905
+ const routeBounds = graph.edges.flatMap((edge) => edge.segments.map(({ p0, c1, c2, p1 }) => {
906
+ const xs = [
907
+ p0.x,
908
+ c1.x,
909
+ c2.x,
910
+ p1.x
911
+ ], ys = [
912
+ p0.y,
913
+ c1.y,
914
+ c2.y,
915
+ p1.y
916
+ ];
917
+ const x = Math.min(...xs), y = Math.min(...ys);
918
+ return {
919
+ x: x - 1,
920
+ y: y - 1,
921
+ w: Math.max(...xs) - x + 2,
922
+ h: Math.max(...ys) - y + 2
923
+ };
924
+ }));
925
+ const free = (box) => !obstacles.some((other) => overlaps(box, other)) && !routeBounds.some((other) => overlaps(box, other));
926
+ for (const f of graph.frames) {
927
+ outlines.push(ctx.rect(f.x, f.y, f.w, f.h, {
928
+ rx: 7,
929
+ fill: a.surface,
930
+ stroke: a.line2,
931
+ meta: {
932
+ kind: "frame",
933
+ id: f.id
934
+ }
935
+ }));
936
+ if (!f.titleRect) continue;
937
+ const r = f.titleRect;
938
+ titles.push(ctx.rect(r.x - 3, r.y, r.w + 6, r.h, {
939
+ rx: 3,
940
+ fill: a.surface
941
+ }));
942
+ f.lines.forEach((line, i) => {
943
+ const y = r.y + (i + .5) * 17, icon = frameById.get(f.id)?.icon;
944
+ if (icon && line.startsWith(iconSlot)) {
945
+ titles.push(drawIcon(ctx, icon, r.x + 7, y, a.fg3));
946
+ titles.push(text(r.x + 18, y, line.slice(1).trimStart(), {
947
+ size: 13,
948
+ weight: 600
949
+ }));
950
+ } else titles.push(text(r.x, y, line, {
951
+ size: 13,
952
+ weight: 600
953
+ }));
954
+ });
955
+ }
956
+ for (const route of graph.edges) {
957
+ if (!route.segments?.length) continue;
958
+ const edge = edges[route.id];
959
+ paths.push(ctx.path(cubicPath(route.segments), {
960
+ fill: "none",
961
+ stroke: a[edge.tone] || a.fg4,
962
+ "stroke-width": 1.25,
963
+ "stroke-dasharray": edge.dashed ? "5 4" : null,
964
+ "marker-start": edge.start ? `url(#${markerId(edge.start, edge.tone)})` : null,
965
+ "marker-end": edge.end ? `url(#${markerId(edge.end, edge.tone)})` : null,
966
+ meta: {
967
+ kind: "edge",
968
+ from: edge.from,
969
+ to: edge.to
970
+ }
971
+ }));
972
+ const r = route.labelRect;
973
+ if (r && route.labelLines.length) {
974
+ const children = [ctx.rect(r.x, r.y, r.w, r.h, {
975
+ rx: 4,
976
+ fill: a.surface,
977
+ stroke: a.line1,
978
+ "stroke-width": .6
979
+ })];
980
+ route.labelLines.forEach((line, i) => {
981
+ const x = r.x + r.w / 2, y = r.y + r.h / 2 + (i - (route.labelLines.length - 1) / 2) * 16;
982
+ if (edge.icon && line.startsWith(iconSlot)) {
983
+ const caption = line.slice(1).trimStart();
984
+ const left = x - (ctx.measure(caption, 12, 400) + 18) / 2;
985
+ children.push(drawIcon(ctx, edge.icon, left + 7, y, a.fg3));
986
+ if (caption) children.push(text(left + 18, y, caption, { size: 12 }));
987
+ } else children.push(text(x, y, line, {
988
+ size: 12,
989
+ anchor: "middle"
990
+ }));
991
+ });
992
+ labels.push({
993
+ ...ctx.el("g", {}, children),
994
+ meta: {
995
+ kind: "edge-label-group",
996
+ from: edge.from,
997
+ to: edge.to
998
+ }
999
+ });
1000
+ }
1001
+ for (const [value, start] of [[edge.c1, true], [edge.c2, false]]) {
1002
+ if (!value) continue;
1003
+ const segment = start ? route.segments[0] : route.segments.at(-1);
1004
+ const p = start ? segment.p0 : segment.p1;
1005
+ const q = start ? segment.c1 || segment.p1 : segment.c2 || segment.p0;
1006
+ const vertical = Math.abs(q.y - p.y) >= Math.abs(q.x - p.x);
1007
+ const sign = Math.sign(vertical ? q.y - p.y : q.x - p.x) || 1;
1008
+ const w = ctx.measure(value, 12, 400);
1009
+ let box;
1010
+ for (const distance of [
1011
+ 10,
1012
+ 18,
1013
+ 26,
1014
+ 36,
1015
+ 48
1016
+ ]) {
1017
+ for (const side of start ? [-1, 1] : [1, -1]) {
1018
+ const candidate = vertical ? {
1019
+ x: p.x + (side < 0 ? -8 - w : 8),
1020
+ y: p.y + sign * distance - 7,
1021
+ w,
1022
+ h: 14
1023
+ } : {
1024
+ x: p.x + (sign < 0 ? -distance - w : distance),
1025
+ y: p.y + side * 12 - 7,
1026
+ w,
1027
+ h: 14
1028
+ };
1029
+ box = candidate;
1030
+ if (free(candidate)) break;
1031
+ }
1032
+ if (free(box)) break;
1033
+ }
1034
+ obstacles.push(box);
1035
+ labels.push(text(box.x, box.y + 7, value, {
1036
+ size: 12,
1037
+ meta: {
1038
+ kind: "cardinality",
1039
+ endpoint: start ? "start" : "end"
1040
+ }
1041
+ }));
1042
+ }
1043
+ }
1044
+ for (const n of graph.nodes) {
1045
+ const card = cardById.get(n.id);
1046
+ if (card.hidden) continue;
1047
+ const { x, y, w, h } = n;
1048
+ const { annotations, sections, lines } = rowsById.get(n.id);
1049
+ const stroke = card.tone ? a[card.tone] : a.line2;
1050
+ const fill = card.tone && card.tone !== "ext" ? ctx.mix(a.node, stroke, a.soft) : a.node;
1051
+ const anchor = requirements || card.note ? "start" : "middle";
1052
+ const tx = anchor === "start" ? x + 14 : x + w / 2;
1053
+ const header = 20 + (annotations.length + Number(Boolean(card.icon))) * 17 + lines.length * 19;
1054
+ const items = [];
1055
+ items.push(ctx.rect(x, y, w, h, {
1056
+ rx: 7,
1057
+ fill,
1058
+ stroke,
1059
+ "stroke-width": 1.25,
1060
+ "stroke-dasharray": card.tone === "ext" ? "4 3" : void 0,
1061
+ meta: {
1062
+ kind: "node",
1063
+ id: card.id,
1064
+ ...card.tone ? { status: card.tone } : {}
1065
+ }
1066
+ }));
1067
+ if (sections.length) items.push(ctx.rect(x + 1, y + 1, w - 2, header - 1, {
1068
+ rx: 6,
1069
+ fill: card.tone && card.tone !== "ext" ? ctx.mix(a.node, stroke, Math.max(.2, a.soft)) : a.line1,
1070
+ "fill-opacity": card.tone && card.tone !== "ext" ? 1 : .3
1071
+ }));
1072
+ let cursor = y + 10;
1073
+ if (card.icon) {
1074
+ items.push(drawIcon(ctx, card.icon, tx + (anchor === "start" ? 7 : 0), cursor + 8.5, stroke));
1075
+ cursor += 17;
1076
+ }
1077
+ const drawRow = (row, px, baseline, opts) => items.push(text(px, baseline, row.text, {
1078
+ size: FONT.label.size,
1079
+ "font-style": row.fontStyle,
1080
+ "text-decoration": row.decoration,
1081
+ ...opts
1082
+ }));
1083
+ for (const row of annotations) {
1084
+ drawRow(row, tx, cursor + 8.5, { anchor });
1085
+ cursor += 17;
1086
+ }
1087
+ for (const line of lines) {
1088
+ items.push(text(tx, cursor + 9.5, line, {
1089
+ ...FONT.title,
1090
+ ...card.note ? { weight: 400 } : {},
1091
+ anchor,
1092
+ fill: a.fg1
1093
+ }));
1094
+ cursor += 19;
1095
+ }
1096
+ cursor += 10;
1097
+ for (const [i, rows] of sections.entries()) {
1098
+ items.push(ctx.line(x + (i ? 9 : 0), cursor, x + w - (i ? 9 : 0), cursor, {
1099
+ stroke: a.line2,
1100
+ "stroke-width": .7
1101
+ }));
1102
+ for (const [j, row] of rows.entries()) drawRow(row, x + 14, cursor + 11 + j * 17, {});
1103
+ cursor += Math.max(1, rows.length) * 17;
1104
+ }
1105
+ if (card.link) boxes.push(ctx.link(card.link.href, items, card.link));
1106
+ else boxes.push(...items);
1107
+ }
1108
+ for (const route of graph.edges) for (const [id, marker, p, q] of [[
1109
+ route.from,
1110
+ edges[route.id].start,
1111
+ route.start,
1112
+ route.end
1113
+ ], [
1114
+ route.to,
1115
+ edges[route.id].end,
1116
+ route.end,
1117
+ route.start
1118
+ ]]) {
1119
+ const card = cardById.get(id);
1120
+ if (marker !== "circle" || !card?.hidden || !p || !q) continue;
1121
+ const horizontal = direction === "LR" || direction === "RL";
1122
+ const sign = (horizontal ? p.x - q.x : p.y - q.y) >= 0 ? 1 : -1;
1123
+ labels.push(text(p.x + (horizontal ? sign * 11 : 0), p.y + (horizontal ? 0 : sign * 14), card.title, {
1124
+ size: 12,
1125
+ anchor: horizontal ? sign > 0 ? "start" : "end" : "middle"
1126
+ }));
1127
+ }
1128
+ const errors = graph.width > 2 * width && cards.length <= limits.large ? [warning("LARGE_DIAGRAM", lang, 1, "图较宽,将横向滚动", "This diagram is wide and will scroll horizontally", "考虑拆分成多张图", "Consider splitting it into several diagrams")] : [];
1129
+ const scene = {
1130
+ width: graph.width,
1131
+ height: graph.height,
1132
+ title: model.title || model.kind || model.type,
1133
+ heading: model.title,
1134
+ children: [
1135
+ cardMarkers(ctx, a, edges),
1136
+ ...outlines,
1137
+ ...paths,
1138
+ ...boxes,
1139
+ ...labels,
1140
+ ...titles
1141
+ ]
1142
+ };
1143
+ return {
1144
+ width: graph.width,
1145
+ height: graph.height,
1146
+ geometry: graph,
1147
+ scene,
1148
+ errors
1149
+ };
1150
+ }
1151
+ var implementation = {
1152
+ parse: parseClass,
1153
+ layout: layoutCards,
1154
+ acceptsPresentation: true,
1155
+ honoredConfig: ["class.hideEmptyMembersBox", "class.hierarchicalNamespaces"]
1156
+ };
1157
+ var implementations = { classDiagram: implementation };
1158
+ export { arrowMarkers, class_exports, directions, finishCards, implementation, implementations, kind, kinds, layoutCards, limits, markers, parseClass as parse, parseClass };