@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,988 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { resolveIconRef } from "../core/icons.js";
|
|
3
|
+
import { normalizeLabel } from "./label.js";
|
|
4
|
+
import { presentation } from "./presentation.js";
|
|
5
|
+
import { FONT, appearance, error, maxWidthOf, textBlock, warning, wrapText } from "./support.js";
|
|
6
|
+
// src/diagram/block.js
|
|
7
|
+
var block_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
|
|
8
|
+
var GAP = 20;
|
|
9
|
+
var PAD = 12;
|
|
10
|
+
var MARGIN = 24;
|
|
11
|
+
var ICON = 16;
|
|
12
|
+
var ICON_GAP = 6;
|
|
13
|
+
var NODE_LABEL_MAX = 240;
|
|
14
|
+
var shapes = [
|
|
15
|
+
[
|
|
16
|
+
"(((",
|
|
17
|
+
")))",
|
|
18
|
+
"doublecircle"
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
"((",
|
|
22
|
+
"))",
|
|
23
|
+
"circle"
|
|
24
|
+
],
|
|
25
|
+
[
|
|
26
|
+
"([",
|
|
27
|
+
"])",
|
|
28
|
+
"stadium"
|
|
29
|
+
],
|
|
30
|
+
[
|
|
31
|
+
"(",
|
|
32
|
+
")",
|
|
33
|
+
"round"
|
|
34
|
+
],
|
|
35
|
+
[
|
|
36
|
+
"[[",
|
|
37
|
+
"]]",
|
|
38
|
+
"subroutine"
|
|
39
|
+
],
|
|
40
|
+
[
|
|
41
|
+
"[(",
|
|
42
|
+
")]",
|
|
43
|
+
"cylinder"
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
"[/",
|
|
47
|
+
"/]",
|
|
48
|
+
"leanRight"
|
|
49
|
+
],
|
|
50
|
+
[
|
|
51
|
+
"[\\",
|
|
52
|
+
"\\]",
|
|
53
|
+
"leanLeft"
|
|
54
|
+
],
|
|
55
|
+
[
|
|
56
|
+
"[/",
|
|
57
|
+
"\\]",
|
|
58
|
+
"trapezoid"
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
"[\\",
|
|
62
|
+
"/]",
|
|
63
|
+
"inverseTrapezoid"
|
|
64
|
+
],
|
|
65
|
+
[
|
|
66
|
+
"[",
|
|
67
|
+
"]",
|
|
68
|
+
"square"
|
|
69
|
+
],
|
|
70
|
+
[
|
|
71
|
+
"{{",
|
|
72
|
+
"}}",
|
|
73
|
+
"hexagon"
|
|
74
|
+
],
|
|
75
|
+
[
|
|
76
|
+
"{",
|
|
77
|
+
"}",
|
|
78
|
+
"diamond"
|
|
79
|
+
],
|
|
80
|
+
[
|
|
81
|
+
">",
|
|
82
|
+
"]",
|
|
83
|
+
"notch"
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
"<[",
|
|
87
|
+
"]>",
|
|
88
|
+
"arrow"
|
|
89
|
+
]
|
|
90
|
+
];
|
|
91
|
+
var link = /^(?:[xo<]?(?:--+[-xo>]|==+[=xo>]|-?\.+-[xo>]?)|~{3,}|==|<--)/;
|
|
92
|
+
var arrowTypes = {
|
|
93
|
+
">": "point",
|
|
94
|
+
"<": "point",
|
|
95
|
+
o: "circle",
|
|
96
|
+
x: "cross"
|
|
97
|
+
};
|
|
98
|
+
var directions = /* @__PURE__ */ new Set([
|
|
99
|
+
"left",
|
|
100
|
+
"right",
|
|
101
|
+
"up",
|
|
102
|
+
"down",
|
|
103
|
+
"x",
|
|
104
|
+
"y"
|
|
105
|
+
]);
|
|
106
|
+
function parse(body, { lang, firstLine = 2, params = "", header = "block" }) {
|
|
107
|
+
const root = { children: [] }, stack = [root], nodes = /* @__PURE__ */ new Map(), edges = [], errors = [];
|
|
108
|
+
const look = presentation({ lang }), notices = /* @__PURE__ */ new Map(), styleLines = /* @__PURE__ */ new Map();
|
|
109
|
+
let defaultStyleLine;
|
|
110
|
+
const note = (issue) => {
|
|
111
|
+
if (!notices.has(issue.message)) notices.set(issue.message, issue);
|
|
112
|
+
};
|
|
113
|
+
const ignoreStyle = (at, zh, en) => note(warning("IGNORED_STYLE", lang, at, zh, en, "使用支持的状态样式", "Use supported status styling"));
|
|
114
|
+
const resolveIcon = (ref, at) => {
|
|
115
|
+
const icon = resolveIconRef(ref);
|
|
116
|
+
if (!icon) note(warning("IGNORED_ASSET", lang, at, `未知图标 ${ref}`, `Unknown icon ${ref}`, "使用内置图标", "Use a supported icon"));
|
|
117
|
+
return icon;
|
|
118
|
+
};
|
|
119
|
+
const normalize = (raw, at) => {
|
|
120
|
+
let icon;
|
|
121
|
+
const source = raw.replace(/(?<![\w:/.-])(?:logos|mdi|tabler|simple-icons):[a-z\d-]+/gi, (ref) => {
|
|
122
|
+
const resolved = resolveIcon(ref, at);
|
|
123
|
+
icon ??= resolved;
|
|
124
|
+
return "";
|
|
125
|
+
});
|
|
126
|
+
const label = normalizeLabel(source, {
|
|
127
|
+
lang,
|
|
128
|
+
line: at
|
|
129
|
+
});
|
|
130
|
+
label.diagnostics.forEach(note);
|
|
131
|
+
return {
|
|
132
|
+
label: label.text,
|
|
133
|
+
icon: label.icon ?? icon
|
|
134
|
+
};
|
|
135
|
+
};
|
|
136
|
+
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));
|
|
138
|
+
if (body.length + params.length > 2e5) {
|
|
139
|
+
bad("块图源码过长", "Block source is too long");
|
|
140
|
+
return {
|
|
141
|
+
model: null,
|
|
142
|
+
errors
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
for (const [i, raw] of [params, ...body.split("\n")].entries()) {
|
|
146
|
+
line = firstLine + i - 1;
|
|
147
|
+
let rest = raw;
|
|
148
|
+
const take = (n) => {
|
|
149
|
+
rest = rest.slice(n);
|
|
150
|
+
};
|
|
151
|
+
const ws = () => take(rest.match(/^\s*/)[0].length);
|
|
152
|
+
const readSpan = () => {
|
|
153
|
+
const m = /^:(\d+)/.exec(rest);
|
|
154
|
+
if (m) take(m[0].length);
|
|
155
|
+
return m ? Number(m[1]) : 1;
|
|
156
|
+
};
|
|
157
|
+
const readNode = () => {
|
|
158
|
+
const m = /^(?![-.])[^()[\]{}<>:=;"\s~]+?(?=$|[()[\]{}<>:=;"\s~]|--|-?\.+-|%%)/.exec(rest);
|
|
159
|
+
if (!m) return null;
|
|
160
|
+
const id = m[0];
|
|
161
|
+
take(id.length);
|
|
162
|
+
const node = {
|
|
163
|
+
id,
|
|
164
|
+
line
|
|
165
|
+
};
|
|
166
|
+
const open = shapes.find(([start]) => rest.startsWith(start))?.[0];
|
|
167
|
+
if (open) {
|
|
168
|
+
take(open.length);
|
|
169
|
+
const quoted = /^\s*"([^"]*)"\s*/.exec(rest);
|
|
170
|
+
let shape;
|
|
171
|
+
if (quoted) {
|
|
172
|
+
take(quoted[0].length);
|
|
173
|
+
shape = shapes.find(([start, end]) => start === open && rest.startsWith(end));
|
|
174
|
+
if (!shape) {
|
|
175
|
+
const close = /^(?:\)\)\)|\)\)|\)\]|\]\)|\]\]|\}\}|[)\]}])/.exec(rest);
|
|
176
|
+
if (close) shape = [
|
|
177
|
+
open,
|
|
178
|
+
close[0],
|
|
179
|
+
"square"
|
|
180
|
+
];
|
|
181
|
+
}
|
|
182
|
+
node.label = quoted[1];
|
|
183
|
+
} else {
|
|
184
|
+
shape = shapes.find(([start, end]) => start === open && rest.includes(end));
|
|
185
|
+
if (shape) {
|
|
186
|
+
const end = rest.indexOf(shape[1]);
|
|
187
|
+
node.label = rest.slice(0, end).trim();
|
|
188
|
+
take(end);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (!shape) return null;
|
|
192
|
+
take(shape[1].length);
|
|
193
|
+
node.shape = shape[2];
|
|
194
|
+
if (node.shape === "arrow") {
|
|
195
|
+
const dirs = /^\s*\(([^)]*)\)/.exec(rest);
|
|
196
|
+
node.directions = dirs?.[1].split(/[\s,]+/).filter(Boolean);
|
|
197
|
+
if (!node.directions?.length || node.directions.some((d) => !directions.has(d))) return null;
|
|
198
|
+
take(dirs[0].length);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const suffix = () => {
|
|
202
|
+
const icon = /^::icon\(\s*([^()]+?)\s*\)/i.exec(rest);
|
|
203
|
+
const classes = /^:::([\w-]+(?:,[\w-]+)*)/.exec(rest);
|
|
204
|
+
if (icon) {
|
|
205
|
+
node.iconRef = icon[1];
|
|
206
|
+
take(icon[0].length);
|
|
207
|
+
} else if (classes) {
|
|
208
|
+
styleLines.set(id, styleLines.get(id) ?? line);
|
|
209
|
+
look.addClasses(id, classes[1].split(","), line);
|
|
210
|
+
take(classes[0].length);
|
|
211
|
+
}
|
|
212
|
+
return icon || classes;
|
|
213
|
+
};
|
|
214
|
+
while (suffix());
|
|
215
|
+
node.span = readSpan();
|
|
216
|
+
while (suffix());
|
|
217
|
+
return node.span > 0 && node.span <= 2e3 ? node : null;
|
|
218
|
+
};
|
|
219
|
+
const insert = (node, group) => {
|
|
220
|
+
const old = nodes.get(node.id);
|
|
221
|
+
if (old) {
|
|
222
|
+
if (node.label !== void 0 && !old.children) Object.assign(old, {
|
|
223
|
+
label: node.label,
|
|
224
|
+
labelLine: node.line,
|
|
225
|
+
shape: node.shape,
|
|
226
|
+
directions: node.directions
|
|
227
|
+
});
|
|
228
|
+
if (node.iconRef) Object.assign(old, {
|
|
229
|
+
iconRef: node.iconRef,
|
|
230
|
+
iconLine: node.line
|
|
231
|
+
});
|
|
232
|
+
} else {
|
|
233
|
+
nodes.set(node.id, node);
|
|
234
|
+
group.children.push(node);
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
while (rest) {
|
|
238
|
+
ws();
|
|
239
|
+
column = raw.length - rest.length + 1 + (i === 0 ? Math.max(0, header.indexOf(params)) : 0);
|
|
240
|
+
if (!rest || /^%%(?!\{)/.test(rest)) break;
|
|
241
|
+
if (rest[0] === ";") {
|
|
242
|
+
take(1);
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const styled = /^(style|class)\s+([^\s,]+(?:\s*,\s*[^\s,]+)*)\s+(.+)$/i.exec(rest);
|
|
246
|
+
if (styled?.[1].toLowerCase() === "style" && /^[\w-]+#[\da-f]+;?\s*$/i.test(styled[3])) {
|
|
247
|
+
ignoreStyle(line, "已忽略缺少冒号的块图样式", "Ignored a block style missing its colon");
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
if (styled && /^style$/i.test(styled[1])) {
|
|
251
|
+
for (const id of styled[2].split(/\s*,\s*/)) {
|
|
252
|
+
look.accept(`style ${id} ${styled[3]}`, line);
|
|
253
|
+
styleLines.set(id, styleLines.get(id) ?? line);
|
|
254
|
+
}
|
|
255
|
+
break;
|
|
256
|
+
}
|
|
257
|
+
if (look.accept(rest, line)) {
|
|
258
|
+
if (styled) for (const id of styled[2].split(/\s*,\s*/)) styleLines.set(id, styleLines.get(id) ?? line);
|
|
259
|
+
if (/^classDef\s+default\b/i.test(rest)) defaultStyleLine ??= line;
|
|
260
|
+
break;
|
|
261
|
+
}
|
|
262
|
+
if (/^interpolate\s/i.test(rest)) {
|
|
263
|
+
ignoreStyle(line, "已忽略插值样式;连线使用网格走线", "Ignored interpolation; edges follow grid routes");
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
const group = stack.at(-1);
|
|
267
|
+
const cols = /^columns\s+(auto|\d+)(?=\s|;|$)/.exec(rest);
|
|
268
|
+
const space = /^space(?=[:;\s]|$)/.exec(rest);
|
|
269
|
+
const begin = /^block(?:-beta)?(?=[:;\s]|$)/.exec(rest);
|
|
270
|
+
const end = /^end(?=[;\s]|$)/.exec(rest);
|
|
271
|
+
if (cols) {
|
|
272
|
+
take(cols[0].length);
|
|
273
|
+
const count = cols[1] === "auto" ? 0 : Number(cols[1]);
|
|
274
|
+
if (count > 2e3 || count === 0 && cols[1] !== "auto") {
|
|
275
|
+
bad("无效的列数", "Invalid column count");
|
|
276
|
+
break;
|
|
277
|
+
}
|
|
278
|
+
group.columns = count;
|
|
279
|
+
} else if (space) {
|
|
280
|
+
take(space[0].length);
|
|
281
|
+
const span = readSpan();
|
|
282
|
+
if (span > 2e3) {
|
|
283
|
+
bad("占位过宽", "Space span is too large");
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
reserved += span;
|
|
287
|
+
if (reserved > 2e4) {
|
|
288
|
+
bad("占位单元格过多", "Too many space cells");
|
|
289
|
+
return {
|
|
290
|
+
model: null,
|
|
291
|
+
errors
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
group.children.push({
|
|
295
|
+
space: true,
|
|
296
|
+
span
|
|
297
|
+
});
|
|
298
|
+
} else if (begin) {
|
|
299
|
+
take(begin[0].length);
|
|
300
|
+
const named = rest[0] === ":" && !/^:\d/.test(rest);
|
|
301
|
+
if (named) take(1);
|
|
302
|
+
const node = named ? readNode() : {
|
|
303
|
+
id: `:block${++anonymous}`,
|
|
304
|
+
line,
|
|
305
|
+
span: readSpan()
|
|
306
|
+
};
|
|
307
|
+
if (!node || !node.span || node.span > 2e3 || stack.length > 32 || nodes.has(node.id)) {
|
|
308
|
+
bad("无效、重复或嵌套过深的复合块", "Invalid, duplicate or excessively nested composite block");
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
node.children = [];
|
|
312
|
+
insert(node, group);
|
|
313
|
+
stack.push(node);
|
|
314
|
+
} else if (end) {
|
|
315
|
+
take(end[0].length);
|
|
316
|
+
if (stack.length === 1) {
|
|
317
|
+
bad("end 没有对应的 block", "No matching block for end");
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
stack.pop();
|
|
321
|
+
} else {
|
|
322
|
+
let from = readNode();
|
|
323
|
+
if (!from) {
|
|
324
|
+
bad("无法识别块图语句", "Unrecognized block statement");
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
insert(from, group);
|
|
328
|
+
ws();
|
|
329
|
+
while (rest) {
|
|
330
|
+
const labeled = /^([xo<]?(?:--|==|-\.))\s*"([^"]*)"\s*/.exec(rest);
|
|
331
|
+
const op = link.exec(labeled ? rest.slice(labeled[0].length) : rest);
|
|
332
|
+
if (!op) break;
|
|
333
|
+
take((labeled?.[0].length ?? 0) + op[0].length);
|
|
334
|
+
ws();
|
|
335
|
+
const to = readNode();
|
|
336
|
+
if (!to) {
|
|
337
|
+
bad("连线缺少终点", "Missing edge endpoint");
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
insert(to, group);
|
|
341
|
+
const symbol = op[0];
|
|
342
|
+
const legacyLabel = !labeled && /^\s+:\s+(.+)$/.exec(rest);
|
|
343
|
+
if (legacyLabel) take(legacyLabel[0].length);
|
|
344
|
+
edges.push({
|
|
345
|
+
from: from.id,
|
|
346
|
+
to: to.id,
|
|
347
|
+
line,
|
|
348
|
+
label: labeled?.[2] ?? legacyLabel?.[1] ?? "",
|
|
349
|
+
arrowStart: arrowTypes[symbol[0]],
|
|
350
|
+
arrowEnd: arrowTypes[symbol.at(-1)],
|
|
351
|
+
thickness: symbol.includes("==") ? 2.5 : 1.5,
|
|
352
|
+
pattern: symbol.includes(".-") ? "dotted" : "solid"
|
|
353
|
+
});
|
|
354
|
+
if (nodes.size > 2e3 || edges.length > 500) {
|
|
355
|
+
bad("块或连线过多", "Too many blocks or edges");
|
|
356
|
+
return {
|
|
357
|
+
model: null,
|
|
358
|
+
errors
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
from = to;
|
|
362
|
+
ws();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
if (nodes.size > 2e3) {
|
|
366
|
+
bad("块过多", "Too many blocks");
|
|
367
|
+
return {
|
|
368
|
+
model: null,
|
|
369
|
+
errors
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (stack.length > 1) {
|
|
375
|
+
line = stack.at(-1).line;
|
|
376
|
+
column = 1;
|
|
377
|
+
bad("复合块缺少 end", "Composite block is missing end");
|
|
378
|
+
}
|
|
379
|
+
if (!nodes.size && !root.children.length && !errors.length) {
|
|
380
|
+
line = firstLine - 1;
|
|
381
|
+
column = 1;
|
|
382
|
+
bad("块图为空", "Empty block diagram");
|
|
383
|
+
}
|
|
384
|
+
errors.push(...look.diagnostics());
|
|
385
|
+
for (const node of nodes.values()) {
|
|
386
|
+
if (node.label !== void 0) Object.assign(node, normalize(node.label, node.labelLine ?? node.line));
|
|
387
|
+
if (node.iconRef) node.icon = resolveIcon(node.iconRef, node.iconLine ?? node.line) ?? node.icon;
|
|
388
|
+
const status = look.statusOf(node.id);
|
|
389
|
+
if (status && node.shape === "arrow" && !node.children) ignoreStyle(styleLines.get(node.id) ?? defaultStyleLine ?? node.line, "块箭头不应用自定义样式", "Block arrows do not apply custom styling");
|
|
390
|
+
else node.status = status;
|
|
391
|
+
node.link = look.linkOf(node.id);
|
|
392
|
+
}
|
|
393
|
+
edges.forEach((edge, index) => Object.assign(edge, normalize(edge.label, edge.line), { status: look.edgeStatusOf(index) }));
|
|
394
|
+
return {
|
|
395
|
+
model: {
|
|
396
|
+
root,
|
|
397
|
+
edges,
|
|
398
|
+
nodeCount: nodes.size
|
|
399
|
+
},
|
|
400
|
+
errors: [...errors, ...notices.values()]
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
function grid(group, measure, pad) {
|
|
404
|
+
const rows = [], columns = group.columns || Infinity;
|
|
405
|
+
let col = 0, occupied = 0, unit = 72, row;
|
|
406
|
+
const currentRow = () => row ??= {
|
|
407
|
+
h: 36,
|
|
408
|
+
cells: []
|
|
409
|
+
};
|
|
410
|
+
const advance = (span) => {
|
|
411
|
+
col += span;
|
|
412
|
+
occupied = Math.max(occupied, col);
|
|
413
|
+
if (col >= columns) {
|
|
414
|
+
rows.push(row);
|
|
415
|
+
row = void 0;
|
|
416
|
+
col = 0;
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
for (const node of group.children) {
|
|
420
|
+
if (node.space) {
|
|
421
|
+
for (let left = node.span; left > 0;) {
|
|
422
|
+
currentRow();
|
|
423
|
+
const step = Math.min(left, columns - col);
|
|
424
|
+
advance(step);
|
|
425
|
+
left -= step;
|
|
426
|
+
}
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
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;
|
|
452
|
+
if (inner) {
|
|
453
|
+
w = Math.max(w, inner.w + 2 * pad);
|
|
454
|
+
h = inner.h + 2 * pad + title;
|
|
455
|
+
}
|
|
456
|
+
const cell = {
|
|
457
|
+
...node,
|
|
458
|
+
col,
|
|
459
|
+
inner,
|
|
460
|
+
lines,
|
|
461
|
+
font,
|
|
462
|
+
title,
|
|
463
|
+
textH,
|
|
464
|
+
...compact ? { diameter: w } : {}
|
|
465
|
+
};
|
|
466
|
+
currentRow().cells.push(cell);
|
|
467
|
+
row.h = Math.max(row.h, h);
|
|
468
|
+
unit = Math.max(unit, Math.ceil((w - (node.span - 1) * GAP) / node.span));
|
|
469
|
+
occupied = Math.max(occupied, col + node.span);
|
|
470
|
+
advance(Math.min(node.span, columns - col));
|
|
471
|
+
}
|
|
472
|
+
if (row) rows.push(row);
|
|
473
|
+
const h = rows.reduce((sum, item) => sum + item.h + GAP, -20);
|
|
474
|
+
return {
|
|
475
|
+
rows,
|
|
476
|
+
unit,
|
|
477
|
+
columns: occupied,
|
|
478
|
+
w: Math.max(0, occupied * (unit + GAP) - GAP),
|
|
479
|
+
h: Math.max(0, h)
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function place(grid, x, y, width, nodes, frames, pad) {
|
|
483
|
+
if (!grid.columns) return;
|
|
484
|
+
const pitch = (width + GAP) / grid.columns;
|
|
485
|
+
for (const row of grid.rows) {
|
|
486
|
+
for (const cell of row.cells) {
|
|
487
|
+
const box = {
|
|
488
|
+
...cell,
|
|
489
|
+
x: x + cell.col * pitch,
|
|
490
|
+
y,
|
|
491
|
+
w: cell.span * pitch - GAP,
|
|
492
|
+
h: row.h
|
|
493
|
+
};
|
|
494
|
+
if (cell.diameter) {
|
|
495
|
+
box.x += (box.w - cell.diameter) / 2;
|
|
496
|
+
box.y += (box.h - cell.diameter) / 2;
|
|
497
|
+
box.w = box.h = cell.diameter;
|
|
498
|
+
} else if (!cell.inner && cell.shape === "arrow" && !cell.textH) {
|
|
499
|
+
box.x += (box.w - 28) / 2;
|
|
500
|
+
box.y += (box.h - 28) / 2;
|
|
501
|
+
box.w = box.h = 28;
|
|
502
|
+
}
|
|
503
|
+
nodes.set(box.id, box);
|
|
504
|
+
if (box.inner) {
|
|
505
|
+
frames.push(box);
|
|
506
|
+
place(box.inner, box.x + pad, y + pad + box.title, box.w - 2 * pad, nodes, frames, pad);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
y += row.h + GAP;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
var rectPoints = [
|
|
513
|
+
[0, 0],
|
|
514
|
+
[1, 0],
|
|
515
|
+
[1, 1],
|
|
516
|
+
[0, 1]
|
|
517
|
+
];
|
|
518
|
+
var polygons = {
|
|
519
|
+
diamond: [
|
|
520
|
+
[.5, 0],
|
|
521
|
+
[1, .5],
|
|
522
|
+
[.5, 1],
|
|
523
|
+
[0, .5]
|
|
524
|
+
],
|
|
525
|
+
hexagon: [
|
|
526
|
+
[.13, 0],
|
|
527
|
+
[.87, 0],
|
|
528
|
+
[1, .5],
|
|
529
|
+
[.87, 1],
|
|
530
|
+
[.13, 1],
|
|
531
|
+
[0, .5]
|
|
532
|
+
],
|
|
533
|
+
notch: [
|
|
534
|
+
[0, 0],
|
|
535
|
+
[1, 0],
|
|
536
|
+
[1, 1],
|
|
537
|
+
[0, 1],
|
|
538
|
+
[.13, .5]
|
|
539
|
+
],
|
|
540
|
+
leanRight: [
|
|
541
|
+
[.13, 0],
|
|
542
|
+
[1, 0],
|
|
543
|
+
[.87, 1],
|
|
544
|
+
[0, 1]
|
|
545
|
+
],
|
|
546
|
+
leanLeft: [
|
|
547
|
+
[0, 0],
|
|
548
|
+
[.87, 0],
|
|
549
|
+
[1, 1],
|
|
550
|
+
[.13, 1]
|
|
551
|
+
],
|
|
552
|
+
trapezoid: [
|
|
553
|
+
[.13, 0],
|
|
554
|
+
[.87, 0],
|
|
555
|
+
[1, 1],
|
|
556
|
+
[0, 1]
|
|
557
|
+
],
|
|
558
|
+
inverseTrapezoid: [
|
|
559
|
+
[0, 0],
|
|
560
|
+
[1, 0],
|
|
561
|
+
[.87, 1],
|
|
562
|
+
[.13, 1]
|
|
563
|
+
]
|
|
564
|
+
};
|
|
565
|
+
function outline(box) {
|
|
566
|
+
let points = polygons[box.shape];
|
|
567
|
+
if (box.shape === "arrow") {
|
|
568
|
+
const dirs = box.directions;
|
|
569
|
+
const active = [
|
|
570
|
+
"up",
|
|
571
|
+
"right",
|
|
572
|
+
"down",
|
|
573
|
+
"left"
|
|
574
|
+
].map((dir, i) => dirs.includes(dir) || dirs.includes(i % 2 ? "x" : "y"));
|
|
575
|
+
const dx = Math.min(.22, 16 / box.w), dy = Math.min(.24, 14 / box.h);
|
|
576
|
+
const [top, right, bottom, left] = active.map((on, i) => on ? [dy, dx][i % 2] : 0);
|
|
577
|
+
const corners = [
|
|
578
|
+
[left, top],
|
|
579
|
+
[1 - right, top],
|
|
580
|
+
[1 - right, 1 - bottom],
|
|
581
|
+
[left, 1 - bottom]
|
|
582
|
+
];
|
|
583
|
+
const heads = [
|
|
584
|
+
[
|
|
585
|
+
[.24, top],
|
|
586
|
+
[.5, 0],
|
|
587
|
+
[.76, top]
|
|
588
|
+
],
|
|
589
|
+
[
|
|
590
|
+
[1 - right, .24],
|
|
591
|
+
[1, .5],
|
|
592
|
+
[1 - right, .76]
|
|
593
|
+
],
|
|
594
|
+
[
|
|
595
|
+
[.76, 1 - bottom],
|
|
596
|
+
[.5, 1],
|
|
597
|
+
[.24, 1 - bottom]
|
|
598
|
+
],
|
|
599
|
+
[
|
|
600
|
+
[left, .76],
|
|
601
|
+
[0, .5],
|
|
602
|
+
[left, .24]
|
|
603
|
+
]
|
|
604
|
+
];
|
|
605
|
+
const vertical = active[0] || active[2], horizontal = active[1] || active[3];
|
|
606
|
+
if (vertical && !horizontal) {
|
|
607
|
+
const inset = Math.min(.28, 12 / box.w);
|
|
608
|
+
corners.forEach((p, i) => {
|
|
609
|
+
p[0] = i === 0 || i === 3 ? inset : 1 - inset;
|
|
610
|
+
});
|
|
611
|
+
heads[0][0][0] = 0;
|
|
612
|
+
heads[0][2][0] = 1;
|
|
613
|
+
heads[2][0][0] = 1;
|
|
614
|
+
heads[2][2][0] = 0;
|
|
615
|
+
} else if (horizontal && !vertical) {
|
|
616
|
+
const inset = Math.min(.24, 12 / box.h);
|
|
617
|
+
corners.forEach((p, i) => {
|
|
618
|
+
p[1] = i < 2 ? inset : 1 - inset;
|
|
619
|
+
});
|
|
620
|
+
heads[1][0][1] = 0;
|
|
621
|
+
heads[1][2][1] = 1;
|
|
622
|
+
heads[3][0][1] = 1;
|
|
623
|
+
heads[3][2][1] = 0;
|
|
624
|
+
}
|
|
625
|
+
points = corners.flatMap((point, i) => {
|
|
626
|
+
if (!active[i]) return [point];
|
|
627
|
+
const next = corners[(i + 1) % 4], head = heads[i], axis = i % 2;
|
|
628
|
+
if (horizontal && vertical) return [point, ...head];
|
|
629
|
+
const shoulder = (p, corner) => p.map((v, k) => k === axis ? v : corner[k]);
|
|
630
|
+
return [
|
|
631
|
+
point,
|
|
632
|
+
shoulder(head[0], point),
|
|
633
|
+
...head,
|
|
634
|
+
shoulder(head[2], next)
|
|
635
|
+
];
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
return points?.map(([x, y]) => [box.x + x * box.w, box.y + y * box.h]);
|
|
639
|
+
}
|
|
640
|
+
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
|
+
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;
|
|
677
|
+
}
|
|
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
|
+
];
|
|
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
|
+
}
|
|
757
|
+
function layout(model, { width, lang, measure }, info) {
|
|
758
|
+
const padding = info.config?.block?.padding;
|
|
759
|
+
const validPadding = Number.isFinite(padding) && padding >= 0 && padding <= 2e3;
|
|
760
|
+
const pad = validPadding ? padding : PAD;
|
|
761
|
+
const plan = grid(model.root, measure, pad), nodes = /* @__PURE__ */ new Map(), frames = [], edges = [], plates = [], errors = [];
|
|
762
|
+
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"));
|
|
775
|
+
}
|
|
776
|
+
const naturalWidth = plan.w + 48;
|
|
777
|
+
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
|
+
return {
|
|
779
|
+
width: naturalWidth,
|
|
780
|
+
height: plan.h + 48,
|
|
781
|
+
pad,
|
|
782
|
+
nodes: boxes.filter((box) => !box.inner),
|
|
783
|
+
frames,
|
|
784
|
+
edges,
|
|
785
|
+
errors
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
function drawShape(ctx, node, style) {
|
|
789
|
+
const { x, y, w, h, shape } = node, [cx, cy] = center(node);
|
|
790
|
+
if (roundShape(node)) {
|
|
791
|
+
const oval = (rx, ry, attrs) => ctx.el("ellipse", {
|
|
792
|
+
cx,
|
|
793
|
+
cy,
|
|
794
|
+
rx,
|
|
795
|
+
ry,
|
|
796
|
+
...attrs
|
|
797
|
+
});
|
|
798
|
+
return [oval(w / 2, h / 2, style), ...shape === "doublecircle" ? [oval(w / 2 - 4, h / 2 - 4, {
|
|
799
|
+
...style,
|
|
800
|
+
fill: "none"
|
|
801
|
+
})] : []];
|
|
802
|
+
}
|
|
803
|
+
const points = outline(node);
|
|
804
|
+
if (points) return [ctx.el("polygon", {
|
|
805
|
+
points: points.map((p) => p.join(",")).join(" "),
|
|
806
|
+
...style
|
|
807
|
+
})];
|
|
808
|
+
if (shape === "cylinder") {
|
|
809
|
+
const ry = 7;
|
|
810
|
+
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`, style), ctx.path(`M${x} ${y + ry} A${w / 2} ${ry} 0 0 0 ${x + w} ${y + ry}`, {
|
|
811
|
+
...style,
|
|
812
|
+
fill: "none"
|
|
813
|
+
})];
|
|
814
|
+
}
|
|
815
|
+
const rx = shape === "stadium" ? Math.min(w, h) / 2 : shape === "round" ? 10 : 0;
|
|
816
|
+
return [ctx.rect(x, y, w, h, {
|
|
817
|
+
...style,
|
|
818
|
+
rx
|
|
819
|
+
}), ...shape === "subroutine" ? [10, w - 10].map((dx) => ctx.path(`M${x + dx} ${y} V${y + h}`, {
|
|
820
|
+
stroke: style.stroke,
|
|
821
|
+
fill: "none"
|
|
822
|
+
})) : []];
|
|
823
|
+
}
|
|
824
|
+
function iconWidth(icon, lines) {
|
|
825
|
+
return icon ? ICON + (lines.length ? ICON_GAP : 0) : 0;
|
|
826
|
+
}
|
|
827
|
+
function caption(ctx, { icon, x, y, lines, font, lineHeight }) {
|
|
828
|
+
const width = iconWidth(icon, lines);
|
|
829
|
+
const text = textBlock(ctx, {
|
|
830
|
+
x: x + width / 2,
|
|
831
|
+
y,
|
|
832
|
+
lines,
|
|
833
|
+
font,
|
|
834
|
+
lineHeight
|
|
835
|
+
});
|
|
836
|
+
if (!icon) return text;
|
|
837
|
+
const [vx, vy, w, h] = icon.viewBox.split(/\s+/).map(Number), scale = ICON / Math.max(w, h);
|
|
838
|
+
const ix = x - (width + maxWidthOf(lines, font, (value, f) => ctx.measure(value, f.size, f.weight))) / 2 + (ICON - w * scale) / 2;
|
|
839
|
+
const iy = y - h * scale / 2;
|
|
840
|
+
const color = ctx.color("fg"), stroke = icon.set === "tabler";
|
|
841
|
+
const glyph = ctx.el("g", {
|
|
842
|
+
transform: `translate(${ix} ${iy}) scale(${scale}) translate(${-vx} ${-vy})`,
|
|
843
|
+
fill: stroke ? "none" : color,
|
|
844
|
+
stroke: stroke ? color : "none",
|
|
845
|
+
"stroke-width": 1.8,
|
|
846
|
+
"stroke-linecap": "round",
|
|
847
|
+
"stroke-linejoin": "round"
|
|
848
|
+
}, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, attrs)));
|
|
849
|
+
return ctx.el("g", {}, [glyph, text]);
|
|
850
|
+
}
|
|
851
|
+
function scene(g, ctx) {
|
|
852
|
+
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 = [];
|
|
884
|
+
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) {
|
|
909
|
+
const [x, y] = center(node);
|
|
910
|
+
const { status } = node, tone = status && ctx.frameTone(ctx.design[status]);
|
|
911
|
+
const parts = drawShape(ctx, node, {
|
|
912
|
+
fill: tone ? ctx.tint(bg, tone, .11) : nodeFill,
|
|
913
|
+
stroke: ctx.tint(bg, tone || ink, .62),
|
|
914
|
+
"stroke-width": 1.25
|
|
915
|
+
});
|
|
916
|
+
parts.push(caption(ctx, {
|
|
917
|
+
...node,
|
|
918
|
+
x,
|
|
919
|
+
y,
|
|
920
|
+
lineHeight: 18
|
|
921
|
+
}));
|
|
922
|
+
children.push(attachLink(node, {
|
|
923
|
+
...ctx.el("g", {}, parts),
|
|
924
|
+
meta: {
|
|
925
|
+
kind: "node",
|
|
926
|
+
id: node.id,
|
|
927
|
+
...status ? { status } : {}
|
|
928
|
+
}
|
|
929
|
+
}));
|
|
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",
|
|
947
|
+
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
|
+
});
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
if (marks.size) children.unshift(ctx.el("defs", {}, [...marks.values()]));
|
|
974
|
+
return {
|
|
975
|
+
width: g.width,
|
|
976
|
+
height: g.height,
|
|
977
|
+
children
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
var implementations = { block: {
|
|
981
|
+
optionMode: "raw",
|
|
982
|
+
parse,
|
|
983
|
+
layout,
|
|
984
|
+
scene,
|
|
985
|
+
acceptsPresentation: true,
|
|
986
|
+
honoredConfig: ["block.padding"]
|
|
987
|
+
} };
|
|
988
|
+
export { block_exports, implementations };
|