@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,948 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { FONT, appearance, error, maxWidthOf, message, snap, warning, wrapText } from "./support.js";
3
+ import { mapConfig, mapIcon, mapLabels } from "./map-label.js";
4
+ // src/diagram/eventmodeling.js
5
+ var eventmodeling_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
6
+ var LIMITS = Object.freeze({
7
+ largeFrames: 60,
8
+ dataLines: 12,
9
+ workFrames: 600,
10
+ chars: 2e5
11
+ });
12
+ var PAD = 20;
13
+ var COLUMN_GAP = 30;
14
+ var LANE_PAD = 12;
15
+ var LANE_GAP = 12;
16
+ var TRACK = 7;
17
+ var SLOT = 6;
18
+ var BOX_PAD = 10;
19
+ var GLYPH_GAP = 6;
20
+ var SIDE_INSET = 12;
21
+ var MIN_LANE_H = 64;
22
+ var MIN_BOX_H = 48;
23
+ var MIN_BOX_W = 132;
24
+ var MAX_BOX_W = 420;
25
+ var NAME_STEP = 18;
26
+ var DATA_STEP = 16;
27
+ var DATA_GAP = 4;
28
+ var CAPTION_STEP = 18;
29
+ var NAME_FONT = Object.freeze({
30
+ size: 13,
31
+ weight: 600
32
+ });
33
+ var GLYPH_FONT = Object.freeze({
34
+ size: 12,
35
+ weight: 650
36
+ });
37
+ var LANE_FONT = Object.freeze({
38
+ size: 12,
39
+ weight: 650
40
+ });
41
+ var DATA_FONT = FONT.label;
42
+ var CAPTION_FONT = FONT.label;
43
+ var UI_LANE = Object.freeze({
44
+ lane: ["界面/自动化", "UI/Automation"],
45
+ prefix: ["界面:", "UI/A: "]
46
+ });
47
+ var MODEL_LANE = Object.freeze({
48
+ lane: ["命令/读模型", "Command/Read Model"],
49
+ prefix: ["命令/读模型:", "C/RM: "]
50
+ });
51
+ var TYPES = Object.freeze({
52
+ ui: Object.freeze({
53
+ band: 0,
54
+ glyph: "UI",
55
+ role: null,
56
+ ...UI_LANE
57
+ }),
58
+ processor: Object.freeze({
59
+ band: 0,
60
+ glyph: "P",
61
+ role: "tone4",
62
+ ...UI_LANE
63
+ }),
64
+ readmodel: Object.freeze({
65
+ band: 100,
66
+ glyph: "R",
67
+ role: "tone3",
68
+ ...MODEL_LANE
69
+ }),
70
+ command: Object.freeze({
71
+ band: 100,
72
+ glyph: "C",
73
+ role: "tone1",
74
+ ...MODEL_LANE
75
+ }),
76
+ event: Object.freeze({
77
+ band: 200,
78
+ glyph: "E",
79
+ role: "tone2",
80
+ lane: ["事件", "Events"],
81
+ prefix: ["事件流:", "Stream: "]
82
+ })
83
+ });
84
+ var TYPE_NAMES = Object.freeze({
85
+ ui: ["UI", "UI"],
86
+ processor: ["处理器", "processor"],
87
+ command: ["命令", "command"],
88
+ readmodel: ["读模型", "read model"],
89
+ event: ["事件", "event"]
90
+ });
91
+ var ENTITY_KEYWORDS = /* @__PURE__ */ new Map([
92
+ ["ui", "ui"],
93
+ ["pcr", "processor"],
94
+ ["processor", "processor"],
95
+ ["rmo", "readmodel"],
96
+ ["readmodel", "readmodel"],
97
+ ["cmd", "command"],
98
+ ["command", "command"],
99
+ ["evt", "event"],
100
+ ["event", "event"]
101
+ ]);
102
+ var FRAME_KEYWORDS = /* @__PURE__ */ new Set([
103
+ "tf",
104
+ "timeframe",
105
+ "rf",
106
+ "resetframe"
107
+ ]);
108
+ var DATA_TYPES = /* @__PURE__ */ new Set([
109
+ "json",
110
+ "jsobj",
111
+ "figma",
112
+ "salt",
113
+ "uri",
114
+ "md",
115
+ "html",
116
+ "text"
117
+ ]);
118
+ var SOURCE_TYPES = Object.freeze({
119
+ ui: Object.freeze(["readmodel"]),
120
+ processor: Object.freeze(["readmodel"]),
121
+ command: Object.freeze(["ui", "processor"]),
122
+ readmodel: Object.freeze(["event"]),
123
+ event: Object.freeze(["command"])
124
+ });
125
+ var typeName = (type, lang) => message(lang, ...TYPE_NAMES[type] || [type, type]);
126
+ var qualifier = (text) => String(text || "").split(".").map((part) => part.trim()).filter(Boolean);
127
+ function stripComments(text, state) {
128
+ let out = "";
129
+ for (let i = 0; i < text.length;) {
130
+ if (state.block) {
131
+ const end = text.indexOf("*/", i);
132
+ if (end < 0) return out;
133
+ i = end + 2;
134
+ state.block = false;
135
+ continue;
136
+ }
137
+ if (text.startsWith("/*", i)) {
138
+ state.block = true;
139
+ i += 2;
140
+ continue;
141
+ }
142
+ if (text.startsWith("//", i) || text.startsWith("%%", i)) break;
143
+ const data = /^(?:"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|\{.*\})/.exec(text.slice(i));
144
+ if (data) {
145
+ out += data[0];
146
+ i += data[0].length;
147
+ continue;
148
+ }
149
+ out += text[i];
150
+ i += 1;
151
+ }
152
+ return out;
153
+ }
154
+ function blockLines(raw) {
155
+ const lines = raw.map((line) => line.replace(/\s+$/, ""));
156
+ while (lines.length && !lines[0].trim()) lines.shift();
157
+ while (lines.length && !lines[lines.length - 1].trim()) lines.pop();
158
+ const indent = Math.min(...lines.filter((line) => line.trim()).map((line) => line.match(/^[ \t]*/)[0].length));
159
+ if (!Number.isFinite(indent) || indent === 0) return lines;
160
+ return lines.map((line) => line.slice(Math.min(indent, line.length - line.trimStart().length)));
161
+ }
162
+ function readGwt(text) {
163
+ const tokens = text.split(/\s+/).filter(Boolean);
164
+ const sections = {
165
+ given: [],
166
+ when: [],
167
+ then: []
168
+ };
169
+ let current = null;
170
+ for (let i = 0; i < tokens.length; i += 1) {
171
+ const key = tokens[i].toLowerCase();
172
+ if (key === "given" || key === "when" || key === "then") {
173
+ current = key;
174
+ continue;
175
+ }
176
+ const type = ENTITY_KEYWORDS.get(key);
177
+ if (!current || !type || !tokens[i + 1]) return null;
178
+ sections[current].push({
179
+ type,
180
+ id: tokens[i + 1]
181
+ });
182
+ i += 1;
183
+ }
184
+ return current === "then" && sections.given.length && sections.then.length ? sections : null;
185
+ }
186
+ function parse(source, info = {}) {
187
+ const lang = info.lang || "zh";
188
+ const start = info.firstLine || 2;
189
+ const raw = String(source ?? "").split("\n").map((line) => line.replace(/\r$/, ""));
190
+ const errors = [], hints = [];
191
+ const frames = [], dataBlocks = [], notes = [], gwt = [], entities = [];
192
+ const normalize = mapLabels(lang, (item) => hints.push(item));
193
+ let { text: title, icon: titleIcon } = info.title ? normalize(info.title, info.headerLine) : {};
194
+ const issue = (line, zh, en, fixZh, fixEn) => errors.push(error(lang, line, zh, en, fixZh, fixEn));
195
+ if (String(source ?? "").length > LIMITS.chars) {
196
+ issue(start, `源码超过 ${LIMITS.chars} 个字符`, `Source exceeds ${LIMITS.chars} characters`, "拆成多张事件模型图", "Split the source into several event modeling diagrams");
197
+ return {
198
+ model: null,
199
+ errors
200
+ };
201
+ }
202
+ const comment = { block: false };
203
+ const readBlock = (from) => {
204
+ const body = [];
205
+ for (let index = from; index < raw.length; index += 1) {
206
+ if (/^}\s*$/.test(raw[index])) return {
207
+ body,
208
+ next: index + 1
209
+ };
210
+ body.push(raw[index]);
211
+ }
212
+ return {
213
+ body: null,
214
+ next: raw.length
215
+ };
216
+ };
217
+ for (let i = 0; i < raw.length; i += 1) {
218
+ const line = start + i;
219
+ const text = stripComments(raw[i], comment).trim();
220
+ if (!text) continue;
221
+ const head = (/^([A-Za-z][\w-]*)/.exec(text)?.[1] || "").toLowerCase();
222
+ if (FRAME_KEYWORDS.has(head)) {
223
+ const match = /^(tf|timeframe|rf|resetframe)\s+(\d{1,3})\s+([A-Za-z]+)\s+([A-Za-z_][\w]*(?:\.[A-Za-z_][\w]*)*)([\s\S]*)$/.exec(text);
224
+ if (!match) {
225
+ issue(line, "时间框写法无效", "Invalid time frame", "使用「tf 01 evt ItemAdded」或「timeframe 01 event ItemAdded」", "Use \"tf 01 evt ItemAdded\" or \"timeframe 01 event ItemAdded\"");
226
+ continue;
227
+ }
228
+ const type = ENTITY_KEYWORDS.get(match[3].toLowerCase());
229
+ if (!type) {
230
+ issue(line, `不支持的时间框类型「${match[3]}」`, `Unknown time frame type: ${match[3]}`, "使用 ui、pcr/processor、cmd/command、rmo/readmodel、evt/event", "Use ui, pcr/processor, cmd/command, rmo/readmodel, or evt/event");
231
+ continue;
232
+ }
233
+ let rest = match[5];
234
+ const sources = [];
235
+ const take = (pattern) => {
236
+ const found = pattern.exec(rest);
237
+ if (!found) return null;
238
+ rest = rest.slice(found[0].length);
239
+ return found;
240
+ };
241
+ let arrow;
242
+ while (arrow = take(/^\s*->>\s*(\d{1,3})(?![\w])/)) sources.push(arrow[1]);
243
+ const ref = take(/^\s*\[\[\s*([A-Za-z_][\w]*)\s*\]\]/);
244
+ rest = rest.trim();
245
+ let inline = null;
246
+ if (rest) {
247
+ let dataType = null, body = rest;
248
+ const typed = /^`([A-Za-z]+)`\s*([\s\S]*)$/.exec(rest);
249
+ if (typed) {
250
+ dataType = typed[1].toLowerCase();
251
+ body = typed[2].trim();
252
+ }
253
+ if (dataType && !DATA_TYPES.has(dataType)) hints.push(warning("DIAGRAM_HINT", lang, line, `未知的行内数据类型「${dataType}」,按文本显示`, `Unknown inline data type: ${dataType}; shown as text`, "使用 json、text 等受支持的类型", "Use a supported type such as json or text"));
254
+ if (/^\{[\s\S]*\}$/.test(body)) body = body.slice(1, -1).trim();
255
+ else if (/^"[\s\S]*"$/.test(body) || /^'[\s\S]*'$/.test(body)) body = body.slice(1, -1).trim();
256
+ else {
257
+ issue(line, "行内数据写法无效", "Invalid inline data", "用花括号或引号包住数据,例如 { description: string }", "Wrap the data in braces or quotes, for example { description: string }");
258
+ body = "";
259
+ }
260
+ inline = {
261
+ type: dataType,
262
+ body
263
+ };
264
+ }
265
+ const parts = qualifier(match[4]);
266
+ frames.push({
267
+ id: match[2],
268
+ kind: /^r/.test(match[1].toLowerCase()) ? "rf" : "tf",
269
+ type,
270
+ identifier: match[4],
271
+ namespace: parts.length === 2 ? parts[0] : null,
272
+ name: parts.length === 2 ? parts[1] : parts.join("."),
273
+ sources,
274
+ dataRef: ref ? ref[1] : null,
275
+ inline,
276
+ line
277
+ });
278
+ continue;
279
+ }
280
+ if (head === "data" || head === "note") {
281
+ const match = head === "data" ? /^data\s+([A-Za-z_][\w]*)\s*(?:`([A-Za-z]+)`)?\s*\{\s*$/.exec(text) : /^note\s+(\d{1,3})\s*(?:`([A-Za-z]+)`)?\s*\{\s*$/.exec(text);
282
+ if (!match) {
283
+ issue(line, `${head} 写法无效`, `Invalid ${head} block`, head === "data" ? "使用「data 名称 {」开块并在下一行起写内容" : "使用「note 01 {」开块并在下一行起写内容", head === "data" ? "Use \"data Name {\" and put the content on the following lines" : "Use \"note 01 {\" and put the content on the following lines");
284
+ continue;
285
+ }
286
+ const opened = readBlock(i + 1);
287
+ i = opened.next - 1;
288
+ if (!opened.body) {
289
+ issue(line, `${head} 数据块缺少结尾的 }`, `The ${head} block is missing its closing }`, "在数据块末尾补上单独一行的 }", "Add a line containing } at the end of the block");
290
+ continue;
291
+ }
292
+ const dataType = match[2] ? match[2].toLowerCase() : null;
293
+ if (dataType && !DATA_TYPES.has(dataType)) hints.push(warning("DIAGRAM_HINT", lang, line, `未知的数据类型「${dataType}」,按文本显示`, `Unknown data type: ${dataType}; shown as text`, "使用 json、text 等受支持的类型", "Use a supported type such as json or text"));
294
+ if (head === "data") dataBlocks.push({
295
+ id: match[1],
296
+ type: dataType,
297
+ lines: blockLines(opened.body),
298
+ line
299
+ });
300
+ else notes.push({
301
+ id: match[1],
302
+ type: dataType,
303
+ lines: blockLines(opened.body),
304
+ line
305
+ });
306
+ continue;
307
+ }
308
+ if (head === "entity") {
309
+ const match = /^entity\s+([A-Za-z_][\w]*(?:\.[A-Za-z_][\w]*)*)\s*$/.exec(text);
310
+ if (!match) issue(line, "entity 写法无效", "Invalid entity declaration", "使用「entity 名称」,不要附加其它内容", "Use \"entity Name\" without anything after it");
311
+ else entities.push({
312
+ name: match[1],
313
+ line
314
+ });
315
+ continue;
316
+ }
317
+ if (head === "gwt") {
318
+ let statement = text;
319
+ while (i + 1 < raw.length) {
320
+ const next = raw[i + 1].trim();
321
+ const keyword = next.split(/\s+/, 1)[0];
322
+ if (!next || next.startsWith("%%")) {
323
+ i += 1;
324
+ continue;
325
+ }
326
+ if (![
327
+ "given",
328
+ "when",
329
+ "then"
330
+ ].includes(keyword) && !ENTITY_KEYWORDS.has(keyword)) break;
331
+ statement += ` ${stripComments(raw[++i], comment).trim()}`;
332
+ }
333
+ const match = /^gwt\s+(\d{1,3})\s+(.*)$/.exec(statement);
334
+ const sections = match && readGwt(match[2]);
335
+ if (!sections) {
336
+ issue(line, "gwt 写法无效", "Invalid gwt statement", "使用「gwt 01 given evt A when cmd B then rmo C」", "Use \"gwt 01 given evt A when cmd B then rmo C\"");
337
+ continue;
338
+ }
339
+ gwt.push({
340
+ id: match[1],
341
+ sections,
342
+ line
343
+ });
344
+ continue;
345
+ }
346
+ if (head === "title") {
347
+ if (!info.title) ({text: title, icon: titleIcon} = normalize(text.replace(/^title\s*:?\s*/i, ""), line));
348
+ continue;
349
+ }
350
+ if (head === "acctitle" || head === "accdescr") {
351
+ if (/^accDescr\s*\{/i.test(text)) {
352
+ while (i + 1 < raw.length && !/^\s*\}/.test(raw[i + 1])) i += 1;
353
+ i += 1;
354
+ }
355
+ continue;
356
+ }
357
+ issue(line, `无法识别这一行「${text}」`, `Unrecognized line: ${text}`, "使用 tf/rf 时间框、data 数据块、note、entity 或 gwt", "Use tf/rf time frames, data blocks, note, entity, or gwt");
358
+ }
359
+ const seen = /* @__PURE__ */ new Set();
360
+ for (const frame of frames) {
361
+ if (seen.has(frame.id)) {
362
+ errors.push({
363
+ ...error(lang, frame.line, `时间框编号「${frame.id}」重复`, `Duplicate event modeling frame ID "${frame.id}"`, "给每个时间框分配唯一编号", "Give every time frame a unique id"),
364
+ code: "eventmodeling.duplicate-frame"
365
+ });
366
+ continue;
367
+ }
368
+ seen.add(frame.id);
369
+ }
370
+ if (frames.length > LIMITS.workFrames) issue(frames[LIMITS.workFrames].line, `时间框超过渲染上限 ${LIMITS.workFrames} 个`, `More than ${LIMITS.workFrames} time frames cannot be drawn`, "按业务阶段拆成多张图", "Split the timeline into several diagrams");
371
+ const byId = new Map(frames.map((frame) => [frame.id, frame]));
372
+ const blocks = new Map(dataBlocks.map((block) => [block.id, block]));
373
+ for (const frame of frames) {
374
+ for (const id of frame.sources) if (!byId.has(id)) issue(frame.line, `引用的时间框「${id}」不存在`, `Unknown time frame reference: ${id}`, "使用本图中已声明的时间框编号", "Use the id of a time frame declared in this diagram");
375
+ if (frame.dataRef && !blocks.has(frame.dataRef)) issue(frame.line, `引用的数据块「${frame.dataRef}」不存在`, `Unknown data block: ${frame.dataRef}`, "先声明 data 数据块,或修正 [[…]] 中的名称", "Declare the data block, or fix the name inside [[…]]");
376
+ }
377
+ for (const item of gwt) if (!byId.has(item.id)) issue(item.line, `gwt 引用的时间框「${item.id}」不存在`, `gwt references an unknown time frame: ${item.id}`, "使用本图中已声明的时间框编号", "Use the id of a time frame declared in this diagram");
378
+ const referenced = new Set(gwt.flatMap((item) => Object.values(item.sections).flat().map((entry) => entry.id)));
379
+ const unused = entities.filter((entry) => !referenced.has(entry.name.split(".").pop()));
380
+ if (unused.length) hints.push(warning("DIAGRAM_HINT", lang, unused[0].line, `entity 声明未在 gwt 中使用:${unused.map((entry) => entry.name).join("、")}`, `Entities not used by any gwt statement: ${unused.map((entry) => entry.name).join(", ")}`, "在 gwt 中引用这些实体,或删除声明", "Reference the entities in a gwt statement, or remove the declarations"));
381
+ return {
382
+ model: {
383
+ type: "eventmodeling",
384
+ title,
385
+ titleIcon,
386
+ frames,
387
+ dataBlocks,
388
+ notes,
389
+ gwt,
390
+ entities
391
+ },
392
+ errors: [...errors, ...hints]
393
+ };
394
+ }
395
+ function validate(model, info = {}) {
396
+ const lang = info.lang || "zh";
397
+ const items = [];
398
+ for (const frame of model.frames) {
399
+ const allowed = SOURCE_TYPES[frame.type];
400
+ if (!allowed || !frame.sources.length) continue;
401
+ for (const id of frame.sources) {
402
+ const source = model.frames.find((entry) => entry.id === id);
403
+ if (source && source !== frame && !allowed.includes(source.type)) items.push({
404
+ frame,
405
+ source
406
+ });
407
+ }
408
+ }
409
+ const self = model.frames.filter((frame) => frame.sources.includes(frame.id));
410
+ const diagnostics = [];
411
+ if (items.length) {
412
+ const first = items[0].frame;
413
+ const detail = items.slice(0, 3).map(({ frame, source }) => `${source.id} ${typeName(source.type, lang)} → ${frame.id} ${typeName(frame.type, lang)}`).join(";");
414
+ const expected = [...new Set(SOURCE_TYPES[first.type])].map((type) => typeName(type, lang)).join("/");
415
+ diagnostics.push(warning("DIAGRAM_HINT", lang, first.line, `「${typeName(first.type, lang)}」只应接收来自「${expected}」的输入:${detail}`, `A ${typeName(first.type, "en")} should only receive input from a ${[...new Set(SOURCE_TYPES[first.type])].map((type) => typeName(type, "en")).join("/")}: ${detail}`, "调整 ->> 指向的来源时间框", "Point ->> at a matching upstream time frame"));
416
+ }
417
+ for (const frame of self) diagnostics.push(warning("DIAGRAM_HINT", lang, frame.line, `时间框「${frame.id}」引用了自己,已忽略`, `Time frame ${frame.id} references itself and is skipped`, "去掉指向自身的 ->>", "Remove the ->> that points at the frame itself"));
418
+ return diagnostics;
419
+ }
420
+ function dataLinesOf(frame, block, measure, maxText, narrow) {
421
+ const source = frame.inline || (block ? {
422
+ type: block.type,
423
+ body: block.lines.join("\n")
424
+ } : null);
425
+ if (!source) return null;
426
+ const rows = String(source.body ?? "").split("\n").map((row) => row.replace(/\s+$/, ""));
427
+ while (rows.length && !rows[0].trim()) rows.shift();
428
+ while (rows.length && !rows[rows.length - 1].trim()) rows.pop();
429
+ const lines = rows.flatMap((row) => wrapText(row, maxText, DATA_FONT, measure));
430
+ const visible = lines.slice(0, narrow ? 4 : LIMITS.dataLines);
431
+ if (lines.length > visible.length) {
432
+ if (narrow) visible.push(`+${lines.length - visible.length}`);
433
+ else visible[visible.length - 1] = "…";
434
+ }
435
+ return {
436
+ type: source.type || null,
437
+ lines: visible,
438
+ total: lines.length,
439
+ text: rows.join("\n")
440
+ };
441
+ }
442
+ function layout(model, options = {}, info = {}) {
443
+ const lang = options.lang || info.lang || "zh";
444
+ const width = Math.max(1, Number(options.width) || 800);
445
+ const narrow = width < 560;
446
+ const padding = mapConfig(mapConfig(info.config, "eventmodeling"), "padding");
447
+ const pad = typeof padding === "number" && Number.isFinite(padding) ? Math.min(120, Math.max(8, padding)) : PAD;
448
+ const measure = options.measure || ((value, font = FONT.body) => [...String(value ?? "")].length * font.size * .56);
449
+ const errors = [];
450
+ if (!model.frames.length) {
451
+ errors.push(error(lang, info.firstLine || 1, "图中没有时间框", "The diagram has no time frame", "至少写一行「tf 01 ui 名称」", "Add at least one \"tf 01 ui Name\" line"));
452
+ return {
453
+ width,
454
+ height: 0,
455
+ errors
456
+ };
457
+ }
458
+ const lanes = [], laneKeys = /* @__PURE__ */ new Map();
459
+ for (const frame of model.frames) {
460
+ const spec = TYPES[frame.type];
461
+ const key = `${spec.band}:${frame.namespace || ""}`;
462
+ let lane = laneKeys.get(key);
463
+ if (!lane) {
464
+ const used = lanes.filter((entry) => entry.band === spec.band).map((entry) => entry.index);
465
+ lane = {
466
+ id: `lane-${lanes.length}`,
467
+ band: spec.band,
468
+ index: frame.namespace ? Math.max(spec.band, ...used) + 1 : spec.band,
469
+ namespace: frame.namespace,
470
+ label: frame.namespace ? message(lang, ...spec.prefix) + frame.namespace : message(lang, ...spec.lane)
471
+ };
472
+ lanes.push(lane);
473
+ laneKeys.set(key, lane);
474
+ }
475
+ frame.lane = lane;
476
+ }
477
+ const rows = [...lanes].sort((a, b) => a.band - b.band || a.index - b.index);
478
+ const maxText = 400;
479
+ const blocks = new Map(model.dataBlocks.map((block) => [block.id, block]));
480
+ const boxes = model.frames.map((frame, index) => {
481
+ const spec = TYPES[frame.type];
482
+ const glyphW = Math.ceil(measure(spec.glyph, GLYPH_FONT));
483
+ const nameLines = wrapText(frame.name, maxText - glyphW - GLYPH_GAP, NAME_FONT, measure);
484
+ const data = dataLinesOf(frame, frame.dataRef ? blocks.get(frame.dataRef) : null, measure, maxText, narrow);
485
+ const contentW = Math.max(maxWidthOf(nameLines, NAME_FONT, measure) + glyphW + GLYPH_GAP, data ? maxWidthOf(data.lines, DATA_FONT, measure) : 0);
486
+ const w = Math.min(MAX_BOX_W, Math.max(MIN_BOX_W, snap(contentW + 20)));
487
+ const contentH = nameLines.length * NAME_STEP + (data?.lines.length ? DATA_GAP + data.lines.length * DATA_STEP : 0);
488
+ const h = Math.max(MIN_BOX_H, snap(20 + contentH));
489
+ return {
490
+ frame,
491
+ index,
492
+ lane: frame.lane,
493
+ glyph: spec.glyph,
494
+ glyphW,
495
+ nameLines,
496
+ data,
497
+ contentW,
498
+ contentH,
499
+ w,
500
+ h,
501
+ x: 0,
502
+ r: 0,
503
+ y: 0
504
+ };
505
+ });
506
+ const labelWidth = Math.max(56, ...rows.map((lane) => Math.ceil(measure(lane.label, LANE_FONT))));
507
+ const gutter = narrow ? LANE_PAD : labelWidth + 24;
508
+ const laneX = pad, columnX = pad + gutter;
509
+ const laneRight = /* @__PURE__ */ new Map();
510
+ let previous = null;
511
+ for (const box of boxes) {
512
+ const own = laneRight.get(box.lane) ?? null;
513
+ let x = previous === null ? columnX : previous + COLUMN_GAP;
514
+ if (own !== null) x = Math.max(x, own + COLUMN_GAP);
515
+ box.x = snap(x);
516
+ box.r = box.x + box.w;
517
+ laneRight.set(box.lane, box.r);
518
+ previous = box.r;
519
+ }
520
+ const contentRight = Math.max(...boxes.map((box) => box.r));
521
+ const laneRightX = Math.max(contentRight + LANE_PAD, pad + labelWidth + 24);
522
+ const rowOf = new Map(rows.map((lane, index) => [lane, index]));
523
+ const boxOf = new Map(boxes.map((box) => [box.frame.id, box]));
524
+ const relations = [];
525
+ for (const box of boxes) {
526
+ const { frame } = box;
527
+ if (frame.sources.length) {
528
+ for (const id of frame.sources) {
529
+ const source = boxOf.get(id);
530
+ if (source && source !== box) relations.push({
531
+ from: id,
532
+ to: frame.id,
533
+ line: frame.line
534
+ });
535
+ }
536
+ continue;
537
+ }
538
+ if (box.index === 0 || frame.kind === "rf") continue;
539
+ for (let k = box.index - 1; k >= 0; k -= 1) {
540
+ if (boxes[k].lane === box.lane) continue;
541
+ relations.push({
542
+ from: boxes[k].frame.id,
543
+ to: frame.id,
544
+ line: frame.line
545
+ });
546
+ break;
547
+ }
548
+ }
549
+ const sideUse = /* @__PURE__ */ new Map(), stripUse = /* @__PURE__ */ new Map(), gapTracks = /* @__PURE__ */ new Map();
550
+ const takeX = (box, side, preferred) => {
551
+ const lo = box.x + SIDE_INSET, hi = box.r - SIDE_INSET;
552
+ const key = `${box.frame.id}:${side}`;
553
+ const used = sideUse.get(key) || [];
554
+ const base = Math.min(hi, Math.max(lo, preferred));
555
+ const offsets = [0];
556
+ for (let step = 1; step <= 24; step += 1) offsets.push(-step * 10, step * 10);
557
+ for (const offset of offsets) {
558
+ const x = Math.round(base + offset);
559
+ if (x < lo - .01 || x > hi + .01 || used.some((value) => Math.abs(value - x) < 8)) continue;
560
+ used.push(x);
561
+ sideUse.set(key, used);
562
+ return x;
563
+ }
564
+ used.push(base);
565
+ sideUse.set(key, used);
566
+ return base;
567
+ };
568
+ const takeStrip = (candidates, laneLo, laneHi) => {
569
+ for (const index of candidates) {
570
+ const left = Math.round(boxes[index].r), room = boxes[index + 1].x - boxes[index].r;
571
+ const list = stripUse.get(index) || [];
572
+ for (let slot = 1; slot * SLOT < room - 2; slot += 1) {
573
+ const x = left + slot * SLOT;
574
+ if (list.some((item) => item.slot === slot && item.lo <= laneHi && laneLo <= item.hi)) continue;
575
+ list.push({
576
+ slot,
577
+ lo: laneLo,
578
+ hi: laneHi
579
+ });
580
+ stripUse.set(index, list);
581
+ return x;
582
+ }
583
+ }
584
+ const index = candidates[0];
585
+ const x = Math.round(boxes[index].r + SLOT);
586
+ const list = stripUse.get(index) || [];
587
+ list.push({
588
+ slot: 1,
589
+ lo: laneLo,
590
+ hi: laneHi
591
+ });
592
+ stripUse.set(index, list);
593
+ return x;
594
+ };
595
+ const takeTrack = (gap, lo, hi) => {
596
+ const list = gapTracks.get(gap) || [];
597
+ for (let track = 0; track < 12; track += 1) {
598
+ if (list.some((item) => item.track === track && item.lo - 4 <= hi && lo <= item.hi + 4)) continue;
599
+ list.push({
600
+ track,
601
+ lo,
602
+ hi
603
+ });
604
+ gapTracks.set(gap, list);
605
+ return track;
606
+ }
607
+ list.push({
608
+ track: 0,
609
+ lo,
610
+ hi
611
+ });
612
+ gapTracks.set(gap, list);
613
+ return 0;
614
+ };
615
+ const plans = [];
616
+ for (const relation of relations) {
617
+ const source = boxOf.get(relation.from), target = boxOf.get(relation.to);
618
+ const sourceRow = rowOf.get(source.lane), targetRow = rowOf.get(target.lane);
619
+ const plan = {
620
+ from: relation.from,
621
+ to: relation.to,
622
+ line: relation.line
623
+ };
624
+ const middle = (Math.max(source.x, target.x) + Math.min(source.r, target.r)) / 2;
625
+ if (sourceRow === targetRow) {
626
+ const below = sourceRow < rows.length - 1 || rows.length === 1;
627
+ const gap = below ? sourceRow : sourceRow - 1;
628
+ plan.kind = "hook";
629
+ plan.gap = gap;
630
+ plan.side = below ? "bottom" : "top";
631
+ plan.exitX = takeX(source, plan.side, middle);
632
+ plan.entryX = takeX(target, plan.side, middle);
633
+ plan.track = takeTrack(gap, Math.min(plan.exitX, plan.entryX), Math.max(plan.exitX, plan.entryX));
634
+ plans.push(plan);
635
+ continue;
636
+ }
637
+ const down = targetRow > sourceRow;
638
+ plan.exitSide = down ? "bottom" : "top";
639
+ plan.entrySide = down ? "top" : "bottom";
640
+ const gapSource = down ? sourceRow : sourceRow - 1;
641
+ const gapTarget = down ? targetRow - 1 : targetRow;
642
+ if (gapSource === gapTarget) {
643
+ plan.exitX = takeX(source, plan.exitSide, middle);
644
+ plan.entryX = takeX(target, plan.entrySide, middle);
645
+ if (Math.abs(plan.exitX - plan.entryX) < .5) {
646
+ plan.kind = "straight";
647
+ plans.push(plan);
648
+ continue;
649
+ }
650
+ plan.kind = "elbow";
651
+ plan.gap = gapSource;
652
+ plan.track = takeTrack(gapSource, Math.min(plan.exitX, plan.entryX), Math.max(plan.exitX, plan.entryX));
653
+ plans.push(plan);
654
+ continue;
655
+ }
656
+ const low = Math.min(source.index, target.index), high = Math.max(source.index, target.index);
657
+ const candidates = [];
658
+ for (let index = low; index < high; index += 1) candidates.push(index);
659
+ plan.stripX = takeStrip(candidates, Math.min(sourceRow, targetRow) + 1, Math.max(sourceRow, targetRow) - 1);
660
+ plan.exitX = takeX(source, plan.exitSide, plan.stripX);
661
+ plan.entryX = takeX(target, plan.entrySide, plan.stripX);
662
+ plan.kind = "cross";
663
+ plan.gapA = gapSource;
664
+ plan.gapB = gapTarget;
665
+ plan.trackA = takeTrack(gapSource, Math.min(plan.exitX, plan.stripX), Math.max(plan.exitX, plan.stripX));
666
+ plan.trackB = takeTrack(gapTarget, Math.min(plan.entryX, plan.stripX), Math.max(plan.entryX, plan.stripX));
667
+ plans.push(plan);
668
+ }
669
+ for (const lane of rows) {
670
+ const own = boxes.filter((box) => box.lane === lane);
671
+ lane.maxH = Math.max(0, ...own.map((box) => box.h));
672
+ lane.h = Math.max(MIN_LANE_H, lane.maxH + 24 + (narrow ? 18 : 0));
673
+ lane.w = laneRightX - laneX;
674
+ lane.x = laneX;
675
+ lane.labelX = lane.x + 10;
676
+ }
677
+ const gapH = rows.map((_, gap) => {
678
+ const tracks = gapTracks.get(gap) || [];
679
+ const depth = Math.max(0, ...tracks.map((item) => item.track));
680
+ return tracks.length ? LANE_GAP + depth * TRACK : gap < rows.length - 1 ? LANE_GAP : 0;
681
+ });
682
+ const { title, titleIcon } = model;
683
+ const titleLines = title || titleIcon ? wrapText(title, laneRightX - pad - (titleIcon ? 20 : 0), FONT.title, measure) : [];
684
+ let cursor = pad + titleLines.length * 20;
685
+ rows.forEach((lane, index) => {
686
+ lane.y = cursor;
687
+ lane.labelY = lane.y + (narrow ? 13 : lane.h / 2);
688
+ cursor += lane.h + gapH[index];
689
+ });
690
+ const stackBottom = cursor;
691
+ for (const box of boxes) box.y = box.lane.y + LANE_PAD + (narrow ? 18 : 0);
692
+ const trackY = (gap, track) => rows[gap].y + rows[gap].h + 6 + track * TRACK;
693
+ const edges = plans.map((plan) => {
694
+ const source = boxOf.get(plan.from), target = boxOf.get(plan.to);
695
+ const exitY = (plan.exitSide || plan.side) === "bottom" ? source.y + source.h : source.y;
696
+ const entryY = (plan.entrySide || plan.side) === "bottom" ? target.y + target.h : target.y;
697
+ let points;
698
+ if (plan.kind === "straight") points = [{
699
+ x: plan.exitX,
700
+ y: exitY
701
+ }, {
702
+ x: plan.entryX,
703
+ y: entryY
704
+ }];
705
+ else if (plan.kind === "elbow" || plan.kind === "hook") points = [
706
+ {
707
+ x: plan.exitX,
708
+ y: exitY
709
+ },
710
+ {
711
+ x: plan.exitX,
712
+ y: trackY(plan.gap, plan.track)
713
+ },
714
+ {
715
+ x: plan.entryX,
716
+ y: trackY(plan.gap, plan.track)
717
+ },
718
+ {
719
+ x: plan.entryX,
720
+ y: entryY
721
+ }
722
+ ];
723
+ else points = [
724
+ {
725
+ x: plan.exitX,
726
+ y: exitY
727
+ },
728
+ {
729
+ x: plan.exitX,
730
+ y: trackY(plan.gapA, plan.trackA)
731
+ },
732
+ {
733
+ x: plan.stripX,
734
+ y: trackY(plan.gapA, plan.trackA)
735
+ },
736
+ {
737
+ x: plan.stripX,
738
+ y: trackY(plan.gapB, plan.trackB)
739
+ },
740
+ {
741
+ x: plan.entryX,
742
+ y: trackY(plan.gapB, plan.trackB)
743
+ },
744
+ {
745
+ x: plan.entryX,
746
+ y: entryY
747
+ }
748
+ ];
749
+ const simplified = points.filter((point, index) => !index || point.x !== points[index - 1].x || point.y !== points[index - 1].y);
750
+ return {
751
+ from: plan.from,
752
+ to: plan.to,
753
+ line: plan.line,
754
+ points: simplified,
755
+ d: simplified.map((point, index) => `${index ? "L" : "M"}${Math.round(point.x)} ${Math.round(point.y)}`).join(" ")
756
+ };
757
+ });
758
+ const captions = [];
759
+ const captionX = laneX + 4, captionWidth = Math.max(120, laneRightX - captionX - 12);
760
+ let captionY = stackBottom + 18;
761
+ const pushCaption = (text, kind, fill) => {
762
+ captions.push({
763
+ x: captionX,
764
+ y: captionY,
765
+ text,
766
+ kind,
767
+ fill
768
+ });
769
+ captionY += CAPTION_STEP;
770
+ };
771
+ const label = (value) => message(lang, ...value);
772
+ for (const block of model.gwt) {
773
+ if (captions.length) captionY += 6;
774
+ pushCaption(`GWT ${block.id}`, "heading");
775
+ for (const key of [
776
+ "given",
777
+ "when",
778
+ "then"
779
+ ]) {
780
+ const list = block.sections[key];
781
+ if (!list.length) continue;
782
+ const text = `${label({
783
+ given: ["给定 Given", "Given"],
784
+ when: ["当 When", "When"],
785
+ then: ["那么 Then", "Then"]
786
+ }[key])}: ${list.map((entry) => `${entry.type} ${entry.id}`).join(" · ")}`;
787
+ for (const line of wrapText(text, captionWidth, CAPTION_FONT, measure)) pushCaption(line, "body");
788
+ }
789
+ }
790
+ for (const note of model.notes) {
791
+ if (captions.length) captionY += 6;
792
+ pushCaption(`Note ${note.id}`, "heading");
793
+ for (const line of note.lines.flatMap((row) => wrapText(row, captionWidth, CAPTION_FONT, measure))) pushCaption(line, "body");
794
+ }
795
+ if (model.entities.length) {
796
+ if (captions.length) captionY += 6;
797
+ pushCaption(label(["实体", "Entities"]), "heading");
798
+ for (const row of wrapText(model.entities.map((entity) => entity.name).join(" · "), captionWidth, CAPTION_FONT, measure)) pushCaption(row, "body");
799
+ }
800
+ const height = captions.length ? captionY + pad - CAPTION_STEP + 4 : stackBottom + pad;
801
+ const sceneWidth = laneRightX + pad, sceneHeight = snap(Math.max(1, height));
802
+ if (model.frames.length > LIMITS.largeFrames || sceneWidth > width * 2) errors.push(warning("LARGE_DIAGRAM", lang, info.firstLine || 1, `图较大(${model.frames.length} 个时间框、宽 ${sceneWidth}px),建议拆成多张`, `Large diagram (${model.frames.length} time frames, ${sceneWidth}px wide); consider splitting it`, "按业务阶段拆分时间线", "Split the timeline by business stage"));
803
+ return {
804
+ type: "eventmodeling",
805
+ width: sceneWidth,
806
+ height: sceneHeight,
807
+ lanes: rows,
808
+ boxes,
809
+ edges,
810
+ captions,
811
+ errors,
812
+ title,
813
+ titleIcon,
814
+ titleLines,
815
+ pad
816
+ };
817
+ }
818
+ function scene(geometry, ctx) {
819
+ const look = appearance(ctx);
820
+ const children = [];
821
+ const { pad, titleIcon, titleLines } = geometry;
822
+ if (titleLines.length) {
823
+ const y = Math.max(9, pad);
824
+ children.push(ctx.textLines(pad + (titleIcon ? 20 : 0), y, titleLines, {
825
+ ...FONT.title,
826
+ leading: 20,
827
+ fill: look.fg1,
828
+ "dominant-baseline": "central"
829
+ }));
830
+ if (titleIcon) children.push(mapIcon(ctx, titleIcon, pad + 7, y, look.fg1));
831
+ }
832
+ const marker = "em-arrow";
833
+ children.push(ctx.el("defs", {}, [ctx.el("marker", {
834
+ id: marker,
835
+ viewBox: "0 0 8 8",
836
+ refX: 7,
837
+ refY: 4,
838
+ markerWidth: 8,
839
+ markerHeight: 8,
840
+ markerUnits: "userSpaceOnUse",
841
+ orient: "auto"
842
+ }, [ctx.path("M0 0 L8 4 L0 8 Z", { fill: ctx.color("muted") })])]));
843
+ for (const [index, lane] of geometry.lanes.entries()) {
844
+ const band = ctx.el("g", {}, [ctx.rect(lane.x, lane.y, lane.w, lane.h, {
845
+ fill: index % 2 ? look.surface2 : look.surface,
846
+ stroke: look.line,
847
+ "stroke-width": 1,
848
+ rx: look.radius,
849
+ meta: {
850
+ kind: "frame",
851
+ id: lane.id
852
+ }
853
+ }), ctx.text(lane.labelX, lane.labelY, lane.label, {
854
+ size: LANE_FONT.size,
855
+ weight: LANE_FONT.weight,
856
+ fill: look.fg3,
857
+ anchor: "start",
858
+ "dominant-baseline": "central"
859
+ })]);
860
+ band.meta = {
861
+ kind: "frame",
862
+ id: lane.id
863
+ };
864
+ children.push(band);
865
+ }
866
+ for (const box of geometry.boxes) {
867
+ const spec = TYPES[box.frame.type];
868
+ const tone = spec.role ? ctx.frameTone(ctx.color(spec.role)) : null;
869
+ const fill = tone ? ctx.tint(look.bg, tone, .12) : look.node;
870
+ const stroke = tone ? ctx.tint(look.bg, tone, .45) : look.line2;
871
+ const ink = ctx.textOn(fill);
872
+ const glyphInk = tone ? ctx.textColor(tone) : look.fg3;
873
+ const contentX = box.x + Math.max(BOX_PAD, (box.w - box.contentW) / 2);
874
+ const contentY = box.y + (box.h - box.contentH) / 2;
875
+ const nameX = contentX + box.glyphW + GLYPH_GAP;
876
+ const nameY = contentY + NAME_STEP / 2;
877
+ const parts = [ctx.rect(box.x, box.y, box.w, box.h, {
878
+ fill,
879
+ stroke,
880
+ "stroke-width": 1,
881
+ rx: look.smallRadius,
882
+ meta: {
883
+ kind: "node",
884
+ id: box.frame.id
885
+ }
886
+ }), ctx.text(contentX, nameY, box.glyph, {
887
+ size: GLYPH_FONT.size,
888
+ weight: GLYPH_FONT.weight,
889
+ fill: glyphInk,
890
+ anchor: "start",
891
+ "dominant-baseline": "central"
892
+ })];
893
+ for (const [index, line] of box.nameLines.entries()) parts.push(ctx.text(nameX, nameY + index * NAME_STEP, line, {
894
+ size: NAME_FONT.size,
895
+ weight: NAME_FONT.weight,
896
+ fill: ink,
897
+ anchor: "start",
898
+ "dominant-baseline": "central"
899
+ }));
900
+ const dataTop = contentY + box.nameLines.length * NAME_STEP + DATA_GAP;
901
+ for (const [index, line] of (box.data?.lines || []).entries()) parts.push(ctx.text(contentX, dataTop + DATA_STEP / 2 + index * DATA_STEP, line, {
902
+ size: DATA_FONT.size,
903
+ weight: DATA_FONT.weight,
904
+ fill: ink,
905
+ anchor: "start",
906
+ "dominant-baseline": "central"
907
+ }));
908
+ const tip = [
909
+ box.frame.id,
910
+ box.frame.identifier,
911
+ box.data?.text
912
+ ].filter(Boolean).join(" · ");
913
+ children.push(ctx.titled(ctx.el("g", {}, parts), tip));
914
+ }
915
+ for (const edge of geometry.edges) children.push(ctx.path(edge.d, {
916
+ fill: "none",
917
+ stroke: ctx.color("muted"),
918
+ "stroke-width": 1.25,
919
+ "stroke-linejoin": "round",
920
+ "marker-end": `url(#${marker})`,
921
+ meta: {
922
+ kind: "edge",
923
+ from: edge.from,
924
+ to: edge.to
925
+ }
926
+ }));
927
+ for (const line of geometry.captions) children.push(ctx.text(line.x, line.y, line.text, {
928
+ size: CAPTION_FONT.size,
929
+ weight: line.kind === "heading" ? LANE_FONT.weight : CAPTION_FONT.weight,
930
+ fill: line.kind === "heading" ? look.fg3 : look.fg2,
931
+ anchor: "start",
932
+ "dominant-baseline": "central"
933
+ }));
934
+ return {
935
+ width: geometry.width,
936
+ height: geometry.height,
937
+ title: geometry.title,
938
+ children
939
+ };
940
+ }
941
+ var implementations = { eventmodeling: {
942
+ parse,
943
+ validate,
944
+ layout,
945
+ scene,
946
+ honoredConfig: ["eventmodeling.padding"]
947
+ } };
948
+ export { eventmodeling_exports, implementations };