@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,748 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { error } from "./support.js";
3
+ // src/diagram/time.js
4
+ var time_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
5
+ var MAX_ITEMS = 80;
6
+ var MAX_LINES = 240;
7
+ var MAX_PACKET_BIT = 4095;
8
+ var kinds = [];
9
+ var issue = (lang, line, text) => error(lang, line, `无法识别或处理语句「${String(text).slice(0, 36)}」`, `Unrecognized or invalid statement: ${String(text).slice(0, 36)}`, "修正这条语句或移除不支持的语法", "Fix this statement or remove unsupported syntax");
10
+ var date = (s) => {
11
+ if (!/^\d{4}-\d{2}-\d{2}$/.test(s)) return NaN;
12
+ const [y, m, d] = s.split("-").map(Number);
13
+ if (y < 1) return NaN;
14
+ const t = /* @__PURE__ */ new Date(0);
15
+ t.setUTCHours(0, 0, 0, 0);
16
+ t.setUTCFullYear(y, m - 1, d);
17
+ const x = new Date(t.getTime());
18
+ return x.getUTCFullYear() === y && x.getUTCMonth() === m - 1 && x.getUTCDate() === d ? t.getTime() : NaN;
19
+ };
20
+ var addDays = (value, days) => {
21
+ if (!Number.isSafeInteger(Number(days))) return "";
22
+ const stamp = date(value) + Number(days) * 864e5;
23
+ if (!Number.isFinite(stamp)) return "";
24
+ const result = new Date(stamp), year = result.getUTCFullYear();
25
+ if (!Number.isFinite(result.getTime()) || year < 1 || year > 9999) return "";
26
+ return `${String(year).padStart(4, "0")}-${String(result.getUTCMonth() + 1).padStart(2, "0")}-${String(result.getUTCDate()).padStart(2, "0")}`;
27
+ };
28
+ var qstr = "(?:\"(?:[^\"\\\\]|\\\\.)*\"|[\\w.-]+)";
29
+ function parse(body, info, kind) {
30
+ const { lang, options = {}, firstLine = 2 } = info, errors = [];
31
+ const lines = String(body).replace(/\r\n?/g, "\n").split("\n");
32
+ for (const key of Object.keys(options)) errors.push(issue(lang, info.headerLine || firstLine - 1, `option ${key}`));
33
+ if (lines.length > MAX_LINES) errors.push(issue(lang, firstLine + MAX_LINES, `scope exceeds ${MAX_LINES} lines`));
34
+ const model = {
35
+ type: kind,
36
+ title: info.title || "",
37
+ items: [],
38
+ sections: [],
39
+ branches: kind === "gitGraph" ? [{
40
+ name: "main",
41
+ head: ""
42
+ }] : [],
43
+ fields: [],
44
+ commits: [],
45
+ config: {}
46
+ };
47
+ let section = "", current = "main", indentColumn = null, itemCount = 0;
48
+ const countItem = (line) => {
49
+ if (itemCount >= MAX_ITEMS) {
50
+ errors.push(issue(lang, line, `scope exceeds ${MAX_ITEMS} items`));
51
+ return false;
52
+ }
53
+ itemCount += 1;
54
+ return true;
55
+ };
56
+ const add = (line, item) => {
57
+ if (countItem(line)) model.items.push({
58
+ ...item,
59
+ line,
60
+ section,
61
+ branch: current
62
+ });
63
+ };
64
+ const unquote = (s) => s?.startsWith("\"") ? s.slice(1, -1).replace(/\\(.)/g, "$1") : s;
65
+ lines.slice(0, MAX_LINES).forEach((raw, i) => {
66
+ const lineNo = firstLine + i, s = raw.trim();
67
+ if (!s || s.startsWith("%%")) return;
68
+ if (/^(title|accTitle|accDescr)(?:\s*:|\s+)/i.test(s)) {
69
+ const value = s.replace(/^[^:\s]+\s*:?[ \t]*/, "");
70
+ if (/^title/i.test(s)) model.title = value;
71
+ else errors.push(issue(lang, lineNo, s));
72
+ return;
73
+ }
74
+ if (kind === "gantt") {
75
+ let m;
76
+ if (m = /^dateFormat\s+(\S+)$/i.exec(s)) {
77
+ if (m[1] !== "YYYY-MM-DD") errors.push(issue(lang, lineNo, s));
78
+ else model.config.dateFormat = m[1];
79
+ return;
80
+ }
81
+ if (m = /^axisFormat\s+(\S+)$/i.exec(s)) {
82
+ if (m[1] !== "%Y-%m") errors.push(issue(lang, lineNo, s));
83
+ else model.config.axisFormat = m[1];
84
+ return;
85
+ }
86
+ if (/^(?:excludes|tickInterval|todayMarker)\b/i.test(s)) {
87
+ errors.push(issue(lang, lineNo, s));
88
+ return;
89
+ }
90
+ if (/^section\s+/i.test(s)) {
91
+ section = s.replace(/^section\s+/i, "").trim();
92
+ model.sections.push(section);
93
+ return;
94
+ }
95
+ m = /^(.+?)\s*:\s*(.*?)\s*$/.exec(s);
96
+ if (m) {
97
+ const name = m[1].trim(), parts = m[2].split(",").map((x) => x.trim()).filter(Boolean);
98
+ let flags = [], id = "", start = "", end = "", depends = "", duration = "";
99
+ for (const p of parts) {
100
+ if (/^(done|active|crit|milestone)$/i.test(p)) {
101
+ flags.push(p.toLowerCase());
102
+ continue;
103
+ }
104
+ if (/^after\s+/i.test(p)) {
105
+ if (depends) errors.push(issue(lang, lineNo, s));
106
+ depends = p.replace(/^after\s+/i, "").trim();
107
+ continue;
108
+ }
109
+ if (/^\d+d$/i.test(p)) {
110
+ if (duration) errors.push(issue(lang, lineNo, s));
111
+ duration = p.toLowerCase();
112
+ continue;
113
+ }
114
+ if (Number.isFinite(date(p))) {
115
+ if (!start) start = p;
116
+ else if (!end) end = p;
117
+ else errors.push(issue(lang, lineNo, s));
118
+ continue;
119
+ }
120
+ if (/^\d{4}/.test(p)) {
121
+ errors.push(issue(lang, lineNo, s));
122
+ continue;
123
+ }
124
+ if (!id && /^[\w.-]+$/.test(p)) {
125
+ id = p;
126
+ continue;
127
+ }
128
+ errors.push(issue(lang, lineNo, s));
129
+ }
130
+ if (!start && !depends) errors.push(issue(lang, lineNo, s));
131
+ if (end && date(end) < date(start)) errors.push(issue(lang, lineNo, s));
132
+ if (duration && !start && !depends) errors.push(issue(lang, lineNo, s));
133
+ if (duration && start && !end) {
134
+ end = addDays(start, Number.parseInt(duration, 10));
135
+ if (!end) errors.push(issue(lang, lineNo, s));
136
+ }
137
+ add(lineNo, {
138
+ name,
139
+ id,
140
+ flags,
141
+ start,
142
+ end: end || start,
143
+ milestone: flags.includes("milestone"),
144
+ depends,
145
+ duration
146
+ });
147
+ return;
148
+ }
149
+ } else if (kind === "timeline") {
150
+ if (/^section\s+/i.test(s)) {
151
+ section = s.slice(8).trim();
152
+ model.sections.push(section);
153
+ return;
154
+ }
155
+ if (s.startsWith(":")) {
156
+ if (!model.items.length) errors.push(issue(lang, lineNo, s));
157
+ else model.items.at(-1).events.push(s.slice(1).trim());
158
+ return;
159
+ }
160
+ const m = /^(.+?)\s*:\s*(.+)$/.exec(s);
161
+ if (m) {
162
+ add(lineNo, {
163
+ label: m[1].trim(),
164
+ events: m[2].split(":").map((x) => x.trim()).filter(Boolean)
165
+ });
166
+ return;
167
+ }
168
+ } else if (kind === "journey") {
169
+ if (/^section\s+/i.test(s)) {
170
+ section = s.slice(8).trim();
171
+ model.sections.push(section);
172
+ return;
173
+ }
174
+ const m = /^(.+?)\s*:\s*(\d+)\s*:\s*(.*)$/.exec(s);
175
+ if (m) {
176
+ const score = Number(m[2]);
177
+ if (score < 1 || score > 5) errors.push(issue(lang, lineNo, s));
178
+ else add(lineNo, {
179
+ name: m[1].trim(),
180
+ score,
181
+ actors: m[3].split(",").map((x) => x.trim()).filter(Boolean)
182
+ });
183
+ return;
184
+ }
185
+ } else if (kind === "gitGraph") {
186
+ let m;
187
+ if (m = new RegExp(`^commit(?:\\s+id:\\s*(${qstr}))?(?:\\s+tag:\\s*(${qstr}))?(?:\\s+type:\\s*(NORMAL|REVERSE|HIGHLIGHT))?$`, "i").exec(s)) {
188
+ const id = unquote(m[1]) || `commit-${model.commits.length + 1}`;
189
+ const commit = {
190
+ id,
191
+ tag: unquote(m[2]) || "",
192
+ commitType: (m[3] || "NORMAL").toUpperCase(),
193
+ branch: current,
194
+ parents: [],
195
+ line: lineNo
196
+ };
197
+ const head = model.branches.find((x) => x.name === current)?.head;
198
+ if (head) commit.parents.push(head);
199
+ if (model.commits.some((x) => x.id === id)) errors.push(issue(lang, lineNo, `duplicate commit ${id}`));
200
+ model.commits.push(commit);
201
+ add(lineNo, {
202
+ action: "commit",
203
+ id,
204
+ tag: commit.tag,
205
+ commitType: commit.commitType
206
+ });
207
+ const branch = model.branches.find((x) => x.name === current);
208
+ if (branch) branch.head = id;
209
+ return;
210
+ }
211
+ if (m = /^branch\s+([\w.-]+)$/i.exec(s)) {
212
+ const name = unquote(m[1]), from = current;
213
+ if (model.branches.some((x) => x.name === name)) errors.push(issue(lang, lineNo, s));
214
+ else {
215
+ model.branches.push({
216
+ name,
217
+ head: model.branches.find((x) => x.name === current)?.head || ""
218
+ });
219
+ current = name;
220
+ }
221
+ add(lineNo, {
222
+ action: "branch",
223
+ name,
224
+ from
225
+ });
226
+ return;
227
+ }
228
+ if (m = /^checkout\s+([\w.-]+)$/i.exec(s)) {
229
+ const name = unquote(m[1]);
230
+ if (!model.branches.some((x) => x.name === name)) errors.push(issue(lang, lineNo, s));
231
+ else current = name;
232
+ add(lineNo, {
233
+ action: "checkout",
234
+ name
235
+ });
236
+ return;
237
+ }
238
+ if (m = new RegExp("^merge\\s+([\\w.-]+)(?:\\s+id:\\s*(" + qstr + "))?$", "i").exec(s)) {
239
+ const name = unquote(m[1]), other = model.branches.find((x) => x.name === name), own = model.branches.find((x) => x.name === current);
240
+ if (!other || name === current || !other.head || !own?.head) errors.push(issue(lang, lineNo, s));
241
+ else {
242
+ const id = unquote(m[2]) || `merge-${model.commits.length + 1}`;
243
+ if (model.commits.some((x) => x.id === id)) errors.push(issue(lang, lineNo, `duplicate commit ${id}`));
244
+ else {
245
+ const commit = {
246
+ id,
247
+ tag: "",
248
+ commitType: "NORMAL",
249
+ branch: current,
250
+ parents: [own.head, other.head],
251
+ line: lineNo,
252
+ merge: name
253
+ };
254
+ model.commits.push(commit);
255
+ own.head = id;
256
+ add(lineNo, {
257
+ action: "merge",
258
+ id,
259
+ name,
260
+ parents: commit.parents
261
+ });
262
+ }
263
+ }
264
+ return;
265
+ }
266
+ } else if (kind === "kanban") {
267
+ const indent = raw.match(/^[ ]*/)[0].length;
268
+ if (/\t/.test(raw)) {
269
+ errors.push(issue(lang, lineNo, s));
270
+ return;
271
+ }
272
+ if (indent === 0) {
273
+ const m = /^([\w.-]+)\[([^\]]+)\]$/.exec(s);
274
+ if (!m || model.sections.some((x) => x.id === m[1])) {
275
+ errors.push(issue(lang, lineNo, s));
276
+ return;
277
+ }
278
+ if (!countItem(lineNo)) return;
279
+ model.sections.push({
280
+ id: m[1],
281
+ name: m[2].trim(),
282
+ line: lineNo
283
+ });
284
+ section = m[1];
285
+ indentColumn = null;
286
+ return;
287
+ }
288
+ if (!section) {
289
+ errors.push(issue(lang, lineNo, s));
290
+ return;
291
+ }
292
+ const m = /^([\w.-]+)\[([^\]]+)\]$/.exec(s);
293
+ if (!m || model.items.some((x) => x.id === m[1])) {
294
+ errors.push(issue(lang, lineNo, s));
295
+ return;
296
+ }
297
+ if (indentColumn == null) indentColumn = indent;
298
+ if (indent !== indentColumn) {
299
+ errors.push(issue(lang, lineNo, s));
300
+ return;
301
+ }
302
+ add(lineNo, {
303
+ id: m[1],
304
+ name: m[2].trim()
305
+ });
306
+ return;
307
+ } else if (kind === "packet-beta") {
308
+ const m = /^(\d+)-(\d+)\s*:\s*"((?:[^"\\]|\\.)*)"\s*$/.exec(s);
309
+ if (m) {
310
+ const start = Number(m[1]), end = Number(m[2]);
311
+ if (!Number.isSafeInteger(start) || !Number.isSafeInteger(end) || start > end || end > MAX_PACKET_BIT || model.fields.some((f) => start <= f.end && end >= f.start)) errors.push(issue(lang, lineNo, s));
312
+ else if (countItem(lineNo)) model.fields.push({
313
+ start,
314
+ end,
315
+ name: m[3].replace(/\\(.)/g, "$1"),
316
+ line: lineNo
317
+ });
318
+ return;
319
+ }
320
+ if (/^\+\d+\s*:/.test(s)) {
321
+ errors.push(issue(lang, lineNo, s));
322
+ return;
323
+ }
324
+ } else if (kind === "quadrantChart") {
325
+ let m;
326
+ if (m = /^([xy])-axis\s+(.+)$/i.exec(s)) {
327
+ const axis = `${m[1].toLowerCase()}axis`;
328
+ if (model[axis]) errors.push(issue(lang, lineNo, s));
329
+ else {
330
+ const labels = m[2].split(/\s*-->\s*/);
331
+ if (labels.length !== 2 || labels.some((x) => !x.trim())) errors.push(issue(lang, lineNo, s));
332
+ else model[axis] = labels.map((x) => x.trim());
333
+ }
334
+ return;
335
+ }
336
+ if (m = /^quadrant-([1-4])\s+(.+)$/i.exec(s)) {
337
+ const key = `quadrant${m[1]}`;
338
+ if (model[key]) errors.push(issue(lang, lineNo, s));
339
+ else model[key] = m[2].trim();
340
+ return;
341
+ }
342
+ if (m = /^(.+?)\s*:\s*([\d.]+)\s*,\s*([\d.]+)\s*$/.exec(s)) {
343
+ const x = Number(m[2]), y = Number(m[3]);
344
+ if (!Number.isFinite(x) || !Number.isFinite(y) || x < 0 || x > 1 || y < 0 || y > 1) errors.push(issue(lang, lineNo, s));
345
+ else add(lineNo, {
346
+ name: m[1].trim(),
347
+ x,
348
+ y
349
+ });
350
+ return;
351
+ }
352
+ }
353
+ errors.push(issue(lang, lineNo, s));
354
+ });
355
+ if (kind === "gantt") {
356
+ const ids = /* @__PURE__ */ new Map(), names = /* @__PURE__ */ new Map();
357
+ for (const task of model.items) {
358
+ if (task.id && ids.has(task.id)) errors.push(issue(lang, task.line, `duplicate task ID ${task.id}`));
359
+ if (task.id) ids.set(task.id, task);
360
+ if (!names.has(task.name)) names.set(task.name, []);
361
+ names.get(task.name).push(task);
362
+ }
363
+ const resolve = (ref) => ids.get(ref) || (names.get(ref)?.length === 1 ? names.get(ref)[0] : void 0), visiting = /* @__PURE__ */ new Set(), done = /* @__PURE__ */ new Set();
364
+ const visit = (task) => {
365
+ if (done.has(task)) return;
366
+ if (visiting.has(task)) {
367
+ errors.push(issue(lang, task.line, `dependency cycle ${task.name}`));
368
+ return;
369
+ }
370
+ visiting.add(task);
371
+ if (task.depends) {
372
+ const prev = resolve(task.depends);
373
+ if (!prev) errors.push(issue(lang, task.line, `after ${task.depends}`));
374
+ else {
375
+ visit(prev);
376
+ if (!task.start && prev.end) task.start = prev.end;
377
+ if (!task.start) errors.push(issue(lang, task.line, `unresolved start ${task.name}`));
378
+ else if (task.duration && !task.end) {
379
+ task.end = addDays(task.start, Number.parseInt(task.duration, 10));
380
+ if (!task.end) errors.push(issue(lang, task.line, task.name));
381
+ }
382
+ }
383
+ }
384
+ if (task.start && !task.end) task.end = task.start;
385
+ if (task.start && task.end && date(task.end) < date(task.start)) errors.push(issue(lang, task.line, task.name));
386
+ visiting.delete(task);
387
+ done.add(task);
388
+ };
389
+ model.items.forEach(visit);
390
+ }
391
+ if (kind === "gitGraph" && !model.commits.length) errors.push(issue(lang, firstLine, "gitGraph needs a commit"));
392
+ if (!model.items.length && !model.fields.length && !(kind === "kanban" && model.sections.length)) errors.push(issue(lang, firstLine, "empty diagram"));
393
+ return {
394
+ model,
395
+ errors
396
+ };
397
+ }
398
+ var text = (ctx, value, x, y, size = 13, fill = ctx.color("ink"), anchor = "start", weight = 400) => ctx.text(x, y, value, {
399
+ size,
400
+ fill,
401
+ anchor,
402
+ weight,
403
+ "dominant-baseline": "middle"
404
+ });
405
+ var wrap = (ctx, value, width, size) => ctx.wrap(String(value), Math.max(size, width), size, 400);
406
+ function layout(model, { measure, width, ctx, renderOptions }) {
407
+ const inSceneTitle = !renderOptions?.frame && Boolean(model.title);
408
+ const headRow = ["gantt", "journey"].includes(model.type) ? 14 : 0;
409
+ const right = 24, top = renderOptions?.frame ? 54 : (inSceneTitle ? 54 : 24) + headRow, rowH = 54;
410
+ const FLAG = /^en\b/i.test(ctx.lang || "zh") ? {
411
+ done: "done",
412
+ active: "in progress",
413
+ crit: "critical",
414
+ milestone: "milestone"
415
+ } : {
416
+ done: "已完成",
417
+ active: "进行中",
418
+ crit: "关键",
419
+ milestone: "里程碑"
420
+ };
421
+ const flagText = (task) => (task.flags || []).map((flag) => FLAG[flag] || flag).join(" · ");
422
+ const leftLabels = {
423
+ gantt: () => model.items.flatMap((x) => [
424
+ x.name,
425
+ x.section || "",
426
+ flagText(x)
427
+ ]),
428
+ journey: () => model.items.flatMap((x) => [x.section || "", x.name]),
429
+ gitGraph: () => model.branches.map((x) => x.name),
430
+ quadrantChart: () => model.yaxis || []
431
+ }[model.type]?.() ?? [];
432
+ const axisLabelWidth = Math.max(0, ...leftLabels.map((s) => measure(s, {
433
+ size: 13,
434
+ weight: 400
435
+ })));
436
+ const left = Math.max(24, axisLabelWidth + 24);
437
+ const minimum = model.type === "timeline" || model.type === "kanban" ? 0 : 300;
438
+ const usable = Math.max(minimum, width - left - right);
439
+ const colors = {
440
+ ink: ctx.color("ink"),
441
+ accent: ctx.color("accent"),
442
+ muted: ctx.color("muted"),
443
+ grid: ctx.color("grid"),
444
+ bg: ctx.color("bg")
445
+ };
446
+ const scene = {
447
+ width: Math.max(width, left + usable + right),
448
+ height: 180,
449
+ title: model.title || model.type,
450
+ description: `Mermaid ${model.type}`,
451
+ children: []
452
+ };
453
+ const el = ctx.el, label = (v, x, y, size = 13, fill = colors.ink, anchor = "start", weight = 400) => scene.children.push(text(ctx, v, x, y, size, fill, anchor, weight));
454
+ const rect = (x, y, w, h, fill = colors.bg, stroke = colors.grid, rx = 4) => scene.children.push(el("rect", {
455
+ x,
456
+ y,
457
+ width: w,
458
+ height: h,
459
+ fill,
460
+ stroke,
461
+ rx
462
+ }));
463
+ const line = (x1, y1, x2, y2, stroke = colors.grid, sw = 1) => scene.children.push(el("line", {
464
+ x1,
465
+ y1,
466
+ x2,
467
+ y2,
468
+ stroke,
469
+ "stroke-width": sw
470
+ }));
471
+ if (inSceneTitle) label(model.title, 22, 24, 17, colors.ink, "start", 650);
472
+ else if (renderOptions?.frame) scene.heading = model.title || void 0;
473
+ if (model.type === "gantt") {
474
+ const headerH = 30, rows = [];
475
+ let section;
476
+ for (const task of model.items) {
477
+ if (task.section && task.section !== section) rows.push({
478
+ header: task.section,
479
+ h: headerH
480
+ });
481
+ section = task.section;
482
+ rows.push({
483
+ task,
484
+ h: rowH
485
+ });
486
+ }
487
+ scene.height = top + rows.reduce((sum, row) => sum + row.h, 0) + 30;
488
+ const items = model.items.filter((x) => x.start), stamps = items.flatMap((x) => [date(x.start), date(x.end)]);
489
+ const first = new Date(Math.min(...stamps));
490
+ first.setUTCDate(1);
491
+ const min = first.getTime(), max = Math.max(...stamps), span = Math.max(864e5, max - min);
492
+ const xdate = (d) => left + (date(d) - min) / span * usable;
493
+ if (items.length) {
494
+ const tick = new Date(min);
495
+ let guard = 0;
496
+ while (tick.getTime() <= max && guard++ < 240) {
497
+ const x = left + (tick.getTime() - min) / span * usable;
498
+ line(x, top - 8, x, scene.height - 18);
499
+ label(`${String(tick.getUTCFullYear()).padStart(4, "0")}-${String(tick.getUTCMonth() + 1).padStart(2, "0")}`, x + 2, top - 18, 12, colors.muted);
500
+ tick.setUTCMonth(tick.getUTCMonth() + 1);
501
+ }
502
+ }
503
+ let y = top;
504
+ for (const row of rows) {
505
+ if (row.header) {
506
+ label(row.header, 12, y + 15, 13, colors.muted, "start", 600);
507
+ y += row.h;
508
+ continue;
509
+ }
510
+ const { task } = row, flags = flagText(task);
511
+ label(task.name, 12, y + (flags ? 8 : 14), 13);
512
+ if (flags) label(flags, 12, y + 26, 12, colors.muted);
513
+ if (task.start) {
514
+ const x = xdate(task.start), x2 = xdate(task.end);
515
+ if (task.milestone) scene.children.push(el("polygon", {
516
+ points: `${x},${y + 5} ${x + 9},${y + 14} ${x},${y + 23} ${x - 9},${y + 14}`,
517
+ fill: colors.accent
518
+ }));
519
+ else rect(x, y + 3, Math.max(4, x2 - x), 22, task.flags.includes("crit") ? colors.accent : colors.ink, "none", 4);
520
+ }
521
+ y += row.h;
522
+ }
523
+ } else if (model.type === "timeline") {
524
+ const n = Math.max(1, model.items.length), columnW = Math.max(180, usable / n), axisY = top + 18;
525
+ const wrapped = model.items.map((item) => ({
526
+ title: wrap(ctx, item.label, columnW - 20, 13),
527
+ events: wrap(ctx, item.events.join(" · "), columnW - 20, 12)
528
+ }));
529
+ const contentH = Math.max(82, ...wrapped.map((x) => x.title.length * 16 + x.events.length * 16 + 28));
530
+ scene.width = Math.max(scene.width, left + n * columnW + right);
531
+ scene.height = top + contentH + 45;
532
+ line(left, axisY, left + n * columnW, axisY, colors.grid, 2);
533
+ model.items.forEach((item, i) => {
534
+ const x = left + (i + .5) * columnW, y = axisY + 18, lines = wrapped[i];
535
+ scene.children.push(el("circle", {
536
+ cx: x,
537
+ cy: axisY,
538
+ r: 5,
539
+ fill: colors.accent
540
+ }));
541
+ if (item.section && item.section !== model.items[i - 1]?.section) label(item.section, x, y, 12, colors.muted, "middle");
542
+ lines.title.forEach((str, j) => label(str, x, y + 20 + j * 16, 13, colors.ink, "middle", 600));
543
+ lines.events.forEach((str, j) => label(str, x, y + 24 + lines.title.length * 16 + j * 16, 12, colors.muted, "middle"));
544
+ });
545
+ } else if (model.type === "journey") {
546
+ const headerH = 30, taskH = 62, rows = [];
547
+ let section;
548
+ for (const item of model.items) {
549
+ if (item.section && item.section !== section) rows.push({
550
+ header: item.section,
551
+ h: headerH
552
+ });
553
+ section = item.section;
554
+ rows.push({
555
+ item,
556
+ h: taskH
557
+ });
558
+ }
559
+ scene.height = top + rows.reduce((sum, row) => sum + row.h, 0) + 12;
560
+ const actorW = Math.max(0, ...model.items.map((x) => measure(x.actors.join(", "), {
561
+ size: 12,
562
+ weight: 400
563
+ }))) + 20;
564
+ const chartW = Math.max(usable, actorW), x0 = left, dx = chartW / 4;
565
+ scene.width = Math.max(scene.width, left + chartW + right);
566
+ for (let score = 1; score <= 5; score++) label(String(score), x0 + (score - 1) * dx, top - 10, 12, colors.muted, "middle");
567
+ let y = top;
568
+ for (const row of rows) {
569
+ if (row.header) {
570
+ label(row.header, 12, y + 15, 13, colors.muted, "start", 600);
571
+ y += row.h;
572
+ continue;
573
+ }
574
+ const { item } = row;
575
+ label(item.name, 12, y + 16, 13);
576
+ line(x0, y + 16, x0 + chartW, y + 16, colors.grid);
577
+ for (let score = 1; score <= 5; score++) scene.children.push(el("circle", {
578
+ cx: x0 + (score - 1) * dx,
579
+ cy: y + 16,
580
+ r: 3 + (score === item.score ? 3 : 0),
581
+ fill: score <= item.score ? colors.accent : colors.bg,
582
+ stroke: colors.accent
583
+ }));
584
+ if (item.actors.length) label(item.actors.join(", "), x0, y + 39, 12, colors.muted);
585
+ y += row.h;
586
+ }
587
+ } else if (model.type === "kanban") {
588
+ const columns = model.sections;
589
+ const headerW = Math.max(0, ...columns.map((x) => measure(x.name, {
590
+ size: 14,
591
+ weight: 650
592
+ }) + 30));
593
+ const columnGap = 10;
594
+ const colW = Math.max(160, (usable + columnGap) / columns.length, headerW);
595
+ scene.width = Math.max(scene.width, left + colW * columns.length - columnGap + right);
596
+ const cardLines = new Map(model.items.map((card) => [card.id, wrap(ctx, card.name, colW - 44, 13)])), rowH = Math.max(52, ...[...cardLines.values()].map((lines) => lines.length * 17 + 30)), maxRows = Math.max(1, ...columns.map((c) => model.items.filter((x) => x.section === c.id).length));
597
+ scene.height = top + 50 + maxRows * rowH;
598
+ columns.forEach((col, i) => {
599
+ const x = left + i * colW;
600
+ rect(x, top, colW - 10, scene.height - top - 20, "transparent", colors.grid, 6);
601
+ label(col.name, x + 10, top + 18, 14, colors.ink, "start", 650);
602
+ model.items.filter((item) => item.section === col.id).forEach((card, row) => {
603
+ const y = top + 38 + row * rowH, lines = cardLines.get(card.id);
604
+ rect(x + 6, y, colW - 22, rowH - 10);
605
+ const first = y + (rowH - 10) / 2 - (lines.length - 1) * 8.5;
606
+ lines.forEach((str, j) => label(str, x + 16, first + j * 17, 13));
607
+ });
608
+ });
609
+ } else if (model.type === "packet-beta") {
610
+ const rows = Math.max(1, ...model.fields.map((f) => Math.floor(f.end / 32) + 1));
611
+ const rowsOfFields = Array.from({ length: rows }, (_item, r) => model.fields.filter((f) => f.start <= r * 32 + 31 && f.end >= r * 32).map((f) => {
612
+ const start = Math.max(f.start, r * 32), end = Math.min(f.end, r * 32 + 31);
613
+ return {
614
+ start,
615
+ end,
616
+ bits: end - start + 1,
617
+ name: f.name
618
+ };
619
+ }));
620
+ const minField = Math.ceil(Math.max(24, ...rowsOfFields.flat().map((f) => measure(String(f.end), {
621
+ size: 12,
622
+ weight: 400
623
+ }) + 12)));
624
+ const pitch = Math.max(usable / 32, ...rowsOfFields.flat().map((f) => minField / f.bits));
625
+ const span = pitch * 32;
626
+ scene.width = Math.max(scene.width, left + span + right);
627
+ let y = top;
628
+ rowsOfFields.forEach((fields, r) => {
629
+ const cells = fields.map((f) => {
630
+ const w = f.bits * pitch;
631
+ return {
632
+ x: left + (f.start - r * 32) * pitch,
633
+ w,
634
+ lines: wrap(ctx, `${f.name} ${f.start}-${f.end}`, w - 12, 12)
635
+ };
636
+ });
637
+ const cellHeight = Math.max(34, ...cells.map((c) => 16 + c.lines.length * 16));
638
+ for (const { x, w, lines } of cells) {
639
+ rect(x, y, w, cellHeight, colors.bg, colors.grid, 0);
640
+ lines.forEach((row, i) => label(row, x + 6, y + 16 + i * 16, 12));
641
+ }
642
+ line(left, y + cellHeight + 2, left + span, y + cellHeight + 2);
643
+ for (const bit of [
644
+ 0,
645
+ 7,
646
+ 15,
647
+ 23,
648
+ 31
649
+ ]) label(String(r * 32 + bit), left + (bit + .5) * pitch, y + cellHeight + 23, 12, colors.muted, "middle");
650
+ y += cellHeight + 48;
651
+ });
652
+ scene.height = y + 25;
653
+ } else if (model.type === "quadrantChart") {
654
+ const x0 = left, y0 = top, h = 270;
655
+ const quadrants = [
656
+ {
657
+ name: model.quadrant1,
658
+ right: true,
659
+ top: true
660
+ },
661
+ {
662
+ name: model.quadrant2,
663
+ right: false,
664
+ top: true
665
+ },
666
+ {
667
+ name: model.quadrant3,
668
+ right: false,
669
+ top: false
670
+ },
671
+ {
672
+ name: model.quadrant4,
673
+ right: true,
674
+ top: false
675
+ }
676
+ ];
677
+ const labelWidth = (value) => measure(value, {
678
+ size: 12,
679
+ weight: 400
680
+ });
681
+ const span = Math.max(usable, ...quadrants.filter((q) => q.name).map((q) => 2 * (labelWidth(q.name) + 16)), ...model.items.map((p) => (labelWidth(p.name) + 16) / Math.max(p.x, 1 - p.x)), (model.xaxis || []).reduce((sum, name) => sum + labelWidth(name), 24));
682
+ scene.width = Math.max(scene.width, x0 + span + right);
683
+ scene.height = y0 + h + 62;
684
+ rect(x0, y0, span, h, "transparent", colors.grid, 0);
685
+ line(x0 + span / 2, y0, x0 + span / 2, y0 + h);
686
+ line(x0, y0 + h / 2, x0 + span, y0 + h / 2);
687
+ for (const q of quadrants) if (q.name) label(q.name, q.right ? x0 + span - 8 : x0 + 8, q.top ? y0 + 16 : y0 + h - 16, 12, colors.muted, q.right ? "end" : "start");
688
+ label(model.xaxis?.[0] || "", x0, y0 + h + 20, 12, colors.muted);
689
+ label(model.xaxis?.[1] || "", x0 + span, y0 + h + 20, 12, colors.muted, "end");
690
+ label(model.yaxis?.[0] || "", x0 - 8, y0 + h, 12, colors.muted, "end");
691
+ label(model.yaxis?.[1] || "", x0 - 8, y0, 12, colors.muted, "end");
692
+ model.items.forEach((p) => {
693
+ const x = x0 + p.x * span, y = y0 + (1 - p.y) * h;
694
+ const direction = p.x > .5 ? -1 : 1;
695
+ scene.children.push(el("circle", {
696
+ cx: x,
697
+ cy: y,
698
+ r: 5,
699
+ fill: colors.accent
700
+ }));
701
+ label(p.name, x + direction * 8, y, 12, colors.ink, direction < 0 ? "end" : "start");
702
+ });
703
+ } else if (model.type === "gitGraph") {
704
+ const commits = model.commits, branches = model.branches, lane = /* @__PURE__ */ new Map([["main", 0]]);
705
+ branches.forEach((b) => {
706
+ if (!lane.has(b.name)) lane.set(b.name, lane.size);
707
+ });
708
+ const commitStep = Math.max(90, ...commits.map((c) => measure(c.tag || c.id, {
709
+ size: 12,
710
+ weight: 400
711
+ }) + 24)), maxLane = Math.max(0, lane.size - 1), yFor = (branch) => top + 22 + (lane.get(branch) || 0) * 58, xFor = (id) => left + 70 + commits.findIndex((c) => c.id === id) * commitStep;
712
+ scene.height = top + 22 + maxLane * 58 + 40;
713
+ scene.width = Math.max(scene.width, left + 110 + commits.length * commitStep);
714
+ branches.forEach((b) => {
715
+ const y = yFor(b.name);
716
+ label(b.name, 12, y, 12, colors.muted);
717
+ line(left, y, left + Math.max(usable, commits.length * commitStep), y, colors.grid);
718
+ });
719
+ commits.forEach((c) => c.parents.forEach((parent) => {
720
+ const p = commits.find((x) => x.id === parent);
721
+ if (p) line(xFor(parent), yFor(p.branch), xFor(c.id), yFor(c.branch), colors.accent, 2);
722
+ }));
723
+ commits.forEach((c) => {
724
+ const x = xFor(c.id), y = yFor(c.branch);
725
+ scene.children.push(el("circle", {
726
+ cx: x,
727
+ cy: y,
728
+ r: 8,
729
+ fill: c.commitType === "HIGHLIGHT" ? colors.accent : colors.bg,
730
+ stroke: colors.accent,
731
+ "stroke-width": 2
732
+ }));
733
+ label(c.tag || c.id, x, y + 20, 12, colors.ink, "middle");
734
+ });
735
+ } else scene.height = top + model.items.length * rowH + 30;
736
+ return {
737
+ width: scene.width,
738
+ height: scene.height,
739
+ scene,
740
+ items: model.items,
741
+ errors: []
742
+ };
743
+ }
744
+ var implementations = Object.fromEntries(kinds.map((kind) => [kind, {
745
+ parse: (body, info) => parse(body, info, kind),
746
+ layout
747
+ }]));
748
+ export { implementations, time_exports };