@fengwk/kchart 0.1.1 → 0.2.0

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 (101) hide show
  1. package/CHANGELOG.md +35 -0
  2. package/README.md +31 -2
  3. package/README.zh-CN.md +31 -2
  4. package/dist/esm/_virtual/_kchart-families.js +2 -1
  5. package/dist/esm/chart/composition.js +7 -5
  6. package/dist/esm/chart/maps.js +9 -7
  7. package/dist/esm/chart/mindmap.js +19 -5
  8. package/dist/esm/chart/multivariate.js +49 -14
  9. package/dist/esm/chart/relations.js +15 -7
  10. package/dist/esm/chart/spec.js +5 -2
  11. package/dist/esm/core/context.js +29 -10
  12. package/dist/esm/core/scene.js +238 -21
  13. package/dist/esm/core/text.js +1 -1
  14. package/dist/esm/diagram/block.js +287 -295
  15. package/dist/esm/diagram/blocks.js +541 -605
  16. package/dist/esm/diagram/board-paint.js +594 -0
  17. package/dist/esm/diagram/board-route.js +60 -0
  18. package/dist/esm/diagram/c4.js +75 -35
  19. package/dist/esm/diagram/class.js +3 -2
  20. package/dist/esm/diagram/cynefin.js +55 -9
  21. package/dist/esm/diagram/eventmodeling.js +4 -2
  22. package/dist/esm/diagram/gantt.js +37 -15
  23. package/dist/esm/diagram/gitgraph.js +26 -5
  24. package/dist/esm/diagram/graph-layout.js +39 -2
  25. package/dist/esm/diagram/graph-route.js +296 -38
  26. package/dist/esm/diagram/info.js +32 -0
  27. package/dist/esm/diagram/journey.js +5 -5
  28. package/dist/esm/diagram/kanban.js +3 -3
  29. package/dist/esm/diagram/layers.js +9 -7
  30. package/dist/esm/diagram/mindmap.js +5 -1
  31. package/dist/esm/diagram/numeric-composition.js +24 -4
  32. package/dist/esm/diagram/numeric-model.js +1 -1
  33. package/dist/esm/diagram/numeric-radar.js +25 -7
  34. package/dist/esm/diagram/numeric-sankey.js +4 -3
  35. package/dist/esm/diagram/numeric-xy.js +12 -2
  36. package/dist/esm/diagram/packet.js +4 -4
  37. package/dist/esm/diagram/quadrant.js +12 -5
  38. package/dist/esm/diagram/railroad-parse.js +20 -10
  39. package/dist/esm/diagram/railroad.js +1 -1
  40. package/dist/esm/diagram/registry.js +46 -8
  41. package/dist/esm/diagram/sequence.js +43 -6
  42. package/dist/esm/diagram/swimlane.js +2 -2
  43. package/dist/esm/diagram/time.js +3 -3
  44. package/dist/esm/diagram/timeline.js +5 -5
  45. package/dist/esm/diagram/treeview.js +5 -3
  46. package/dist/esm/diagram/venn.js +35 -15
  47. package/dist/esm/diagram/wardley.js +61 -26
  48. package/dist/esm/diagram/zenuml.js +338 -0
  49. package/dist/esm/index.d.ts +3 -1
  50. package/dist/esm/mermaid.d.ts +1 -1
  51. package/dist/esm/mermaid.js +2 -1
  52. package/dist/esm/node.js +8 -2
  53. package/dist/esm/src/families.js +6 -1
  54. package/dist/units/chart/composition.js +1 -1
  55. package/dist/units/chart/maps.js +1 -1
  56. package/dist/units/chart/mindmap.js +1 -1
  57. package/dist/units/chart/multivariate.js +1 -1
  58. package/dist/units/chart/relations.js +1 -1
  59. package/dist/units/chart/spec.js +1 -1
  60. package/dist/units/core/context.js +1 -1
  61. package/dist/units/core/scene.js +1 -1
  62. package/dist/units/core/text.js +1 -1
  63. package/dist/units/diagram/block.js +2 -2
  64. package/dist/units/diagram/blocks.js +2 -2
  65. package/dist/units/diagram/board-paint.js +1 -0
  66. package/dist/units/diagram/board-route.js +1 -0
  67. package/dist/units/diagram/c4.js +1 -1
  68. package/dist/units/diagram/class.js +2 -2
  69. package/dist/units/diagram/cynefin.js +2 -2
  70. package/dist/units/diagram/eventmodeling.js +1 -1
  71. package/dist/units/diagram/gantt.js +3 -3
  72. package/dist/units/diagram/gitgraph.js +2 -1
  73. package/dist/units/diagram/graph-layout.js +1 -1
  74. package/dist/units/diagram/graph-route.js +1 -1
  75. package/dist/units/diagram/info.js +2 -0
  76. package/dist/units/diagram/journey.js +3 -3
  77. package/dist/units/diagram/kanban.js +1 -1
  78. package/dist/units/diagram/layers.js +1 -1
  79. package/dist/units/diagram/mindmap.js +1 -1
  80. package/dist/units/diagram/numeric-composition.js +1 -1
  81. package/dist/units/diagram/numeric-model.js +1 -1
  82. package/dist/units/diagram/numeric-radar.js +2 -2
  83. package/dist/units/diagram/numeric-sankey.js +1 -1
  84. package/dist/units/diagram/numeric-xy.js +1 -1
  85. package/dist/units/diagram/packet.js +1 -1
  86. package/dist/units/diagram/quadrant.js +1 -1
  87. package/dist/units/diagram/railroad-parse.js +2 -2
  88. package/dist/units/diagram/railroad.js +1 -1
  89. package/dist/units/diagram/registry.js +2 -2
  90. package/dist/units/diagram/sequence.js +1 -1
  91. package/dist/units/diagram/swimlane.js +2 -2
  92. package/dist/units/diagram/time.js +1 -1
  93. package/dist/units/diagram/timeline.js +1 -1
  94. package/dist/units/diagram/treeview.js +1 -1
  95. package/dist/units/diagram/venn.js +1 -1
  96. package/dist/units/diagram/wardley.js +4 -4
  97. package/dist/units/diagram/zenuml.js +2 -0
  98. package/dist/units.json +79 -51
  99. package/package.json +1 -1
  100. package/dist/esm/diagram/relations.js +0 -966
  101. package/dist/units/diagram/relations.js +0 -3
