@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
@@ -1,5 +1,8 @@
1
1
  import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
2
  import { resolveIconRef } from "../core/icons.js";
3
+ import { boardLook, drawEdge, drawFrame, drawLabel, frameTitle, markers, measureLabel, surfaceAt, titleRect } from "./board-paint.js";
4
+ import { routeBoard } from "./board-route.js";
5
+ import "./graph-route.js";
3
6
  import { normalizeLabel } from "./label.js";
4
7
  import { presentation } from "./presentation.js";
5
8
  import { FONT, appearance, error, maxWidthOf, textBlock, warning, wrapText } from "./support.js";
@@ -110,7 +113,7 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
110
113
  const note = (issue) => {
111
114
  if (!notices.has(issue.message)) notices.set(issue.message, issue);
112
115
  };
113
- const ignoreStyle = (at, zh, en) => note(warning("IGNORED_STYLE", lang, at, zh, en, "使用支持的状态样式", "Use supported status styling"));
116
+ const ignoreStyle = (at, zh, en) => note(warning("IGNORED_STYLE", lang, at, zh, en, "移除此样式;普通块可使用 A:::ok 标记状态", "Remove this style; use A:::ok to mark the status of an ordinary block"));
114
117
  const resolveIcon = (ref, at) => {
115
118
  const icon = resolveIconRef(ref);
116
119
  if (!icon) note(warning("IGNORED_ASSET", lang, at, `未知图标 ${ref}`, `Unknown icon ${ref}`, "使用内置图标", "Use a supported icon"));
@@ -134,9 +137,9 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
134
137
  };
135
138
  };
136
139
  let anonymous = 0, reserved = 0, line = firstLine, column = 1;
