@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,729 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { resolveIconRef } from "../core/icons.js";
3
+ import { normalizeLabel } from "./label.js";
4
+ import { presentation } from "./presentation.js";
5
+ import { FONT, appearance, blocking, error, maxWidthOf, textBlock, warning, wrapText } from "./support.js";
6
+ // src/diagram/architecture.js
7
+ var architecture_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
8
+ var ICONS = new Map(Object.entries({
9
+ cloud: "cloud",
10
+ database: "database",
11
+ disk: "stack-2",
12
+ internet: "world",
13
+ server: "server",
14
+ browser: "browser"
15
+ }));
16
+ var DIR = {
17
+ L: [-1, 0],
18
+ R: [1, 0],
19
+ T: [0, -1],
20
+ B: [0, 1]
21
+ };
22
+ var ID = String.raw`\w(?:[-\w]*\w)?`;
23
+ var QUOTED = String.raw`"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'`;
24
+ var TITLE = String.raw`\[(${QUOTED}|[^\[\]\r\n]+)\]`;
25
+ var DECL = new RegExp(String.raw`^(group|service)\s+(${ID})(?:\s*\(([\w:-]+)\)|\s*(${QUOTED}|\((?:${QUOTED})\)))?(?:\s*${TITLE})?(?:\s+in\s+(${ID}))?$`);
26
+ var JUNCTION = new RegExp(String.raw`^junction\s+(${ID})(?:\s+in\s+(${ID}))?$`);
27
+ var EDGE = new RegExp(String.raw`^(${ID})\s*(\{group\})?\s*:\s*([LRTB])\s*(<)?\s*(?:--|-\s*${TITLE}\s*-)\s*(>)?\s*([LRTB])\s*:\s*(${ID})\s*(\{group\})?$`);
28
+ var ALIGN = new RegExp(String.raw`^align\s+(row|column)\s+(${ID}(?:\s+${ID})*)$`);
29
+ var COMMENT = new RegExp(String.raw`(${TITLE}|${QUOTED})|%%.*`, "g");
30
+ var unquote = (raw) => raw?.replace(/^("|')(.*)\1$/, "$2").replace(/\\(["'\\])/g, "$1") ?? "";
31
+ var SUFFIX = /(?:\s*:::(?<class>[\w-]+)|\s*::icon\((?<icon>[\w:-]+)\))$/;
32
+ function parse(source, info) {
33
+ const items = /* @__PURE__ */ new Map(), nodes = [], groups = [], edges = [], aligns = [], errors = [], seenAssets = /* @__PURE__ */ new Set();
34
+ const look = presentation({ lang: info.lang });
35
+ const asset = (diagnostic) => {
36
+ if (seenAssets.has(diagnostic.message)) return;
37
+ seenAssets.add(diagnostic.message);
38
+ errors.push(diagnostic);
39
+ };
40
+ const caption = (raw, line) => {
41
+ const result = normalizeLabel(unquote(raw), {
42
+ lang: info.lang,
43
+ line
44
+ });
45
+ result.diagnostics.forEach(asset);
46
+ return result;
47
+ };
48
+ const fail = (line, zh, en) => errors.push(error(info.lang, line, zh, en, "检查架构图声明与引用", "Check architecture declarations and references"));
49
+ if (source.length > 2e5) {
50
+ fail(info.firstLine, "架构图源文本过长", "Architecture source is too long");
51
+ return { errors };
52
+ }
53
+ let title;
54
+ for (const [i, raw] of source.split("\n").entries()) {
55
+ let text = raw.replace(COMMENT, (_, label) => label ?? "").trim();
56
+ if (!text) continue;
57
+ const line = info.firstLine + i;
58
+ if (look.accept(text, line)) continue;
59
+ const heading = /^title(?:\s+|:\s*)(.+)$/.exec(text);
60
+ if (heading) {
61
+ title = caption(heading[1], line).text;
62
+ continue;
63
+ }
64
+ const classes = [];
65
+ let suffixIcon, suffix;
66
+ if (/^(?:group|service|junction)\s/.test(text)) while (suffix = SUFFIX.exec(text)) {
67
+ if (suffix.groups.class) classes.unshift(suffix.groups.class);
68
+ else suffixIcon ??= suffix.groups.icon;
69
+ text = text.slice(0, suffix.index);
70
+ }
71
+ const declaration = DECL.exec(text), junction = JUNCTION.exec(text), edge = EDGE.exec(text), align = ALIGN.exec(text);
72
+ if (declaration || junction) {
73
+ const [, kind, id, icon, iconText, label, parent] = declaration || [
74
+ "",
75
+ "junction",
76
+ junction[1],
77
+ null,
78
+ null,
79
+ null,
80
+ junction[2]
81
+ ];
82
+ if (items.has(id) || parent && items.get(parent)?.kind !== "group") {
83
+ fail(line, `标识「${id}」重复或父组未声明`, `Duplicate id "${id}" or undeclared parent group`);
84
+ continue;
85
+ }
86
+ if (kind === "group" && iconText) fail(line, "组图标不支持纯文本", "Group icons do not support plain text");
87
+ if (kind === "junction" && suffixIcon) fail(line, "接点不支持图标", "Junctions do not support icons");
88
+ const ref = suffixIcon ?? icon;
89
+ const resolved = ref && ref !== "blank" ? resolveIconRef(ICONS.get(ref) ?? ref) : null;
90
+ if (ref && ref !== "blank" && !resolved) asset(warning("IGNORED_ASSET", info.lang, line, `未知图标 ${ref}`, `Unknown icon ${ref}`, "使用内置架构图图标或注册图标集", "Use a built-in icon or register its icon set"));
91
+ const title = caption(label, line), content = caption(iconText?.replace(/^\((.*)\)$/, "$1"), line);
92
+ if (classes.length) look.addClasses(id, classes, line);
93
+ const item = {
94
+ id,
95
+ kind,
96
+ parent,
97
+ icon: resolved ?? content.icon ?? title.icon,
98
+ iconText: content.text,
99
+ label: title.text,
100
+ line,
101
+ depth: (items.get(parent)?.depth ?? -1) + 1
102
+ };
103
+ items.set(id, item);
104
+ (kind === "group" ? groups : nodes).push(item);
105
+ } else if (edge) {
106
+ const [, from, lhsGroup, lhs, back, label, forward, rhs, to, rhsGroup] = edge;
107
+ const a = items.get(from), b = items.get(to);
108
+ if (!a || !b || a.kind === "group" || b.kind === "group" || lhsGroup && !a.parent || rhsGroup && !b.parent || (lhsGroup || rhsGroup) && a.parent === b.parent) {
109
+ fail(line, "边需要已声明的端点;组边必须跨越组边界", "Edges need declared endpoints; group edges must connect across group boundaries");
110
+ continue;
111
+ }
112
+ const captioned = caption(label, line);
113
+ edges.push({
114
+ from,
115
+ to,
116
+ lhs,
117
+ rhs,
118
+ lhsGroup: lhsGroup ? a.parent : null,
119
+ rhsGroup: rhsGroup ? b.parent : null,
120
+ back: Boolean(back),
121
+ forward: Boolean(forward),
122
+ label: captioned.text,
123
+ icon: captioned.icon,
124
+ line
125
+ });
126
+ } else if (align) {
127
+ const [, direction, ids] = align, members = ids.split(/\s+/);
128
+ if (members.length < 2 || new Set(members).size !== members.length || members.some((id) => !items.has(id) || items.get(id).kind === "group")) {
129
+ fail(line, "对齐至少需要两个不同的已声明服务或接点", "An alignment needs at least two distinct declared services or junctions");
130
+ continue;
131
+ }
132
+ aligns.push({
133
+ axis: direction === "row" ? 0 : 1,
134
+ members,
135
+ line
136
+ });
137
+ } else fail(line, "无法识别架构图语句", "Unrecognized architecture statement");
138
+ }
139
+ if (items.size > 400 || edges.length > 600 || aligns.length > 400) fail(info.firstLine, "架构图超过安全上限(400 个元素、600 条边或 400 条对齐),请拆分", "Architecture exceeds its safety cap (400 items, 600 edges, or 400 alignments); split it into smaller diagrams");
140
+ errors.push(...look.diagnostics());
141
+ for (const item of items.values()) {
142
+ item.status = look.statusOf(item.id);
143
+ item.link = look.linkOf(item.id);
144
+ }
145
+ edges.forEach((edge, index) => {
146
+ edge.status = look.edgeStatusOf(index);
147
+ });
148
+ const model = {
149
+ items,
150
+ nodes,
151
+ groups,
152
+ edges,
153
+ aligns,
154
+ title
155
+ };
156
+ if (!errors.some(blocking)) {
157
+ const { xy, conflict } = place(model);
158
+ if (conflict) fail(conflict, "架构图对齐与端口顺序冲突", "Architecture alignment conflicts with port order");
159
+ else model.xy = xy;
160
+ }
161
+ return {
162
+ model,
163
+ errors
164
+ };
165
+ }
166
+ function solve(ids, constraints) {
167
+ const root = new Map(ids.map((id) => [id, id]));
168
+ const find = (id) => {
169
+ while (root.get(id) !== id) id = root.get(id);
170
+ return id;
171
+ };
172
+ for (const [a, b, distance] of constraints) if (!distance) root.set(find(b), find(a));
173
+ const tracks = new Map(ids.map((id) => [find(id), {
174
+ next: [],
175
+ count: 0,
176
+ index: 0
177
+ }]));
178
+ for (const [from, to, distance] of constraints) if (distance) {
179
+ const a = tracks.get(find(from)), b = tracks.get(find(to));
180
+ a.next.push(b);
181
+ b.count++;
182
+ }
183
+ const queue = [...tracks.values()].filter((track) => !track.count);
184
+ for (const a of queue) for (const b of a.next) {
185
+ b.index = Math.max(b.index, a.index + 1);
186
+ if (!--b.count) queue.push(b);
187
+ }
188
+ return queue.length === tracks.size ? new Map(ids.map((id) => [id, tracks.get(find(id)).index])) : null;
189
+ }
190
+ function place({ items, nodes, edges, aligns }, preferStraight = true) {
191
+ const children = /* @__PURE__ */ new Map();
192
+ for (const item of items.values()) {
193
+ if (!children.has(item.parent)) children.set(item.parent, []);
194
+ children.get(item.parent).push(item);
195
+ }
196
+ const ids = [];
197
+ const visit = (parent) => {
198
+ for (const item of children.get(parent) ?? []) if (item.kind === "group") visit(item.id);
199
+ else ids.push(item.id);
200
+ };
201
+ visit(void 0);
202
+ if (ids.length > 60 && !edges.length && !aligns.length) return { xy: [new Map(ids.map((id) => [id, 0])), new Map(ids.map((id, index) => [id, index]))] };
203
+ if (ids.length > 60 && ids.length === items.size) {
204
+ const pinned = new Set(edges.flatMap(({ from, to }) => [from, to]));
205
+ for (const { members } of aligns) for (const id of members) pinned.add(id);
206
+ const free = ids.filter((id) => !pinned.has(id));
207
+ if (free.length) {
208
+ const placement = place({
209
+ items: new Map([...items].filter(([id]) => pinned.has(id))),
210
+ nodes: nodes.filter((node) => pinned.has(node.id)),
211
+ edges,
212
+ aligns
213
+ }, preferStraight);
214
+ if (!placement.xy) return placement;
215
+ const [columns, rows] = placement.xy;
216
+ let row = Math.max(-1, ...rows.values()) + 1;
217
+ for (const id of free) {
218
+ columns.set(id, 0);
219
+ rows.set(id, row++);
220
+ }
221
+ return placement;
222
+ }
223
+ }
224
+ const constraints = [[], []], perpendicular = [[], []];
225
+ const add = (axis, relation) => {
226
+ const rules = constraints[axis];
227
+ rules.push(relation);
228
+ if (solve(ids, rules)) return true;
229
+ rules.pop();
230
+ return false;
231
+ };
232
+ for (const { from, to, lhs, rhs } of edges) for (let axis = 0; axis < 2; axis++) {
233
+ const a = DIR[lhs][axis], b = DIR[rhs][axis];
234
+ if (a === b) {
235
+ if (!a) perpendicular[axis].push([
236
+ from,
237
+ to,
238
+ 0
239
+ ]);
240
+ } else add(axis, a > b ? [
241
+ from,
242
+ to,
243
+ 1
244
+ ] : [
245
+ to,
246
+ from,
247
+ 1
248
+ ]);
249
+ }
250
+ for (const { axis, members, line } of aligns) for (let i = 1; i < members.length; i++) if (!add(axis, [
251
+ members[i - 1],
252
+ members[i],
253
+ 1
254
+ ]) || !add(1 - axis, [
255
+ members[0],
256
+ members[i],
257
+ 0
258
+ ])) return { conflict: line };
259
+ for (let axis = 0; preferStraight && axis < 2; axis++) {
260
+ const rank = solve(ids, constraints[axis]), neighbors = new Map(ids.map((id) => [id, []]));
261
+ for (const [a, b] of perpendicular[axis]) {
262
+ neighbors.get(a).push(rank.get(b));
263
+ neighbors.get(b).push(rank.get(a));
264
+ }
265
+ const median = new Map([...neighbors].map(([id, tracks]) => [id, tracks.sort((a, b) => a - b)[Math.floor(tracks.length / 2)]]));
266
+ const penalty = ([a, b]) => Math.abs(rank.get(a) - median.get(b)) + Math.abs(rank.get(b) - median.get(a));
267
+ for (const relation of perpendicular[axis].sort((a, b) => penalty(a) - penalty(b))) add(axis, relation);
268
+ }
269
+ let xy = constraints.map((rules) => solve(ids, rules));
270
+ for (let i = 0; i < ids.length; i++) for (let j = 0; j < i; j++) {
271
+ const a = ids[j], b = ids[i];
272
+ if (!xy.every((track) => track.get(a) === track.get(b))) continue;
273
+ if (!add(1, [
274
+ a,
275
+ b,
276
+ 1
277
+ ]) && !add(1, [
278
+ b,
279
+ a,
280
+ 1
281
+ ]) && !add(0, [
282
+ a,
283
+ b,
284
+ 1
285
+ ]) && !add(0, [
286
+ b,
287
+ a,
288
+ 1
289
+ ])) return preferStraight ? place({
290
+ items,
291
+ nodes,
292
+ edges,
293
+ aligns
294
+ }, false) : { conflict: items.get(b).line };
295
+ xy = constraints.map((rules) => solve(ids, rules));
296
+ i = -1;
297
+ break;
298
+ }
299
+ return { xy };
300
+ }
301
+ var rect = (l, t, r, b) => ({
302
+ l,
303
+ t,
304
+ r,
305
+ b,
306
+ x: (l + r) / 2,
307
+ y: (t + b) / 2
308
+ });
309
+ var inflate = ({ l, t, r, b }, n) => rect(l - n, t - n, r + n, b + n);
310
+ var same = (a, b) => a.x === b.x && a.y === b.y;
311
+ var along = (a, b) => a.x === b.x || a.y === b.y;
312
+ var between = (a, b, c) => along(a, c) && dist(a, c) === dist(a, b) + dist(b, c);
313
+ var step = ({ x, y }, side, n) => ({
314
+ x: x + DIR[side][0] * n,
315
+ y: y + DIR[side][1] * n
316
+ });
317
+ var dist = (a, b) => Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
318
+ var overlap = (a, b, c, d) => Math.min(Math.max(a, b), Math.max(c, d)) - Math.max(Math.min(a, b), Math.min(c, d));
319
+ var inBox = (a, { l, t, r, b }) => l < a.x && a.x < r && t < a.y && a.y < b;
320
+ var intersects = (a, b, { l, t, r, b: bottom }) => a.y === b.y ? t < a.y && a.y < bottom && overlap(a.x, b.x, l, r) > 0 : l < a.x && a.x < r && overlap(a.y, b.y, t, bottom) > 0;
321
+ var coincides = (a, b, c, d) => a.y === b.y ? c.y === d.y && Math.abs(a.y - c.y) <= 1 && overlap(a.x, b.x, c.x, d.x) > 0 : c.x === d.x && Math.abs(a.x - c.x) <= 1 && overlap(a.y, b.y, c.y, d.y) > 0;
322
+ var segments = (points) => points.slice(1).map((point, i) => [points[i], point]);
323
+ function route(start, end, boxes, used) {
324
+ const clear = (a, b) => !boxes.some((box) => intersects(a, b, box)) && !used.some(([c, d]) => coincides(a, b, c, d));
325
+ if (along(start, end) && clear(start, end)) return [start, end];
326
+ const xs = /* @__PURE__ */ new Set([start.x, end.x]), ys = /* @__PURE__ */ new Set([start.y, end.y]);
327
+ for (const { l, t, r, b } of boxes) {
328
+ xs.add(l);
329
+ xs.add(r);
330
+ ys.add(t);
331
+ ys.add(b);
332
+ }
333
+ for (const [a, b] of used) {
334
+ if (a.x === b.x) {
335
+ xs.add(a.x - 8);
336
+ xs.add(a.x + 8);
337
+ }
338
+ if (a.y === b.y) {
339
+ ys.add(a.y - 8);
340
+ ys.add(a.y + 8);
341
+ }
342
+ }
343
+ const x = [...xs].sort((a, b) => a - b), y = [...ys].sort((a, b) => a - b);
344
+ const candidates = [...x.map((px) => [
345
+ start,
346
+ {
347
+ x: px,
348
+ y: start.y
349
+ },
350
+ {
351
+ x: px,
352
+ y: end.y
353
+ },
354
+ end
355
+ ]), ...y.map((py) => [
356
+ start,
357
+ {
358
+ x: start.x,
359
+ y: py
360
+ },
361
+ {
362
+ x: end.x,
363
+ y: py
364
+ },
365
+ end
366
+ ])].map((points) => points.filter((p, i) => !i || !same(p, points[i - 1])));
367
+ candidates.sort((a, b) => cost(a) - cost(b));
368
+ const simple = candidates.find((points) => segments(points).every(([a, b]) => clear(a, b)));
369
+ if (simple) return simple;
370
+ const queue = [], best = /* @__PURE__ */ new Map();
371
+ function offer(i, j, dir, distance, prev) {
372
+ const key = `${i}:${j}:${dir}`, p = {
373
+ x: x[i],
374
+ y: y[j]
375
+ };
376
+ if (distance >= (best.get(key) ?? Infinity) || boxes.some((box) => inBox(p, box))) return;
377
+ best.set(key, distance);
378
+ queue.push({
379
+ i,
380
+ j,
381
+ dir,
382
+ distance,
383
+ prev,
384
+ p,
385
+ key,
386
+ score: distance + dist(p, end)
387
+ });
388
+ }
389
+ offer(x.indexOf(start.x), y.indexOf(start.y), -1, 0, null);
390
+ for (let work = 0; queue.length && work < 6e3; work++) {
391
+ queue.sort((a, b) => b.score - a.score);
392
+ const current = queue.pop();
393
+ if (current.distance !== best.get(current.key)) continue;
394
+ if (same(current.p, end)) {
395
+ const points = [];
396
+ for (let p = current; p; p = p.prev) points.push(p.p);
397
+ return points.reverse();
398
+ }
399
+ for (const [di, dj] of Object.values(DIR)) {
400
+ const i = current.i + di, j = current.j + dj, dir = di ? 0 : 1;
401
+ if (x[i] === void 0 || y[j] === void 0) continue;
402
+ const p = {
403
+ x: x[i],
404
+ y: y[j]
405
+ };
406
+ if (clear(current.p, p)) offer(i, j, dir, current.distance + dist(current.p, p) + (dir === current.dir ? 0 : 12), current);
407
+ }
408
+ }
409
+ return null;
410
+ }
411
+ var cost = (points) => segments(points).reduce((sum, [a, b]) => sum + dist(a, b) + 12, 0);
412
+ function iconShape(ctx, icon, x, y, size, ink) {
413
+ if (!icon) return [];
414
+ const [l, t, w, h] = icon.viewBox.split(/\s+/).map(Number);
415
+ const scale = size / Math.max(w, h);
416
+ const solid = icon.set === "simple";
417
+ return [ctx.el("g", {
418
+ transform: `translate(${x - (l + w / 2) * scale} ${y - (t + h / 2) * scale}) scale(${scale})`,
419
+ fill: solid ? ink : "none",
420
+ stroke: solid ? "none" : ink,
421
+ "stroke-width": 1.6,
422
+ "stroke-linejoin": "round",
423
+ "stroke-linecap": "round"
424
+ }, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, attrs)))];
425
+ }
426
+ function trackCenters(sizes, gap) {
427
+ let end = 0;
428
+ return sizes.map((size) => {
429
+ const start = end;
430
+ end += size + gap;
431
+ return start + size / 2;
432
+ });
433
+ }
434
+ var honoredConfig = [
435
+ "architecture.padding",
436
+ "architecture.iconSize",
437
+ "architecture.fontSize"
438
+ ];
439
+ var configKey = (map, name) => Object.entries(map ?? {}).find(([key]) => key.toLowerCase() === name.toLowerCase())?.[1];
440
+ function layout(model, { width, measure, ctx, lang }, info) {
441
+ const { items, nodes, groups, edges, xy } = model;
442
+ const failure = (line, zh, en) => ({ errors: [error(lang, line, zh, en, "调整对齐、端口或组的连接", "Adjust alignment, ports, or group connections")] });
443
+ const style = appearance(ctx), ink = ctx.frameTone(style.fg2), edgeInk = ctx.frameTone(style.fg1);
444
+ const config = configKey(info.config, "architecture");
445
+ const dimension = (name, fallback, min, max) => {
446
+ const value = configKey(config, name);
447
+ return typeof value === "number" && Number.isFinite(value) ? Math.round(Math.max(min, Math.min(max, value))) : fallback;
448
+ };
449
+ const iconSize = dimension("iconSize", 32, 16, 160);
450
+ const padding = dimension("padding", 14, 10, 80);
451
+ const font = {
452
+ ...FONT.body,
453
+ size: dimension("fontSize", FONT.body.size, 12, 48)
454
+ };
455
+ const leading = Math.ceil(font.size * 1.22);
456
+ const rows = (value) => value ? wrapText(value, Math.max(112, font.size * 7), font, measure) : [];
457
+ const withLink = (item, children) => item.link ? [ctx.link(item.link.href, children, item.link)] : children;
458
+ const sizes = [[], []], cards = /* @__PURE__ */ new Map(), frames = /* @__PURE__ */ new Map();
459
+ for (const node of nodes) {
460
+ const text = rows(node.iconText);
461
+ const contents = node.icon ? text : [];
462
+ const labels = [...node.icon ? [] : text, ...rows(node.label)];
463
+ const textWidth = maxWidthOf([...labels, ...contents], font, measure) + 20;
464
+ const w = node.kind === "junction" ? 0 : Math.ceil(Math.max(node.icon ? Math.max(76, iconSize + 24) : 40, textWidth));
465
+ const contentHeight = node.icon ? Math.max(58, 20 + contents.length * leading + iconSize + (contents.length ? 8 : 0)) : 0;
466
+ const h = !w ? 0 : node.icon ? contentHeight + (labels.length ? 6 + labels.length * leading : 0) : Math.max(36, labels.length * leading + 16);
467
+ const card = {
468
+ labels,
469
+ contents,
470
+ contentHeight,
471
+ w,
472
+ h
473
+ };
474
+ cards.set(node.id, card);
475
+ for (const [axis, size] of [w, h].entries()) {
476
+ const i = xy[axis].get(node.id);
477
+ sizes[axis][i] = Math.max(size, sizes[axis][i] || 0);
478
+ }
479
+ }
480
+ const depth = groups.reduce((n, group) => Math.max(n, group.depth + 1), 0);
481
+ const labelGap = Math.max(0, ...edges.filter((e) => e.label || e.icon).map((e) => Math.ceil(maxWidthOf(rows(e.label), font, measure)) + (e.icon ? 24 : 0) + 24));
482
+ const frameHeader = 30 + padding;
483
+ const centers = [trackCenters(sizes[0], Math.max(44, labelGap, 2 * padding + 16) + padding * Math.max(0, depth - 1)), trackCenters(sizes[1], (depth ? 22 + frameHeader + padding : 50) + (frameHeader + padding) * Math.max(0, depth - 1))];
484
+ for (const [id, card] of cards) {
485
+ const [x, y] = centers.map((axis, i) => axis[xy[i].get(id)]);
486
+ Object.assign(card, rect(x - card.w / 2, y - card.h / 2, x + card.w / 2, y + card.h / 2));
487
+ }
488
+ let emptyStart = Math.max(0, ...[...cards.values()].map((card) => card.r)) + 50 + depth * 14;
489
+ for (const group of [...groups].reverse()) {
490
+ const children = [...items.values()].filter((item) => item.parent === group.id).map((item) => cards.get(item.id) ?? frames.get(item.id));
491
+ const text = group.label.split("\n"), headerWidth = maxWidthOf(text, FONT.title, measure) + (group.icon ? 26 : 0) + 24;
492
+ const extra = (text.length - 1) * 16;
493
+ const left = children.length ? Math.min(...children.map((child) => child.l)) - padding : emptyStart;
494
+ const top = children.length ? Math.min(...children.map((child) => child.t)) - frameHeader - extra : 0;
495
+ const right = Math.max(left + Math.max(66, headerWidth), ...children.map((child) => child.r + padding));
496
+ const bottom = Math.max(top + 56 + extra, ...children.map((child) => child.b + padding));
497
+ frames.set(group.id, {
498
+ ...rect(left, top, right, bottom),
499
+ header: text || group.icon ? rect(left - 4, top + 4, left + headerWidth - 6, top + 27 + extra) : null
500
+ });
501
+ if (!children.length) emptyStart = right + 30 + depth * 14;
502
+ }
503
+ const cardShapes = [], frameShapes = [], paths = [], plates = [], used = [], labelBoxes = [];
504
+ for (const group of groups) {
505
+ const { l, t, r, b } = frames.get(group.id);
506
+ const stroke = style[group.status] ?? ink;
507
+ const shapes = [ctx.rect(l, t, r - l, b - t, {
508
+ rx: style.radius,
509
+ fill: style.surface,
510
+ stroke,
511
+ meta: {
512
+ kind: "frame",
513
+ id: group.id
514
+ }
515
+ }), ...iconShape(ctx, group.icon, l + 20, t + 15, 18, stroke)];
516
+ const lines = group.label.split("\n");
517
+ if (group.label) shapes.push(textBlock(ctx, {
518
+ x: l + (group.icon ? 38 : 12),
519
+ y: t + 15 + (lines.length - 1) * 8,
520
+ lines,
521
+ lineHeight: 16,
522
+ font: FONT.title,
523
+ anchor: "start"
524
+ }));
525
+ frameShapes.push(...withLink(group, shapes));
526
+ }
527
+ for (const node of nodes) {
528
+ const { l, t, w, h, x, y, labels, contents, contentHeight } = cards.get(node.id);
529
+ if (node.kind === "junction") {
530
+ if (node.link) cardShapes.push(...withLink(node, [ctx.shape("circle", {
531
+ cx: x,
532
+ cy: y,
533
+ r: 6,
534
+ fill: "transparent"
535
+ })]));
536
+ continue;
537
+ }
538
+ const iconY = contents.length ? t + 10 + iconSize / 2 : t + contentHeight / 2;
539
+ const stroke = style[node.status] ?? ink;
540
+ const shapes = [ctx.rect(l, t, w, h, {
541
+ rx: style.smallRadius,
542
+ fill: style.node,
543
+ stroke,
544
+ meta: {
545
+ kind: "node",
546
+ id: node.id
547
+ }
548
+ }), ...iconShape(ctx, node.icon, x, iconY, iconSize, style[node.status] ?? edgeInk)];
549
+ if (contents.length) shapes.push(textBlock(ctx, {
550
+ x,
551
+ y: t + contentHeight - 10 - contents.length * leading / 2,
552
+ lines: contents,
553
+ lineHeight: leading,
554
+ font
555
+ }));
556
+ if (labels.length) shapes.push(textBlock(ctx, {
557
+ x,
558
+ y: node.icon ? t + h - 8 - labels.length * leading / 2 : y,
559
+ lines: labels,
560
+ lineHeight: leading,
561
+ font
562
+ }));
563
+ cardShapes.push(...withLink(node, shapes));
564
+ }
565
+ const ports = /* @__PURE__ */ new Map();
566
+ const ends = edges.map((edge) => [[
567
+ edge.from,
568
+ edge.lhs,
569
+ edge.lhsGroup
570
+ ], [
571
+ edge.to,
572
+ edge.rhs,
573
+ edge.rhsGroup
574
+ ]].map(([id, side, group]) => {
575
+ const end = {
576
+ id,
577
+ side,
578
+ group,
579
+ box: group ? frames.get(group) : cards.get(id)
580
+ };
581
+ const key = `${group ?? id}:${side}`;
582
+ if (!ports.has(key)) ports.set(key, []);
583
+ ports.get(key).push(end);
584
+ return end;
585
+ }));
586
+ for (const [a, b] of ends) {
587
+ a.other = b.box;
588
+ b.other = a.box;
589
+ }
590
+ for (const list of ports.values()) {
591
+ const { side, box, group } = list[0], horizontal = side === "L" || side === "R";
592
+ const coordinate = horizontal ? "y" : "x";
593
+ list.sort((a, b) => a.other[coordinate] - b.other[coordinate]);
594
+ const low = horizontal ? box.t + (group ? 34 : 10) : box.l + 10;
595
+ const high = horizontal ? box.b - 10 : box.r - 10;
596
+ list.forEach((end, i) => {
597
+ const mid = Math.max(low, Math.min(high, box[coordinate]));
598
+ const pitch = Math.min(12, (high - low) / Math.max(1, list.length - 1));
599
+ const start = Math.max(low, Math.min(high - pitch * (list.length - 1), mid - pitch * (list.length - 1) / 2));
600
+ const offset = high < low ? box[coordinate] : start + i * pitch;
601
+ end.point = horizontal ? {
602
+ x: side === "L" ? box.l : box.r,
603
+ y: offset
604
+ } : {
605
+ x: offset,
606
+ y: side === "T" ? box.t : box.b
607
+ };
608
+ end.tip = step(end.point, side, 10);
609
+ });
610
+ }
611
+ const within = (id, group) => {
612
+ for (let parent = items.get(id).parent; parent; parent = items.get(parent).parent) if (parent === group) return true;
613
+ return false;
614
+ };
615
+ const portSegments = ends.map((pair) => pair.map(({ point, tip }) => [point, tip]));
616
+ for (const [i, edge] of edges.entries()) {
617
+ const [a, b] = ends[i];
618
+ const boxes = [];
619
+ for (const [id, card] of cards) if (card.w) boxes.push(inflate(card, 6));
620
+ else if (id !== a.id && id !== b.id) boxes.push(inflate(card, 4));
621
+ for (const [id, frame] of frames) if (id === a.group || id === b.group || !within(a.id, id) && !within(b.id, id)) boxes.push(inflate(frame, 6));
622
+ else if (frame.header) boxes.push(inflate(frame.header, 4));
623
+ const stubs = portSegments[i];
624
+ const path = route(a.tip, b.tip, boxes, [...used, ...portSegments.slice(i).flat()]);
625
+ if (!path || stubs.some(([p, q]) => used.some(([c, d]) => coincides(p, q, c, d)))) return failure(edge.line, "无法为边找到不重叠的路径", "Could not route edge without overlapping another edge or element");
626
+ const joints = [
627
+ a.point,
628
+ ...path,
629
+ b.point
630
+ ].filter((p, j, all) => !j || !same(p, all[j - 1]));
631
+ const points = joints.filter((p, j) => !j || j === joints.length - 1 || !between(joints[j - 1], p, joints[j + 1]));
632
+ const parts = segments(points);
633
+ used.push(...parts);
634
+ const stroke = style[edge.status] ?? edgeInk;
635
+ paths.push(ctx.shape("polyline", {
636
+ points: points.map((p) => `${p.x},${p.y}`).join(" "),
637
+ fill: "none",
638
+ stroke,
639
+ "stroke-width": 1.5,
640
+ "stroke-linejoin": "round",
641
+ "stroke-linecap": "round"
642
+ }, {
643
+ kind: "edge",
644
+ from: edge.from,
645
+ to: edge.to
646
+ }));
647
+ for (const [end, arrow] of [[a, edge.back], [b, edge.forward]]) if (arrow) {
648
+ const base = step(end.point, end.side, 8), [dx, dy] = DIR[end.side];
649
+ paths.push(ctx.shape("polygon", {
650
+ points: `${end.point.x},${end.point.y} ${base.x - dy * 4},${base.y + dx * 4} ${base.x + dy * 4},${base.y - dx * 4}`,
651
+ fill: stroke
652
+ }));
653
+ }
654
+ if (edge.label || edge.icon) plates.push({
655
+ edge,
656
+ parts
657
+ });
658
+ }
659
+ const labels = [];
660
+ for (const { edge, parts } of plates) {
661
+ const lines = rows(edge.label), w = Math.ceil(maxWidthOf(lines, font, measure)) + 14 + (edge.icon ? 24 : 0), h = Math.max(lines.length * leading, edge.icon ? 16 : 0) + 6;
662
+ const candidates = [...parts].sort(([a, b], [c, d]) => dist(c, d) - dist(a, b)).flatMap(([a, b]) => {
663
+ const x = (a.x + b.x) / 2, y = (a.y + b.y) / 2;
664
+ return a.y === b.y ? [rect(x - w / 2, y - h - 5, x + w / 2, y - 5), rect(x - w / 2, y + 5, x + w / 2, y + h + 5)] : [rect(x + 5, y - h / 2, x + w + 5, y + h / 2), rect(x - w - 5, y - h / 2, x - 5, y + h / 2)];
665
+ });
666
+ const obstacles = [
667
+ ...cards.values(),
668
+ ...labelBoxes,
669
+ ...[...frames.values()].map((f) => f.header).filter(Boolean)
670
+ ];
671
+ const box = candidates.find((box) => !obstacles.some((o) => overlap(o.l, o.r, box.l, box.r) > -4 && overlap(o.t, o.b, box.t, box.b) > -4) && !used.some(([a, b]) => intersects(a, b, inflate(box, 2))));
672
+ if (!box) return failure(edge.line, "没有空间放置边标签", "No room for edge label");
673
+ labelBoxes.push(box);
674
+ labels.push({
675
+ ...ctx.el("g", {}, [
676
+ ctx.rect(box.l, box.t, w, h, {
677
+ rx: style.smallRadius,
678
+ fill: style.surface
679
+ }),
680
+ ...iconShape(ctx, edge.icon, box.l + 17, box.y, 16, edgeInk),
681
+ textBlock(ctx, {
682
+ x: box.x + (edge.icon ? 12 : 0),
683
+ y: box.y,
684
+ lines,
685
+ lineHeight: leading,
686
+ font
687
+ })
688
+ ]),
689
+ meta: {
690
+ kind: "edge-label-group",
691
+ from: edge.from,
692
+ to: edge.to
693
+ }
694
+ });
695
+ }
696
+ const bounds = [
697
+ ...cards.values(),
698
+ ...frames.values(),
699
+ ...labelBoxes,
700
+ ...used.flat().map(({ x, y }) => rect(x, y, x, y))
701
+ ];
702
+ const l = Math.min(0, ...bounds.map((box) => box.l)), t = Math.min(0, ...bounds.map((box) => box.t));
703
+ const naturalWidth = 40 + Math.max(0, ...bounds.map((box) => box.r)) - l;
704
+ const height = 40 + Math.max(0, ...bounds.map((box) => box.b)) - t;
705
+ const children = [ctx.el("g", { transform: `translate(${20 - l} ${20 - t})` }, [
706
+ ...frameShapes,
707
+ ...paths,
708
+ ...cardShapes,
709
+ ...labels
710
+ ])];
711
+ const errors = naturalWidth > width * 2 || nodes.length > 60 ? [warning("LARGE_DIAGRAM", lang, 1, "架构图较大", "Large architecture diagram", "考虑拆分架构图", "Consider splitting the diagram")] : [];
712
+ return {
713
+ scene: {
714
+ width: naturalWidth,
715
+ height,
716
+ children,
717
+ title: model.title,
718
+ heading: model.title
719
+ },
720
+ errors
721
+ };
722
+ }
723
+ var implementations = { "architecture-beta": {
724
+ parse,
725
+ layout,
726
+ acceptsPresentation: true,
727
+ honoredConfig
728
+ } };
729
+ export { architecture_exports, implementations };