@@ -47,7 +47,7 @@ function hits(a, b, rect) {
47
47
  }
48
48
  return lo < hi;
49
49
  }
50
- function attachment(node, side, type, offset = 0) {
50
+ function attachment(node, side, type, offset = 0, coord) {
51
51
  const v = vertical(side), sign = side === "top" || side === "left" ? -1 : 1;
52
52
  const shape = !node.frame && (type === "flowchart" ? flowOutlineName(node.shape) : node.kind === "choice" ? "diam" : node.kind === "start" || node.kind === "end" ? "circle" : node.kind === "state" ? "rounded" : node.kind === "note" ? "note" : null);
53
53
  const plain = v ? {
@@ -57,8 +57,8 @@ function attachment(node, side, type, offset = 0) {
57
57
  x: sign < 0 ? 0 : node.w,
58
58
  y: node.h / 2
59
59
  };
60
- if (v) plain.x += node.w * offset;
61
- else plain.y += node.h * offset;
60
+ if (v) plain.x = coord === void 0 ? plain.x + node.w * offset : coord - node.x;
61
+ else plain.y = coord === void 0 ? plain.y + node.h * offset : coord - node.y;
62
62
  const local = shape ? outlinePort(shapeOutline(shape, node.w, node.h, node.direction), side, v ? plain.x : plain.y) : plain;
63
63
  const port = {
64
64
  x: node.x + local.x,
@@ -92,15 +92,29 @@ function crosses(a, b) {
92
92
  const side = (p, q, r) => (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x);
93
93
  return side(a.p0, a.p1, b.p0) * side(a.p0, a.p1, b.p1) < -1e-6 && side(b.p0, b.p1, a.p0) * side(b.p0, b.p1, a.p1) < -1e-6;
94
94
  }
95
+ function cornerHook(points, box, axis) {
96
+ if (points.length < 4) return false;
97
+ const [port, lead, turn, next] = points;
98
+ const d = lead[axis] - port[axis];
99
+ return d * (next[axis] - turn[axis]) < 0 && (d * (next[axis] - port[axis]) < 0 || hits(turn, next, expand(box, 12)));
100
+ }
95
101
  function shortest(from, to, obstacles, occupied, type, bounds, label, original, back, frameSides, arrivals, clearOf = [], lanes = {
96
102
  x: [],
97
103
  y: []
98
- }) {
99
- let best = null, score = Infinity;
104
+ }, board = false, forward = null) {
105
+ let best = null, score = Infinity, needsDetour = true;
100
106
  const fixed = {};
101
107
  for (const [key, span] of [["x", "w"], ["y", "h"]]) {
102
- const frameTracks = frameSides.filter(({ p0, p1 }) => p0[key] === p1[key]).flatMap(({ p0 }) => [p0[key] - 8, p0[key] + 8]);
103
- const tracks = back ? occupied.filter(({ p0, p1 }) => Math.abs(p0[key] - p1[key]) < .01).flatMap(({ p0 }) => [p0[key] - 12, p0[key] + 12]) : [];
108
+ const frameTracks = frameSides.filter(({ p0, p1 }) => p0[key] === p1[key]).flatMap(({ p0 }) => {
109
+ const at = p0[key], clearance = board && label ? Math.max(8, label[span] / 2 + 2) : 8;
110
+ return clearance === 8 ? [at - 8, at + 8] : [
111
+ at - 8,
112
+ at + 8,
113
+ at - clearance,
114
+ at + clearance
115
+ ];
116
+ });
117
+ const tracks = back || board || forward ? occupied.filter(({ p0, p1 }) => Math.abs(p0[key] - p1[key]) < .01).flatMap(({ p0 }) => [p0[key] - 12, p0[key] + 12]) : [];
104
118
  fixed[key] = [.../* @__PURE__ */ new Set([
105
119
  6,
106
120
  bounds[span] - 6,
@@ -120,9 +134,54 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
120
134
  };
121
135
  const clear = (a, b) => !obstacles.some((r) => hits(a, b, r));
122
136
  const fromBox = expand(from, 4), toBox = expand(to, 4);
123
- const search = (source, target, startOffset, endOffset, detour) => {
124
- const { port: fromPort, stub: a } = attachment(from, source, type, startOffset);
125
- const { port: toPort, stub: b } = attachment(to, target, type, endOffset);
137
+ const inside = board && (board.insideFrom ? from : board.insideTo ? to : null);
138
+ const fitsInside = (p) => !inside || p.x >= inside.x && p.x <= inside.x + inside.w && p.y >= inside.y && p.y <= inside.y + inside.h;
139
+ const orderedPort = (port, side, peer, ports) => {
140
+ const axis = vertical(side) ? "x" : "y", span = axis === "x" ? "w" : "h";
141
+ const at = peer[axis] + peer[span] / 2;
142
+ return ports.every((old) => {
143
+ if (side !== old.side) return true;
144
+ const delta = at - (old.peer[axis] + old.peer[span] / 2);
145
+ return Math.abs(port[axis] - old.port[axis]) >= 3.999 && delta * (port[axis] - old.port[axis]) >= 0;
146
+ });
147
+ };
148
+ const attach = (box, side, offset, coord) => {
149
+ if (!board) return attachment(box, side, type, offset, coord);
150
+ const v = vertical(side), first = side === "top" || side === "left";
151
+ const along = coord ?? (v ? box.x + box.w * (.5 + offset) : box.y + box.h * (.5 + offset));
152
+ const face = v ? box.y + (first ? 0 : box.h) : box.x + (first ? 0 : box.w);
153
+ const lead = face + (first ? -1 : 1) * (inside ? 4 : 8) * (box === inside ? -1 : 1);
154
+ return v ? {
155
+ port: {
156
+ x: along,
157
+ y: face
158
+ },
159
+ stub: {
160
+ x: along,
161
+ y: lead
162
+ }
163
+ } : {
164
+ port: {
165
+ x: face,
166
+ y: along
167
+ },
168
+ stub: {
169
+ x: lead,
170
+ y: along
171
+ }
172
+ };
173
+ };
174
+ const search = (source, target, startOffset, endOffset, detour, sharedCoord) => {
175
+ const start = attach(from, source, startOffset, sharedCoord), end = attach(to, target, endOffset, sharedCoord);
176
+ const { port: fromPort } = start, { port: toPort } = end;
177
+ const mid = sharedCoord === void 0 ? null : {
178
+ x: (fromPort.x + toPort.x) / 2,
179
+ y: (fromPort.y + toPort.y) / 2
180
+ };
181
+ const a = mid ?? start.stub, b = mid ?? end.stub;
182
+ const pointsToward = ({ port, stub }) => (mid.x - port.x) * (stub.x - port.x) + (mid.y - port.y) * (stub.y - port.y) > 0;
183
+ if (mid && (!pointsToward(start) || !pointsToward(end))) return;
184
+ if (board && (!orderedPort(fromPort, source, to, board.fromPorts) || !orderedPort(toPort, target, from, board.toPorts))) return;
126
185
  if (!clear(fromPort, a) || !clear(b, toPort)) return;
127
186
  if (arrivals.some((edge) => distance(edge.end, toPort) < 8)) return;
128
187
  if (distance(fromPort, a) + distance(a, b) + distance(b, toPort) >= score) return;
@@ -133,7 +192,8 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
133
192
  ...mids,
134
193
  b,
135
194
  toPort
136
- ].filter((p, i, all) => !i || distance(p, all[i - 1]) > .001);
195
+ ].filter((p, i, all) => !i || distance(p, all[i - 1]) > (board ? 0 : .001));
196
+ if (inside && !points.every(fitsInside)) return;
137
197
  let length = 0;
138
198
  for (let i = 1; i < points.length; i++) {
139
199
  const prev = points[i - 2], a = points[i - 1], b = points[i];
@@ -141,17 +201,31 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
141
201
  length += distance(a, b);
142
202
  }
143
203
  const compact = points.filter((p, i) => !i || i === points.length - 1 || Math.abs(distance(points[i - 1], p) + distance(p, points[i + 1]) - distance(points[i - 1], points[i + 1])) > .001 || points[i - 1].x !== points[i + 1].x && points[i - 1].y !== points[i + 1].y);
144
- const cost = length + (compact.length - 2) * 16;
204
+ const bends = compact.length - 2;
205
+ if (forward?.maxBends != null && bends > forward.maxBends) return;
206
+ if (forward?.refine && (cornerHook(compact, from, forward.lateral) || cornerHook(compact.slice(-4).reverse(), to, forward.lateral))) return;
207
+ const cost = length + bends * 16;
145
208
  if (cost >= score) return;
146
209
  for (let i = 2; i < points.length - 1; i++) {
147
- if (hits(points[i - 1], points[i], fromBox) || hits(points[i - 1], points[i], toBox)) return;
210
+ if (from !== inside && hits(points[i - 1], points[i], fromBox) || to !== inside && hits(points[i - 1], points[i], toBox)) return;
148
211
  if (!clear(points[i - 1], points[i])) return;
149
212
  }
150
213
  const segments = compact.slice(1).map((p, i) => straight(compact[i], p));
151
- if (segments.some(({ p0, p1 }) => occupied.some((seg) => sharesRun(p0, p1, seg.p0, seg.p1, back ? 12 : 1, back ? 8 : 4)) || clearOf.some((seg) => sharesRun(p0, p1, seg.p0, seg.p1, EDGE_CLEAR, 4)) || frameSides.some((seg) => sharesRun(p0, p1, seg.p0, seg.p1, 8, 12)) || arrivals.some((edge) => edge.segments.some((seg) => crosses({
214
+ if (segments.some(({ p0, p1 }) => occupied.some((seg) => sharesRun(p0, p1, seg.p0, seg.p1, board ? EDGE_CLEAR : back ? 12 : 1, board ? 0 : back ? 8 : 4)) || clearOf.some((seg) => sharesRun(p0, p1, seg.p0, seg.p1, forward && (forward.maxBends == null || from.parent !== to.parent) ? 12 : EDGE_CLEAR, 4)) || forward?.siblings.some((seg) => crosses({
215
+ p0,
216
+ p1
217
+ }, seg)) || frameSides.some((seg) => sharesRun(p0, p1, seg.p0, seg.p1, 8, 12)) || arrivals.some((edge) => edge.segments.some((seg) => crosses({
152
218
  p0,
153
219
  p1
154
220
  }, seg) || sharesRun(p0, p1, seg.p0, seg.p1, 8, 8))))) return;
221
+ let crossingPenalty = 0;
222
+ if (forward) {
223
+ for (const seg of segments) for (const other of occupied) if (crosses(seg, other)) crossingPenalty += 1;
224
+ }
225
+ if (forward?.maxBends != null && crossingPenalty) return;
226
+ const retreat = forward ? segments.reduce((sum, seg) => sum + Math.max(0, forward.coordinate(seg.p0) - forward.coordinate(seg.p1)), 0) : 0;
227
+ const flowCost = cost + crossingPenalty * 240 + retreat * 6;
228
+ if (flowCost >= score) return;
155
229
  let labelRect = label;
156
230
  if (label) {
157
231
  const box = (p) => ({
@@ -163,11 +237,20 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
163
237
  from,
164
238
  to,
165
239
  ...obstacles
166
- ].some((r) => overlaps(box, r));
240
+ ].some((r) => r !== inside && overlaps(box, r)) && (!board || fitsInside(box) && fitsInside({
241
+ x: box.x + box.w,
242
+ y: box.y + box.h
243
+ }) && box.x >= 0 && box.y >= 0 && box.x + box.w <= bounds.w && box.y + box.h <= bounds.h && !occupied.some((seg) => hits(seg.p0, seg.p1, expand(box, 2))) && !frameSides.some((seg) => hits(seg.p0, seg.p1, box)));
167
244
  const legs = compact.slice(1).map((p, i) => [compact[i], p]).sort(([p, q], [r, s]) => distance(r, s) - distance(p, q));
168
245
  labelRect = null;
169
246
  for (const [p, q] of legs) {
170
- for (const f of [
247
+ for (const f of board ? [
248
+ .5,
249
+ .35,
250
+ .65,
251
+ .2,
252
+ .8
253
+ ] : [
171
254
  .5,
172
255
  .35,
173
256
  .65
@@ -190,11 +273,17 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
190
273
  start: fromPort,
191
274
  end: toPort,
192
275
  labelRect,
193
- segments
276
+ segments,
277
+ ...board ? {
278
+ source,
279
+ target
280
+ } : {}
194
281
  };
195
- score = cost;
282
+ score = flowCost;
283
+ needsDetour = crossingPenalty > 0 || retreat > .001 || forward?.refine && bends > 2;
196
284
  };
197
285
  if (a.x === b.x || a.y === b.y) validate([]);
286
+ if (sharedCoord !== void 0) return;
198
287
  validate([{
199
288
  x: a.x,
200
289
  y: b.y
@@ -203,8 +292,17 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
203
292
  x: b.x,
204
293
  y: a.y
205
294
  }]);
295
+ if (forward?.maxBends === 1) return;
206
296
  const ends = distance(fromPort, a) + distance(b, toPort) - .01;
207
- const xs = candidates(a, b, "x", "w"), ys = candidates(a, b, "y", "h");
297
+ const tracksNear = (key, span) => {
298
+ const middle = candidates(a, b, key, span);
299
+ return forward?.refine && forward.maxBends == null && !forward.straightOnly ? /* @__PURE__ */ new Set([
300
+ ...middle,
301
+ ...candidates(a, a, key, span).slice(0, 6),
302
+ ...candidates(b, b, key, span).slice(0, 6)
303
+ ]) : middle;
304
+ };
305
+ const xs = tracksNear("x", "w"), ys = tracksNear("y", "h");
208
306
  for (const x of xs) if (ends + Math.abs(a.x - x) + Math.abs(a.y - b.y) + Math.abs(x - b.x) < score) validate([{
209
307
  x,
210
308
  y: a.y
@@ -219,12 +317,52 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
219
317
  x: b.x,
220
318
  y
221
319
  }]);
222
- if (detour && !best) {
320
+ if (detour && (!best || forward && needsDetour)) {
321
+ if (forward) {
322
+ const axis = vertical(forward.exit) ? "x" : "y", along = axis === "x" ? "y" : "x";
323
+ const rails = candidates(a, b, axis, axis === "x" ? "w" : "h");
324
+ const depth = axis === "x" ? "h" : "w", span = axis === "x" ? "w" : "h";
325
+ const near = (p) => /* @__PURE__ */ new Set([p[along], ...candidates(p, p, along, depth).slice(0, 9)]);
326
+ const starts = near(a), endsAt = near(b);
327
+ const walls = frameSides.filter(({ p0, p1 }) => p0[axis] === p1[axis]).map(({ p0 }) => p0[axis]);
328
+ const low = Math.min(...walls) - 8, high = Math.max(...walls) + 8;
329
+ const railTracks = [.../* @__PURE__ */ new Set([
330
+ ...lanes[axis],
331
+ ...rails.slice(0, 12),
332
+ low,
333
+ high,
334
+ low - 12,
335
+ high + 12,
336
+ 6,
337
+ bounds[span] - 6
338
+ ])];
339
+ for (const rail of railTracks) for (const at of starts) for (const bt of endsAt) {
340
+ if (rail < 4 || rail > bounds[span] - 4 || ends + Math.abs(a[along] - at) + Math.abs(at - bt) + Math.abs(bt - b[along]) + Math.abs(a[axis] - rail) + Math.abs(b[axis] - rail) >= score) continue;
341
+ validate([
342
+ {
343
+ ...a,
344
+ [along]: at
345
+ },
346
+ {
347
+ [axis]: rail,
348
+ [along]: at
349
+ },
350
+ {
351
+ [axis]: rail,
352
+ [along]: bt
353
+ },
354
+ {
355
+ ...b,
356
+ [along]: bt
357
+ }
358
+ ]);
359
+ }
360
+ }
223
361
  const near = (p, key, span) => candidates(p, p, key, span).slice(0, 7);
224
362
  const aX = near(a, "x", "w"), bX = near(b, "x", "w");
225
363
  const aY = near(a, "y", "h"), bY = near(b, "y", "h");
226
364
  const gapTracks = (list, key, span) => [.../* @__PURE__ */ new Set([...lanes[key].filter((v) => v >= 4 && v <= bounds[span] - 4), ...list.slice(0, 8)])];
227
- for (const y of gapTracks(ys, "y", "h")) for (const x1 of aX) for (const x2 of bX) {
365
+ for (const y of gapTracks(candidates(a, b, "y", "h"), "y", "h")) for (const x1 of aX) for (const x2 of bX) {
228
366
  if (ends + Math.abs(a.x - x1) + Math.abs(a.y - y) + Math.abs(x1 - x2) + Math.abs(y - b.y) + Math.abs(x2 - b.x) >= score) continue;
229
367
  validate([
230
368
  {
@@ -245,7 +383,7 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
245
383
  }
246
384
  ]);
247
385
  }
248
- for (const x of gapTracks(xs, "x", "w")) for (const y1 of aY) for (const y2 of bY) {
386
+ for (const x of gapTracks(candidates(a, b, "x", "w"), "x", "w")) for (const y1 of aY) for (const y2 of bY) {
249
387
  if (ends + Math.abs(a.y - y1) + Math.abs(a.x - x) + Math.abs(y1 - y2) + Math.abs(x - b.x) + Math.abs(y2 - b.y) >= score) continue;
250
388
  validate([
251
389
  {
@@ -268,27 +406,101 @@ function shortest(from, to, obstacles, occupied, type, bounds, label, original,
268
406
  }
269
407
  }
270
408
  };
271
- for (const source of sides) for (const target of sides) search(source, target, 0, 0, false);
409
+ if (board) for (const [key, span, low, high] of [[
410
+ "x",
411
+ "w",
412
+ "top",
413
+ "bottom"
414
+ ], [
415
+ "y",
416
+ "h",
417
+ "left",
418
+ "right"
419
+ ]]) {
420
+ const inset = (box) => Math.min(8, box[span] / 4);
421
+ const overlapLo = Math.max(from[key], to[key]), overlapHi = Math.min(from[key] + from[span], to[key] + to[span]);
422
+ if (overlapHi <= overlapLo) continue;
423
+ const lo = Math.min((overlapLo + overlapHi) / 2, Math.max(from[key] + inset(from), to[key] + inset(to)));
424
+ const hi = Math.max((overlapLo + overlapHi) / 2, Math.min(from[key] + from[span] - inset(from), to[key] + to[span] - inset(to)));
425
+ const clamp = (value) => Math.max(lo, Math.min(hi, value));
426
+ const preferred = clamp(to[key] + to[span] / 2);
427
+ const cross = key === "x" ? "y" : "x", size = key === "x" ? "h" : "w";
428
+ const pair = from[cross] + from[size] <= to[cross] ? [high, low] : to[cross] + to[size] <= from[cross] ? [low, high] : null;
429
+ const pairs = inside ? [[low, low], [high, high]] : pair ? [pair] : [];
430
+ for (const faces of pairs) for (const coord of /* @__PURE__ */ new Set([
431
+ preferred,
432
+ (lo + hi) / 2,
433
+ clamp(preferred - 12),
434
+ clamp(preferred + 12),
435
+ lo,
436
+ hi
437
+ ])) {
438
+ search(...faces, 0, 0, false, coord);
439
+ if (best && !inside) return best;
440
+ }
441
+ }
442
+ if (forward) {
443
+ const { exit, enter } = forward;
444
+ const axis = vertical(exit) ? "x" : "y", span = vertical(exit) ? "w" : "h";
445
+ for (const box of [to, from]) {
446
+ const coord = box[axis] + box[span] / 2;
447
+ if ([from, to].every((box) => coord >= box[axis] + 6 && coord <= box[axis] + box[span] - 6)) search(exit, enter, 0, 0, false, coord);
448
+ }
449
+ if (forward.straightOnly) return best;
450
+ }
451
+ const sources = forward ? sides.filter((side) => side !== forward.enter) : sides;
452
+ const targets = forward ? sides.filter((side) => side !== forward.exit) : sides;
453
+ for (const source of sources) for (const target of targets) {
454
+ search(source, target, 0, 0, false);
455
+ if (forward?.maxBends != null) {
456
+ const shift = (box, side) => {
457
+ const axis = vertical(side) ? "x" : "y", span = vertical(side) ? "w" : "h";
458
+ return lanes[axis].filter((coord) => coord >= box[axis] + 6 && coord <= box[axis] + box[span] - 6).slice(0, 3).map((coord) => (coord - box[axis]) / box[span] - .5);
459
+ };
460
+ for (const offset of shift(from, source)) search(source, target, offset, 0, false);
461
+ for (const offset of shift(to, target)) search(source, target, 0, offset, false);
462
+ }
463
+ }
464
+ if (forward?.maxBends != null) return best;
272
465
  for (const offset of [
273
466
  -.2,
274
467
  .2,
275
468
  -.35,
276
469
  .35
277
470
  ]) {
278
- if (best) break;
279
- for (const source of sides) for (const target of sides) for (const [u, v] of [
471
+ if (best && !board && (!forward || !needsDetour)) break;
472
+ for (const source of sources) for (const target of targets) for (const [u, v] of [
280
473
  [offset, 0],
281
474
  [0, offset],
282
475
  [offset, offset]
283
476
  ]) search(source, target, u, v, false);
284
477
  }
478
+ if (forward?.refine && needsDetour) {
479
+ const axis = forward.lateral, span = axis === "x" ? "w" : "h";
480
+ const offsets = (box) => {
481
+ const tracks = fixed[axis].filter((at) => at >= box[axis] + 6 && at <= box[axis] + box[span] - 6).sort((a, b) => a - b);
482
+ const center = box[axis] + box[span] / 2;
483
+ const extremes = [...tracks.slice(0, 3), ...tracks.slice(-3)];
484
+ return [.../* @__PURE__ */ new Set([...extremes, ...tracks.sort((a, b) => Math.abs(a - center) - Math.abs(b - center)).slice(0, 4)])].map((at) => (at - center) / box[span]);
485
+ };
486
+ for (const offset of offsets(from)) for (const end of [
487
+ 0,
488
+ -.2,
489
+ .2
490
+ ]) search(forward.exit, forward.enter, offset, end, false);
491
+ for (const offset of offsets(to)) for (const start of [
492
+ 0,
493
+ -.2,
494
+ .2
495
+ ]) search(forward.exit, forward.enter, start, offset, false);
496
+ }
285
497
  for (const offset of [
286
498
  0,
287
499
  -.2,
288
500
  .2
289
501
  ]) {
290
- if (best) break;
291
- for (const source of sides) for (const target of sides) search(source, target, offset, offset, true);
502
+ if (best && (!forward || !needsDetour)) break;
503
+ for (const source of sources) for (const target of targets) search(source, target, offset, offset, true);
292
504
  }
293
505
  return best;
294
506
  }
@@ -320,7 +532,7 @@ function borders({ x, y, w, h }) {
320
532
  straight(d, a)
321
533
  ];
322
534
  }
323
- function repairRoutes(geometry, redirects = /* @__PURE__ */ new Map()) {
535
+ function repairRoutes(geometry, redirects = /* @__PURE__ */ new Map(), { board = false } = {}) {
324
536
  const nodes = new Map(geometry.nodes.map((node) => [node.id, node]));
325
537
  const frames = new Map(geometry.frames.map((frame) => [frame.id, {
326
538
  ...frame,
@@ -329,16 +541,39 @@ function repairRoutes(geometry, redirects = /* @__PURE__ */ new Map()) {
329
541
  const endpoint = (id) => nodes.get(id) ?? frames.get(id);
330
542
  const ancestors = (node) => {
331
543
  const result = /* @__PURE__ */ new Set([node.id]);
332
- for (let id = node.parent; id; id = frames.get(id)?.parent) result.add(id);
544
+ for (let id = node.parent; board ? id != null : id; id = frames.get(id)?.parent) result.add(id);
333
545
  return result;
334
546
  };
335
- const checkTracks = nodes.size > 30;
547
+ const checkTracks = !board && nodes.size > 30;
336
548
  const pending = new Set(redirects.keys());
337
549
  const settled = [];
550
+ const ports = /* @__PURE__ */ new Map();
551
+ const reserve = (box, peer, port, side) => {
552
+ if (!ports.has(box.id)) ports.set(box.id, []);
553
+ ports.get(box.id).push({
554
+ peer,
555
+ port,
556
+ side
557
+ });
558
+ };
338
559
  const direction = geometry.direction;
560
+ const faces = {
561
+ TB: ["bottom", "top"],
562
+ BT: ["top", "bottom"],
563
+ LR: ["right", "left"],
564
+ RL: ["left", "right"]
565
+ }[direction];
566
+ const lateral = faces && (vertical(faces[0]) ? "x" : "y");
567
+ const straighten = !board && geometry.model?.variant !== "agentflow" && (geometry.type === "flowchart" || geometry.type === "state");
568
+ const projectForward = straighten && !geometry.model?.edges.some((edge) => edge.fromFrame || edge.toFrame);
569
+ const polishFlow = projectForward && geometry.type === "flowchart" && !geometry.model?.nodes.some((node) => node.block);
339
570
  const coordinate = (p) => direction === "LR" ? p.x : direction === "RL" ? -p.x : direction === "BT" ? -p.y : p.y;
340
571
  const back = (e) => e.reversed || e.start && e.end && coordinate(e.start) > coordinate(e.end) + 1;
341
- const ordered = [...geometry.edges].sort((a, b) => Number(!!back(a)) - Number(!!back(b)));
572
+ const span = (edge) => {
573
+ const { from, to } = redirects.get(edge.id);
574
+ return Math.abs(coordinate(endpoint(from) ?? edge.start) - coordinate(endpoint(to) ?? edge.end));
575
+ };
576
+ const ordered = [...geometry.edges].sort((a, b) => Number(!!back(a)) - Number(!!back(b)) || (projectForward ? Number(pending.has(a.id)) - Number(pending.has(b.id)) || (pending.has(a.id) && pending.has(b.id) ? span(a) - span(b) : 0) : 0));
342
577
  const frameSides = [...frames.values()].flatMap(borders);
343
578
  const nodeBorders = new Map([...nodes.values()].map((node) => [node.id, borders(node)]));
344
579
  for (const edge of ordered) {
@@ -346,7 +581,8 @@ function repairRoutes(geometry, redirects = /* @__PURE__ */ new Map()) {
346
581
  const redirect = redirects.get(edge.id);
347
582
  const from = endpoint(redirect?.from ?? edge.from), to = endpoint(redirect?.to ?? edge.to);
348
583
  if (!from || !to || from === to) continue;
349
- const exempt = /* @__PURE__ */ new Set([...ancestors(from), ...ancestors(to)]);
584
+ const fromAncestors = ancestors(from), toAncestors = ancestors(to);
585
+ const exempt = /* @__PURE__ */ new Set([...fromAncestors, ...toAncestors]);
350
586
  const boxes = [...nodes.values(), ...frames.values()].filter((box) => !exempt.has(box.id));
351
587
  const isBack = !!back(edge);
352
588
  const obstacles = boxes.map((box) => expand(box, box.frame ? 5 : 8));
@@ -377,12 +613,14 @@ function repairRoutes(geometry, redirects = /* @__PURE__ */ new Map()) {
377
613
  ];
378
614
  collision = edge.segments.some((seg) => avoid.some((other) => sharesRun(seg.p0, seg.p1, other.p0, other.p1)));
379
615
  }
380
- if (!collision) {
616
+ const jog = straighten && !redirect && !isBack && lateral && edge.segments.length > 2 && Math.abs(edge.start[lateral] - edge.end[lateral]) < 24 && edge.segments.every((seg) => coordinate(seg.p0) <= coordinate(seg.p1) + .001);
617
+ const polish = polishFlow && !redirect && !isBack && lateral && edge.segments.length >= 3 && edge.segments.length <= 7 && edge.segments.filter(({ p0, p1 }) => Math.abs(p0.x - p1.x) < .01 || Math.abs(p0.y - p1.y) < .01).length >= 3 && Math.abs(edge.end[lateral] - edge.start[lateral]) >= 8 && (Math.abs(edge.segments[0].p1[lateral] - edge.start[lateral]) > .01 || Math.abs(edge.end[lateral] - edge.segments.at(-1).p0[lateral]) > .01);
618
+ if (!collision && !jog && !polish) {
381
619
  if (checkTracks) settled.push(...edge.segments);
382
620
  continue;
383
621
  }
384
- const labelObstacles = geometry.edges.filter((other) => other !== edge && other.labelRect).map((other) => expand(other.labelRect, 2));
385
- const forwardRuns = isBack ? [] : geometry.edges.filter((other) => other !== edge && other.style !== "invisible" && !pending.has(other.id) && !back(other)).flatMap((other) => other.segments);
622
+ const labelObstacles = geometry.edges.filter((other) => other !== edge && other.labelRect && (!(board || projectForward) || !pending.has(other.id))).map((other) => expand(other.labelRect, 2));
623
+ const forwardRuns = isBack || board ? [] : geometry.edges.filter((other) => other !== edge && other.style !== "invisible" && !pending.has(other.id) && !back(other)).flatMap((other) => other.segments);
386
624
  const lanes = {
387
625
  x: [],
388
626
  y: []
@@ -391,17 +629,37 @@ function repairRoutes(geometry, redirects = /* @__PURE__ */ new Map()) {
391
629
  if (Math.abs(p0.x - p1.x) < .01) lanes.x.push(p0.x);
392
630
  if (Math.abs(p0.y - p1.y) < .01) lanes.y.push(p0.y);
393
631
  }
394
- const route = (clearOf) => shortest(from, to, [
632
+ const boardPorts = board && {
633
+ insideFrom: toAncestors.has(from.id),
634
+ insideTo: fromAncestors.has(to.id),
635
+ fromPorts: ports.get(from.id) ?? [],
636
+ toPorts: ports.get(to.id) ?? []
637
+ };
638
+ const forward = (projectForward && redirect || jog || polish) && !isBack && faces && {
639
+ exit: faces[0],
640
+ enter: faces[1],
641
+ straightOnly: !!jog && !polish,
642
+ coordinate,
643
+ lateral,
644
+ maxBends: polish ? 1 : void 0,
645
+ refine: polishFlow,
646
+ siblings: geometry.edges.filter((other) => other !== edge && other.from === from.id && other.style !== "invisible" && !pending.has(other.id)).flatMap((other) => other.segments)
647
+ };
648
+ const route = (clearOf, rule) => shortest(from, to, [
395
649
  ...obstacles,
396
650
  ...titles,
397
651
  ...labelObstacles
398
652
  ], occupied ?? occupiedOf(), geometry.type, {
399
653
  w: geometry.width,
400
654
  h: geometry.height
401
- }, edge.labelRect, !redirect, isBack, frameSides, arrivals, clearOf, lanes);
402
- const replacement = forwardRuns.length && route(forwardRuns) || route([]);
655
+ }, edge.labelRect, !redirect, isBack, frameSides, arrivals, clearOf, lanes, boardPorts, rule);
656
+ const replacement = forward && (route(forwardRuns, forward) || redirect && route([], forward)) || collision && (forwardRuns.length && route(forwardRuns) || route([]));
403
657
  if (replacement) {
404
658
  pending.delete(edge.id);
659
+ if (board) {
660
+ reserve(from, to, replacement.start, replacement.source);
661
+ reserve(to, from, replacement.end, replacement.target);
662
+ }
405
663
  Object.assign(edge, replacement, {
406
664
  from: from.id,
407
665
  to: to.id,
@@ -0,0 +1,32 @@
1
+ import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { error } from "./support.js";
3
+ // src/diagram/info.js
4
+ var info_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
5
+ function parse(body, info) {
6
+ const errors = [];
7
+ const issue = (line) => errors.push(error(info.lang, line, "info 只接受可选的 showInfo", "info only accepts the optional showInfo keyword", "使用 `info` 或 `info showInfo`", "Use `info` or `info showInfo`"));
8
+ if (info.params && !/^showInfo$/i.test(info.params)) issue(info.headerLine);
9
+ for (const [i, line] of body.split("\n").entries()) if (line.trim() && !/^showInfo$/i.test(line.trim())) issue(info.firstLine + i);
10
+ return {
11
+ model: null,
12
+ errors
13
+ };
14
+ }
15
+ function layout(_model, { ctx }) {
16
+ const label = `KChart v0.2.0`;
17
+ return { scene: {
18
+ width: Math.ceil(ctx.measure(label, 13, 400)) + 24,
19
+ height: 40,
20
+ title: label,
21
+ children: [ctx.text(12, 24, label, {
22
+ size: 13,
23
+ fill: ctx.color("muted")
24
+ })]
25
+ } };
26
+ }
27
+ var implementations = { info: {
28
+ parse,
29
+ layout,
30
+ optionMode: "raw"
31
+ } };
32
+ export { implementations, info_exports };
@@ -271,7 +271,7 @@ function drawIcon(ctx, icon, x, y, size = ICON_SIZE, color = ctx.color("fg")) {
271
271
  }, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, attrs)));
272
272
  }
273
273
  function layout(model, options = {}) {
274
- const { width = 800, ctx, renderOptions = {} } = options;
274
+ const { width = 800, ctx, renderOptions = {}, compact, embedHeading } = options;
275
275
  if (!ctx) throw new TypeError("journey layout needs a theme context");
276
276
  const look = appearance(ctx);
277
277
  const measure = options.measure || ((value, font) => ctx.measure(value, font.size, font.weight));
@@ -311,10 +311,10 @@ function layout(model, options = {}) {
311
311
  const sectionWidth = Math.max(0, ...groups.flatMap((group) => group.name.split("\n").map((line, index) => widthOf(line, sectionFont) + (index === 0 ? iconSlot(group.icon) : 0))));
312
312
  const pitch = bodyFont.size + 5;
313
313
  const left = Math.max(config.leftMargin || 0, LAYOUT.labelX + Math.max(labelWidth, sectionWidth, widthOf(scoreCaption, labelFont)) + LAYOUT.labelPad + FACE_R + LAYOUT.plotGap);
314
- const plotW = Math.max(140, width - left - LAYOUT.right);
314
+ const plotW = compact ? 140 : Math.max(140, width - left - LAYOUT.right);
315
315
  const x0 = left, x1 = left + plotW, step = plotW / 4;
316
- const sceneWidth = Math.ceil(Math.max(width, x1 + LAYOUT.right));
317
- const inSceneTitle = Boolean(model.title) && !renderOptions?.frame;
316
+ const sceneWidth = Math.ceil(Math.max(compact ? 0 : width, x1 + LAYOUT.right));
317
+ const inSceneTitle = Boolean(model.title) && !renderOptions?.frame && !embedHeading;
318
318
  if (inSceneTitle) {
319
319
  const titleText = ctx.text(LAYOUT.labelX + iconSlot(model.titleIcon), titleSize + 12, model.title, {
320
320
  size: titleSize,
@@ -498,7 +498,7 @@ function layout(model, options = {}) {
498
498
  height,
499
499
  children,
500
500
  ...model.title ? { title: model.title } : {},
501
- ...renderOptions?.frame && model.title ? { heading: model.title } : {}
501
+ ...(renderOptions?.frame || embedHeading) && model.title ? { heading: model.title } : {}
502
502
  },
503
503
  errors
504
504
  };
@@ -168,13 +168,13 @@ function drawIcon(ctx, icon, x, y, ink) {
168
168
  "stroke-linejoin": "round"
169
169
  }, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, attrs)));
170
170
  }
171
- function layout(model, { width, ctx, renderOptions }, info = {}) {
171
+ function layout(model, { width, ctx, renderOptions, compact, embedHeading }, info = {}) {
172
172
  const { columns, config, title, count } = model, t = appearance(ctx), gap = 14, pad = 18;
173
173
  const colWidth = Math.max(180, Math.min(560, Number(config.sectionWidth) || 220));
174
174
  const inset = Math.max(8, Math.min(28, Number(config.padding) || 10));
175
175
  const colsWidth = columns.length * (colWidth + gap) - gap;
176
- const w = Math.max(width, colsWidth + 36), left = (w - colsWidth) / 2;
177
- const titleInline = title && !renderOptions?.frame, top = titleInline ? 52 : pad;
176
+ const w = Math.max(compact ? 0 : width, colsWidth + 36), left = (w - colsWidth) / 2;
177
+ const titleInline = title && !renderOptions?.frame && !embedHeading, top = titleInline ? 52 : pad;
178
178
  const cardWidth = colWidth - 2 * inset, cardPad = 12, inner = cardWidth - 24;
179
179
  const headers = columns.map((c) => ctx.wrap(c.label, cardWidth - (c.icon ? 22 : 0), FONT.title.size, FONT.title.weight));
180
180
  const headerHeight = Math.max(...headers.map((lines) => lines.length)) * 18 + 22;
@@ -109,7 +109,7 @@ function parse(source, info) {
109
109
  errors
110
110
  };
111
111
  }
112
- function layout(model, { measure, width, lang }) {
112
+ function layout(model, { measure, width, lang, compact }) {
113
113
  const errors = [], groups = [], layers = [], nodes = [];
114
114
  const all = model.layered ? model.layers.flatMap((l) => l.groups) : model.groups;
115
115
  for (const group of all) for (const item of group.items) if (measure(item.label.replace(/^[+~=]\s*/, ""), FONT.body) > LIMITS.labelWidth) errors.push(issue(lang, item.line, `能力项「${item.label}」过长`, `Capability label is too long: ${item.label}`, "把说明移到图外,名称缩短至约 16 个汉字", "Move descriptions outside; shorten the label to about 16 CJK characters"));
@@ -180,7 +180,7 @@ function layout(model, { measure, width, lang }) {
180
180
  line: row.line
181
181
  }, x + PAD, ny, labelW, ih, true);
182
182
  const mins = row.items.map((item) => snap(Math.max(64, measure(item.label, FONT.body) + 16)));
183
- const ws = distribute(mins, mins.map((n) => n + 8), inside - labelW - ITEM_GAP * row.items.length);
183
+ const ws = compact ? mins : distribute(mins, mins.map((n) => n + 8), inside - labelW - ITEM_GAP * row.items.length);
184
184
  let nx = x + PAD + labelW + ITEM_GAP;
185
185
  row.items.forEach((item, i) => {
186
186
  makeNode(item, nx, ny, ws[i], ih);
@@ -194,11 +194,11 @@ function layout(model, { measure, width, lang }) {
194
194
  for (let ri = 0; ri < countRows; ri += 1) {
195
195
  const items = g.items.slice(ri * cols, (ri + 1) * cols);
196
196
  const db = g.flag === "db";
197
- const count = db ? cols : items.length;
198
- const cw = Math.floor((inside - (count - 1) * ITEM_GAP) / count / 4) * 4;
197
+ const count = db || compact ? cols : items.length;
198
+ const cw = compact ? cellW(g, 16) : Math.floor((inside - (count - 1) * ITEM_GAP) / count / 4) * 4;
199
199
  let nx = x + PAD + (db ? Math.floor((inside - (items.length * cw + (items.length - 1) * ITEM_GAP)) / 8) * 4 : 0);
200
200
  items.forEach((item, ci) => {
201
- const nw = !db && ci === items.length - 1 ? x + w - PAD - nx : cw;
201
+ const nw = !db && !compact && ci === items.length - 1 ? x + w - PAD - nx : cw;
202
202
  makeNode(item, nx, top + ri * (ih + ITEM_GAP), nw, ih);
203
203
  nx += cw + ITEM_GAP;
204
204
  });
@@ -220,7 +220,8 @@ function layout(model, { measure, width, lang }) {
220
220
  for (const layer of model.layers) {
221
221
  let rows = 1;
222
222
  while (rows < LIMITS.items * 2 && total(layer.groups, rows, 16) > available) rows += 1;
223
- const widths = distribute(layer.groups.map((g) => groupW(g, rows, 16)), layer.groups.map((g) => groupW(g, rows, 24)), available - GAP * (layer.groups.length - 1));
223
+ const mins = layer.groups.map((g) => groupW(g, rows, 16)), ideals = layer.groups.map((g) => groupW(g, rows, 24));
224
+ const widths = compact ? mins : distribute(mins, ideals, available - GAP * (layer.groups.length - 1));
224
225
  const descriptionLines = layer.description ? wrapText(layer.description, nameWidth - 8, FONT.label, measure) : [];
225
226
  const h = Math.max(...layer.groups.map((g) => groupH(g, rows)), 32 + descriptionLines.length * 16);
226
227
  const color = layers.length % 6 + 1;
@@ -261,7 +262,8 @@ function layout(model, { measure, width, lang }) {
261
262
  }
262
263
  if (current.length) lines.push(current);
263
264
  for (const line of lines) {
264
- const ws = distribute(line.map((g) => groupW(g, rowCount(g), 16)), line.map((g) => groupW(g, rowCount(g), 24)), available - GAP * (line.length - 1));
265
+ const mins = line.map((g) => groupW(g, rowCount(g), 16));
266
+ const ws = compact ? mins : distribute(mins, line.map((g) => groupW(g, rowCount(g), 24)), available - GAP * (line.length - 1));
265
267
  const h = Math.max(...line.map((g) => groupH(g, rowCount(g))));
266
268
  let x = 4;
267
269
  line.forEach((g, i) => {
@@ -254,10 +254,14 @@ var mindmapRegistry = createRegistry(validate);
254
254
  mindmapRegistry.register("mindmap", layouts.get("mindmap"));
255
255
  function layout(model, options = {}, info = {}) {
256
256
  const radial = model.nodeCount > 80 && model.layout !== "radial";
257
+ const renderOptions = options.renderOptions || options;
257
258
  const scene = mindmapRegistry.layout(radial ? {
258
259
  ...model,
259
260
  layout: "radial"
260
- } : model, options.renderOptions || options);
261
+ } : model, options.compact ? {
262
+ ...renderOptions,
263
+ width: options.width
264
+ } : renderOptions);
261
265
  const requested = options.width || 800;
262
266
  const errors = model.nodeCount > 60 || model.maxDepth > 5 || scene.width > requested * 2 ? [warning("LARGE_DIAGRAM", info.lang || options.lang || "zh", info.firstLine || 1, radial ? "大型脑图使用径向布局以避免重叠" : "脑图较大,可在图内滚动查看", radial ? "Large mindmap uses radial layout to avoid overlaps" : "Large mindmap; scroll within the figure", "拆分脑图以便阅读", "Split the mindmap for readability")] : [];
263
267
  return {