@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,629 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { register } from "./registry.js";
3
+ import { statusMarks } from "../core/theme.js";
4
+ // src/chart/mindmap.js
5
+ var mindmap_exports = /* @__PURE__ */ __exportAll({
6
+ install: () => install,
7
+ mindmapPresentation: () => mindmapPresentation
8
+ });
9
+ var GAP_X = 44;
10
+ var COMPACT_GAP = 24;
11
+ var GAP_Y = 18;
12
+ var PAD_X = 14;
13
+ var TEXT_WIDTH = 220;
14
+ var MARGIN = 24;
15
+ var PLANS = [GAP_X, COMPACT_GAP].flatMap((gap) => [
16
+ TEXT_WIDTH,
17
+ 190,
18
+ 160,
19
+ 140,
20
+ 120
21
+ ].map((cap) => ({
22
+ gap,
23
+ cap
24
+ })));
25
+ var DIAGRAM_PLANS = [GAP_X, COMPACT_GAP].map((gap) => ({
26
+ gap,
27
+ cap: TEXT_WIDTH
28
+ }));
29
+ var mindLayouts = [
30
+ "bilateral",
31
+ "right",
32
+ "left",
33
+ "radial",
34
+ "org",
35
+ "brace",
36
+ "fishbone",
37
+ "timeline"
38
+ ];
39
+ var treeLayouts = [
40
+ "right",
41
+ "left",
42
+ "org",
43
+ "brace"
44
+ ];
45
+ var mindmapPresentation = Symbol("mindmap presentation");
46
+ var LEAF_LINE_GAP = 8;
47
+ var BRANCH_RADIUS = 7;
48
+ var hasFrame = (variant, depth, children) => depth === 0 || Boolean(children?.length) || variant === "cards";
49
+ var nodeTextX = (box, alignRight) => alignRight ? box.x + box.w - PAD_X : box.x + PAD_X;
50
+ var NODE_SHAPES = [
51
+ "rounded",
52
+ "rect",
53
+ "circle",
54
+ "hexagon",
55
+ "bang",
56
+ "cloud",
57
+ "none"
58
+ ];
59
+ var shapeOf = (source) => source.shape || "";
60
+ var OUTLINE_INSET = 9;
61
+ var frameOf = (shape, variant, depth, children) => shape === "none" ? false : shape ? true : hasFrame(variant, depth, children);
62
+ var pointsOf = (points) => points.map(([x, y]) => `${x},${y}`).join(" ");
63
+ function starPoints(x, y, w, h) {
64
+ const ix0 = x + OUTLINE_INSET, ix1 = x + w - OUTLINE_INSET, iy0 = y + OUTLINE_INSET, iy1 = y + h - OUTLINE_INSET;
65
+ const ox0 = x + 1, ox1 = x + w - 1, oy0 = y + 1, oy1 = y + h - 1;
66
+ const stepsX = Math.min(6, Math.max(2, Math.round((ix1 - ix0) / 30)));
67
+ const stepsY = Math.min(4, Math.max(2, Math.round((iy1 - iy0) / 24)));
68
+ const zigzag = (ax, ay, bx, by, cx, cy, dx, dy, steps) => {
69
+ const points = [];
70
+ for (let i = 0; i <= steps; i += 1) {
71
+ const t = i / steps;
72
+ points.push([ax + (bx - ax) * t, ay + (by - ay) * t]);
73
+ if (i === steps) break;
74
+ const u = (i + .5) / steps;
75
+ points.push([cx + (dx - cx) * u, cy + (dy - cy) * u]);
76
+ }
77
+ return points;
78
+ };
79
+ return [
80
+ ...zigzag(ix0, iy0, ix1, iy0, ox0, oy0, ox1, oy0, stepsX),
81
+ ...zigzag(ix1, iy0, ix1, iy1, ox1, oy0, ox1, oy1, stepsY),
82
+ ...zigzag(ix1, iy1, ix0, iy1, ox1, oy1, ox0, oy1, stepsX),
83
+ ...zigzag(ix0, iy1, ix0, iy0, ox0, oy1, ox0, oy0, stepsY)
84
+ ];
85
+ }
86
+ function cloudPath(x, y, w, h) {
87
+ const ix0 = x + OUTLINE_INSET, ix1 = x + w - OUTLINE_INSET, iy0 = y + OUTLINE_INSET, iy1 = y + h - OUTLINE_INSET;
88
+ const ox0 = x + 1, ox1 = x + w - 1, oy0 = y + 1, oy1 = y + h - 1;
89
+ const stepsX = Math.min(6, Math.max(2, Math.round((ix1 - ix0) / 34)));
90
+ const stepsY = Math.min(4, Math.max(2, Math.round((iy1 - iy0) / 30)));
91
+ const bumps = (ax, ay, bx, by, cx, cy, dx, dy, steps) => {
92
+ let d = "";
93
+ for (let i = 0; i < steps; i += 1) {
94
+ const t0 = i / steps, t1 = (i + 1) / steps, mid = (t0 + t1) / 2;
95
+ d += ` Q${cx + (dx - cx) * mid} ${cy + (dy - cy) * mid} ${ax + (bx - ax) * t1} ${ay + (by - ay) * t1}`;
96
+ }
97
+ return d;
98
+ };
99
+ return `M${ix0} ${iy0}` + bumps(ix0, iy0, ix1, iy0, ox0, oy0, ox1, oy0, stepsX) + bumps(ix1, iy0, ix1, iy1, ox1, oy0, ox1, oy1, stepsY) + bumps(ix1, iy1, ix0, iy1, ox1, oy1, ox0, oy1, stepsX) + bumps(ix0, iy1, ix0, iy0, ox0, oy1, ox0, oy0, stepsY) + " Z";
100
+ }
101
+ function circleFits(textW, textH, w, h) {
102
+ const rx = w / 2 - 3, ry = h / 2 - 3;
103
+ return rx > 0 && ry > 0 && (textW / 2 / rx) ** 2 + (textH / 2 / ry) ** 2 <= .95;
104
+ }
105
+ function outlineOf(ctx, node, box, style, meta) {
106
+ const { x, y, w, h } = box, shape = node.shape;
107
+ if (shape === "none") return [];
108
+ if (shape === "rect") return [ctx.rect(x, y, w, h, {
109
+ ...style,
110
+ rx: 1,
111
+ meta
112
+ })];
113
+ if (shape === "circle") {
114
+ const textH = node.lines.length * 16 + node.noteLines.length * 15;
115
+ if (circleFits(node.contentW, textH, w, h)) return [ctx.shape("ellipse", {
116
+ cx: x + w / 2,
117
+ cy: y + h / 2,
118
+ rx: w / 2,
119
+ ry: h / 2,
120
+ ...style
121
+ }, meta)];
122
+ return [ctx.rect(x, y, w, h, {
123
+ ...style,
124
+ rx: Math.min(w, h) / 2,
125
+ meta
126
+ })];
127
+ }
128
+ if (shape === "hexagon") {
129
+ const cut = Math.min(h / 2, w / 3);
130
+ return [ctx.shape("polygon", {
131
+ points: pointsOf([
132
+ [x + cut, y],
133
+ [x + w - cut, y],
134
+ [x + w, y + h / 2],
135
+ [x + w - cut, y + h],
136
+ [x + cut, y + h],
137
+ [x, y + h / 2]
138
+ ]),
139
+ ...style
140
+ }, meta)];
141
+ }
142
+ if (shape === "bang") return [ctx.shape("polygon", {
143
+ points: pointsOf(starPoints(x, y, w, h)),
144
+ ...style
145
+ }, meta)];
146
+ if (shape === "cloud") return [ctx.path(cloudPath(x, y, w, h), {
147
+ ...style,
148
+ "stroke-linejoin": "round",
149
+ meta
150
+ })];
151
+ return [ctx.rect(x, y, w, h, {
152
+ rx: node.depth === 0 ? 10 : 6,
153
+ ...style,
154
+ meta
155
+ })];
156
+ }
157
+ /** 按节点换行上限 cap 与层距 gap 排出整棵树,返回已平移到正坐标的节点框与内容宽度。 */
158
+ function arrange(spec, ctx, mode, { cap, gap }) {
159
+ const nodes = [];
160
+ const textWidth = cap - 28;
161
+ function visit(source, parent = null, depth = 0) {
162
+ if (source.icon) ctx.fit("此布局不支持节点图标", "删掉节点的 icon,保留文字标签");
163
+ const shape = shapeOf(source);
164
+ if (shape && !NODE_SHAPES.includes(shape)) ctx.fit(`不支持节点形状 ${shape}`, "选择受控形状或省略节点 shape");
165
+ const weight = depth === 0 ? 650 : 500;
166
+ const frame = frameOf(shape, spec.variant, depth, source.children);
167
+ const textOnly = mode === "bilateral" && !frame;
168
+ const decoration = source[mindmapPresentation], icon = decoration?.icon;
169
+ const wrapOptions = { bounded: textOnly && !decoration };
170
+ const marker = !icon && source.status ? statusMarks[source.status] : "";
171
+ const markerWidth = icon ? 18 : marker ? ctx.measure(marker, 12, 700) + 6 : 0;
172
+ const budget = decoration ? source.label.split(/\s+/).reduce((width, word) => /[A-Za-z]/.test(word) ? Math.max(width, Math.ceil(ctx.measure(word, 13, weight)) + 1) : width, textWidth - markerWidth) : textWidth - markerWidth;
173
+ const lines = ctx.wrap(source.label, budget, 13, weight, wrapOptions);
174
+ const noteLines = source.note ? ctx.wrap(source.note, textWidth, 12, 400, wrapOptions) : [];
175
+ const widths = [...lines.map((s) => ctx.measure(s, 13, weight)), ...noteLines.map((s) => ctx.measure(s, 12, 400))];
176
+ const measuredWidth = Math.max(...widths) + 28 + markerWidth;
177
+ const n = {
178
+ id: source.id || `n${nodes.length}`,
179
+ label: source.label,
180
+ note: source.note || "",
181
+ status: source.status || "",
182
+ marker,
183
+ markerWidth,
184
+ parent,
185
+ depth,
186
+ children: [],
187
+ lines,
188
+ noteLines,
189
+ shape,
190
+ frame,
191
+ icon,
192
+ link: decoration?.link,
193
+ w: Math.max(70, textOnly || decoration ? measuredWidth : Math.min(cap, measuredWidth)),
194
+ contentW: measuredWidth - 28,
195
+ h: Math.max(34, lines.length * 17 + noteLines.length * 15 + 18)
196
+ };
197
+ nodes.push(n);
198
+ n.children = (source.children || []).map((child) => visit(child, n, depth + 1));
199
+ return n;
200
+ }
201
+ const root = visit(spec.data);
202
+ const leaves = (n) => n.children.length ? n.children.reduce((sum, c) => sum + leaves(c), 0) : 1;
203
+ const maxDepth = Math.max(...nodes.map((n) => n.depth));
204
+ const maxH = Math.max(...nodes.map((n) => n.h));
205
+ const pitch = maxH + GAP_Y;
206
+ const coords = /* @__PURE__ */ new Map(), fishboneSpine = /* @__PURE__ */ new Map();
207
+ let fishboneSpineEnd = 0;
208
+ const place = (n, x, y) => coords.set(n, {
209
+ x,
210
+ y,
211
+ cx: x + n.w / 2,
212
+ cy: y + n.h / 2,
213
+ w: n.w,
214
+ h: n.h,
215
+ contentW: n.contentW,
216
+ frame: n.frame
217
+ });
218
+ const levelWidths = (group) => Array.from({ length: maxDepth + 1 }, (_, d) => Math.max(0, ...group.filter((n) => n.depth === d).map((n) => n.w)));
219
+ const stack = (node, top, xOf) => {
220
+ place(node, xOf(node), top + leaves(node) * pitch / 2 - node.h / 2);
221
+ let at = top;
222
+ for (const child of node.children) {
223
+ stack(child, at, xOf);
224
+ at += leaves(child) * pitch;
225
+ }
226
+ };
227
+ if (mode === "right" || mode === "left" || mode === "brace") {
228
+ const levelW = levelWidths(nodes), col = [0];
229
+ for (let d = 0; d < maxDepth; d++) col[d + 1] = col[d] + levelW[d] + gap;
230
+ stack(root, 0, (n) => mode === "left" ? -col[n.depth] - n.w : col[n.depth]);
231
+ } else if (mode === "bilateral") {
232
+ const sides = [[], []], mass = [0, 0];
233
+ for (const child of root.children) {
234
+ const side = mass[0] <= mass[1] ? 0 : 1;
235
+ sides[side].push(child);
236
+ mass[side] += leaves(child);
237
+ }
238
+ const subtree = (n) => [n, ...n.children.flatMap(subtree)];
239
+ place(root, 0, -root.h / 2);
240
+ sides.forEach((group, side) => {
241
+ const levelW = levelWidths(group.flatMap(subtree)), start = [0, gap];
242
+ for (let d = 1; d < maxDepth; d++) start[d + 1] = start[d] + levelW[d] + gap;
243
+ let at = -mass[side] * pitch / 2;
244
+ for (const node of group) {
245
+ stack(node, at, (n) => side === 0 ? -start[n.depth] - n.w : root.w + start[n.depth]);
246
+ at += leaves(node) * pitch;
247
+ }
248
+ });
249
+ } else if (mode === "org" || mode === "timeline") {
250
+ const subW = /* @__PURE__ */ new Map();
251
+ const calcW = (node) => {
252
+ const childW = node.children.reduce((sum, child) => sum + calcW(child), 0) + Math.max(0, node.children.length - 1) * gap;
253
+ const width = Math.max(node.w, childW);
254
+ subW.set(node, width);
255
+ return width;
256
+ };
257
+ calcW(root);
258
+ const levels = Array.from({ length: maxDepth + 1 }, (_, d) => Math.max(...nodes.filter((n) => n.depth === d).map((n) => n.h)));
259
+ const levelY = [0];
260
+ for (let d = 1; d <= maxDepth; d++) levelY[d] = levelY[d - 1] + levels[d - 1] + GAP_Y + (mode === "timeline" && d === 1 ? 24 : 0);
261
+ const org = (node, left, depth, fixedY) => {
262
+ const center = left + subW.get(node) / 2;
263
+ place(node, center - node.w / 2, fixedY ?? levelY[depth]);
264
+ let x = left;
265
+ for (const child of node.children) {
266
+ const childY = mode === "timeline" && depth === 0 ? levelY[1] + levels[1] + GAP_Y : void 0;
267
+ org(child, x, depth + 1, childY);
268
+ x += subW.get(child) + gap;
269
+ }
270
+ };
271
+ org(root, 0, 0);
272
+ if (mode === "timeline") {
273
+ const stageGap = 36, baseline = maxH + 50;
274
+ let x = root.w + (gap === GAP_X ? 100 : 28);
275
+ for (const stage of root.children) {
276
+ const positionStage = (node, left, depth) => {
277
+ const center = left + subW.get(node) / 2;
278
+ const y = depth === 1 ? baseline + (root.h - node.h) / 2 : baseline + Math.max(root.h, levels[1]) + GAP_Y + (depth - 2) * (maxH + GAP_Y);
279
+ place(node, center - node.w / 2, y);
280
+ let xx = left;
281
+ for (const child of node.children) {
282
+ positionStage(child, xx, depth + 1);
283
+ xx += subW.get(child) + gap;
284
+ }
285
+ };
286
+ positionStage(stage, x, 1);
287
+ x += subW.get(stage) + stageGap;
288
+ }
289
+ place(root, 0, baseline);
290
+ }
291
+ } else if (mode === "radial") {
292
+ const angleOf = /* @__PURE__ */ new Map();
293
+ const assign = (node, lo, hi) => {
294
+ angleOf.set(node, (lo + hi) / 2);
295
+ const total = node.children.reduce((sum, child) => sum + leaves(child), 0) || 1;
296
+ let cur = lo;
297
+ for (const child of node.children) {
298
+ const next = cur + (hi - lo) * leaves(child) / total;
299
+ assign(child, cur, next);
300
+ cur = next;
301
+ }
302
+ };
303
+ assign(root, -Math.PI, Math.PI);
304
+ const radiusOf = (node) => Math.hypot(node.w, node.h) / 2;
305
+ const byDepth = Array.from({ length: maxDepth + 1 }, (_, d) => nodes.filter((n) => n.depth === d).sort((a, b) => angleOf.get(a) - angleOf.get(b)));
306
+ const levelRadius = byDepth.map((level) => Math.max(0, ...level.map(radiusOf)));
307
+ const radius = [0];
308
+ place(root, -root.w / 2, -root.h / 2);
309
+ for (let d = 1; d <= maxDepth; d++) {
310
+ const level = byDepth[d];
311
+ let need = radius[d - 1] + levelRadius[d - 1] + levelRadius[d] + 12;
312
+ if (level.length > 1) level.forEach((node, index) => {
313
+ const next = level[(index + 1) % level.length];
314
+ const delta = (angleOf.get(next) - angleOf.get(node) + 2 * Math.PI) % (2 * Math.PI) || 2 * Math.PI;
315
+ const chord = 2 * Math.sin(Math.min(Math.PI, delta) / 2);
316
+ need = Math.max(need, (radiusOf(node) + radiusOf(next) + 12) / chord);
317
+ });
318
+ radius[d] = need;
319
+ for (const node of level) {
320
+ const a = angleOf.get(node);
321
+ place(node, radius[d] * Math.cos(a) - node.w / 2, radius[d] * Math.sin(a) - node.h / 2);
322
+ }
323
+ }
324
+ } else if (mode === "fishbone") {
325
+ const baseY = 0, rootX = 0, cats = root.children;
326
+ const column = cap + gap, pitchX = Math.max(column, ...cats.map((cat) => cat.w + gap));
327
+ place(root, rootX, baseY - root.h / 2);
328
+ for (let ci = 0; ci < cats.length; ci++) {
329
+ const cat = cats[ci], sign = ci % 2 ? 1 : -1;
330
+ const spineX = rootX + root.w + 48 + ci * pitchX;
331
+ const branchNodes = [];
332
+ const collectBranch = (node) => {
333
+ for (const child of node.children) {
334
+ branchNodes.push(child);
335
+ collectBranch(child);
336
+ }
337
+ };
338
+ collectBranch(cat);
339
+ const branchGap = GAP_Y;
340
+ const span = (node) => node.h + (node.children.length ? node.children.reduce((sum, child) => sum + span(child), 0) + node.children.length * branchGap : 0);
341
+ const branchHeight = cat.children.reduce((sum, child) => sum + span(child), 0) + Math.max(0, cat.children.length - 1) * branchGap;
342
+ const categoryY = baseY + sign * (root.h / 2 + 36 + Math.max(cat.h, branchHeight) / 2);
343
+ fishboneSpine.set(cat, spineX);
344
+ fishboneSpineEnd = spineX;
345
+ place(cat, spineX + sign * 32 - cat.w / 2, categoryY - cat.h / 2);
346
+ let cursor = sign > 0 ? categoryY + cat.h / 2 + branchGap : categoryY - cat.h / 2 - branchGap;
347
+ const branch = (parent, depth) => {
348
+ for (const child of parent.children) {
349
+ const block = span(child);
350
+ const y = sign > 0 ? cursor : cursor - child.h;
351
+ place(child, spineX + depth * column, y);
352
+ const next = sign > 0 ? cursor + child.h + branchGap : cursor - child.h - branchGap;
353
+ const saved = cursor;
354
+ cursor = next;
355
+ branch(child, depth + 1);
356
+ cursor = sign > 0 ? Math.max(cursor, saved + block + branchGap) : Math.min(cursor, saved - block - branchGap);
357
+ }
358
+ };
359
+ branch(cat, 1);
360
+ }
361
+ }
362
+ const minX = Math.min(...nodes.map((n) => coords.get(n).x)), minY = Math.min(...nodes.map((n) => coords.get(n).y));
363
+ for (const n of nodes) {
364
+ const p = coords.get(n);
365
+ place(n, p.x + MARGIN - minX, p.y + MARGIN - minY);
366
+ }
367
+ for (const [cat, x] of fishboneSpine) fishboneSpine.set(cat, x + MARGIN - minX);
368
+ const width = Math.ceil(Math.max(...nodes.map((n) => coords.get(n).x + n.w)) + MARGIN);
369
+ return {
370
+ nodes,
371
+ root,
372
+ coords,
373
+ fishboneSpine,
374
+ fishboneSpineEnd: fishboneSpineEnd + MARGIN - minX,
375
+ gap,
376
+ width
377
+ };
378
+ }
379
+ function bilateralPath(a, b, parentIsRoot, gap, diagram) {
380
+ const left = b.cx < a.cx, direction = left ? -1 : 1;
381
+ const fromX = a.frame ? left ? a.x : a.x + a.w : parentIsRoot ? a.cx + direction * (a.contentW / 2 + LEAF_LINE_GAP) : nodeTextX(a, left) + direction * (a.contentW + LEAF_LINE_GAP);
382
+ const childSide = left ? b.x + b.w : b.x;
383
+ const toX = b.frame ? childSide : nodeTextX(b, left) - direction * LEAF_LINE_GAP;
384
+ const start = `M${fromX} ${a.cy}`, end = `L${toX} ${b.cy}`;
385
+ if (a.cy === b.cy) return `${start} ${end}`;
386
+ if (diagram) {
387
+ const control = childSide - direction * gap / 2;
388
+ return `${start} C${control} ${(a.cy + b.cy) / 2} ${control} ${b.cy} ${toX} ${b.cy}`;
389
+ }
390
+ const turnX = childSide - direction * gap / 2;
391
+ const dy = b.cy - a.cy, vertical = Math.sign(dy);
392
+ const radius = Math.min(BRANCH_RADIUS, gap / 4, Math.abs(dy) / 2);
393
+ return [
394
+ start,
395
+ `L${turnX - direction * radius} ${a.cy}`,
396
+ `Q${turnX} ${a.cy} ${turnX} ${a.cy + vertical * radius}`,
397
+ `L${turnX} ${b.cy - vertical * radius}`,
398
+ `Q${turnX} ${b.cy} ${turnX + direction * radius} ${b.cy}`,
399
+ end
400
+ ].join(" ");
401
+ }
402
+ function build(spec, ctx, isMindmap) {
403
+ const mode = spec.layout || (isMindmap ? "bilateral" : "right");
404
+ const variant = isMindmap ? spec.variant || "editorial" : "editorial";
405
+ if (!(isMindmap ? mindLayouts : treeLayouts).includes(mode)) ctx.fit(`不支持布局 ${mode}`, "选择该图型的受控 layout");
406
+ if (!["editorial", "cards"].includes(variant)) ctx.fit(`不支持变体 ${variant}`, "选择该图型的受控 variant");
407
+ let plan;
408
+ for (const option of spec[mindmapPresentation] ? DIAGRAM_PLANS : PLANS) {
409
+ plan = arrange(spec, ctx, mode, option);
410
+ if (plan.width <= ctx.width) break;
411
+ }
412
+ const { nodes, root, coords, fishboneSpine, fishboneSpineEnd } = plan;
413
+ const rootPosition = coords.get(root);
414
+ const edgeList = [];
415
+ const boundary = (box, dx, dy) => {
416
+ const sx = dx ? box.w / 2 / Math.abs(dx) : Infinity, sy = dy ? box.h / 2 / Math.abs(dy) : Infinity, t = Math.min(sx, sy);
417
+ return {
418
+ x: box.cx + dx * t,
419
+ y: box.cy + dy * t
420
+ };
421
+ };
422
+ for (const n of nodes) if (n.parent) {
423
+ const a = coords.get(n.parent), b = coords.get(n);
424
+ let d;
425
+ if (mode === "bilateral") d = bilateralPath(a, b, n.parent === root, plan.gap, spec[mindmapPresentation]);
426
+ else if (mode === "org") {
427
+ const top = {
428
+ x: a.cx,
429
+ y: a.y + a.h
430
+ }, bottom = {
431
+ x: b.cx,
432
+ y: b.y
433
+ }, sharedY = (top.y + bottom.y) / 2;
434
+ d = `M${top.x} ${top.y} L${top.x} ${sharedY} L${bottom.x} ${sharedY} L${bottom.x} ${bottom.y}`;
435
+ } else if (mode === "fishbone" && n.parent === root) {
436
+ const spineX = fishboneSpine.get(n), rootEdge = {
437
+ x: a.x + a.w,
438
+ y: a.cy
439
+ }, branchEnd = boundary(b, spineX - b.cx, a.cy - b.cy);
440
+ d = `M${rootEdge.x} ${rootEdge.y} L${spineX} ${rootEdge.y} L${branchEnd.x} ${branchEnd.y}`;
441
+ } else {
442
+ const dx = b.cx - a.cx, dy = b.cy - a.cy;
443
+ const from = boundary(a, dx, dy), to = boundary(b, -dx, -dy);
444
+ if (mode === "brace") {
445
+ const trunk = Math.min(...n.parent.children.map((child) => coords.get(child).x)) - plan.gap / 2;
446
+ const out = {
447
+ x: a.x + a.w,
448
+ y: a.cy
449
+ }, into = {
450
+ x: b.x,
451
+ y: b.cy
452
+ };
453
+ d = `M${out.x} ${out.y} L${trunk} ${out.y} L${trunk} ${into.y} L${into.x} ${into.y}`;
454
+ } else d = `M${from.x} ${from.y} L${to.x} ${to.y}`;
455
+ }
456
+ edgeList.push({
457
+ from: n.parent.id,
458
+ to: n.id,
459
+ d,
460
+ kind: mode,
461
+ parent: n.parent,
462
+ child: n
463
+ });
464
+ }
465
+ const rootTone = ctx.frameTone(ctx.color("accent"), true), branchTone = ctx.frameTone(ctx.palette.accent);
466
+ const svgNodes = [], rootText = ctx.textOn(rootTone);
467
+ for (const n of nodes) {
468
+ const p = coords.get(n), rootNode = n === root, card = variant === "cards";
469
+ const framed = n.frame, first = svgNodes.length;
470
+ const tone = spec[mindmapPresentation] && n.status ? ctx.design[n.status] : null;
471
+ const nodeInk = rootNode && framed ? ctx.textOn(tone || rootTone) : ctx.color("fg");
472
+ if (framed) {
473
+ const fill = rootNode ? tone || rootTone : ctx.tint(ctx.palette.bg, tone || branchTone, card ? .035 : .12);
474
+ const stroke = rootNode ? tone || rootTone : tone || (card ? ctx.color("axis") : ctx.tint(ctx.palette.bg, branchTone, .28));
475
+ svgNodes.push(...outlineOf(ctx, n, p, {
476
+ fill,
477
+ stroke,
478
+ "stroke-width": rootNode || card ? 1 : .8
479
+ }, spec[mindmapPresentation] ? {
480
+ kind: "node",
481
+ id: n.id
482
+ } : {
483
+ kind: isMindmap ? "mindmap" : "tree",
484
+ node: n.id,
485
+ related: descendants(n)
486
+ }));
487
+ }
488
+ const leftLeaf = mode === "bilateral" && !framed && p.cx < rootPosition.cx;
489
+ const anchor = rootNode ? "middle" : leftLeaf ? "end" : "start";
490
+ const tx = rootNode ? p.cx : framed ? p.x + Math.max(PAD_X, (n.w - n.contentW) / 2) : nodeTextX(p, leftLeaf);
491
+ const labelX = rootNode ? tx + n.markerWidth / 2 : tx + (leftLeaf ? -n.markerWidth : n.markerWidth);
492
+ const markerX = rootNode ? tx : tx + (leftLeaf ? -n.markerWidth / 2 : n.markerWidth / 2);
493
+ const weight = rootNode ? 650 : 500;
494
+ const labelStep = 16, total = n.lines.length * labelStep + n.noteLines.length * 15;
495
+ const labelY = p.cy - total / 2 + 12;
496
+ svgNodes.push(...ctx.textLines(labelX, labelY, n.lines, {
497
+ anchor,
498
+ size: 13,
499
+ weight,
500
+ fill: nodeInk,
501
+ meta: {
502
+ kind: isMindmap ? "mindmap" : "tree",
503
+ node: n.id,
504
+ related: descendants(n),
505
+ role: "label"
506
+ }
507
+ }).children);
508
+ if (n.marker) svgNodes.push(ctx.text(markerX, labelY, n.marker, {
509
+ anchor: "middle",
510
+ size: 12,
511
+ weight: 700,
512
+ fill: rootNode && framed ? rootText : ctx.textColor(ctx.design[n.status]),
513
+ meta: {
514
+ kind: isMindmap ? "mindmap" : "tree",
515
+ node: n.id,
516
+ related: descendants(n),
517
+ role: "status",
518
+ status: n.status
519
+ }
520
+ }));
521
+ if (n.icon) {
522
+ const [minX, minY, w, h] = String(n.icon.viewBox).split(/[\s,]+/).map(Number);
523
+ const scale = 12 / Math.max(w, h);
524
+ const gx = rootNode ? labelX - ctx.measure(n.lines[0], 13, weight) / 2 - 18 : leftLeaf ? tx - 12 : tx;
525
+ svgNodes.push(ctx.el("g", { transform: `translate(${gx - minX * scale} ${labelY - 10 - minY * scale}) scale(${scale})` }, n.icon.elements.map(({ tag, attrs }) => ctx.shape(tag, {
526
+ ...attrs,
527
+ ...n.icon.set === "tabler" ? {
528
+ fill: "none",
529
+ stroke: nodeInk,
530
+ "stroke-width": 2
531
+ } : { fill: nodeInk }
532
+ }))));
533
+ }
534
+ let y = labelY + n.lines.length * labelStep;
535
+ for (const line of n.noteLines) {
536
+ svgNodes.push(ctx.text(tx, y, line, {
537
+ anchor,
538
+ size: 12,
539
+ fill: rootNode && framed ? rootText : ctx.color("muted"),
540
+ meta: {
541
+ kind: isMindmap ? "mindmap" : "tree",
542
+ node: n.id,
543
+ related: descendants(n),
544
+ role: "note"
545
+ }
546
+ }));
547
+ y += 15;
548
+ }
549
+ if (n.link) svgNodes.push(ctx.link(n.link.href, svgNodes.splice(first), n.link));
550
+ }
551
+ function descendants(n) {
552
+ return [n.id, ...n.children.flatMap(descendants)];
553
+ }
554
+ const edgeTone = ctx.frameTone(ctx.color("tone5"));
555
+ const paths = edgeList.map((e) => ctx.path(e.d, {
556
+ fill: "none",
557
+ stroke: edgeTone,
558
+ "stroke-width": 1.4,
559
+ "stroke-linecap": "round",
560
+ "stroke-linejoin": "round",
561
+ meta: spec[mindmapPresentation] ? {
562
+ kind: "edge",
563
+ from: e.from,
564
+ to: e.to
565
+ } : {
566
+ kind: isMindmap ? "mindmap" : "tree",
567
+ node: e.child.id,
568
+ related: descendants(e.child),
569
+ from: e.from,
570
+ to: e.to
571
+ }
572
+ }));
573
+ if (mode === "fishbone" && root.children.length) {
574
+ const r = coords.get(root);
575
+ const spine = `M${r.x + r.w} ${r.cy} L${Math.max(r.x + r.w, fishboneSpineEnd)} ${r.cy}`;
576
+ paths.push(ctx.path(spine, {
577
+ fill: "none",
578
+ stroke: edgeTone,
579
+ "stroke-width": 2,
580
+ meta: {
581
+ kind: isMindmap ? "mindmap" : "tree",
582
+ node: root.id,
583
+ related: descendants(root),
584
+ role: "spine"
585
+ }
586
+ }));
587
+ }
588
+ const sceneNodes = nodes.map((n) => {
589
+ const p = coords.get(n);
590
+ return {
591
+ id: n.id,
592
+ x: p.x,
593
+ y: p.y,
594
+ w: n.w,
595
+ h: n.h,
596
+ parent: n.parent?.id || null,
597
+ depth: n.depth,
598
+ label: n.label,
599
+ note: n.note,
600
+ ...n.status ? { status: n.status } : {}
601
+ };
602
+ });
603
+ const pairs = mode === "bilateral" ? [] : edgeList.flatMap((e) => [...e.d.matchAll(/(?:M|L)\s*(-?\d+(?:\.\d+)?)\s+(-?\d+(?:\.\d+)?)/g)].map((m) => [Number(m[1]), Number(m[2])]));
604
+ return {
605
+ width: Math.ceil(Math.max(...nodes.map((n) => coords.get(n).x + n.w), ...pairs.map((p) => p[0])) + MARGIN),
606
+ height: Math.ceil(Math.max(...nodes.map((n) => coords.get(n).y + n.h), ...pairs.map((p) => p[1])) + MARGIN),
607
+ title: spec.title || (isMindmap ? "Mindmap" : "Tree"),
608
+ children: [...paths, ...svgNodes],
609
+ nodes: sceneNodes,
610
+ edges: edgeList.map(({ from, to, d, kind }) => ({
611
+ from,
612
+ to,
613
+ d,
614
+ kind
615
+ })),
616
+ layout: mode,
617
+ variant,
618
+ anchor: {
619
+ x: rootPosition.cx,
620
+ y: rootPosition.cy
621
+ }
622
+ };
623
+ }
624
+ function install({ register }) {
625
+ register("mindmap", (spec, ctx) => build(spec, ctx, true));
626
+ register("tree", (spec, ctx) => build(spec, ctx, false));
627
+ }
628
+ install({ register });
629
+ export { install, mindmapPresentation, mindmap_exports };