@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,852 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { cubicPath } from "../core/geometry.js";
|
|
3
|
+
import { resolveIconRef } from "../core/icons.js";
|
|
4
|
+
import { stripEmphasis } from "./emphasis.js";
|
|
5
|
+
import { normalizeLabel } from "./label.js";
|
|
6
|
+
import { presentation } from "./presentation.js";
|
|
7
|
+
import { layoutGraph } from "./graph-layout.js";
|
|
8
|
+
import { DIRECTIONS, FONT, appearance, error, maxWidthOf, textBlock, warning, wrapText } from "./support.js";
|
|
9
|
+
// src/diagram/usecase.js
|
|
10
|
+
var usecase_exports = /* @__PURE__ */ __exportAll({ implementations: () => implementations });
|
|
11
|
+
var quoted = /^(?:"[^"]*"|'[^']*')$/;
|
|
12
|
+
var bare = /^[\w]+$/;
|
|
13
|
+
var label = (raw) => quoted.test(raw.trim()) ? raw.trim().slice(1, -1) : raw.trim();
|
|
14
|
+
var classSuffix = /\s*:::\s*([\w-]+(?:\s*,\s*[\w-]+)*)$/;
|
|
15
|
+
var configKey = (source, name) => Object.entries(source ?? {}).find(([key]) => key.toLowerCase() === name.toLowerCase())?.[1];
|
|
16
|
+
var iconSlot = "";
|
|
17
|
+
var honoredConfig = [
|
|
18
|
+
"actorFontSize",
|
|
19
|
+
"usecaseFontSize",
|
|
20
|
+
"diagramPadding"
|
|
21
|
+
].map((key) => `usecase.${key}`);
|
|
22
|
+
function scan(text, visit) {
|
|
23
|
+
let quote = "", depth = 0;
|
|
24
|
+
for (let i = 0; i < text.length; i++) {
|
|
25
|
+
const ch = text[i];
|
|
26
|
+
if (quote) {
|
|
27
|
+
if (ch === "\\" && text[i + 1] === quote && depth && text.includes("@{")) i++;
|
|
28
|
+
else if (ch === quote) quote = "";
|
|
29
|
+
} else {
|
|
30
|
+
if (!depth && visit?.(i) === false) break;
|
|
31
|
+
if (ch === "\"" || ch === "'") quote = ch;
|
|
32
|
+
else if ("([{".includes(ch)) depth++;
|
|
33
|
+
else if (")]}".includes(ch)) depth--;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return !quote && depth === 0;
|
|
37
|
+
}
|
|
38
|
+
function split(text, separator) {
|
|
39
|
+
const parts = [];
|
|
40
|
+
let start = 0;
|
|
41
|
+
scan(text, (i) => {
|
|
42
|
+
if (text[i] === separator) {
|
|
43
|
+
parts.push(text.slice(start, i).trim());
|
|
44
|
+
start = i + 1;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return [...parts, text.slice(start).trim()];
|
|
48
|
+
}
|
|
49
|
+
function metadata(raw) {
|
|
50
|
+
const fields = Object.create(null);
|
|
51
|
+
for (const field of split(raw, ",").filter(Boolean)) {
|
|
52
|
+
const result = /^(\w+)\s*:\s*(.+)$/s.exec(field);
|
|
53
|
+
if (!result || !/^(?:[\w-]+|"[^"]*"|'[^']*')$/.test(result[2].trim())) return null;
|
|
54
|
+
fields[result[1]] = label(result[2]);
|
|
55
|
+
}
|
|
56
|
+
return fields;
|
|
57
|
+
}
|
|
58
|
+
function entity(text) {
|
|
59
|
+
const suffix = classSuffix.exec(text);
|
|
60
|
+
if (suffix) text = text.slice(0, suffix.index);
|
|
61
|
+
const m = /^([\w]+|"[^"]*"|'[^']*')\s*(?:\(([^]*?)\)|\[([^]*?)\])?\s*(?:@\{([^]*?)\})?\s*(?:<<([^<>]+)>>)?$/.exec(text);
|
|
62
|
+
if (!m) return null;
|
|
63
|
+
const name = bare.test(m[1]) ? void 0 : label(m[1]);
|
|
64
|
+
const fields = m[4] == null ? {} : metadata(m[4]);
|
|
65
|
+
if (!fields) return null;
|
|
66
|
+
return {
|
|
67
|
+
id: name == null ? m[1] : normalizeLabel(stripEmphasis(name)).text.replace(/\W/g, "_"),
|
|
68
|
+
label: m[2] != null || m[3] != null ? label(m[2] ?? m[3]) : name,
|
|
69
|
+
shape: m[3] != null ? "rect" : m[2] != null ? "ellipse" : void 0,
|
|
70
|
+
stereotype: m[5]?.trim(),
|
|
71
|
+
fields,
|
|
72
|
+
classes: suffix?.[1].split(/\s*,\s*/)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function relation(text) {
|
|
76
|
+
let result;
|
|
77
|
+
scan(text, (i) => {
|
|
78
|
+
if (!"-.<ox".includes(text[i])) return;
|
|
79
|
+
const op = /^(?:--\s+((?:"[^"]*"|'[^']*'|.+?))\s+(-{2,}>)|(-{2,}>|--\|>|<-{2,}|--[ox]|[ox]--|\.{2}>|-{2,}))/.exec(text.slice(i));
|
|
80
|
+
if (!op || !op[2] && !op[3]) return;
|
|
81
|
+
let lhs = text.slice(0, i).trim();
|
|
82
|
+
const edgeId = /\s+(\w+)@$/.exec(lhs);
|
|
83
|
+
if (edgeId) lhs = lhs.slice(0, edgeId.index).trimEnd();
|
|
84
|
+
const from = entity(lhs);
|
|
85
|
+
if (!from) return;
|
|
86
|
+
let rhs = text.slice(i + op[0].length).trim(), caption = op[1] ? label(op[1]) : "", dependency;
|
|
87
|
+
const operator = op[2] || op[3];
|
|
88
|
+
if (operator === "..>") {
|
|
89
|
+
const kind = /^:\s*(include|extend)\s+/.exec(rhs);
|
|
90
|
+
if (!kind) return;
|
|
91
|
+
dependency = kind[1];
|
|
92
|
+
caption = `«${dependency}»`;
|
|
93
|
+
rhs = rhs.slice(kind[0].length);
|
|
94
|
+
}
|
|
95
|
+
const to = entity(rhs);
|
|
96
|
+
if (!to) return;
|
|
97
|
+
result = {
|
|
98
|
+
from,
|
|
99
|
+
to,
|
|
100
|
+
id: edgeId?.[1],
|
|
101
|
+
label: caption,
|
|
102
|
+
dependency,
|
|
103
|
+
extra: Math.min(40, Math.max(0, (operator.match(/-+/)?.[0].length ?? 2) - 2)),
|
|
104
|
+
start: operator.startsWith("<") ? "arrow" : operator.startsWith("o") ? "circle" : operator.startsWith("x") ? "cross" : null,
|
|
105
|
+
end: operator === "--|>" ? "triangle" : operator.endsWith(">") ? "arrow" : operator.endsWith("o") ? "circle" : operator.endsWith("x") ? "cross" : null
|
|
106
|
+
};
|
|
107
|
+
return false;
|
|
108
|
+
});
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
function jsonRows(raw) {
|
|
112
|
+
const parsed = JSON.parse(raw);
|
|
113
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new SyntaxError("Expected a JSON object");
|
|
114
|
+
const tokens = raw.match(/"(?:\\.|[^"\\])*"|[{}\[\]:,]|[^\s{}\[\]:,]+/g);
|
|
115
|
+
const rows = [];
|
|
116
|
+
let i = 0;
|
|
117
|
+
function read(path, depth = 0) {
|
|
118
|
+
if (depth > 128) throw new SyntaxError("JSON nesting exceeds the work limit");
|
|
119
|
+
const token = tokens[i++];
|
|
120
|
+
if (token !== "{" && token !== "[") {
|
|
121
|
+
rows.push([path, token.startsWith("\"") ? JSON.parse(token) : token]);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const close = token === "{" ? "}" : "]";
|
|
125
|
+
if (tokens[i] === close) rows.push([path || "—", token + close]);
|
|
126
|
+
for (let index = 0; tokens[i] !== close; index++) {
|
|
127
|
+
if (index) i++;
|
|
128
|
+
if (token === "[") read(`${path}[${index}]`, depth + 1);
|
|
129
|
+
else {
|
|
130
|
+
const key = JSON.parse(tokens[i++]);
|
|
131
|
+
i++;
|
|
132
|
+
read(path ? `${path}.${key}` : key, depth + 1);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
i++;
|
|
136
|
+
}
|
|
137
|
+
read("");
|
|
138
|
+
return rows;
|
|
139
|
+
}
|
|
140
|
+
function parse(body, info) {
|
|
141
|
+
const errors = [], symbols = /* @__PURE__ */ new Map(), edges = [], notes = [], assignments = [];
|
|
142
|
+
const look = presentation(info), seenAssets = /* @__PURE__ */ new Set();
|
|
143
|
+
const reportAsset = (diagnostic) => {
|
|
144
|
+
if (!seenAssets.has(diagnostic.message)) errors.push(diagnostic);
|
|
145
|
+
seenAssets.add(diagnostic.message);
|
|
146
|
+
};
|
|
147
|
+
const clean = (text, line) => {
|
|
148
|
+
const result = normalizeLabel(stripEmphasis(text ?? ""), {
|
|
149
|
+
lang: info.lang,
|
|
150
|
+
line
|
|
151
|
+
});
|
|
152
|
+
result.diagnostics.forEach(reportAsset);
|
|
153
|
+
return result;
|
|
154
|
+
};
|
|
155
|
+
const fix = ["检查用例声明和关系两端", "Check declarations and relation endpoints"];
|
|
156
|
+
const fail = (line, en, zh = "无效或冲突的用例声明") => errors.push(error(info.lang, line, zh, en, ...fix));
|
|
157
|
+
const asset = (line, ref) => reportAsset(warning("IGNORED_ASSET", info.lang, line, `未知图标 ${ref}`, `Unknown icon ${ref}`));
|
|
158
|
+
let direction = "LR", parent = null;
|
|
159
|
+
const config = configKey(info.config, "usecase");
|
|
160
|
+
function fontSize(key) {
|
|
161
|
+
const value = configKey(config, key);
|
|
162
|
+
const size = typeof value === "number" && Number.isFinite(value) ? Math.max(12, Math.min(16, value)) : FONT.body.size;
|
|
163
|
+
if (value != null && size !== value) errors.push(warning("IGNORED_CONFIG", info.lang, info.configLines?.[`usecase.${key.toLowerCase()}`] || info.headerLine, `${key} 按 ${size}px 绘制`, `${key} is rendered at ${size}px`));
|
|
164
|
+
return size;
|
|
165
|
+
}
|
|
166
|
+
const fonts = {
|
|
167
|
+
actor: fontSize("actorFontSize"),
|
|
168
|
+
usecase: fontSize("usecaseFontSize")
|
|
169
|
+
};
|
|
170
|
+
function declare(item, kind, line, owner = null) {
|
|
171
|
+
const existing = symbols.get(item.id);
|
|
172
|
+
const node = existing || {
|
|
173
|
+
id: item.id,
|
|
174
|
+
line,
|
|
175
|
+
fields: Object.create(null)
|
|
176
|
+
};
|
|
177
|
+
if (existing && kind && existing.kind && existing.kind !== kind) fail(line, `Conflicting entity kind: ${item.id}`);
|
|
178
|
+
if (owner && node.parent && node.parent !== owner) fail(line, `Entity belongs to two systems: ${item.id}`);
|
|
179
|
+
for (const key of [
|
|
180
|
+
"label",
|
|
181
|
+
"shape",
|
|
182
|
+
"stereotype"
|
|
183
|
+
]) {
|
|
184
|
+
if (item[key] != null && node[key] != null && item[key] !== node[key]) fail(line, `Conflicting ${key}: ${item.id}`);
|
|
185
|
+
if (item[key] != null) node[key] = item[key];
|
|
186
|
+
}
|
|
187
|
+
node.kind ||= kind;
|
|
188
|
+
if (owner) node.parent = owner;
|
|
189
|
+
Object.assign(node.fields, item.fields);
|
|
190
|
+
if (item.classes) look.addClasses(item.id, item.classes, line);
|
|
191
|
+
symbols.set(item.id, node);
|
|
192
|
+
return node;
|
|
193
|
+
}
|
|
194
|
+
if (info.params) fail(info.headerLine, "Unexpected usecase-beta header suffix");
|
|
195
|
+
if (body.length > 2e5) {
|
|
196
|
+
fail(info.firstLine, "Use case source is too long", "用例源码太长");
|
|
197
|
+
return {
|
|
198
|
+
model: null,
|
|
199
|
+
errors
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
const rows = body.split("\n");
|
|
203
|
+
for (let i = 0; i < rows.length; i++) {
|
|
204
|
+
let text = rows[i].trim();
|
|
205
|
+
const line = info.firstLine + i;
|
|
206
|
+
if (!text || text.startsWith("%%") || look.accept(text, line)) continue;
|
|
207
|
+
if (/^json\s/i.test(text)) while (!scan(text) && i + 1 < rows.length) text += `\n${rows[++i]}`;
|
|
208
|
+
const dir = /^direction\s+(\w+)$/i.exec(text);
|
|
209
|
+
const boundary = /^systemBoundary\s+(.+)$/.exec(text);
|
|
210
|
+
const actor = /^actor\s+(.+)$/.exec(text);
|
|
211
|
+
const note = /^note\s+for\s+(\w+)\s+(.+)$/.exec(text);
|
|
212
|
+
const suffix = /^json\s/.test(text) ? classSuffix.exec(text) : null;
|
|
213
|
+
const json = /^json\s+(\w+)\s*@\{([^]*)\}$/.exec(suffix ? text.slice(0, suffix.index) : text);
|
|
214
|
+
const assign = /^(\w+)\s*@\{([^]*)\}$/.exec(text);
|
|
215
|
+
if (dir && !parent && [...DIRECTIONS, "TD"].includes(dir[1].toUpperCase())) direction = dir[1].toUpperCase().replace("TD", "TB");
|
|
216
|
+
else if (text === "end" && parent) parent = null;
|
|
217
|
+
else if (boundary && !parent && entity(boundary[1])) {
|
|
218
|
+
const item = entity(boundary[1]);
|
|
219
|
+
delete item.shape;
|
|
220
|
+
parent = declare(item, "boundary", line).id;
|
|
221
|
+
} else if (note && !parent && quoted.test(note[2])) notes.push({
|
|
222
|
+
id: `@note${notes.length}`,
|
|
223
|
+
kind: "note",
|
|
224
|
+
target: note[1],
|
|
225
|
+
label: label(note[2]),
|
|
226
|
+
line
|
|
227
|
+
});
|
|
228
|
+
else if (json && !parent) try {
|
|
229
|
+
declare({
|
|
230
|
+
id: json[1],
|
|
231
|
+
classes: suffix?.[1].split(/\s*,\s*/)
|
|
232
|
+
}, "json", line).rows = jsonRows(`{${json[2]}}`);
|
|
233
|
+
} catch (cause) {
|
|
234
|
+
if (!(cause instanceof SyntaxError)) throw cause;
|
|
235
|
+
fail(line, "Expected a strict JSON object", "需要合法的 JSON 对象");
|
|
236
|
+
}
|
|
237
|
+
else if (assign && !parent && metadata(assign[2])) assignments.push({
|
|
238
|
+
id: assign[1],
|
|
239
|
+
fields: metadata(assign[2]),
|
|
240
|
+
line
|
|
241
|
+
});
|
|
242
|
+
else {
|
|
243
|
+
const expression = actor ? actor[1] : text;
|
|
244
|
+
const parts = actor ? split(expression, ",") : [expression];
|
|
245
|
+
const edge = parent ? null : relation(parts.at(-1));
|
|
246
|
+
const items = (edge ? parts.slice(0, -1) : parts).map(entity);
|
|
247
|
+
if (items.some((item) => !item) || !edge && !items.length) {
|
|
248
|
+
fail(line, "Invalid use case statement");
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
for (const item of items) declare(item, actor ? "actor" : "usecase", line, parent);
|
|
252
|
+
if (edge) {
|
|
253
|
+
const from = declare(edge.from, actor ? "actor" : null, line);
|
|
254
|
+
const to = declare(edge.to, null, line);
|
|
255
|
+
if (edge.id) declare({ id: edge.id }, "edge", line);
|
|
256
|
+
edges.push({
|
|
257
|
+
...edge,
|
|
258
|
+
from: from.id,
|
|
259
|
+
to: to.id,
|
|
260
|
+
line
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (parent) fail(symbols.get(parent).line, "Unclosed system boundary", "系统边界缺少 end");
|
|
266
|
+
for (const { id, fields, line } of assignments) {
|
|
267
|
+
const node = symbols.get(id);
|
|
268
|
+
if (node) Object.assign(node.fields, fields);
|
|
269
|
+
else fail(line, `Unknown metadata target: ${id}`);
|
|
270
|
+
}
|
|
271
|
+
const nodes = [], boundaries = [];
|
|
272
|
+
for (const node of symbols.values()) {
|
|
273
|
+
node.kind ||= "usecase";
|
|
274
|
+
const normalized = clean(node.label ?? node.id, node.line);
|
|
275
|
+
node.label = normalized.text;
|
|
276
|
+
node.icon = normalized.icon;
|
|
277
|
+
if (node.stereotype) node.stereotype = clean(node.stereotype, node.line).text;
|
|
278
|
+
node.status = look.statusOf(node.id);
|
|
279
|
+
node.link = look.linkOf(node.id);
|
|
280
|
+
const { type, business, icon } = node.fields;
|
|
281
|
+
const allowed = {
|
|
282
|
+
actor: [
|
|
283
|
+
"type",
|
|
284
|
+
"business",
|
|
285
|
+
"icon"
|
|
286
|
+
],
|
|
287
|
+
usecase: ["business"],
|
|
288
|
+
boundary: ["type"],
|
|
289
|
+
json: [],
|
|
290
|
+
edge: ["animate", "animation"]
|
|
291
|
+
}[node.kind];
|
|
292
|
+
if (Object.keys(node.fields).some((key) => !allowed.includes(key))) fail(node.line, `Invalid metadata for ${node.id}`);
|
|
293
|
+
node.business = business === "true";
|
|
294
|
+
if (business != null && !["true", "false"].includes(business)) fail(node.line, "business requires a boolean");
|
|
295
|
+
if (node.kind === "actor") {
|
|
296
|
+
node.variant = type || "normal";
|
|
297
|
+
if (![
|
|
298
|
+
"normal",
|
|
299
|
+
"hollow",
|
|
300
|
+
"awesome"
|
|
301
|
+
].includes(node.variant) || icon && node.variant !== "normal" || node.business && (icon || node.variant === "awesome")) fail(node.line, `Invalid actor variant: ${node.id}`);
|
|
302
|
+
if (icon) {
|
|
303
|
+
node.icon = resolveIconRef(icon);
|
|
304
|
+
if (!node.icon) asset(node.line, icon);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (node.kind === "usecase" && node.business && node.shape === "rect") fail(node.line, "A business use case must be elliptical");
|
|
308
|
+
if (node.kind === "boundary") {
|
|
309
|
+
if (type && !["rect", "package"].includes(type)) fail(node.line, `Invalid system boundary type: ${type}`);
|
|
310
|
+
boundaries.push({
|
|
311
|
+
...node,
|
|
312
|
+
package: type === "package"
|
|
313
|
+
});
|
|
314
|
+
} else if (node.kind !== "edge") nodes.push(node);
|
|
315
|
+
}
|
|
316
|
+
for (const [index, edge] of edges.entries()) {
|
|
317
|
+
edge.status = look.edgeStatusOf(index) ?? (edge.id ? look.statusOf(edge.id) : null);
|
|
318
|
+
edge.link = edge.id ? look.linkOf(edge.id) : null;
|
|
319
|
+
const normalized = clean(edge.label, edge.line);
|
|
320
|
+
edge.label = normalized.text;
|
|
321
|
+
edge.icon = normalized.icon;
|
|
322
|
+
const from = symbols.get(edge.from).kind, to = symbols.get(edge.to).kind;
|
|
323
|
+
if (["boundary", "edge"].includes(from) || ["boundary", "edge"].includes(to) || edge.dependency && (from !== "usecase" || to !== "usecase") || edge.end === "triangle" && (from !== to || !["actor", "usecase"].includes(from)) || edge.start && from === "json" || edge.end && edge.end !== "arrow" && to === "json") fail(edge.line, "Invalid relation endpoints");
|
|
324
|
+
}
|
|
325
|
+
for (const note of notes) {
|
|
326
|
+
if (!["actor", "usecase"].includes(symbols.get(note.target)?.kind)) fail(note.line, `Unknown note target: ${note.target}`);
|
|
327
|
+
const normalized = clean(note.label, note.line);
|
|
328
|
+
note.label = normalized.text;
|
|
329
|
+
note.icon = normalized.icon;
|
|
330
|
+
nodes.push(note);
|
|
331
|
+
edges.push({
|
|
332
|
+
from: note.id,
|
|
333
|
+
to: note.target,
|
|
334
|
+
line: note.line,
|
|
335
|
+
note: true
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
if (nodes.length > 2e3 || edges.length > 5e3) fail(info.firstLine, "Use case diagram exceeds the work limit", "用例图超过处理上限");
|
|
339
|
+
errors.push(...look.diagnostics());
|
|
340
|
+
return {
|
|
341
|
+
model: {
|
|
342
|
+
nodes,
|
|
343
|
+
boundaries,
|
|
344
|
+
edges,
|
|
345
|
+
direction,
|
|
346
|
+
fonts,
|
|
347
|
+
padding: configKey(config, "diagramPadding")
|
|
348
|
+
},
|
|
349
|
+
errors
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function measureNode(node, measure, cap, fonts) {
|
|
353
|
+
const font = node.kind === "note" || node.kind === "json" ? FONT.label : {
|
|
354
|
+
...FONT.body,
|
|
355
|
+
size: fonts[node.kind] ?? FONT.body.size
|
|
356
|
+
};
|
|
357
|
+
const pitch = Math.max(17, font.size + 4);
|
|
358
|
+
const iconWidth = node.kind !== "actor" && node.icon ? 20 : 0;
|
|
359
|
+
const authored = node.label.split("\n");
|
|
360
|
+
const fitted = node.kind === "usecase" && node.shape !== "rect" && authored.length > 1;
|
|
361
|
+
const lines = wrapText(node.label, fitted ? Math.max(cap, 200 - iconWidth) : cap, font, measure);
|
|
362
|
+
const stereo = node.stereotype ? wrapText(`«${node.stereotype}»`, cap, FONT.label, measure) : [];
|
|
363
|
+
const rows = node.rows?.map((row) => row.map((value) => wrapText(value, cap / 2, font, measure)));
|
|
364
|
+
const columns = rows ? [0, 1].map((index) => Math.max(26, ...rows.map((row) => maxWidthOf(row[index], font, measure))) + 16) : [];
|
|
365
|
+
const textHeight = pitch * (Math.max(lines.length, iconWidth ? 1 : 0) + stereo.length);
|
|
366
|
+
const textWidth = Math.max(maxWidthOf(lines, font, measure), maxWidthOf(stereo, FONT.label, measure));
|
|
367
|
+
return {
|
|
368
|
+
lines,
|
|
369
|
+
stereo,
|
|
370
|
+
rows,
|
|
371
|
+
columns,
|
|
372
|
+
w: Math.max(node.kind === "usecase" ? 120 : 70, textWidth + iconWidth + (node.kind === "usecase" && node.shape !== "rect" ? 52 : 30), columns[0] + columns[1] || 0),
|
|
373
|
+
h: node.kind === "actor" ? 54 + textHeight : rows ? 27 + rows.reduce((n, row) => n + rowHeight(row), 0) : textHeight + 30,
|
|
374
|
+
font,
|
|
375
|
+
pitch,
|
|
376
|
+
iconWidth
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
var rowHeight = (row) => 16 * Math.max(row[0].length, row[1].length) + 8;
|
|
380
|
+
function layout(model, options) {
|
|
381
|
+
const { measure, width, lang } = options;
|
|
382
|
+
const diagrams = /* @__PURE__ */ new Map();
|
|
383
|
+
let geometry;
|
|
384
|
+
for (const cap of [
|
|
385
|
+
180,
|
|
386
|
+
140,
|
|
387
|
+
100
|
|
388
|
+
]) {
|
|
389
|
+
const measures = /* @__PURE__ */ new Map();
|
|
390
|
+
const nodes = model.nodes.map((node, seq) => {
|
|
391
|
+
const metrics = measureNode(node, measure, cap, model.fonts);
|
|
392
|
+
diagrams.set(node.id, {
|
|
393
|
+
...node,
|
|
394
|
+
...metrics
|
|
395
|
+
});
|
|
396
|
+
const key = `\uE000${seq}\uE001`;
|
|
397
|
+
measures.set(key, Math.min(200, metrics.w - 28));
|
|
398
|
+
return {
|
|
399
|
+
id: node.id,
|
|
400
|
+
seq,
|
|
401
|
+
parent: node.parent,
|
|
402
|
+
shape: "rect",
|
|
403
|
+
label: key,
|
|
404
|
+
desc: Array.from({ length: Math.max(0, Math.ceil((metrics.h - 44) / 17)) }, () => "·")
|
|
405
|
+
};
|
|
406
|
+
});
|
|
407
|
+
const subgraphs = model.boundaries.map((boundary, seq) => ({
|
|
408
|
+
id: boundary.id,
|
|
409
|
+
title: boundary.label,
|
|
410
|
+
seq,
|
|
411
|
+
depth: 1
|
|
412
|
+
}));
|
|
413
|
+
geometry = layoutGraph({
|
|
414
|
+
type: "usecase-beta",
|
|
415
|
+
direction: model.direction,
|
|
416
|
+
nodes,
|
|
417
|
+
subgraphs,
|
|
418
|
+
edges: model.edges.map((edge, seq) => ({
|
|
419
|
+
...edge,
|
|
420
|
+
seq,
|
|
421
|
+
label: edge.icon ? `${iconSlot}${edge.label ? ` ${edge.label}` : ""}` : edge.label,
|
|
422
|
+
startArrow: Boolean(edge.start),
|
|
423
|
+
endArrow: Boolean(edge.end)
|
|
424
|
+
}))
|
|
425
|
+
}, {
|
|
426
|
+
...options,
|
|
427
|
+
measure: (text, font) => measures.get(text) ?? (text.startsWith(iconSlot) ? 16 + measure(text.slice(1), font) : measure(text, font))
|
|
428
|
+
});
|
|
429
|
+
if (geometry.width <= width * 1.6) break;
|
|
430
|
+
}
|
|
431
|
+
const errors = [];
|
|
432
|
+
if (geometry.width > width * 2 || model.nodes.length > 60) errors.push(warning("LARGE_DIAGRAM", lang, model.nodes[0]?.line, "用例图较大,可拆成多张图", "Large use case diagram; consider splitting it"));
|
|
433
|
+
attachRoutedLabels(geometry);
|
|
434
|
+
return {
|
|
435
|
+
...geometry,
|
|
436
|
+
diagrams,
|
|
437
|
+
uml: model,
|
|
438
|
+
errors
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
function attachRoutedLabels(geometry) {
|
|
442
|
+
const segmentBox = ({ p0, p1, c1, c2 }) => {
|
|
443
|
+
const lowX = Math.min(p0.x, p1.x, c1.x, c2.x), lowY = Math.min(p0.y, p1.y, c1.y, c2.y);
|
|
444
|
+
return {
|
|
445
|
+
x: lowX,
|
|
446
|
+
y: lowY,
|
|
447
|
+
w: Math.max(p0.x, p1.x, c1.x, c2.x) - lowX,
|
|
448
|
+
h: Math.max(p0.y, p1.y, c1.y, c2.y) - lowY
|
|
449
|
+
};
|
|
450
|
+
};
|
|
451
|
+
const touches = (a, b, gap = 0) => a.x < b.x + b.w + gap && b.x < a.x + a.w + gap && a.y < b.y + b.h + gap && b.y < a.y + a.h + gap;
|
|
452
|
+
const titleRects = geometry.frames.map((frame) => frame.titleRect).filter(Boolean);
|
|
453
|
+
for (const edge of geometry.edges) {
|
|
454
|
+
const label = edge.labelRect;
|
|
455
|
+
if (!label || edge.segments.some((segment) => touches(label, segmentBox(segment), 1))) continue;
|
|
456
|
+
const others = geometry.edges.filter((other) => other !== edge);
|
|
457
|
+
const occupied = [
|
|
458
|
+
...geometry.nodes,
|
|
459
|
+
...titleRects,
|
|
460
|
+
...others.map((other) => other.labelRect).filter(Boolean),
|
|
461
|
+
...others.flatMap((other) => other.segments.map(segmentBox))
|
|
462
|
+
];
|
|
463
|
+
let best = null, bestDistance = Infinity;
|
|
464
|
+
for (const segment of edge.segments) {
|
|
465
|
+
const run = segmentBox(segment);
|
|
466
|
+
if (run.h || run.w < label.w + 8) continue;
|
|
467
|
+
for (let x = run.x + 4; x + label.w <= run.x + run.w - 4; x += 8) {
|
|
468
|
+
const candidate = {
|
|
469
|
+
...label,
|
|
470
|
+
x,
|
|
471
|
+
y: run.y - label.h / 2
|
|
472
|
+
};
|
|
473
|
+
if (occupied.some((box) => touches(candidate, box, 3))) continue;
|
|
474
|
+
if (geometry.frames.some((frame) => touches(candidate, frame, 3) && (x < frame.x + 3 || x + label.w > frame.x + frame.w - 3 || candidate.y < frame.y + 3 || candidate.y + label.h > frame.y + frame.h - 3))) continue;
|
|
475
|
+
const distance = Math.hypot(candidate.x - label.x, candidate.y - label.y);
|
|
476
|
+
if (distance < bestDistance) {
|
|
477
|
+
best = candidate;
|
|
478
|
+
bestDistance = distance;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (best) edge.labelRect = best;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
function markerDefs(ctx, a) {
|
|
486
|
+
const ink = a.fg4;
|
|
487
|
+
const marker = (id, refX, glyph) => ctx.el("marker", {
|
|
488
|
+
id: `uml-${id}`,
|
|
489
|
+
viewBox: "0 0 12 12",
|
|
490
|
+
refX,
|
|
491
|
+
refY: 6,
|
|
492
|
+
markerWidth: 12,
|
|
493
|
+
markerHeight: 12,
|
|
494
|
+
markerUnits: "userSpaceOnUse",
|
|
495
|
+
orient: "auto-start-reverse"
|
|
496
|
+
}, [glyph]);
|
|
497
|
+
return ctx.el("defs", {}, [
|
|
498
|
+
marker("arrow", 10, ctx.path("M2 2 L10 6 L2 10 Z", { fill: ink })),
|
|
499
|
+
marker("dependency", 10, ctx.path("M2 2 L10 6 L2 10 Z", { fill: a.info })),
|
|
500
|
+
marker("triangle", 11, ctx.path("M1 1 L11 6 L1 11 Z", {
|
|
501
|
+
fill: a.surface,
|
|
502
|
+
stroke: ink,
|
|
503
|
+
"stroke-width": 1.25
|
|
504
|
+
})),
|
|
505
|
+
marker("circle", 10, ctx.circle(7, 6, 3, {
|
|
506
|
+
fill: a.surface,
|
|
507
|
+
stroke: ink,
|
|
508
|
+
"stroke-width": 1.25
|
|
509
|
+
})),
|
|
510
|
+
marker("cross", 9, ctx.path("M3 3 L9 9 M3 9 L9 3", {
|
|
511
|
+
stroke: ink,
|
|
512
|
+
"stroke-width": 1.25
|
|
513
|
+
}))
|
|
514
|
+
]);
|
|
515
|
+
}
|
|
516
|
+
function iconAt(ctx, icon, x, y, size, color) {
|
|
517
|
+
const [left, top, w, h] = icon.viewBox.split(/\s+/).map(Number), scale = size / Math.max(w, h);
|
|
518
|
+
return ctx.el("g", { transform: `translate(${x - (left + w / 2) * scale} ${y - (top + h / 2) * scale}) scale(${scale})` }, icon.elements.map(({ tag, attrs }) => ctx.shape(tag, {
|
|
519
|
+
...attrs,
|
|
520
|
+
...icon.set === "simple" ? { fill: color } : {
|
|
521
|
+
fill: "none",
|
|
522
|
+
stroke: color,
|
|
523
|
+
"stroke-width": 1.6,
|
|
524
|
+
"stroke-linecap": "round",
|
|
525
|
+
"stroke-linejoin": "round"
|
|
526
|
+
}
|
|
527
|
+
})));
|
|
528
|
+
}
|
|
529
|
+
var linked = (ctx, item, drawing) => item.link ? ctx.link(item.link.href, [drawing], item.link) : drawing;
|
|
530
|
+
function drawNode(ctx, box, node, a) {
|
|
531
|
+
const { x, y, w, h } = box, cx = x + w / 2, cy = y + h / 2;
|
|
532
|
+
const meta = {
|
|
533
|
+
kind: "node",
|
|
534
|
+
id: node.id,
|
|
535
|
+
...node.status ? { status: node.status } : {}
|
|
536
|
+
};
|
|
537
|
+
const stroke = a[node.status] || ctx.tint(a.bg, ctx.frameTone(ctx.color("tone1")), .52);
|
|
538
|
+
const fill = node.status ? ctx.tint(a.node, stroke, .09) : a.node;
|
|
539
|
+
const shape = {
|
|
540
|
+
fill,
|
|
541
|
+
stroke,
|
|
542
|
+
"stroke-width": 1.25,
|
|
543
|
+
meta
|
|
544
|
+
};
|
|
545
|
+
const block = (lines, px, py, font = FONT.body, fill = a.fg1, anchor = "middle", lineHeight = 17) => textBlock(ctx, {
|
|
546
|
+
x: px,
|
|
547
|
+
y: py,
|
|
548
|
+
lines,
|
|
549
|
+
font,
|
|
550
|
+
fill,
|
|
551
|
+
anchor,
|
|
552
|
+
lineHeight
|
|
553
|
+
});
|
|
554
|
+
const stereoH = node.stereo.length * node.pitch;
|
|
555
|
+
const labelH = node.lines.length * node.pitch;
|
|
556
|
+
const centerText = (lines, px, py) => block(lines, px, py, node.font, a.fg1, "middle", node.pitch);
|
|
557
|
+
const text = (center) => {
|
|
558
|
+
const labelY = center + stereoH / 2;
|
|
559
|
+
const shift = node.iconWidth && node.lines.length ? node.iconWidth / 2 : 0;
|
|
560
|
+
const iconX = shift ? cx - Math.max(...node.lines.map((line) => ctx.measure(line, node.font.size, node.font.weight))) / 2 - 2 : cx;
|
|
561
|
+
return [
|
|
562
|
+
block(node.stereo, cx, center - labelH / 2, FONT.label, a.muted),
|
|
563
|
+
centerText(node.lines, cx + shift, labelY),
|
|
564
|
+
...node.iconWidth ? [iconAt(ctx, node.icon, iconX, labelY, 14, stroke)] : []
|
|
565
|
+
];
|
|
566
|
+
};
|
|
567
|
+
if (node.kind === "actor") {
|
|
568
|
+
const py = cy - (stereoH + labelH) / 2 - 20;
|
|
569
|
+
const pen = {
|
|
570
|
+
fill: "none",
|
|
571
|
+
stroke: node.status ? stroke : a.fg2,
|
|
572
|
+
"stroke-width": 1.6,
|
|
573
|
+
"stroke-linecap": "round",
|
|
574
|
+
"stroke-linejoin": "round"
|
|
575
|
+
};
|
|
576
|
+
const body = node.variant === "hollow" ? "M-13 16 H13 V22 H5 L12 34 H5 L0 27 L-5 34 H-12 L-5 22 H-13 Z" : node.variant === "awesome" ? "M-16 35 C-16 11 16 11 16 35 Z" : "M0 14 V26 M-13 18 H13 M0 26 L-11 36 M0 26 L11 36";
|
|
577
|
+
const glyph = node.icon ? ctx.el("g", {}, [ctx.rect(cx - 16, py + 2, 32, 32, {
|
|
578
|
+
fill,
|
|
579
|
+
stroke,
|
|
580
|
+
rx: a.smallRadius
|
|
581
|
+
}), iconAt(ctx, node.icon, cx, py + 18, 21, node.status ? stroke : a.fg2)]) : ctx.el("g", { transform: `translate(${cx} ${py})` }, [
|
|
582
|
+
ctx.circle(0, 7, 7, {
|
|
583
|
+
...pen,
|
|
584
|
+
fill: node.variant === "awesome" ? a.surface2 : a.node
|
|
585
|
+
}),
|
|
586
|
+
ctx.path(body, {
|
|
587
|
+
...pen,
|
|
588
|
+
...node.variant === "awesome" ? { fill: a.surface2 } : {}
|
|
589
|
+
}),
|
|
590
|
+
...node.business ? [ctx.path("M-17 36 L17 2", pen)] : []
|
|
591
|
+
]);
|
|
592
|
+
return ctx.el("g", {}, [
|
|
593
|
+
ctx.rect(x, y, w, h, {
|
|
594
|
+
fill: "none",
|
|
595
|
+
stroke: "none",
|
|
596
|
+
meta
|
|
597
|
+
}),
|
|
598
|
+
glyph,
|
|
599
|
+
...text(cy + 22)
|
|
600
|
+
]);
|
|
601
|
+
}
|
|
602
|
+
if (node.kind === "note") {
|
|
603
|
+
const fold = 8;
|
|
604
|
+
return ctx.el("g", {}, [
|
|
605
|
+
ctx.shape("polygon", {
|
|
606
|
+
points: `${x},${y} ${x + w - fold},${y} ${x + w},${y + fold} ${x + w},${y + h} ${x},${y + h}`,
|
|
607
|
+
...shape,
|
|
608
|
+
fill: a.surface2,
|
|
609
|
+
stroke: ctx.tint(a.bg, a.warn, .48)
|
|
610
|
+
}, meta),
|
|
611
|
+
ctx.path(`M${x + w - fold} ${y} v${fold} h${fold}`, {
|
|
612
|
+
stroke: ctx.tint(a.bg, a.warn, .48),
|
|
613
|
+
fill: "none"
|
|
614
|
+
}),
|
|
615
|
+
block(node.lines, x + 12 + node.iconWidth, cy, FONT.label, a.fg2, "start"),
|
|
616
|
+
...node.icon ? [iconAt(ctx, node.icon, x + 18, cy, 14, a.fg2)] : []
|
|
617
|
+
]);
|
|
618
|
+
}
|
|
619
|
+
if (node.kind === "json") {
|
|
620
|
+
const head = y + (h - 27 - node.rows.reduce((n, row) => n + rowHeight(row), 0)) / 2;
|
|
621
|
+
const children = [
|
|
622
|
+
ctx.rect(x, y, w, h, {
|
|
623
|
+
...shape,
|
|
624
|
+
rx: a.smallRadius
|
|
625
|
+
}),
|
|
626
|
+
ctx.rect(x + 1, y + 1, w - 2, head + 25 - y, {
|
|
627
|
+
fill: ctx.tint(a.bg, a.info, .06),
|
|
628
|
+
rx: a.smallRadius
|
|
629
|
+
}),
|
|
630
|
+
block(node.lines, cx, head + 13, {
|
|
631
|
+
...FONT.label,
|
|
632
|
+
weight: 600
|
|
633
|
+
})
|
|
634
|
+
];
|
|
635
|
+
let rowY = head + 27;
|
|
636
|
+
for (const row of node.rows) {
|
|
637
|
+
const height = rowHeight(row);
|
|
638
|
+
children.push(ctx.path(`M${x} ${rowY} H${x + w}`, { stroke: a.line2 }), block(row[0], x + 8, rowY + height / 2, FONT.label, a.muted, "start", 16), block(row[1], x + w - 8, rowY + height / 2, FONT.label, a.fg2, "end", 16));
|
|
639
|
+
rowY += height;
|
|
640
|
+
}
|
|
641
|
+
return ctx.el("g", {}, children);
|
|
642
|
+
}
|
|
643
|
+
const outline = node.shape === "rect" ? ctx.rect(x, y, w, h, {
|
|
644
|
+
...shape,
|
|
645
|
+
rx: a.radius
|
|
646
|
+
}) : ctx.shape("ellipse", {
|
|
647
|
+
cx,
|
|
648
|
+
cy,
|
|
649
|
+
rx: w / 2,
|
|
650
|
+
ry: h / 2,
|
|
651
|
+
fill,
|
|
652
|
+
stroke,
|
|
653
|
+
"stroke-width": 1.25
|
|
654
|
+
}, meta);
|
|
655
|
+
return ctx.el("g", {}, [outline, ...text(cy)]);
|
|
656
|
+
}
|
|
657
|
+
function attachEllipse(point, box) {
|
|
658
|
+
const rx = box.w / 2, ry = box.h / 2;
|
|
659
|
+
const x = box.x + rx, y = box.y + ry;
|
|
660
|
+
const dx = (point.x - x) / rx, dy = (point.y - y) / ry;
|
|
661
|
+
if (Math.abs(dx) >= Math.abs(dy)) return {
|
|
662
|
+
x: x + Math.sign(dx) * rx * Math.sqrt(Math.max(0, 1 - dy * dy)),
|
|
663
|
+
y: point.y
|
|
664
|
+
};
|
|
665
|
+
return {
|
|
666
|
+
x: point.x,
|
|
667
|
+
y: y + Math.sign(dy) * ry * Math.sqrt(Math.max(0, 1 - dx * dx))
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
function detour(segments, edge, geometry) {
|
|
671
|
+
if (segments.length !== 1 || edge.labelRect) return segments;
|
|
672
|
+
const { p0, p1, c1, c2 } = segments[0];
|
|
673
|
+
const axis = p0.y === p1.y ? "x" : p0.x === p1.x ? "y" : null;
|
|
674
|
+
if (!axis) return segments;
|
|
675
|
+
const cross = axis === "x" ? "y" : "x", size = axis === "x" ? "w" : "h", band = axis === "x" ? "h" : "w";
|
|
676
|
+
const low = Math.min(p0[axis], p1[axis]), high = Math.max(p0[axis], p1[axis]);
|
|
677
|
+
const crosses = (track) => (box) => box[axis] < high && box[axis] + box[size] > low && track >= box[cross] - 3 && track <= box[cross] + box[band] + 3;
|
|
678
|
+
if (c1[cross] !== p0[cross] || c2[cross] !== p0[cross]) return segments;
|
|
679
|
+
const obstacles = geometry.nodes.filter((n) => n.id !== edge.from && n.id !== edge.to);
|
|
680
|
+
const blocked = obstacles.filter(crosses(p0[cross]));
|
|
681
|
+
if (!blocked.length) return segments;
|
|
682
|
+
const clear = 10, direction = Math.sign(p1[axis] - p0[axis]);
|
|
683
|
+
const entry = direction > 0 ? Math.min(...blocked.map((n) => n[axis])) : Math.max(...blocked.map((n) => n[axis] + n[size]));
|
|
684
|
+
const exit = direction > 0 ? Math.max(...blocked.map((n) => n[axis] + n[size])) : Math.min(...blocked.map((n) => n[axis]));
|
|
685
|
+
if (direction * (entry - p0[axis]) < clear || direction * (p1[axis] - exit) < clear) return segments;
|
|
686
|
+
const tracks = [...blocked.flatMap((n) => [n[cross] + n[band] + clear, n[cross] - clear])];
|
|
687
|
+
const headers = geometry.frames.map((frame) => frame.titleRect).filter(Boolean);
|
|
688
|
+
const track = tracks.find((value) => value >= 12 && value <= (axis === "x" ? geometry.height : geometry.width) - 12 && !obstacles.some(crosses(value)) && !headers.some(crosses(value)));
|
|
689
|
+
if (track === void 0) return segments;
|
|
690
|
+
const points = [
|
|
691
|
+
p0,
|
|
692
|
+
{
|
|
693
|
+
[axis]: entry - direction * clear,
|
|
694
|
+
[cross]: track
|
|
695
|
+
},
|
|
696
|
+
{
|
|
697
|
+
[axis]: exit + direction * clear,
|
|
698
|
+
[cross]: track
|
|
699
|
+
},
|
|
700
|
+
p1
|
|
701
|
+
];
|
|
702
|
+
return points.slice(1).map((p1, index) => {
|
|
703
|
+
const p0 = points[index], middle = (p0[axis] + p1[axis]) / 2;
|
|
704
|
+
return {
|
|
705
|
+
p0,
|
|
706
|
+
p1,
|
|
707
|
+
c1: {
|
|
708
|
+
...p0,
|
|
709
|
+
[axis]: middle
|
|
710
|
+
},
|
|
711
|
+
c2: {
|
|
712
|
+
...p1,
|
|
713
|
+
[axis]: middle
|
|
714
|
+
}
|
|
715
|
+
};
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
function scene(geometry, ctx) {
|
|
719
|
+
const { uml, diagrams } = geometry, a = appearance(ctx);
|
|
720
|
+
const children = [markerDefs(ctx, a)], headers = [];
|
|
721
|
+
const boxes = new Map(geometry.nodes.map((box) => [box.id, box]));
|
|
722
|
+
for (const [index, frame] of geometry.frames.entries()) {
|
|
723
|
+
const system = uml.boundaries.find((boundary) => boundary.id === frame.id);
|
|
724
|
+
const tone = ctx.frameTone(ctx.color(`tone${index % 6 + 1}`));
|
|
725
|
+
const stroke = system.status ? a[system.status] : ctx.tint(a.bg, tone, .43), fill = ctx.tint(a.bg, system.status ? stroke : tone, .06);
|
|
726
|
+
const { x, y, w, h } = frame;
|
|
727
|
+
const lines = frame.lines;
|
|
728
|
+
const headerWidth = Math.min(w - 12, Math.max(64, ...lines.map((line) => ctx.measure(line, FONT.title.size, FONT.title.weight) + 18)));
|
|
729
|
+
const th = 17 * lines.length + 6, meta = {
|
|
730
|
+
kind: "frame",
|
|
731
|
+
id: frame.id
|
|
732
|
+
};
|
|
733
|
+
const tx = x + (system.package ? 0 : 7), ty = y + (system.package ? 0 : 4);
|
|
734
|
+
if (system.package) children.push(linked(ctx, system, ctx.path(`M${x} ${y + h} V${y} H${x + headerWidth} v${th} H${x + w} V${y + h} Z`, {
|
|
735
|
+
fill,
|
|
736
|
+
stroke,
|
|
737
|
+
"stroke-linejoin": "round",
|
|
738
|
+
meta
|
|
739
|
+
})));
|
|
740
|
+
else {
|
|
741
|
+
children.push(linked(ctx, system, ctx.rect(x, y, w, h, {
|
|
742
|
+
fill,
|
|
743
|
+
stroke,
|
|
744
|
+
rx: a.radius,
|
|
745
|
+
meta
|
|
746
|
+
})));
|
|
747
|
+
headers.push(ctx.rect(tx, ty, headerWidth, th, {
|
|
748
|
+
fill: a.surface,
|
|
749
|
+
stroke,
|
|
750
|
+
rx: a.smallRadius
|
|
751
|
+
}));
|
|
752
|
+
}
|
|
753
|
+
headers.push(textBlock(ctx, {
|
|
754
|
+
x: tx + 9,
|
|
755
|
+
y: ty + th / 2,
|
|
756
|
+
lines,
|
|
757
|
+
font: FONT.title,
|
|
758
|
+
anchor: "start",
|
|
759
|
+
lineHeight: 17
|
|
760
|
+
}));
|
|
761
|
+
}
|
|
762
|
+
for (const edge of geometry.edges) {
|
|
763
|
+
const spec = uml.edges[edge.id];
|
|
764
|
+
const segments = detour(edge.segments, edge, geometry).map((segment) => ({ ...segment }));
|
|
765
|
+
if (!segments.length) continue;
|
|
766
|
+
const first = segments[0], last = segments.at(-1);
|
|
767
|
+
for (const [id, segment, pointKey, controlKey] of [[
|
|
768
|
+
edge.from,
|
|
769
|
+
first,
|
|
770
|
+
"p0",
|
|
771
|
+
"c1"
|
|
772
|
+
], [
|
|
773
|
+
edge.to,
|
|
774
|
+
last,
|
|
775
|
+
"p1",
|
|
776
|
+
"c2"
|
|
777
|
+
]]) {
|
|
778
|
+
const node = diagrams.get(id);
|
|
779
|
+
if (node.kind !== "usecase" || node.shape === "rect") continue;
|
|
780
|
+
const point = segment[pointKey], control = segment[controlKey];
|
|
781
|
+
const end = attachEllipse(point, boxes.get(id));
|
|
782
|
+
segment[pointKey] = end;
|
|
783
|
+
segment[controlKey] = {
|
|
784
|
+
x: control.x + end.x - point.x,
|
|
785
|
+
y: control.y + end.y - point.y
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
children.push(linked(ctx, spec, ctx.path(cubicPath(segments), {
|
|
789
|
+
fill: "none",
|
|
790
|
+
stroke: a[spec.status] || (spec.dependency ? a.info : a.fg4),
|
|
791
|
+
"stroke-width": 1.25,
|
|
792
|
+
...spec.dependency || spec.note ? { "stroke-dasharray": spec.note ? "2 4" : "4 3" } : {},
|
|
793
|
+
...spec.start ? { "marker-start": `url(#uml-${spec.start})` } : {},
|
|
794
|
+
...spec.end ? { "marker-end": `url(#uml-${spec.dependency ? "dependency" : spec.end})` } : {},
|
|
795
|
+
meta: {
|
|
796
|
+
kind: "edge",
|
|
797
|
+
from: edge.from,
|
|
798
|
+
to: edge.to,
|
|
799
|
+
...spec.status ? { status: spec.status } : {}
|
|
800
|
+
}
|
|
801
|
+
})));
|
|
802
|
+
}
|
|
803
|
+
children.push(...headers);
|
|
804
|
+
for (const box of geometry.nodes) children.push(linked(ctx, diagrams.get(box.id), drawNode(ctx, box, diagrams.get(box.id), a)));
|
|
805
|
+
for (const edge of geometry.edges) {
|
|
806
|
+
if (!edge.labelRect) continue;
|
|
807
|
+
const { x, y, w, h } = edge.labelRect;
|
|
808
|
+
const spec = uml.edges[edge.id];
|
|
809
|
+
const lines = spec.icon ? edge.labelLines.map((line) => line.replace(/^\uFFFC\s*/, "")) : edge.labelLines;
|
|
810
|
+
const first = lines[0] || "";
|
|
811
|
+
const firstY = y + h / 2 - (lines.length - 1) * 8;
|
|
812
|
+
const glyphWidth = first ? ctx.measure(first, FONT.label.size, FONT.label.weight) : 0;
|
|
813
|
+
const hasText = spec.icon && first;
|
|
814
|
+
const group = ctx.el("g", {}, [
|
|
815
|
+
ctx.rect(x, y, w, h, {
|
|
816
|
+
fill: a.surface,
|
|
817
|
+
stroke: a.line2,
|
|
818
|
+
rx: a.smallRadius
|
|
819
|
+
}),
|
|
820
|
+
textBlock(ctx, {
|
|
821
|
+
x: x + w / 2 + (hasText ? 9 : 0),
|
|
822
|
+
y: y + h / 2,
|
|
823
|
+
lines,
|
|
824
|
+
font: FONT.label,
|
|
825
|
+
lineHeight: 16,
|
|
826
|
+
fill: a.fg2
|
|
827
|
+
}),
|
|
828
|
+
...spec.icon ? [iconAt(ctx, spec.icon, x + w / 2 - (hasText ? glyphWidth / 2 + 2 : 0), firstY, 12, a.fg2)] : []
|
|
829
|
+
]);
|
|
830
|
+
group.meta = {
|
|
831
|
+
kind: "edge-label-group",
|
|
832
|
+
from: edge.from,
|
|
833
|
+
to: edge.to
|
|
834
|
+
};
|
|
835
|
+
children.push(linked(ctx, spec, group));
|
|
836
|
+
}
|
|
837
|
+
const pad = typeof uml.padding === "number" && Number.isFinite(uml.padding) ? Math.max(0, Math.min(200, uml.padding)) - 16 : 0;
|
|
838
|
+
return {
|
|
839
|
+
width: Math.max(1, geometry.width + 2 * pad),
|
|
840
|
+
height: Math.max(1, geometry.height + 2 * pad),
|
|
841
|
+
children: pad ? [ctx.el("g", { transform: `translate(${pad} ${pad})` }, children)] : children
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
var implementations = { "usecase-beta": {
|
|
845
|
+
optionMode: "raw",
|
|
846
|
+
acceptsPresentation: true,
|
|
847
|
+
honoredConfig,
|
|
848
|
+
parse,
|
|
849
|
+
layout,
|
|
850
|
+
scene
|
|
851
|
+
} };
|
|
852
|
+
export { implementations, usecase_exports };
|