137
- const bad = (zh, en) => errors.push(error(lang, line, zh, en, "检查 block 语法或拆分图", "Check block syntax or split the diagram", column));
140
+ const bad = (zh, en, fixZh, fixEn) => errors.push(error(lang, line, zh, en, fixZh, fixEn, column));
138
141
  if (body.length + params.length > 2e5) {
139
- bad("块图源码过长", "Block source is too long");
142
+ bad("块图源码过长", "Block source is too long", "拆分块图,使每张图的源码不超过 200,000 个字符", "Split the source into diagrams of at most 200,000 characters");
140
143
  return {
141
144
  model: null,
142
145
  errors
@@ -272,7 +275,7 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
272
275
  take(cols[0].length);
273
276
  const count = cols[1] === "auto" ? 0 : Number(cols[1]);
274
277
  if (count > 2e3 || count === 0 && cols[1] !== "auto") {
275
- bad("无效的列数", "Invalid column count");
278
+ bad("无效的列数", "Invalid column count", "使用 columns auto 或指定 1–2000 列", "Use columns auto or set a count from 1 to 2000");
276
279
  break;
277
280
  }
278
281
  group.columns = count;
@@ -280,12 +283,12 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
280
283
  take(space[0].length);
281
284
  const span = readSpan();
282
285
  if (span > 2e3) {
283
- bad("占位过宽", "Space span is too large");
286
+ bad("占位过宽", "Space span is too large", "将 space:N 中的 N 减至 2000 以内", "Reduce N in space:N to at most 2000");
284
287
  break;
285
288
  }
286
289
  reserved += span;
287
290
  if (reserved > 2e4) {
288
- bad("占位单元格过多", "Too many space cells");
291
+ bad("占位单元格过多", "Too many space cells", "减少 space:N 占位,使总数不超过 20,000", "Reduce space:N placeholders to at most 20,000 cells in total");
289
292
  return {
290
293
  model: null,
291
294
  errors
@@ -305,7 +308,9 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
305
308
  span: readSpan()
306
309
  };
307
310
  if (!node || !node.span || node.span > 2e3 || stack.length > 32 || nodes.has(node.id)) {
308
- bad("无效、重复或嵌套过深的复合块", "Invalid, duplicate or excessively nested composite block");
311
+ if (!node || !node.span || node.span > 2e3) bad("无效的复合块声明", "Invalid composite block declaration", "使用 block:Group 并用 end 结束;跨度须为 1–2000", "Use block:Group with a matching end; keep the span between 1 and 2000");
312
+ else if (stack.length > 32) bad("复合块嵌套过深", "Composite blocks are nested too deeply", "将嵌套层数减至 32 层以内", "Reduce nesting to at most 32 levels");
313
+ else bad(`复合块标识符「${node.id}」已使用`, `Composite block ID "${node.id}" is already in use`, "为 block: 后的标识符换一个未使用的名称", "Use a unique ID after block:");
309
314
  break;
310
315
  }
311
316
  node.children = [];
@@ -314,14 +319,14 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
314
319
  } else if (end) {
315
320
  take(end[0].length);
316
321
  if (stack.length === 1) {
317
- bad("end 没有对应的 block", "No matching block for end");
322
+ bad("end 没有对应的 block", "No matching block for end", "删除多余的 end,或在对应的子块前添加 block:Group", "Remove the stray end or open a group with block:Group before its children");
318
323
  break;
319
324
  }
320
325
  stack.pop();
321
326
  } else {
322
327
  let from = readNode();
323
328
  if (!from) {
324
- bad("无法识别块图语句", "Unrecognized block statement");
329
+ bad("无法识别块图语句", "Unrecognized block statement", "使用块声明 A[\"名称\"]、占位 space 或连线 A --> B", "Use a block such as A[\"Label\"], a space placeholder, or an edge such as A --> B");
325
330
  break;
326
331
  }
327
332
  insert(from, group);
@@ -334,7 +339,7 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
334
339
  ws();
335
340
  const to = readNode();
336
341
  if (!to) {
337
- bad("连线缺少终点", "Missing edge endpoint");
342
+ bad("连线缺少终点", "Missing edge endpoint", "在箭头后添加目标块标识符,例如 A --> B", "Add a destination block ID after the arrow, as in A --> B");
338
343
  break;
339
344
  }
340
345
  insert(to, group);
@@ -352,7 +357,7 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
352
357
  pattern: symbol.includes(".-") ? "dotted" : "solid"
353
358
  });
354
359
  if (nodes.size > 2e3 || edges.length > 500) {
355
- bad("块或连线过多", "Too many blocks or edges");
360
+ bad("块或连线过多", "Too many blocks or edges", "拆分为每张最多 2000 个块、500 条连线的图", "Split into diagrams of at most 2000 blocks and 500 edges each");
356
361
  return {
357
362
  model: null,
358
363
  errors
@@ -363,7 +368,7 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
363
368
  }
364
369
  }
365
370
  if (nodes.size > 2e3) {
366
- bad("块过多", "Too many blocks");
371
+ bad("块过多", "Too many blocks", "拆分为每张最多 2000 个块的图", "Split into diagrams of at most 2000 blocks each");
367
372
  return {
368
373
  model: null,
369
374
  errors
@@ -374,12 +379,12 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
374
379
  if (stack.length > 1) {
375
380
  line = stack.at(-1).line;
376
381
  column = 1;
377
- bad("复合块缺少 end", "Composite block is missing end");
382
+ bad("复合块缺少 end", "Composite block is missing end", "在子块之后单独写一行 end;如果它是普通块,去掉 block: 前缀", "Put end on its own line after the children, or remove the block: prefix for an ordinary block");
378
383
  }
379
384
  if (!nodes.size && !root.children.length && !errors.length) {
380
385
  line = firstLine - 1;
381
386
  column = 1;
382
- bad("块图为空", "Empty block diagram");
387
+ bad("块图为空", "Empty block diagram", "在头行后添加块,例如 A[\"名称\"]", "Add a block after the header, for example A[\"Label\"]");
383
388
  }
384
389
  errors.push(...look.diagnostics());
385
390
  for (const node of nodes.values()) {
@@ -400,7 +405,8 @@ function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
400
405
  errors: [...errors, ...notices.values()]
401
406
  };
402
407
  }
403
- function grid(group, measure, pad) {
408
+ function grid(group, measure, pad, spacing) {
409
+ const gaps = spacing.get(group) ?? newGaps();
404
410
  const rows = [], columns = group.columns || Infinity;
405
411
  let col = 0, occupied = 0, unit = 72, row;
406
412
  const currentRow = () => row ??= {
@@ -426,43 +432,55 @@ function grid(group, measure, pad) {
426
432
  }
427
433
  continue;
428
434
  }
429
- const inner = node.children ? grid(node, measure, pad) : null;
430
- const font = inner ? FONT.title : FONT.body;
431
- const label = node.label ?? (inner ? "" : node.id);
432
- const lines = label.trim() ? wrapText(label, NODE_LABEL_MAX - iconWidth(node.icon, [label]), font, measure) : [];
433
- const textW = maxWidthOf(lines, font, measure) + iconWidth(node.icon, lines);
434
- const textH = Math.max(lines.length * 18, node.icon ? ICON : 0);
435
- const title = inner && textH ? textH + 4 : 0;
436
- let w = textW + 28, h = textH + 20;
437
- const compact = !inner && [
438
- "diamond",
439
- "circle",
440
- "doublecircle"
441
- ].includes(node.shape);
442
- if (compact) w = h = Math.ceil(node.shape === "diamond" ? textW + textH + 24 : Math.hypot(textW, textH) + 24);
443
- else if (node.shape === "arrow") {
444
- w += 28;
445
- h += 24;
446
- } else if (node.shape === "cylinder") h += 12;
447
- else if (node.shape && ![
448
- "square",
449
- "round",
450
- "stadium"
451
- ].includes(node.shape)) w += 20;
435
+ const inner = node.children ? grid(node, measure, pad, spacing) : null;
436
+ let cell, w, h;
452
437
  if (inner) {
453
- w = Math.max(w, inner.w + 2 * pad);
454
- h = inner.h + 2 * pad + title;
438
+ const title = frameTitle({
439
+ title: node.label ?? "",
440
+ icon: node.icon
441
+ }, measure);
442
+ const inset = inner.gaps.inset.map((gap, i) => Math.max(gap, i === 0 && title.band ? title.band : pad));
443
+ const [top, right, bottom, left] = inset;
444
+ w = Math.max(title.minW, inner.w + left + right);
445
+ h = inner.h + top + bottom;
446
+ cell = {
447
+ ...node,
448
+ col,
449
+ inner,
450
+ title,
451
+ inset
452
+ };
453
+ } else {
454
+ const label = node.label ?? node.id;
455
+ const lines = label.trim() ? wrapText(label, NODE_LABEL_MAX - iconWidth(node.icon, [label]), FONT.body, measure) : [];
456
+ const textW = maxWidthOf(lines, FONT.body, measure) + iconWidth(node.icon, lines);
457
+ const textH = Math.max(lines.length * 18, node.icon ? ICON : 0);
458
+ w = textW + 28;
459
+ h = textH + 20;
460
+ const compact = [
461
+ "diamond",
462
+ "circle",
463
+ "doublecircle"
464
+ ].includes(node.shape);
465
+ if (compact) w = h = Math.ceil(node.shape === "diamond" ? textW + textH + 24 : Math.hypot(textW, textH) + 24);
466
+ else if (node.shape === "arrow") {
467
+ w += 28;
468
+ h += 24;
469
+ } else if (node.shape === "cylinder") h += 12;
470
+ else if (node.shape && ![
471
+ "square",
472
+ "round",
473
+ "stadium"
474
+ ].includes(node.shape)) w += 20;
475
+ cell = {
476
+ ...node,
477
+ col,
478
+ lines,
479
+ font: FONT.body,
480
+ textH,
481
+ ...compact ? { diameter: w } : {}
482
+ };
455
483
  }
456
- const cell = {
457
- ...node,
458
- col,
459
- inner,
460
- lines,
461
- font,
462
- title,
463
- textH,
464
- ...compact ? { diameter: w } : {}
465
- };
466
484
  currentRow().cells.push(cell);
467
485
  row.h = Math.max(row.h, h);
468
486
  unit = Math.max(unit, Math.ceil((w - (node.span - 1) * GAP) / node.span));
@@ -470,26 +488,66 @@ function grid(group, measure, pad) {
470
488
  advance(Math.min(node.span, columns - col));
471
489
  }
472
490
  if (row) rows.push(row);
473
- const h = rows.reduce((sum, item) => sum + item.h + GAP, -20);
491
+ const xGaps = Array.from({ length: Math.max(0, occupied - 1) }, (_, i) => gaps.x.get(i) ?? GAP);
492
+ const yGaps = rows.slice(1).map((_, i) => gaps.y.get(i) ?? GAP);
474
493
  return {
494
+ group,
495
+ gaps,
475
496
  rows,
476
- unit,
497
+ xGaps,
498
+ yGaps,
477
499
  columns: occupied,
478
- w: Math.max(0, occupied * (unit + GAP) - GAP),
479
- h: Math.max(0, h)
500
+ w: occupied * unit + sum(xGaps),
501
+ h: sum(rows.map((row) => row.h)) + sum(yGaps)
480
502
  };
481
503
  }
482
- function place(grid, x, y, width, nodes, frames, pad) {
504
+ var sum = (values) => values.reduce((a, b) => a + b, 0);
505
+ var newGaps = () => ({
506
+ x: /* @__PURE__ */ new Map(),
507
+ y: /* @__PURE__ */ new Map(),
508
+ inset: [
509
+ 0,
510
+ 0,
511
+ 0,
512
+ 0
513
+ ]
514
+ });
515
+ function place(grid, x, y, width, nodes, frames, corridors, parent, outline) {
483
516
  if (!grid.columns) return;
484
- const pitch = (width + GAP) / grid.columns;
485
- for (const row of grid.rows) {
517
+ const unit = (width - sum(grid.xGaps)) / grid.columns;
518
+ const xs = [x];
519
+ const corridor = (axis, index, start, size, lo, hi, inset = false) => {
520
+ if (size > 0) corridors.push({
521
+ group: grid.group,
522
+ axis,
523
+ index,
524
+ start,
525
+ size,
526
+ lo,
527
+ hi,
528
+ inset
529
+ });
530
+ };
531
+ for (const [i, gap] of grid.xGaps.entries()) {
532
+ corridor("x", i, xs[i] + unit, gap, y, y + grid.h);
533
+ xs.push(xs[i] + unit + gap);
534
+ }
535
+ if (outline) {
536
+ corridor("y", 0, outline.y, y - outline.y, x, x + width, true);
537
+ corridor("x", 1, x + width, outline.x + outline.w - x - width, y, y + grid.h, true);
538
+ corridor("y", 2, y + grid.h, outline.y + outline.h - y - grid.h, x, x + width, true);
539
+ corridor("x", 3, outline.x, x - outline.x, y, y + grid.h, true);
540
+ }
541
+ for (const [r, row] of grid.rows.entries()) {
486
542
  for (const cell of row.cells) {
543
+ const end = cell.col + cell.span - 1;
487
544
  const box = {
488
545
  ...cell,
489
- x: x + cell.col * pitch,
546
+ x: xs[cell.col],
490
547
  y,
491
- w: cell.span * pitch - GAP,
492
- h: row.h
548
+ w: xs[end] - xs[cell.col] + unit,
549
+ h: row.h,
550
+ ...parent ? { parent } : {}
493
551
  };
494
552
  if (cell.diameter) {
495
553
  box.x += (box.w - cell.diameter) / 2;
@@ -503,18 +561,55 @@ function place(grid, x, y, width, nodes, frames, pad) {
503
561
  nodes.set(box.id, box);
504
562
  if (box.inner) {
505
563
  frames.push(box);
506
- place(box.inner, box.x + pad, y + pad + box.title, box.w - 2 * pad, nodes, frames, pad);
564
+ const [top, right, , left] = cell.inset;
565
+ place(box.inner, box.x + left, y + top, box.w - left - right, nodes, frames, corridors, box.id, box);
507
566
  }
508
567
  }
509
- y += row.h + GAP;
568
+ const gap = grid.yGaps[r] ?? 0;
569
+ corridor("y", r, y + row.h, gap, x, x + width);
570
+ y += row.h + gap;
510
571
  }
511
572
  }
512
- var rectPoints = [
513
- [0, 0],
514
- [1, 0],
515
- [1, 1],
516
- [0, 1]
517
- ];
573
+ function widenCorridors(corridors, routes, relations, nodes, spacing) {
574
+ let changed = false;
575
+ for (const { group, axis, index, start, size, lo, hi, inset } of corridors) {
576
+ const along = axis === "x" ? "y" : "x", span = axis === "x" ? "w" : "h", end = start + size;
577
+ const inside = (coord) => start < coord && coord < end;
578
+ let required = size;
579
+ if (!inset) for (const { from, to, label } of relations) {
580
+ if (!label || label[span] + 16 <= required) continue;
581
+ const crossSpan = axis === "x" ? "h" : "w";
582
+ const a = nodes.get(from), b = nodes.get(to);
583
+ const [first, second] = a[axis] < b[axis] ? [a, b] : [b, a];
584
+ if (Math.abs(first[axis] + first[span] - start) < .01 && Math.abs(second[axis] - end) < .01 && Math.min(hi, a[along] + a[crossSpan], b[along] + b[crossSpan]) > Math.max(lo, a[along], b[along])) required = label[span] + 16;
585
+ }
586
+ const events = [];
587
+ for (const { points, labelRect: label } of routes) {
588
+ if (label && inside(label[axis] + label[span] / 2)) {
589
+ const crossSpan = axis === "x" ? "h" : "w", at = label[along] + label[crossSpan] / 2;
590
+ if (lo <= at && at <= hi) required = Math.max(required, label[span] + 16);
591
+ }
592
+ for (let i = 1; i < points.length; i++) {
593
+ const a = points[i - 1], b = points[i];
594
+ if (a[axis] !== b[axis] || !inside(a[axis])) continue;
595
+ const first = Math.max(lo, Math.min(a[along], b[along])), last = Math.min(hi, Math.max(a[along], b[along]));
596
+ if (last > first) events.push([first, 1], [last, -1]);
597
+ }
598
+ }
599
+ let tracks = 0;
600
+ for (const [, delta] of events.sort(([a, da], [b, db]) => a - b || da - db)) {
601
+ tracks += delta;
602
+ if (tracks > 0) required = Math.max(required, 16 + (tracks - 1) * 12);
603
+ }
604
+ if (required <= size + .01) continue;
605
+ const gaps = spacing.get(group) ?? newGaps();
606
+ spacing.set(group, gaps);
607
+ if (inset) gaps.inset[index] = Math.ceil(required);
608
+ else gaps[axis].set(index, Math.ceil(required));
609
+ changed = true;
610
+ }
611
+ return changed;
612
+ }
518
613
  var polygons = {
519
614
  diamond: [
520
615
  [.5, 0],
@@ -638,148 +733,101 @@ function outline(box) {
638
733
  return points?.map(([x, y]) => [box.x + x * box.w, box.y + y * box.h]);
639
734
  }
640
735
  var center = (b) => [b.x + b.w / 2, b.y + b.h / 2];
641
- var cross = (a, b) => a[0] * b[1] - a[1] * b[0];
642
- var vector = (a, b) => [b[0] - a[0], b[1] - a[1]];
643
736
  var roundShape = (b) => b.shape === "circle" || b.shape === "doublecircle";
644
- function attach(box, toward) {
645
- const c = center(box), d = vector(c, toward);
646
- if (roundShape(box)) {
647
- const t = 1 / Math.hypot(d[0] / (box.w / 2), d[1] / (box.h / 2));
648
- return c.map((v, i) => v + d[i] * t);
649
- }
650
- const points = outline(box) || rectPoints.map(([x, y]) => [box.x + x * box.w, box.y + y * box.h]);
651
- let t = Infinity;
652
- for (const [i, p] of points.entries()) {
653
- const q = points[(i + 1) % points.length], side = vector(p, q), det = cross(d, side);
654
- if (Math.abs(det) < 1e-6) continue;
655
- const offset = vector(c, p), u = cross(offset, d) / det, distance = cross(offset, side) / det;
656
- if (u >= 0 && u <= 1 && distance > 0) t = Math.min(t, distance);
657
- }
658
- return Number.isFinite(t) ? c.map((v, i) => v + d[i] * t) : c;
659
- }
660
- var inside = (outer, inner) => outer.x <= inner.x && outer.y <= inner.y && outer.x + outer.w >= inner.x + inner.w && outer.y + outer.h >= inner.y + inner.h;
661
- var intersects = (a, b) => a.x < b.x + b.w && b.x < a.x + a.w && a.y < b.y + b.h && b.y < a.y + a.h;
662
- function hits([a, b], box) {
663
- let lo = 0, hi = 1;
664
- for (const [i, low, high] of [[
665
- 0,
666
- box.x - 2,
667
- box.x + box.w + 2
668
- ], [
669
- 1,
670
- box.y - 2,
671
- box.y + box.h + 2
672
- ]]) {
673
- const d = b[i] - a[i];
674
- if (d === 0) {
675
- if (a[i] < low || a[i] > high) return false;
676
- continue;
737
+ function loop(box) {
738
+ const [cx, cy] = center(box), right = box.x + box.w, top = box.y;
739
+ return [
740
+ {
741
+ x: right,
742
+ y: cy
743
+ },
744
+ {
745
+ x: right + 12,
746
+ y: cy
747
+ },
748
+ {
749
+ x: right + 12,
750
+ y: top - 12
751
+ },
752
+ {
753
+ x: cx,
754
+ y: top - 12
755
+ },
756
+ {
757
+ x: cx,
758
+ y: top
677
759
  }
678
- const t = (low - a[i]) / d, u = (high - a[i]) / d;
679
- lo = Math.max(lo, Math.min(t, u));
680
- hi = Math.min(hi, Math.max(t, u));
681
- }
682
- return lo <= hi;
683
- }
684
- var segments = (points) => points.slice(1).map((b, i) => [points[i], b]);
685
- var length = ([a, b]) => Math.hypot(...vector(a, b));
686
- function route(edge, a, b, boxes, used, measure, budget) {
687
- const start = center(a), end = center(b);
688
- const obstacles = boxes.filter((box) => box !== a && box !== b && !(box.inner && (inside(box, a) || inside(box, b))));
689
- const lines = edge.label.trim() ? wrapText(edge.label, 176, FONT.label, measure) : [];
690
- const labelW = maxWidthOf(lines, FONT.label, measure) + iconWidth(edge.icon, lines) + 12;
691
- const labelH = Math.max(lines.length * 16, edge.icon ? ICON : 0) + 4;
692
- const candidates = [
693
- [],
694
- [[start[0], end[1]]],
695
- [[end[0], start[1]]]
696
760
  ];
697
- for (const lane of [...new Set(boxes.flatMap((box) => [box.x - GAP / 2, box.x + box.w + GAP / 2]))]) candidates.push([[lane, start[1]], [lane, end[1]]]);
698
- for (const lane of [...new Set(boxes.flatMap((box) => [box.y - GAP / 2, box.y + box.h + GAP / 2]))]) candidates.push([[start[0], lane], [end[0], lane]]);
699
- if (a === b) candidates.unshift([
700
- [a.x + a.w + 8, start[1]],
701
- [a.x + a.w + 8, a.y - 8],
702
- [start[0], a.y - 8]
703
- ]);
704
- let best;
705
- for (const via of candidates) {
706
- const path = [
707
- start,
708
- ...via,
709
- end
710
- ].filter((p, i, all) => !i || length([all[i - 1], p]) > .01);
711
- if (path.length < 2) continue;
712
- const first = attach(a, path[1]), last = attach(b, path.at(-2));
713
- if (length([start, first]) >= length([start, path[1]]) || length([end, last]) >= length([end, path.at(-2)])) continue;
714
- path[0] = first;
715
- path[path.length - 1] = last;
716
- const legs = segments(path), cost = legs.reduce((sum, leg) => sum + length(leg), via.length * 12);
717
- if (best && cost >= best.cost) continue;
718
- if ((budget.left -= legs.length * (obstacles.length + 1)) < 0) return best;
719
- if (legs.some((leg) => obstacles.some((box) => hits(leg, box)))) continue;
720
- let plate;
721
- if (lines.length || edge.icon) {
722
- for (const [p, q] of [...legs].sort((i, j) => length(j) - length(i))) {
723
- const x = (p[0] + q[0] - labelW) / 2, y = (p[1] + q[1] - labelH) / 2;
724
- for (const offset of [
725
- [0, 0],
726
- [0, -labelH / 2 - 4],
727
- [0, labelH / 2 + 4],
728
- [-labelW / 2 - 4, 0],
729
- [labelW / 2 + 4, 0]
730
- ]) {
731
- const box = {
732
- x: x + offset[0],
733
- y: y + offset[1],
734
- w: labelW,
735
- h: labelH
736
- };
737
- if (!boxes.some((node) => (!node.inner || obstacles.includes(node)) && intersects(node, box)) && !used.some((other) => intersects(other, box))) {
738
- plate = box;
739
- break;
740
- }
741
- }
742
- if (plate) break;
743
- }
744
- if (!plate) continue;
745
- }
746
- best = {
747
- ...edge,
748
- cost,
749
- path,
750
- plate,
751
- lines
752
- };
753
- if (!via.length) return best;
754
- }
755
- return best;
756
761
  }
757
762
  function layout(model, { width, lang, measure }, info) {
758
763
  const padding = info.config?.block?.padding;
759
764
  const validPadding = Number.isFinite(padding) && padding >= 0 && padding <= 2e3;
760
765
  const pad = validPadding ? padding : PAD;
761
- const plan = grid(model.root, measure, pad), nodes = /* @__PURE__ */ new Map(), frames = [], edges = [], plates = [], errors = [];
766
+ const spacing = /* @__PURE__ */ new Map(), edges = [], errors = [];
762
767
  if (padding !== void 0 && !validPadding) errors.push(warning("IGNORED_CONFIG", lang, info.configLines?.["block.padding"] ?? info.headerLine ?? 1, "已忽略无效的 block.padding", "Ignored invalid block.padding", "使用 0 到 2000 之间的数值", "Use a number from 0 to 2000"));
763
- place(plan, MARGIN, MARGIN, plan.w, nodes, frames, pad);
764
- const boxes = [...nodes.values()], budget = { left: 2e6 };
765
- for (const edge of model.edges) {
766
- const routed = route(edge, nodes.get(edge.from), nodes.get(edge.to), boxes, plates, measure, budget);
767
- if (budget.left < 0) {
768
- errors.push(error(lang, edge.line, "块图连线过于复杂", "Block routing is too complex", "拆分图表", "Split the diagram"));
769
- break;
770
- }
771
- if (routed) {
772
- edges.push(routed);
773
- if (routed.plate) plates.push(routed.plate);
774
- } else errors.push(error(lang, edge.line, `无法为 ${edge.from} → ${edge.to} 留出连线空间`, `No clear route for ${edge.from} → ${edge.to}`, "增加 space 留出走线空间", "Add space cells to make room for the edge"));
768
+ const labels = model.edges.map((edge) => measureLabel(edge.label, measure, edge.icon));
769
+ const relations = model.edges.flatMap((edge, id) => edge.from === edge.to ? [] : [{
770
+ id,
771
+ from: edge.from,
772
+ to: edge.to,
773
+ ...labels[id] ? { label: labels[id] } : {}
774
+ }]);
775
+ let nodes, frames, leaves, naturalWidth, height, routed;
776
+ for (let pass = 0; pass < 3; pass++) {
777
+ const plan = grid(model.root, measure, pad, spacing), corridors = [];
778
+ nodes = /* @__PURE__ */ new Map();
779
+ frames = [];
780
+ place(plan, MARGIN, MARGIN, plan.w, nodes, frames, corridors);
781
+ leaves = [...nodes.values()].filter((box) => !box.inner);
782
+ naturalWidth = plan.w + 48;
783
+ height = plan.h + 48;
784
+ routed = routeBoard({
785
+ width: naturalWidth,
786
+ height,
787
+ nodes: leaves,
788
+ frames: frames.map((frame) => ({
789
+ ...frame,
790
+ titleRect: titleRect(frame, frame.title)
791
+ })),
792
+ edges: relations
793
+ });
794
+ if (pass === 2 || !widenCorridors(corridors, routed.edges, relations, nodes, spacing)) break;
775
795
  }
776
- const naturalWidth = plan.w + 48;
796
+ const routes = new Map(routed.edges.map((route) => [route.id, route]));
797
+ model.edges.forEach((edge, id) => {
798
+ const route = routes.get(id), label = labels[id];
799
+ if (edge.from === edge.to) {
800
+ const points = loop(nodes.get(edge.from));
801
+ const rect = label && {
802
+ x: points[2].x + 4,
803
+ y: points[2].y - label.h / 2,
804
+ w: label.w,
805
+ h: label.h
806
+ };
807
+ edges.push({
808
+ ...edge,
809
+ points,
810
+ label: rect ? {
811
+ rect,
812
+ lines: label.lines
813
+ } : null
814
+ });
815
+ } else if (route) edges.push({
816
+ ...edge,
817
+ points: route.points,
818
+ label: label && route.labelRect ? {
819
+ rect: route.labelRect,
820
+ lines: label.lines
821
+ } : null
822
+ });
823
+ else errors.push(error(lang, edge.line, `无法为 ${edge.from} → ${edge.to} 留出连线空间`, `No clear route for ${edge.from} → ${edge.to}`, "增加 space 留出走线空间", "Add space cells to make room for the edge"));
824
+ });
777
825
  if (naturalWidth > 2 * width || model.nodeCount > 60) errors.push(warning("LARGE_DIAGRAM", lang, info.headerLine || 1, "块图较大,可滚动查看", "Large block diagram; scroll to view it", "考虑拆分块图", "Consider splitting the diagram"));
778
826
  return {
779
827
  width: naturalWidth,
780
- height: plan.h + 48,
828
+ height,
781
829
  pad,
782
- nodes: boxes.filter((box) => !box.inner),
830
+ nodes: leaves,
783
831
  frames,
784
832
  edges,
785
833
  errors
@@ -848,66 +896,23 @@ function caption(ctx, { icon, x, y, lines, font, lineHeight }) {
848
896
  }, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, attrs)));
849
897
  return ctx.el("g", {}, [glyph, text]);
850
898
  }
899
+ var markKinds = {
900
+ point: "arrow",
901
+ circle: "circle",
902
+ cross: "cross"
903
+ };
851
904
  function scene(g, ctx) {
852
905
  const ink = ctx.color("muted"), bg = ctx.design.bg, nodeFill = appearance(ctx).node;
853
- const markerId = (kind, status) => `block-${kind}${status ? `-${status}` : ""}`;
854
- const marker = (id, child) => ctx.el("marker", {
855
- id,
856
- viewBox: "0 0 10 10",
857
- refX: 9,
858
- refY: 5,
859
- markerWidth: 7,
860
- markerHeight: 7,
861
- markerUnits: "userSpaceOnUse",
862
- orient: "auto-start-reverse"
863
- }, [child]);
864
- const marks = /* @__PURE__ */ new Map();
865
- const mark = (kind, status, color) => {
866
- const id = markerId(kind, status);
867
- if (!marks.has(id)) {
868
- const child = kind === "point" ? ctx.path("M0 0 L10 5 L0 10 Z", { fill: color }) : kind === "circle" ? ctx.el("circle", {
869
- cx: 5,
870
- cy: 5,
871
- r: 4,
872
- fill: bg,
873
- stroke: color
874
- }) : ctx.path("M1 1 L9 9 M9 1 L1 9", {
875
- fill: "none",
876
- stroke: color,
877
- "stroke-width": 1.8
878
- });
879
- marks.set(id, marker(id, child));
880
- }
881
- return `url(#${id})`;
882
- };
883
- const children = [];
906
+ const look = boardLook(ctx), marks = markers(ctx);
884
907
  const attachLink = (node, shape) => node.link ? ctx.link(node.link.href, [shape], node.link) : shape;
885
- for (const [i, frame] of g.frames.entries()) {
886
- const { x, y, w, h, status } = frame;
887
- const tone = ctx.frameTone(status ? ctx.design[status] : ctx.color(`tone${i % 6 + 1}`));
888
- const shape = ctx.rect(x, y, w, h, {
889
- rx: 5,
890
- fill: ctx.tint(bg, tone, .07),
891
- stroke: ctx.tint(bg, tone, .45)
892
- });
893
- const text = caption(ctx, {
894
- ...frame,
895
- x: x + w / 2,
896
- y: y + g.pad + frame.textH / 2,
897
- lineHeight: 18
898
- });
899
- children.push(attachLink(frame, {
900
- ...ctx.el("g", {}, [shape, text]),
901
- meta: {
902
- kind: "frame",
903
- id: frame.id,
904
- ...status ? { status } : {}
905
- }
906
- }));
907
- }
908
- for (const node of g.nodes) {
908
+ const groups = g.frames.map((frame) => ({
909
+ ...frame,
910
+ style: frame.status
911
+ }));
912
+ const frames = groups.map((frame) => attachLink(frame, drawFrame(ctx, frame, look)));
913
+ const nodes = g.nodes.map((node) => {
909
914
  const [x, y] = center(node);
910
- const { status } = node, tone = status && ctx.frameTone(ctx.design[status]);
915
+ const { status } = node, tone = status && ctx.design[status];
911
916
  const parts = drawShape(ctx, node, {
912
917
  fill: tone ? ctx.tint(bg, tone, .11) : nodeFill,
913
918
  stroke: ctx.tint(bg, tone || ink, .62),
@@ -919,62 +924,49 @@ function scene(g, ctx) {
919
924
  y,
920
925
  lineHeight: 18
921
926
  }));
922
- children.push(attachLink(node, {
927
+ return attachLink(node, {
923
928
  ...ctx.el("g", {}, parts),
924
929
  meta: {
925
930
  kind: "node",
926
931
  id: node.id,
927
932
  ...status ? { status } : {}
928
933
  }
934
+ });
935
+ });
936
+ const edges = [], labels = [];
937
+ for (const edge of g.edges) {
938
+ const { from, to, pattern, thickness, arrowStart, arrowEnd, label, icon, status } = edge;
939
+ const stroke = thickness > 2 ? {
940
+ ...look.edge("plain", status),
941
+ width: 2.5
942
+ } : look.edge(pattern === "dotted" ? "dashed" : "plain", status);
943
+ const end = (kind) => kind && marks.url(markKinds[kind], stroke.stroke);
944
+ edges.push(drawEdge(ctx, edge, stroke, {
945
+ start: end(arrowStart),
946
+ end: end(arrowEnd)
929
947
  }));
930
- }
931
- for (const { from, to, path, pattern, thickness, arrowStart, arrowEnd, plate, lines, icon, status } of g.edges) {
932
- const color = status ? ctx.frameTone(ctx.design[status], true) : ink;
933
- const attrs = {
934
- fill: "none",
935
- stroke: color,
936
- "stroke-width": thickness,
937
- "stroke-linecap": "round",
938
- "stroke-linejoin": "round"
939
- };
940
- if (pattern === "dotted") attrs["stroke-dasharray"] = "2 4";
941
- if (arrowStart) attrs["marker-start"] = mark(arrowStart, status, color);
942
- if (arrowEnd) attrs["marker-end"] = mark(arrowEnd, status, color);
943
- children.push({
944
- ...ctx.path(`M${path.map((p) => p.join(" ")).join(" L")}`, attrs),
945
- meta: {
946
- kind: "edge",
948
+ if (label) {
949
+ const { rect } = label;
950
+ labels.push(drawLabel(ctx, {
947
951
  from,
948
- to
949
- }
950
- });
951
- if (plate) {
952
- const { x, y, w, h } = plate;
953
- children.push({
954
- ...ctx.el("g", {}, [ctx.rect(x, y, w, h, {
955
- fill: bg,
956
- rx: 4
957
- }), caption(ctx, {
958
- x: x + w / 2,
959
- y: y + h / 2,
960
- lines,
961
- icon,
962
- font: FONT.label,
963
- lineHeight: 16
964
- })]),
965
- meta: {
966
- kind: "edge-label-group",
967
- from,
968
- to
969
- }
970
- });
952
+ to,
953
+ rect,
954
+ lines: label.lines,
955
+ icon,
956
+ plate: surfaceAt(groups, look, rect.x + rect.w / 2, rect.y + rect.h / 2)
957
+ }, look));
971
958
  }
972
959
  }
973
- if (marks.size) children.unshift(ctx.el("defs", {}, [...marks.values()]));
974
960
  return {
975
961
  width: g.width,
976
962
  height: g.height,
977
- children
963
+ children: [
964
+ ...marks.defs(),
965
+ ...frames,
966
+ ...edges,
967
+ ...nodes,
968
+ ...labels
969
+ ]
978
970
  };
979
971
  }
980
972
  var implementations = { block: {