@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.
- package/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE.md +11 -0
- package/README.md +68 -2
- package/README.zh-CN.md +69 -0
- package/dist/LICENSE +21 -0
- package/dist/NOTICE.md +11 -0
- package/dist/assets/ICONS-LICENSE.txt +53 -0
- package/dist/assets/maps/LICENSE.md +75 -0
- package/dist/assets/maps/METADATA.md +9 -0
- package/dist/assets/maps/REGIONS.md +28 -0
- package/dist/assets/maps/us-states.geojson +1 -0
- package/dist/assets/maps/world.geojson +1 -0
- package/dist/esm/_virtual/_kchart-families.js +48 -0
- package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
- package/dist/esm/assets/icons.js +11 -0
- package/dist/esm/assets/maps/us-states.js +3 -0
- package/dist/esm/assets/maps/world.js +3 -0
- package/dist/esm/assets/themes/claude.js +69 -0
- package/dist/esm/assets/themes/cloudmusic.js +62 -0
- package/dist/esm/assets/themes/kanagawa.js +67 -0
- package/dist/esm/assets/themes/obsidian.js +67 -0
- package/dist/esm/assets/themes/paper.js +61 -0
- package/dist/esm/assets/themes/terminal.js +60 -0
- package/dist/esm/assets/themes/tokyo.js +62 -0
- package/dist/esm/chart/basic.js +1313 -0
- package/dist/esm/chart/catalog.js +476 -0
- package/dist/esm/chart/composition.js +844 -0
- package/dist/esm/chart/context.js +189 -0
- package/dist/esm/chart/distribution.js +442 -0
- package/dist/esm/chart/fields.js +67 -0
- package/dist/esm/chart/maps.js +563 -0
- package/dist/esm/chart/mindmap.js +629 -0
- package/dist/esm/chart/multivariate.js +1229 -0
- package/dist/esm/chart/registry.js +108 -0
- package/dist/esm/chart/relations.js +949 -0
- package/dist/esm/chart/spec.js +1415 -0
- package/dist/esm/chart/stat-compare.js +302 -0
- package/dist/esm/chart/stat-trend.js +181 -0
- package/dist/esm/chart/statistics.js +1007 -0
- package/dist/esm/chart/unit-variants.js +645 -0
- package/dist/esm/chart/variants.js +552 -0
- package/dist/esm/core/color.js +197 -0
- package/dist/esm/core/context.js +256 -0
- package/dist/esm/core/diagnostics.js +23 -0
- package/dist/esm/core/format.js +167 -0
- package/dist/esm/core/geometry.js +130 -0
- package/dist/esm/core/icon-data.js +5 -0
- package/dist/esm/core/icons.js +173 -0
- package/dist/esm/core/scale.js +112 -0
- package/dist/esm/core/scene.js +234 -0
- package/dist/esm/core/svg.js +89 -0
- package/dist/esm/core/text.js +184 -0
- package/dist/esm/core/theme.js +318 -0
- package/dist/esm/diagram/agentflow.js +596 -0
- package/dist/esm/diagram/architecture.js +729 -0
- package/dist/esm/diagram/block.js +988 -0
- package/dist/esm/diagram/blocks.js +687 -0
- package/dist/esm/diagram/c4.js +1002 -0
- package/dist/esm/diagram/class.js +1158 -0
- package/dist/esm/diagram/cynefin.js +749 -0
- package/dist/esm/diagram/emphasis.js +7 -0
- package/dist/esm/diagram/er.js +657 -0
- package/dist/esm/diagram/eventmodeling.js +948 -0
- package/dist/esm/diagram/gantt-date.js +323 -0
- package/dist/esm/diagram/gantt.js +661 -0
- package/dist/esm/diagram/gitgraph.js +472 -0
- package/dist/esm/diagram/graph-layout.js +4046 -0
- package/dist/esm/diagram/graph-parse.js +1366 -0
- package/dist/esm/diagram/graph-route.js +490 -0
- package/dist/esm/diagram/graph.js +553 -0
- package/dist/esm/diagram/ishikawa.js +496 -0
- package/dist/esm/diagram/journey.js +512 -0
- package/dist/esm/diagram/kanban.js +280 -0
- package/dist/esm/diagram/label.js +56 -0
- package/dist/esm/diagram/layers.js +385 -0
- package/dist/esm/diagram/map-label.js +44 -0
- package/dist/esm/diagram/mindmap.js +277 -0
- package/dist/esm/diagram/numeric-composition.js +78 -0
- package/dist/esm/diagram/numeric-model.js +86 -0
- package/dist/esm/diagram/numeric-parse.js +205 -0
- package/dist/esm/diagram/numeric-radar.js +355 -0
- package/dist/esm/diagram/numeric-sankey.js +201 -0
- package/dist/esm/diagram/numeric-xy.js +422 -0
- package/dist/esm/diagram/packet.js +175 -0
- package/dist/esm/diagram/presentation.js +252 -0
- package/dist/esm/diagram/quadrant.js +297 -0
- package/dist/esm/diagram/railroad-parse.js +465 -0
- package/dist/esm/diagram/railroad.js +294 -0
- package/dist/esm/diagram/registry.js +296 -0
- package/dist/esm/diagram/relations.js +966 -0
- package/dist/esm/diagram/requirement.js +341 -0
- package/dist/esm/diagram/sequence.js +1228 -0
- package/dist/esm/diagram/shapes.js +366 -0
- package/dist/esm/diagram/source.js +538 -0
- package/dist/esm/diagram/support.js +153 -0
- package/dist/esm/diagram/swimlane-mermaid.js +140 -0
- package/dist/esm/diagram/swimlane.js +1186 -0
- package/dist/esm/diagram/time.js +748 -0
- package/dist/esm/diagram/timeline.js +376 -0
- package/dist/esm/diagram/tree-family-icon.js +22 -0
- package/dist/esm/diagram/treeview.js +293 -0
- package/dist/esm/diagram/usecase.js +852 -0
- package/dist/esm/diagram/venn.js +1038 -0
- package/dist/esm/diagram/wardley.js +838 -0
- package/dist/esm/index.d.ts +133 -0
- package/dist/esm/index.js +92 -0
- package/dist/esm/interaction.js +561 -0
- package/dist/esm/mermaid.d.ts +28 -0
- package/dist/esm/mermaid.js +70 -0
- package/dist/esm/node.d.ts +68 -0
- package/dist/esm/node.js +373 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/page-api.js +65 -0
- package/dist/esm/runtime.js +352 -0
- package/dist/esm/src/families.js +209 -0
- package/dist/page-api.js +1 -0
- package/dist/units/chart/basic.js +1 -0
- package/dist/units/chart/catalog.js +1 -0
- package/dist/units/chart/composition.js +1 -0
- package/dist/units/chart/context.js +1 -0
- package/dist/units/chart/distribution.js +1 -0
- package/dist/units/chart/fields.js +1 -0
- package/dist/units/chart/maps.js +1 -0
- package/dist/units/chart/mindmap.js +1 -0
- package/dist/units/chart/multivariate.js +1 -0
- package/dist/units/chart/registry.js +1 -0
- package/dist/units/chart/relations.js +1 -0
- package/dist/units/chart/spec.js +3 -0
- package/dist/units/chart/stat-compare.js +1 -0
- package/dist/units/chart/stat-trend.js +1 -0
- package/dist/units/chart/statistics.js +1 -0
- package/dist/units/chart/unit-variants.js +1 -0
- package/dist/units/chart/variants.js +1 -0
- package/dist/units/core/color.js +1 -0
- package/dist/units/core/context.js +1 -0
- package/dist/units/core/diagnostics.js +1 -0
- package/dist/units/core/format.js +1 -0
- package/dist/units/core/geometry.js +1 -0
- package/dist/units/core/icon-data.js +1 -0
- package/dist/units/core/icons.js +1 -0
- package/dist/units/core/scale.js +1 -0
- package/dist/units/core/scene.js +1 -0
- package/dist/units/core/svg.js +1 -0
- package/dist/units/core/text.js +2 -0
- package/dist/units/core/theme.js +1 -0
- package/dist/units/diagram/agentflow.js +12 -0
- package/dist/units/diagram/architecture.js +4 -0
- package/dist/units/diagram/block.js +2 -0
- package/dist/units/diagram/blocks.js +2 -0
- package/dist/units/diagram/c4.js +6 -0
- package/dist/units/diagram/class.js +5 -0
- package/dist/units/diagram/cynefin.js +4 -0
- package/dist/units/diagram/emphasis.js +1 -0
- package/dist/units/diagram/er.js +9 -0
- package/dist/units/diagram/eventmodeling.js +5 -0
- package/dist/units/diagram/gantt-date.js +1 -0
- package/dist/units/diagram/gantt.js +3 -0
- package/dist/units/diagram/gitgraph.js +2 -0
- package/dist/units/diagram/graph-layout.js +4 -0
- package/dist/units/diagram/graph-parse.js +9 -0
- package/dist/units/diagram/graph-route.js +1 -0
- package/dist/units/diagram/graph.js +1 -0
- package/dist/units/diagram/ishikawa.js +2 -0
- package/dist/units/diagram/journey.js +5 -0
- package/dist/units/diagram/kanban.js +2 -0
- package/dist/units/diagram/label.js +4 -0
- package/dist/units/diagram/layers.js +1 -0
- package/dist/units/diagram/map-label.js +1 -0
- package/dist/units/diagram/mindmap.js +2 -0
- package/dist/units/diagram/numeric-composition.js +1 -0
- package/dist/units/diagram/numeric-model.js +1 -0
- package/dist/units/diagram/numeric-parse.js +2 -0
- package/dist/units/diagram/numeric-radar.js +3 -0
- package/dist/units/diagram/numeric-sankey.js +2 -0
- package/dist/units/diagram/numeric-xy.js +2 -0
- package/dist/units/diagram/packet.js +2 -0
- package/dist/units/diagram/presentation.js +2 -0
- package/dist/units/diagram/quadrant.js +2 -0
- package/dist/units/diagram/railroad-parse.js +4 -0
- package/dist/units/diagram/railroad.js +1 -0
- package/dist/units/diagram/registry.js +2 -0
- package/dist/units/diagram/relations.js +3 -0
- package/dist/units/diagram/requirement.js +3 -0
- package/dist/units/diagram/sequence.js +2 -0
- package/dist/units/diagram/shapes.js +60 -0
- package/dist/units/diagram/source.js +20 -0
- package/dist/units/diagram/support.js +3 -0
- package/dist/units/diagram/swimlane-mermaid.js +3 -0
- package/dist/units/diagram/swimlane.js +2 -0
- package/dist/units/diagram/time.js +3 -0
- package/dist/units/diagram/timeline.js +2 -0
- package/dist/units/diagram/tree-family-icon.js +1 -0
- package/dist/units/diagram/treeview.js +2 -0
- package/dist/units/diagram/usecase.js +3 -0
- package/dist/units/diagram/venn.js +2 -0
- package/dist/units/diagram/wardley.js +4 -0
- package/dist/units/interaction.js +1 -0
- package/dist/units/runtime.js +1 -0
- package/dist/units.json +712 -0
- package/package.json +30 -4
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { FONT, appearance, error, fallbackMeasure, maxWidthOf, snap, wrapText } from "./support.js";
|
|
3
|
+
import { geometry, has } from "../core/icons.js";
|
|
4
|
+
// src/diagram/blocks.js
|
|
5
|
+
var blocks_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
|
|
6
|
+
var LIMITS = Object.freeze({
|
|
7
|
+
depth: 4,
|
|
8
|
+
edges: 8,
|
|
9
|
+
boxes: 80,
|
|
10
|
+
items: 160,
|
|
11
|
+
labelChars: 24,
|
|
12
|
+
labelLines: 2,
|
|
13
|
+
labelWidth: 312,
|
|
14
|
+
titleLines: 3
|
|
15
|
+
});
|
|
16
|
+
var GAP = 16;
|
|
17
|
+
var ARROW = 56;
|
|
18
|
+
var PAD = 12;
|
|
19
|
+
var ITEM_GAP = 8;
|
|
20
|
+
var ITEM_H = 36;
|
|
21
|
+
var HEADER = 36;
|
|
22
|
+
var MARGIN = 4;
|
|
23
|
+
var SPARSE_CELL = 260;
|
|
24
|
+
var issue = (lang, line, zh, en, fixZh, fixEn) => error(lang, line, zh, en, fixZh, fixEn);
|
|
25
|
+
function parse(source, info) {
|
|
26
|
+
const { lang, options = {}, firstLine = 2 } = info;
|
|
27
|
+
const errors = Object.keys(options).map((key) => issue(lang, firstLine - 1, `blocks 不支持选项「${key}」`, `Unsupported blocks option: ${key}`, "删除这个选项", "Remove this option"));
|
|
28
|
+
const nodes = [], edges = [], stack = [], indents = [];
|
|
29
|
+
for (const [i, raw] of String(source).split("\n").entries()) {
|
|
30
|
+
if (!raw.trim() || raw.trimStart().startsWith("%%")) continue;
|
|
31
|
+
const line = firstLine + i;
|
|
32
|
+
if (/^\s*\t/.test(raw)) {
|
|
33
|
+
errors.push(issue(lang, line, "缩进不能使用 Tab", "Tabs are not allowed in indentation", "把 Tab 换成空格", "Replace tabs with spaces"));
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const indent = raw.match(/^ */)[0].length, text = raw.slice(indent).trim();
|
|
37
|
+
const edge = text.match(/^(.+?)\s*(->|~>)\s*(.+?)(?:\s*[::]\s*(.*))?$/), head = /^(row|col|box)(?:\s|$)/.test(text);
|
|
38
|
+
while (stack.length && indent <= stack.at(-1).indent) stack.pop();
|
|
39
|
+
const parent = stack.at(-1);
|
|
40
|
+
if (edge && !head && (!parent || parent.kind !== "box" || parent.inline)) {
|
|
41
|
+
if (indent) errors.push(issue(lang, line, "关系必须写在顶格", "Relations must start at column 0", "去掉这一行的缩进", "Remove the indentation from this relation"));
|
|
42
|
+
edges.push({
|
|
43
|
+
from: edge[1].trim(),
|
|
44
|
+
to: edge[3].trim(),
|
|
45
|
+
async: edge[2] === "~>",
|
|
46
|
+
label: (edge[4] || "").trim(),
|
|
47
|
+
line
|
|
48
|
+
});
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (!head && parent?.kind === "box" && !parent.inline) {
|
|
52
|
+
parent.items.push(...parseItems(text, line, lang, errors));
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const depth = stack.length;
|
|
56
|
+
indents[depth] ??= indent;
|
|
57
|
+
if (indents[depth] !== indent) {
|
|
58
|
+
errors.push(issue(lang, line, "同级条目的缩进不一致", "Inconsistent indentation at the same level", `把这一行缩进改为 ${indents[depth]} 个空格`, `Indent this line with ${indents[depth]} spaces`));
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (stack.filter((entry) => entry.kind === "box").length + (text.startsWith("box") ? 1 : 0) > LIMITS.depth) {
|
|
62
|
+
errors.push(issue(lang, line, `嵌套超过 ${LIMITS.depth} 层`, `Nesting exceeds ${LIMITS.depth} levels`, "把内层拆成另一张图", "Split the inner level into another diagram"));
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const node = readNode(text, line, indent, lang, errors);
|
|
66
|
+
if (!node) continue;
|
|
67
|
+
(parent ? parent.children : nodes).push(node);
|
|
68
|
+
stack.push(node);
|
|
69
|
+
}
|
|
70
|
+
const boxes = [], items = [];
|
|
71
|
+
walk(nodes, null, (node, parent) => {
|
|
72
|
+
node.parent = parent;
|
|
73
|
+
if (node.kind === "box") boxes.push(node);
|
|
74
|
+
items.push(...node.items);
|
|
75
|
+
if (node.kind !== "box" && node.items.length) errors.push(issue(lang, node.line, "能力项只能写在 box 内", "Capabilities belong inside a box", "把这一行改成 box,或移入上一个 box", "Change this line into a box, or move it into the previous box"));
|
|
76
|
+
if (node.children.length && node.items.length) errors.push(issue(lang, node.line, "box 不能同时写行内能力项和子容器", "A box cannot mix inline capabilities and child containers", "改成缩进的能力项,或去掉子容器", "Use indented capabilities, or remove the child containers"));
|
|
77
|
+
});
|
|
78
|
+
const names = /* @__PURE__ */ new Map();
|
|
79
|
+
for (const box of boxes) {
|
|
80
|
+
if (!box.title) errors.push(issue(lang, box.line, "box 名称不能为空", "Box name cannot be empty", "写上唯一名称", "Add a unique name"));
|
|
81
|
+
const list = names.get(box.title) || [];
|
|
82
|
+
list.push(box);
|
|
83
|
+
names.set(box.title, list);
|
|
84
|
+
}
|
|
85
|
+
for (const [title, list] of names) if (list.length > 1) errors.push(issue(lang, list[1].line, `容器名称「${title}」重复`, `Duplicate container name: ${title}`, "改成唯一名称;关系只能指向唯一容器", "Use a unique name; relations can only target one container"));
|
|
86
|
+
for (const edge of edges) for (const end of ["from", "to"]) if ((names.get(edge[end]) || []).length !== 1) errors.push(issue(lang, edge.line, `关系指向的容器「${edge[end]}」不存在或不唯一`, `Relation target "${edge[end]}" is missing or not unique`, "改成图中唯一的容器名称", "Use the unique name of a container in this diagram"));
|
|
87
|
+
if (edges.length > LIMITS.edges) errors.push(issue(lang, edges[LIMITS.edges].line, `关系最多 ${LIMITS.edges} 条`, `At most ${LIMITS.edges} relations`, "删掉次要关系,或改用泳道图、流程图", "Remove secondary relations, or use a swimlane or flowchart"));
|
|
88
|
+
for (const edge of edges) if ([...edge.label].length > LIMITS.labelChars) errors.push(issue(lang, edge.line, `关系标签「${edge.label}」过长`, `Relation label is too long: ${edge.label}`, `把标签缩到 ${LIMITS.labelChars} 个字符以内,或把说明移到图外`, `Keep the label within ${LIMITS.labelChars} characters, or move the description outside`));
|
|
89
|
+
if (boxes.length > LIMITS.boxes) errors.push(issue(lang, boxes[LIMITS.boxes].line, `容器最多 ${LIMITS.boxes} 个`, `At most ${LIMITS.boxes} containers`, "拆成总图和分图", "Split into an overview and detail diagrams"));
|
|
90
|
+
if (items.length > LIMITS.items) errors.push(issue(lang, items[LIMITS.items].line, `能力项最多 ${LIMITS.items} 个`, `At most ${LIMITS.items} capabilities`, "把说明移到图外,或拆成两张图", "Move descriptions outside, or split into two diagrams"));
|
|
91
|
+
for (const item of items) if (item.icon && !has(item.icon)) errors.push(issue(lang, item.line, `未知图标「${item.icon}」`, `Unknown icon: ${item.icon}`, "换成图标库中的名称,例如 database", "Use a name from the icon library"));
|
|
92
|
+
if (!nodes.length) errors.push(issue(lang, firstLine, "图中没有容器", "Diagram has no containers", "添加 row、col 或 box", "Add a row, col, or box"));
|
|
93
|
+
return {
|
|
94
|
+
model: {
|
|
95
|
+
type: "blocks",
|
|
96
|
+
nodes,
|
|
97
|
+
edges,
|
|
98
|
+
boxes,
|
|
99
|
+
items
|
|
100
|
+
},
|
|
101
|
+
errors
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function readNode(text, line, indent, lang, errors) {
|
|
105
|
+
const head = text.match(/^(row|col|box)(?:\s+(.*))?$/);
|
|
106
|
+
if (!head) {
|
|
107
|
+
errors.push(issue(lang, line, "无法识别这一行", "Unrecognized line", "使用 row、col、box,或「容器 -> 容器: 标签」", "Use row, col, box, or \"container -> container: label\""));
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const kind = head[1], rest = (head[2] || "").trim();
|
|
111
|
+
if (kind !== "box") {
|
|
112
|
+
const weighted = rest.match(/^(\d+(?:\.\d+)?)(?:\s+(.*))?$/), weight = weighted ? Number(weighted[1]) : 1;
|
|
113
|
+
if (weighted && !(weight > 0)) errors.push(issue(lang, line, "宽度权重必须是正数", "Width weight must be a positive number", "写成正数,例如 col 3", "Use a positive number, for example col 3"));
|
|
114
|
+
else if (rest && !weighted) errors.push(issue(lang, line, `${kind} 后面只能写宽度权重`, `Only a width weight may follow ${kind}`, "删掉多余文字,或改成 box", "Remove the extra text, or change this into a box"));
|
|
115
|
+
return {
|
|
116
|
+
kind,
|
|
117
|
+
weight,
|
|
118
|
+
explicit: Boolean(weighted),
|
|
119
|
+
indent,
|
|
120
|
+
line,
|
|
121
|
+
title: "",
|
|
122
|
+
flag: "",
|
|
123
|
+
items: [],
|
|
124
|
+
children: []
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
let body = rest, weight = 1, explicit = false;
|
|
128
|
+
const declared = rest.match(/^(\d+(?:\.\d+)?)\s+(?![::\s])([\s\S]*)$/);
|
|
129
|
+
if (declared) {
|
|
130
|
+
weight = Number(declared[1]);
|
|
131
|
+
explicit = weight > 0;
|
|
132
|
+
body = declared[2];
|
|
133
|
+
if (!explicit) weight = 1;
|
|
134
|
+
}
|
|
135
|
+
const colon = body.search(/[::]/), header = (colon < 0 ? body : body.slice(0, colon)).trim(), inline = colon < 0 ? "" : body.slice(colon + 1);
|
|
136
|
+
const flag = header.match(/\s*\{(db|ext)\}\s*$/)?.[1] || "", title = header.replace(/\s*\{(db|ext)\}\s*$/, "").trim();
|
|
137
|
+
const badFlag = /\{(?!db\}|ext\})/.test(header) || /\}\s*\S/.test(header);
|
|
138
|
+
if (badFlag || !title && body) errors.push(issue(lang, line, "box 写法无效", "Invalid box syntax", "使用「box 名称 [{db}|{ext}] [: 能力项, 能力项]」", "Use \"box Name [{db}|{ext}] [: item, item]\""));
|
|
139
|
+
const items = inline ? parseItems(inline, line, lang, errors) : [];
|
|
140
|
+
return {
|
|
141
|
+
kind,
|
|
142
|
+
weight,
|
|
143
|
+
explicit,
|
|
144
|
+
indent,
|
|
145
|
+
line,
|
|
146
|
+
title,
|
|
147
|
+
flag: badFlag ? "" : flag,
|
|
148
|
+
inline: items.length > 0,
|
|
149
|
+
items,
|
|
150
|
+
children: []
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function parseItems(text, line, lang, errors) {
|
|
154
|
+
return text.split(/[,,]/).map((part) => {
|
|
155
|
+
const raw = part.trim(), icon = raw.match(/^\[icon:([a-z0-9][\w.-]*)\]\s*/i), value = icon ? raw.slice(icon[0].length) : raw;
|
|
156
|
+
const mark = value.match(/^([+~=])\s*/)?.[1] || "", name = value.replace(/^[+~=]\s*/, "").trim();
|
|
157
|
+
if (!name) errors.push(issue(lang, line, "能力项名称不能为空", "Capability labels cannot be empty", "删除空项或补上名称", "Remove the empty item or add a label"));
|
|
158
|
+
if (/\[icon:/.test(raw) && !icon) errors.push(issue(lang, line, "图标写法无效", "Invalid icon syntax", "写成 [icon:database] 名称", "Write [icon:database] name"));
|
|
159
|
+
return {
|
|
160
|
+
label: mark ? `${mark} ${name}` : name,
|
|
161
|
+
name,
|
|
162
|
+
mark,
|
|
163
|
+
tone: {
|
|
164
|
+
"+": "new",
|
|
165
|
+
"~": "info",
|
|
166
|
+
"=": "ok"
|
|
167
|
+
}[mark] || "",
|
|
168
|
+
icon: icon?.[1] || "",
|
|
169
|
+
line
|
|
170
|
+
};
|
|
171
|
+
}).filter((item) => item.name);
|
|
172
|
+
}
|
|
173
|
+
function walk(nodes, parent, visit) {
|
|
174
|
+
for (const node of nodes) {
|
|
175
|
+
visit(node, parent);
|
|
176
|
+
walk(node.children, node, visit);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function layout(model, { width, lang, measure }) {
|
|
180
|
+
const errors = [], root = model.nodes.length === 1 ? model.nodes[0] : {
|
|
181
|
+
kind: "col",
|
|
182
|
+
weight: 1,
|
|
183
|
+
children: model.nodes,
|
|
184
|
+
items: [],
|
|
185
|
+
title: "",
|
|
186
|
+
line: model.nodes[0]?.line || 1
|
|
187
|
+
};
|
|
188
|
+
const boxes = [], items = [];
|
|
189
|
+
const collect = (node) => {
|
|
190
|
+
if (node.kind === "box") boxes.push(node);
|
|
191
|
+
items.push(...node.items || []);
|
|
192
|
+
node.children.forEach(collect);
|
|
193
|
+
};
|
|
194
|
+
collect(root);
|
|
195
|
+
for (const item of items) if ([...item.name].length > LIMITS.labelChars || measure(item.name, 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"));
|
|
196
|
+
for (const box of boxes) if (measure(box.title, FONT.title) > LIMITS.labelWidth * 2) errors.push(issue(lang, box.line, `容器标题「${box.title}」过长`, `Container title is too long: ${box.title}`, "缩短标题,把说明放到图注", "Shorten the title and move the description to the caption"));
|
|
197
|
+
if (errors.length) return {
|
|
198
|
+
width,
|
|
199
|
+
height: 0,
|
|
200
|
+
errors
|
|
201
|
+
};
|
|
202
|
+
const byTitle = new Map(boxes.map((box) => [box.title, box])), resolved = model.edges.map((edge) => ({
|
|
203
|
+
...edge,
|
|
204
|
+
fromBox: byTitle.get(edge.from),
|
|
205
|
+
toBox: byTitle.get(edge.to)
|
|
206
|
+
})), pairs = /* @__PURE__ */ new Map();
|
|
207
|
+
for (const edge of resolved) {
|
|
208
|
+
const key = pairOf(edge.fromBox, edge.toBox);
|
|
209
|
+
if (!pairs.has(key)) pairs.set(key, []);
|
|
210
|
+
pairs.get(key).push(edge);
|
|
211
|
+
}
|
|
212
|
+
const natural = sizeOf(root, measure, pairs), canvas = Math.max(Math.floor(width), natural.w + 8);
|
|
213
|
+
const placed = place(root, MARGIN, MARGIN, canvas - 8, measure, pairs), frames = [], nodes = [];
|
|
214
|
+
flatten(placed, frames, nodes);
|
|
215
|
+
const edges = [];
|
|
216
|
+
for (const edge of resolved) {
|
|
217
|
+
const route = routeEdge(edge, frames, nodes, measure);
|
|
218
|
+
if (!route) errors.push(issue(lang, edge.line, `容器「${edge.from}」与「${edge.to}」之间没有可用箭头通道`, `No clear arrow lane between "${edge.from}" and "${edge.to}"`, "让两个容器只隔一条空间隙,并避开能力项;或改用泳道图、流程图", "Leave one empty gap between the containers and clear of capability items, or use a swimlane or flowchart"));
|
|
219
|
+
else edges.push(route);
|
|
220
|
+
}
|
|
221
|
+
if (errors.length) return {
|
|
222
|
+
width: canvas,
|
|
223
|
+
height: placed.h + 8,
|
|
224
|
+
frames,
|
|
225
|
+
nodes,
|
|
226
|
+
edges,
|
|
227
|
+
errors
|
|
228
|
+
};
|
|
229
|
+
return {
|
|
230
|
+
type: "blocks",
|
|
231
|
+
width: canvas,
|
|
232
|
+
height: snap(placed.h + 8),
|
|
233
|
+
frames,
|
|
234
|
+
nodes,
|
|
235
|
+
edges,
|
|
236
|
+
errors: []
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
var textWidth = (text, font, measure) => measure(String(text || ""), font);
|
|
240
|
+
function itemMin(item, measure) {
|
|
241
|
+
return snap(Math.max(48, textWidth(item.label, FONT.body, measure) + 16 + (item.icon ? 28 : 0)));
|
|
242
|
+
}
|
|
243
|
+
function cellOf(items, measure) {
|
|
244
|
+
return items.length ? snap(Math.max(72, ...items.map((item) => itemMin(item, measure)))) : 72;
|
|
245
|
+
}
|
|
246
|
+
function gridOf(items, cols, cell) {
|
|
247
|
+
const used = Math.max(1, cols), rows = Math.max(1, Math.ceil(items.length / used));
|
|
248
|
+
return {
|
|
249
|
+
cols: used,
|
|
250
|
+
rows,
|
|
251
|
+
cell,
|
|
252
|
+
width: used * cell + ITEM_GAP * (used - 1),
|
|
253
|
+
height: rows * ITEM_H + ITEM_GAP * (rows - 1)
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
function chooseGrid(items, measure, limit) {
|
|
257
|
+
const cell = cellOf(items, measure), total = Math.max(1, items.length), ceiling = Math.min(total, Math.ceil(Math.sqrt(total)));
|
|
258
|
+
let best = null;
|
|
259
|
+
for (let cols = 1; cols <= total; cols += 1) {
|
|
260
|
+
const grid = gridOf(items, cols, cell);
|
|
261
|
+
if (grid.width > limit && cols !== 1) break;
|
|
262
|
+
const score = [
|
|
263
|
+
Math.floor((limit - ITEM_GAP * (cols - 1)) / cols) > SPARSE_CELL ? 1 : 0,
|
|
264
|
+
grid.rows * grid.cols - total,
|
|
265
|
+
Math.abs(grid.cols - grid.rows),
|
|
266
|
+
grid.rows,
|
|
267
|
+
Math.abs(grid.cols - ceiling)
|
|
268
|
+
];
|
|
269
|
+
if (!best || lexLess(score, best.score)) best = {
|
|
270
|
+
grid,
|
|
271
|
+
score
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
return best ? best.grid : gridOf(items, 1, cell);
|
|
275
|
+
}
|
|
276
|
+
function lexLess(a, b) {
|
|
277
|
+
for (let i = 0; i < a.length; i += 1) if (a[i] !== b[i]) return a[i] < b[i];
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
var LABEL_WRAP = 160;
|
|
281
|
+
function labelPlan(text, measure) {
|
|
282
|
+
if (!text) return {
|
|
283
|
+
lines: [],
|
|
284
|
+
w: 0,
|
|
285
|
+
h: 0
|
|
286
|
+
};
|
|
287
|
+
const lines = wrapText(text, LABEL_WRAP, FONT.label, measure).slice(0, LIMITS.labelLines);
|
|
288
|
+
return {
|
|
289
|
+
lines,
|
|
290
|
+
w: snap(maxWidthOf(lines, FONT.label, measure) + 12),
|
|
291
|
+
h: lines.length * 16 + 2
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function sizeOf(node, measure, pairs) {
|
|
295
|
+
if (node.kind !== "box" && !node.children.length && !node.items?.length) return {
|
|
296
|
+
w: 72,
|
|
297
|
+
h: 36
|
|
298
|
+
};
|
|
299
|
+
if (node.kind === "box" && node.items?.length && !node.children.length) {
|
|
300
|
+
const grid = gridOf(node.items, 1, cellOf(node.items, measure)), title = snap(textWidth(node.title, FONT.title, measure) + 24);
|
|
301
|
+
return {
|
|
302
|
+
w: Math.max(grid.width, title) + 24,
|
|
303
|
+
h: HEADER + grid.height + PAD,
|
|
304
|
+
grid
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
if (node.kind === "box" && !node.children.length) {
|
|
308
|
+
const w = Math.max(48, snap(textWidth(node.title, FONT.title, measure)) + 24);
|
|
309
|
+
return {
|
|
310
|
+
w: snap(w),
|
|
311
|
+
h: 48
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
const kids = node.children.map((child) => ({
|
|
315
|
+
child,
|
|
316
|
+
size: sizeOf(child, measure, pairs)
|
|
317
|
+
})), vertical = node.kind === "col", gaps = gapPlan(node, kids.map((item) => item.child), pairs, measure);
|
|
318
|
+
const cross = Math.max(72, ...kids.map((item) => vertical ? item.size.w : item.size.h)), main = kids.reduce((sum, item) => sum + (vertical ? item.size.h : item.size.w), 0) + gaps.reduce((a, b) => a + b, 0);
|
|
319
|
+
let w = vertical ? cross : main, h = vertical ? main : cross;
|
|
320
|
+
if (node.kind === "box") {
|
|
321
|
+
const title = snap(textWidth(node.title, FONT.title, measure) + 24);
|
|
322
|
+
w = Math.max(w, title) + 24;
|
|
323
|
+
h += 48;
|
|
324
|
+
}
|
|
325
|
+
return {
|
|
326
|
+
w: snap(w),
|
|
327
|
+
h: snap(h),
|
|
328
|
+
kids
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function gapPlan(node, children, pairs, measure) {
|
|
332
|
+
const vertical = node.kind === "col", gaps = [];
|
|
333
|
+
for (let i = 0; i < children.length - 1; i += 1) {
|
|
334
|
+
const edges = [];
|
|
335
|
+
for (const a of boxesIn(children[i])) for (const b of boxesIn(children[i + 1])) edges.push(...pairs.get(pairOf(a, b)) || []);
|
|
336
|
+
const need = (edge) => {
|
|
337
|
+
const plan = labelPlan(edge.label, measure);
|
|
338
|
+
return vertical ? plan.h ? plan.h + 48 : 0 : plan.w + 24;
|
|
339
|
+
};
|
|
340
|
+
gaps.push(edges.length ? Math.max(ARROW, ...edges.map(need)) : GAP);
|
|
341
|
+
}
|
|
342
|
+
return gaps;
|
|
343
|
+
}
|
|
344
|
+
function pairOf(a, b) {
|
|
345
|
+
return [a.title, b.title].sort().join("\0");
|
|
346
|
+
}
|
|
347
|
+
function boxesIn(node, out = []) {
|
|
348
|
+
if (node.kind === "box") out.push(node);
|
|
349
|
+
for (const child of node.children || []) boxesIn(child, out);
|
|
350
|
+
return out;
|
|
351
|
+
}
|
|
352
|
+
function place(node, x, y, w, measure, pairs, minH = 0) {
|
|
353
|
+
const box = {
|
|
354
|
+
...node,
|
|
355
|
+
x,
|
|
356
|
+
y,
|
|
357
|
+
w,
|
|
358
|
+
h: 0,
|
|
359
|
+
children: [],
|
|
360
|
+
itemNodes: []
|
|
361
|
+
}, framed = node.kind === "box";
|
|
362
|
+
const innerX = framed ? x + PAD : x, innerY = framed ? y + HEADER : y, innerW = framed ? w - 24 : w, padding = framed ? 48 : 0;
|
|
363
|
+
if (framed && node.items?.length && !node.children.length) {
|
|
364
|
+
const grid = chooseGrid(node.items, measure, innerW), cols = grid.cols;
|
|
365
|
+
const cw = Math.max(grid.cell, Math.floor((innerW - ITEM_GAP * (cols - 1)) / cols));
|
|
366
|
+
const left = innerX + Math.max(0, Math.floor((innerW - cw * cols - ITEM_GAP * (cols - 1)) / 2));
|
|
367
|
+
node.items.forEach((item, index) => box.itemNodes.push({
|
|
368
|
+
...item,
|
|
369
|
+
x: left + index % cols * (cw + ITEM_GAP),
|
|
370
|
+
y: innerY + Math.floor(index / cols) * 44,
|
|
371
|
+
w: cw,
|
|
372
|
+
h: ITEM_H
|
|
373
|
+
}));
|
|
374
|
+
box.h = Math.max(minH, padding + grid.height);
|
|
375
|
+
return box;
|
|
376
|
+
}
|
|
377
|
+
const kids = node.children || [];
|
|
378
|
+
if (!kids.length) {
|
|
379
|
+
box.h = Math.max(minH, padding);
|
|
380
|
+
return box;
|
|
381
|
+
}
|
|
382
|
+
const horizontal = node.kind !== "col", sizes = kids.map((child) => sizeOf(child, measure, pairs)), gaps = gapPlan(node, kids, pairs, measure), mins = sizes.map((size) => horizontal ? size.w : size.h);
|
|
383
|
+
const anyExplicit = kids.some((child) => child.explicit);
|
|
384
|
+
if (horizontal && !anyExplicit) {
|
|
385
|
+
const groups = /* @__PURE__ */ new Map();
|
|
386
|
+
kids.forEach((child, index) => {
|
|
387
|
+
if (child.kind !== "box" || child.children.length || !child.items?.length) return;
|
|
388
|
+
if (!groups.has(child.items.length)) groups.set(child.items.length, []);
|
|
389
|
+
groups.get(child.items.length).push(index);
|
|
390
|
+
});
|
|
391
|
+
for (const indexes of groups.values()) {
|
|
392
|
+
if (indexes.length < 2) continue;
|
|
393
|
+
const widest = Math.max(...indexes.map((index) => mins[index]));
|
|
394
|
+
for (const index of indexes) mins[index] = widest;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
const bare = (child) => child.kind === "box" && !child.children.length && !child.items?.length;
|
|
398
|
+
const weights = kids.map((child, i) => anyExplicit ? child.explicit ? child.weight : 1 : bare(child) ? 0 : Math.max(72, mins[i]));
|
|
399
|
+
if (weights.every((weight) => weight === 0)) weights.fill(1);
|
|
400
|
+
const gapSum = gaps.reduce((a, b) => a + b, 0), share = horizontal ? distribute(mins, weights, Math.max(innerW - gapSum, mins.reduce((a, b) => a + b, 0))) : mins;
|
|
401
|
+
const placed = kids.map((child, index) => ({
|
|
402
|
+
child,
|
|
403
|
+
w: horizontal ? share[index] : innerW,
|
|
404
|
+
x: horizontal ? innerX + share.slice(0, index).reduce((a, b) => a + b, 0) + gaps.slice(0, index).reduce((a, b) => a + b, 0) : innerX
|
|
405
|
+
}));
|
|
406
|
+
if (horizontal) {
|
|
407
|
+
const first = placed.map((item) => place(item.child, item.x, innerY, item.w, measure, pairs));
|
|
408
|
+
const rowH = Math.max(minH - padding, ...first.map((child) => child.h));
|
|
409
|
+
box.children = first.map((child, index) => child.h === rowH ? child : place(kids[index], placed[index].x, innerY, placed[index].w, measure, pairs, rowH));
|
|
410
|
+
box.h = padding + rowH;
|
|
411
|
+
return box;
|
|
412
|
+
}
|
|
413
|
+
const natural = kids.map((child) => place(child, innerX, innerY, innerW, measure, pairs).h);
|
|
414
|
+
const extra = Math.max(0, minH - padding - natural.reduce((a, b) => a + b, 0) - gaps.reduce((a, b) => a + b, 0));
|
|
415
|
+
const each = Math.floor(extra / kids.length);
|
|
416
|
+
let cursor = innerY;
|
|
417
|
+
kids.forEach((child, index) => {
|
|
418
|
+
const grow = each + (index === kids.length - 1 ? extra - each * kids.length : 0);
|
|
419
|
+
const item = place(child, innerX, cursor, innerW, measure, pairs, natural[index] + grow);
|
|
420
|
+
box.children.push(item);
|
|
421
|
+
cursor += item.h + (gaps[index] || 0);
|
|
422
|
+
});
|
|
423
|
+
box.h = Math.max(minH, padding + cursor - innerY);
|
|
424
|
+
return box;
|
|
425
|
+
}
|
|
426
|
+
function distribute(mins, weights, space) {
|
|
427
|
+
const minSum = mins.reduce((a, b) => a + b, 0), extra = Math.max(0, space - minSum), weightSum = weights.reduce((a, b) => a + b, 0);
|
|
428
|
+
const out = mins.map((min, i) => min + Math.floor(extra * weights[i] / weightSum)), takers = weights.map((weight, i) => weight > 0 ? i : -1).filter((i) => i >= 0);
|
|
429
|
+
let rest = space - out.reduce((a, b) => a + b, 0);
|
|
430
|
+
for (let k = 0; rest > 0; k += 1, rest -= 1) out[takers[k % takers.length]] += 1;
|
|
431
|
+
return out;
|
|
432
|
+
}
|
|
433
|
+
function flatten(n, frames, nodes) {
|
|
434
|
+
if (n.kind === "box") frames.push(n);
|
|
435
|
+
for (const item of n.itemNodes || []) nodes.push({
|
|
436
|
+
...item,
|
|
437
|
+
parent: n
|
|
438
|
+
});
|
|
439
|
+
for (const c of n.children || []) flatten(c, frames, nodes);
|
|
440
|
+
}
|
|
441
|
+
function contains(outer, inner) {
|
|
442
|
+
return outer.x <= inner.x + .5 && outer.y <= inner.y + .5 && outer.x + outer.w + .5 >= inner.x + inner.w && outer.y + outer.h + .5 >= inner.y + inner.h;
|
|
443
|
+
}
|
|
444
|
+
function overlap(a, b) {
|
|
445
|
+
return a.w > 0 && a.h > 0 && b.w > 0 && b.h > 0 && a.x < b.x + b.w - .5 && b.x < a.x + a.w - .5 && a.y < b.y + b.h - .5 && b.y < a.y + a.h - .5;
|
|
446
|
+
}
|
|
447
|
+
function routeEdge(edge, frames, nodes = [], measure = fallbackMeasure) {
|
|
448
|
+
const a = frames.find((frame) => frame.title === edge.from), b = frames.find((frame) => frame.title === edge.to);
|
|
449
|
+
if (!a || !b || a === b) return null;
|
|
450
|
+
const candidates = [];
|
|
451
|
+
if (a.x + a.w <= b.x + .5) candidates.push({
|
|
452
|
+
dir: "right",
|
|
453
|
+
gap: {
|
|
454
|
+
x: a.x + a.w,
|
|
455
|
+
y: Math.max(a.y, b.y),
|
|
456
|
+
w: b.x - (a.x + a.w),
|
|
457
|
+
h: Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y)
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
if (b.x + b.w <= a.x + .5) candidates.push({
|
|
461
|
+
dir: "left",
|
|
462
|
+
gap: {
|
|
463
|
+
x: b.x + b.w,
|
|
464
|
+
y: Math.max(a.y, b.y),
|
|
465
|
+
w: a.x - (b.x + b.w),
|
|
466
|
+
h: Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y)
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
if (a.y + a.h <= b.y + .5) candidates.push({
|
|
470
|
+
dir: "down",
|
|
471
|
+
gap: {
|
|
472
|
+
x: Math.max(a.x, b.x),
|
|
473
|
+
y: a.y + a.h,
|
|
474
|
+
w: Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x),
|
|
475
|
+
h: b.y - (a.y + a.h)
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
if (b.y + b.h <= a.y + .5) candidates.push({
|
|
479
|
+
dir: "up",
|
|
480
|
+
gap: {
|
|
481
|
+
x: Math.max(a.x, b.x),
|
|
482
|
+
y: b.y + b.h,
|
|
483
|
+
w: Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x),
|
|
484
|
+
h: a.y - (b.y + b.h)
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
const ranked = candidates.filter((item) => item.gap.w >= 24 && item.gap.h >= 24).map((item) => ({
|
|
488
|
+
...item,
|
|
489
|
+
lane: arrowLane(item, frames, nodes, a, b, edge.label, measure)
|
|
490
|
+
})).filter((item) => item.lane);
|
|
491
|
+
if (!ranked.length) return null;
|
|
492
|
+
ranked.sort((p, q) => laneArea(q.lane) - laneArea(p.lane));
|
|
493
|
+
const { dir, lane } = ranked[0], midX = lane.x + lane.w / 2, midY = lane.y + lane.h / 2, horizontal = dir === "right" || dir === "left";
|
|
494
|
+
const path = horizontal ? dir === "right" ? `M${lane.x + 4} ${midY} H${lane.x + lane.w - 4}` : `M${lane.x + lane.w - 4} ${midY} H${lane.x + 4}` : dir === "down" ? `M${midX} ${lane.y + 4} V${lane.y + lane.h - 4}` : `M${midX} ${lane.y + lane.h - 4} V${lane.y + 4}`;
|
|
495
|
+
const plan = labelPlan(edge.label || "", measure), room = Math.max(24, (horizontal ? ranked[0].gap.w : lane.w) - 8);
|
|
496
|
+
const lines = edge.label && plan.w > room ? wrapText(edge.label, Math.max(24, room - 12), FONT.label, measure).slice(0, LIMITS.labelLines) : plan.lines;
|
|
497
|
+
const labelW = lines.length ? snap(maxWidthOf(lines, FONT.label, measure) + 12) : 0, labelH = lines.length ? Math.max(18, lines.length * 16 + 2) : 0;
|
|
498
|
+
return {
|
|
499
|
+
...edge,
|
|
500
|
+
dir,
|
|
501
|
+
gap: lane,
|
|
502
|
+
path,
|
|
503
|
+
labelLines: lines,
|
|
504
|
+
labelX: midX,
|
|
505
|
+
labelY: midY,
|
|
506
|
+
labelBox: {
|
|
507
|
+
x: midX - labelW / 2,
|
|
508
|
+
y: midY - labelH / 2,
|
|
509
|
+
w: labelW,
|
|
510
|
+
h: labelH
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
function laneArea(box) {
|
|
515
|
+
return box.w * box.h;
|
|
516
|
+
}
|
|
517
|
+
function arrowLane(item, frames, nodes, a, b, label, measure) {
|
|
518
|
+
const { dir, gap } = item, horizontal = dir === "right" || dir === "left", span = horizontal ? gap.h : gap.w, cross = horizontal ? gap.w : gap.h;
|
|
519
|
+
if (cross < 24 || span < 24) return null;
|
|
520
|
+
const thickness = cross - 8, along = Math.min(Math.max(64, measure(label, FONT.label) + 20), Math.max(24, span - 12));
|
|
521
|
+
if (thickness < 20 || along < 20) return null;
|
|
522
|
+
const lane = horizontal ? {
|
|
523
|
+
x: gap.x + (gap.w - thickness) / 2,
|
|
524
|
+
y: gap.y + (gap.h - along) / 2,
|
|
525
|
+
w: thickness,
|
|
526
|
+
h: along
|
|
527
|
+
} : {
|
|
528
|
+
x: gap.x + (gap.w - along) / 2,
|
|
529
|
+
y: gap.y + (gap.h - thickness) / 2,
|
|
530
|
+
w: along,
|
|
531
|
+
h: thickness
|
|
532
|
+
};
|
|
533
|
+
const blockedByFrame = frames.some((frame) => frame !== a && frame !== b && overlap(frame, lane) && !(contains(frame, a) && contains(frame, b)));
|
|
534
|
+
const blockedByItem = nodes.some((node) => overlap(node, lane));
|
|
535
|
+
return blockedByFrame || blockedByItem ? null : lane;
|
|
536
|
+
}
|
|
537
|
+
function scene(g, ctx) {
|
|
538
|
+
const d = ctx.design, appearance$1 = appearance(ctx), frameColor = (line) => ctx.frameTone(ctx.color(`tone${line % 6 + 1}`));
|
|
539
|
+
const children = [ctx.el("defs", {}, [marker("blocks-arrow", ctx, false), marker("blocks-arrow-async", ctx, true)])];
|
|
540
|
+
for (const frame of g.frames) {
|
|
541
|
+
const tone = frameColor(frame.line), fill = ctx.tint(d.bg, tone, .07), stroke = ctx.tint(d.bg, tone, .45);
|
|
542
|
+
const shape = frame.flag === "db" ? cylinder(frame, ctx, fill, stroke) : ctx.rect(frame.x, frame.y, frame.w, frame.h, {
|
|
543
|
+
rx: appearance$1.radius,
|
|
544
|
+
fill,
|
|
545
|
+
stroke,
|
|
546
|
+
"stroke-width": 1,
|
|
547
|
+
...frame.flag === "ext" ? { "stroke-dasharray": "5 4" } : {}
|
|
548
|
+
});
|
|
549
|
+
const titleLines = wrapText(frame.title, Math.max(48, frame.w - 24), FONT.title, (v, f) => ctx.measure(v, f.size, f.weight)).slice(0, LIMITS.titleLines);
|
|
550
|
+
const titleY = frame.itemNodes?.length || frame.children?.length ? frame.y + 20 : frame.y + frame.h / 2;
|
|
551
|
+
const frameChildren = [shape, ...titleLines.map((line, i) => ctx.text(frame.x + frame.w / 2, titleY + (i - (titleLines.length - 1) / 2) * 18, line, {
|
|
552
|
+
anchor: "middle",
|
|
553
|
+
size: FONT.title.size,
|
|
554
|
+
weight: FONT.title.weight,
|
|
555
|
+
fill: ctx.color("fg"),
|
|
556
|
+
"dominant-baseline": "central"
|
|
557
|
+
}))];
|
|
558
|
+
children.push({
|
|
559
|
+
...ctx.el("g", {}, frameChildren),
|
|
560
|
+
meta: {
|
|
561
|
+
kind: "container",
|
|
562
|
+
name: frame.title,
|
|
563
|
+
...frame.flag ? { flag: frame.flag } : {}
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
}
|
|
567
|
+
for (const node of g.nodes) {
|
|
568
|
+
const tone = node.tone ? d[node.tone] : frameColor(node.parent?.line || 1);
|
|
569
|
+
const parts = [ctx.rect(node.x, node.y, node.w, node.h, {
|
|
570
|
+
rx: appearance$1.smallRadius,
|
|
571
|
+
fill: appearance$1.node,
|
|
572
|
+
stroke: (node.tone ? ctx.mix : ctx.tint)(d.bg, tone, .4),
|
|
573
|
+
"stroke-width": 1
|
|
574
|
+
})];
|
|
575
|
+
if (node.icon) {
|
|
576
|
+
const geo = geometry(node.icon);
|
|
577
|
+
for (const item of geo?.elements || []) parts.push(ctx.shape(item.tag, {
|
|
578
|
+
...item.attrs,
|
|
579
|
+
transform: `translate(${node.x + 8} ${node.y + 8}) scale(.8)`,
|
|
580
|
+
fill: "none",
|
|
581
|
+
stroke: ctx.color("muted"),
|
|
582
|
+
"stroke-width": 1.5
|
|
583
|
+
}));
|
|
584
|
+
}
|
|
585
|
+
const textX = node.x + node.w / 2 + (node.icon ? 12 : 0), textY = node.y + node.h / 2;
|
|
586
|
+
if (node.mark && node.tone) {
|
|
587
|
+
const attrs = {
|
|
588
|
+
x: textX - ctx.measure(node.label, FONT.body.size, FONT.body.weight) / 2,
|
|
589
|
+
y: textY,
|
|
590
|
+
"text-anchor": "start",
|
|
591
|
+
"dominant-baseline": "central",
|
|
592
|
+
"font-size": FONT.body.size,
|
|
593
|
+
"font-weight": FONT.body.weight,
|
|
594
|
+
fill: ctx.color("fg")
|
|
595
|
+
};
|
|
596
|
+
parts.push(ctx.el("text", attrs, [{
|
|
597
|
+
...ctx.el("tspan", { fill: tone }),
|
|
598
|
+
text: node.mark
|
|
599
|
+
}, {
|
|
600
|
+
...ctx.el("tspan"),
|
|
601
|
+
text: ` ${node.name}`
|
|
602
|
+
}]));
|
|
603
|
+
} else parts.push(ctx.text(textX, textY, node.label, {
|
|
604
|
+
anchor: "middle",
|
|
605
|
+
size: FONT.body.size,
|
|
606
|
+
fill: ctx.color("fg"),
|
|
607
|
+
"dominant-baseline": "central"
|
|
608
|
+
}));
|
|
609
|
+
children.push({
|
|
610
|
+
...ctx.el("g", {}, parts),
|
|
611
|
+
meta: {
|
|
612
|
+
kind: "capability",
|
|
613
|
+
label: node.name,
|
|
614
|
+
container: node.parent?.title,
|
|
615
|
+
...node.mark ? { mark: node.mark } : {}
|
|
616
|
+
}
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
for (const edge of g.edges) {
|
|
620
|
+
const lineAttrs = {
|
|
621
|
+
d: edge.path,
|
|
622
|
+
fill: "none",
|
|
623
|
+
stroke: ctx.color("muted"),
|
|
624
|
+
"stroke-width": 2,
|
|
625
|
+
"stroke-linecap": "round",
|
|
626
|
+
...edge.async ? { "stroke-dasharray": "5 4" } : {},
|
|
627
|
+
"marker-end": `url(#${edge.async ? "blocks-arrow-async" : "blocks-arrow"})`
|
|
628
|
+
};
|
|
629
|
+
const parts = [ctx.path(edge.path, lineAttrs)];
|
|
630
|
+
if (edge.label) {
|
|
631
|
+
const lines = edge.labelLines || wrapText(edge.label, 160, FONT.label, (v, f) => ctx.measure(v, f.size, f.weight)).slice(0, LIMITS.labelLines);
|
|
632
|
+
const { x, y, w, h } = edge.labelBox;
|
|
633
|
+
parts.push(ctx.rect(x, y, w, h, {
|
|
634
|
+
rx: 4,
|
|
635
|
+
fill: d.bg
|
|
636
|
+
}));
|
|
637
|
+
lines.forEach((line, i) => parts.push(ctx.text(edge.labelX, edge.labelY + (i - (lines.length - 1) / 2) * 16, line, {
|
|
638
|
+
anchor: "middle",
|
|
639
|
+
size: FONT.label.size,
|
|
640
|
+
fill: ctx.color("fg"),
|
|
641
|
+
"dominant-baseline": "central"
|
|
642
|
+
})));
|
|
643
|
+
}
|
|
644
|
+
children.push({
|
|
645
|
+
...ctx.el("g", {}, parts),
|
|
646
|
+
meta: {
|
|
647
|
+
kind: "relation",
|
|
648
|
+
from: edge.from,
|
|
649
|
+
to: edge.to,
|
|
650
|
+
async: edge.async
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
}
|
|
654
|
+
return {
|
|
655
|
+
width: g.width,
|
|
656
|
+
height: g.height,
|
|
657
|
+
children
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
function marker(id, ctx, dashed) {
|
|
661
|
+
return ctx.el("marker", {
|
|
662
|
+
id,
|
|
663
|
+
viewBox: "0 0 10 10",
|
|
664
|
+
refX: 8,
|
|
665
|
+
refY: 5,
|
|
666
|
+
markerWidth: 8,
|
|
667
|
+
markerHeight: 8,
|
|
668
|
+
orient: "auto-start-reverse"
|
|
669
|
+
}, [ctx.path("M1 1 L9 5 L1 9 Z", {
|
|
670
|
+
fill: ctx.color("muted"),
|
|
671
|
+
...dashed ? { "stroke-dasharray": "5 4" } : {}
|
|
672
|
+
})]);
|
|
673
|
+
}
|
|
674
|
+
function cylinder(f, ctx, fill, stroke) {
|
|
675
|
+
const ry = Math.min(7, Math.max(4, f.h / 8)), x = f.x, y = f.y, w = f.w, h = f.h;
|
|
676
|
+
return ctx.path(`M${x} ${y + ry} A${w / 2} ${ry} 0 0 1 ${x + w} ${y + ry} V${y + h - ry} A${w / 2} ${ry} 0 0 1 ${x} ${y + h - ry} Z M${x} ${y + ry} A${w / 2} ${ry} 0 0 0 ${x + w} ${y + ry}`, {
|
|
677
|
+
fill,
|
|
678
|
+
stroke,
|
|
679
|
+
"stroke-width": 1.5
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
var implementations = { blocks: {
|
|
683
|
+
parse,
|
|
684
|
+
layout,
|
|
685
|
+
scene
|
|
686
|
+
} };
|
|
687
|
+
export { blocks_exports, implementations };
|