@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,1228 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { safeHref } from "../core/scene.js";
|
|
3
|
+
import { resolveIconRef } from "../core/icons.js";
|
|
4
|
+
import { normalizeLabel } from "./label.js";
|
|
5
|
+
import { presentation, roleOfColor } from "./presentation.js";
|
|
6
|
+
import { FONT, appearance, error, maxWidthOf, snap, warning, wrapText } from "./support.js";
|
|
7
|
+
// src/diagram/sequence.js
|
|
8
|
+
var sequence_exports = /* @__PURE__ */ __exportAll({
|
|
9
|
+
LIMITS: () => LIMITS,
|
|
10
|
+
implementations: () => implementations
|
|
11
|
+
});
|
|
12
|
+
var LIMITS = Object.freeze({
|
|
13
|
+
participants: 300,
|
|
14
|
+
messages: 500,
|
|
15
|
+
fragmentDepth: 32,
|
|
16
|
+
lines: 4
|
|
17
|
+
});
|
|
18
|
+
var SECTIONS = /* @__PURE__ */ new Set([
|
|
19
|
+
"else",
|
|
20
|
+
"and",
|
|
21
|
+
"option"
|
|
22
|
+
]);
|
|
23
|
+
var TYPES = /* @__PURE__ */ new Set([
|
|
24
|
+
"actor",
|
|
25
|
+
"boundary",
|
|
26
|
+
"collections",
|
|
27
|
+
"control",
|
|
28
|
+
"database",
|
|
29
|
+
"entity",
|
|
30
|
+
"participant",
|
|
31
|
+
"queue"
|
|
32
|
+
]);
|
|
33
|
+
var ARROWS = /* @__PURE__ */ new Map();
|
|
34
|
+
for (const [stem, head, start] of [
|
|
35
|
+
["->>", "arrow"],
|
|
36
|
+
["->", "none"],
|
|
37
|
+
["-x", "cross"],
|
|
38
|
+
["-)", "open"],
|
|
39
|
+
[
|
|
40
|
+
"<<->>",
|
|
41
|
+
"arrow",
|
|
42
|
+
"arrow"
|
|
43
|
+
],
|
|
44
|
+
["-|\\", "top"],
|
|
45
|
+
["-|/", "bottom"],
|
|
46
|
+
["-\\\\", "stick-top"],
|
|
47
|
+
["-//", "stick-bottom"],
|
|
48
|
+
[
|
|
49
|
+
"/|-",
|
|
50
|
+
"none",
|
|
51
|
+
"top"
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
"\\|-",
|
|
55
|
+
"none",
|
|
56
|
+
"bottom"
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
"//-",
|
|
60
|
+
"none",
|
|
61
|
+
"stick-top"
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
"\\\\-",
|
|
65
|
+
"none",
|
|
66
|
+
"stick-bottom"
|
|
67
|
+
]
|
|
68
|
+
]) for (const dashed of [false, true]) {
|
|
69
|
+
const token = dashed ? stem.replace("-", "--") : stem;
|
|
70
|
+
const style = head === "cross" ? "lost" : head === "open" ? "async" : dashed ? "return" : "sync";
|
|
71
|
+
ARROWS.set(token, {
|
|
72
|
+
token,
|
|
73
|
+
head,
|
|
74
|
+
start,
|
|
75
|
+
dashed,
|
|
76
|
+
style
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
var arrowPattern = new RegExp([...ARROWS.keys()].sort((a, b) => b.length - a.length).map((token) => token.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), "i");
|
|
80
|
+
var validId = (id) => /^[^\s<>::,;@()+]+$/.test(id);
|
|
81
|
+
var issue = (lang, line, zh, en, fixZh, fixEn) => error(lang, line, zh, en, fixZh, fixEn);
|
|
82
|
+
var colorPattern = /^(?:rgba?|hsla?)\([^)]*\)/i;
|
|
83
|
+
function splitLine(raw) {
|
|
84
|
+
const parts = [];
|
|
85
|
+
let start = 0;
|
|
86
|
+
for (const match of raw.matchAll(/"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|&?#(?:x[\da-f]+|\w+);|&\w+;|%%|#|;/gi)) {
|
|
87
|
+
if (match[0] === "#" && /^\s*link\s+[^\s:]+\s*:[^@]*@\s*\S*$/i.test(raw.slice(start, match.index))) continue;
|
|
88
|
+
if (match[0] === ";" || match[0] === "#" || match[0] === "%%") {
|
|
89
|
+
parts.push(raw.slice(start, match.index));
|
|
90
|
+
if (match[0] !== ";") return parts;
|
|
91
|
+
start = match.index + 1;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
parts.push(raw.slice(start));
|
|
95
|
+
return parts;
|
|
96
|
+
}
|
|
97
|
+
function metadata(raw = "") {
|
|
98
|
+
const pairs = raw.matchAll(/(?:^|,)\s*["']?(type|alias|icon)["']?\s*:\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|[^,]+)\s*(?=,|$)/g);
|
|
99
|
+
return Object.fromEntries(Array.from(pairs, ([, key, value]) => {
|
|
100
|
+
value = value.trim();
|
|
101
|
+
if (value.startsWith("\"")) try {
|
|
102
|
+
return [key, JSON.parse(value)];
|
|
103
|
+
} catch {}
|
|
104
|
+
return [key, value.replace(/^(["'])(.*)\1$/, "$2")];
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
function parse(source, info) {
|
|
108
|
+
const { lang, options = {}, firstLine } = info;
|
|
109
|
+
const look = presentation({ lang });
|
|
110
|
+
const menuLinks = /* @__PURE__ */ new Map(), ignoredColors = /* @__PURE__ */ new Map();
|
|
111
|
+
const errors = [];
|
|
112
|
+
for (const key of Object.keys(options)) errors.push(issue(lang, firstLine - 1, `sequenceDiagram 不支持选项「${key}」`, `Unsupported sequenceDiagram option: ${key}`, "删掉第一行的选项", "Remove the option from the first line"));
|
|
113
|
+
const participants = [];
|
|
114
|
+
const byId = /* @__PURE__ */ new Map();
|
|
115
|
+
const statements = [];
|
|
116
|
+
const stack = [];
|
|
117
|
+
let messages = 0, title;
|
|
118
|
+
let pendingCreate, pendingDestroy;
|
|
119
|
+
const warned = /* @__PURE__ */ new Set();
|
|
120
|
+
const warnOnce = (code, line, ...messages) => {
|
|
121
|
+
if (warned.has(code)) return;
|
|
122
|
+
warned.add(code);
|
|
123
|
+
errors.push(warning(code, lang, line, ...messages));
|
|
124
|
+
};
|
|
125
|
+
const recordWarning = (d) => {
|
|
126
|
+
const key = `${d.code}:${d.message}`;
|
|
127
|
+
if (!warned.has(key)) {
|
|
128
|
+
warned.add(key);
|
|
129
|
+
errors.push(d);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
const label = (raw, line) => {
|
|
133
|
+
const { text, icon, diagnostics } = normalizeLabel(raw.replace(/^(?:no)?wrap\s*:\s*/i, ""), {
|
|
134
|
+
lang,
|
|
135
|
+
line
|
|
136
|
+
});
|
|
137
|
+
diagnostics.forEach(recordWarning);
|
|
138
|
+
return {
|
|
139
|
+
text,
|
|
140
|
+
icon
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
const actorRef = (source, line) => {
|
|
144
|
+
const [id, ...classes] = source.split(":::");
|
|
145
|
+
if (classes.length) look.addClasses(id, classes, line);
|
|
146
|
+
return id;
|
|
147
|
+
};
|
|
148
|
+
const lifecycleError = ({ id, line }, zh, en) => errors.push(issue(lang, line, `${id}:${zh}`, `${id}: ${en}`, "修正下一条消息的参与者", "Correct the participants in the next message"));
|
|
149
|
+
function ensure(id, line, name = id) {
|
|
150
|
+
if (byId.has(id)) return byId.get(id);
|
|
151
|
+
if (participants.length >= LIMITS.participants) {
|
|
152
|
+
errors.push(issue(lang, line, `参与者超过 ${LIMITS.participants} 个`, `More than ${LIMITS.participants} participants`, `删到 ${LIMITS.participants} 个参与者,或拆成两张图`, `Keep at most ${LIMITS.participants} participants, or split the diagram`));
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const actor = {
|
|
156
|
+
id,
|
|
157
|
+
name,
|
|
158
|
+
kind: "participant",
|
|
159
|
+
line,
|
|
160
|
+
index: participants.length
|
|
161
|
+
};
|
|
162
|
+
participants.push(actor);
|
|
163
|
+
byId.set(id, actor);
|
|
164
|
+
return actor;
|
|
165
|
+
}
|
|
166
|
+
function close(line) {
|
|
167
|
+
const open = stack.at(-1);
|
|
168
|
+
if (!open) {
|
|
169
|
+
errors.push(issue(lang, line, "end 没有对应的开始语句", "end has no matching start", "删掉这个 end,或在它前面补上片段、rect 或 box", "Remove this end, or add the fragment, rect, or box it closes"));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
stack.pop();
|
|
173
|
+
statements.push({
|
|
174
|
+
type: "end",
|
|
175
|
+
line,
|
|
176
|
+
target: open
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
const lines = String(source).split("\n");
|
|
180
|
+
function statement(raw, lineNo) {
|
|
181
|
+
if (!raw.trim() || raw.trimStart().startsWith("%%")) return;
|
|
182
|
+
if (/^\s*\t|^\s+\t/.test(raw) || raw.includes(" ")) {
|
|
183
|
+
errors.push(issue(lang, lineNo, "时序图语句不能使用 Tab", "Tabs are not allowed in sequence statements", "把 Tab 换成空格", "Replace tabs with spaces"));
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const line = raw.trim();
|
|
187
|
+
const heading = /^title(?:\s*:\s*|\s+)(.+)$/i.exec(line);
|
|
188
|
+
if (heading) {
|
|
189
|
+
title = label(heading[1], lineNo).text;
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const menu = /^(link|links|properties|details)\s+([^\s:]+)\s*:\s*(.*)$/i.exec(line);
|
|
193
|
+
if (menu) {
|
|
194
|
+
const [, kind, id, body] = menu;
|
|
195
|
+
warnOnce("IGNORED_INTERACTION", lineNo, "已省略参与者菜单和详情界面;仅保留首个安全链接", "Participant menus and details are omitted; only the first safe URL is kept", "把其他必要链接放在图外", "Put other essential links outside the diagram");
|
|
196
|
+
if (!menuLinks.has(id)) {
|
|
197
|
+
let entries = [];
|
|
198
|
+
if (kind.toLowerCase() === "link") {
|
|
199
|
+
const parts = /^(.*?)@\s*(.+)$/.exec(body);
|
|
200
|
+
if (parts) entries = [[parts[1].trim(), parts[2]]];
|
|
201
|
+
} else if (kind.toLowerCase() === "links") try {
|
|
202
|
+
const value = JSON.parse(body);
|
|
203
|
+
if (value && typeof value === "object" && !Array.isArray(value)) entries = Object.entries(value);
|
|
204
|
+
} catch {}
|
|
205
|
+
for (const [tooltip, url] of entries) {
|
|
206
|
+
const href = typeof url === "string" && safeHref(url.trim());
|
|
207
|
+
if (href) {
|
|
208
|
+
menuLinks.set(id, {
|
|
209
|
+
href,
|
|
210
|
+
tooltip
|
|
211
|
+
});
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
if (look.accept(line, lineNo)) return;
|
|
219
|
+
const decl = /^(?:(create)\s+)?(participant|actor)\s+([^\s@]+)(?:\s*@\{([^}]*)\})?(?:\s+as\s+(.+))?$/i.exec(line);
|
|
220
|
+
if (decl) {
|
|
221
|
+
const [, create, keyword, ref, rawMeta, alias] = decl;
|
|
222
|
+
const [, actorId, iconRef] = /^(.*?)::icon\(([^)]+)\)$/.exec(ref) ?? [];
|
|
223
|
+
const id = actorRef(actorId ?? ref, lineNo);
|
|
224
|
+
const meta = metadata(rawMeta);
|
|
225
|
+
if (!validId(id)) {
|
|
226
|
+
errors.push(issue(lang, lineNo, "参与者 ID 无效", "Invalid participant id", "写成 participant ID as 名称", "Write participant ID as Name"));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
if (create && (byId.has(id) || pendingCreate)) {
|
|
230
|
+
lifecycleError({
|
|
231
|
+
id,
|
|
232
|
+
line: lineNo
|
|
233
|
+
}, "重复创建或已有待创建的参与者", "Duplicate or already pending creation");
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const actor = ensure(id, lineNo);
|
|
237
|
+
if (!actor) return;
|
|
238
|
+
const kind = meta.type || keyword.toLowerCase();
|
|
239
|
+
if (!TYPES.has(kind)) warnOnce("DIAGRAM_HINT", lineNo, "未知参与者类型,使用普通方框", "Unknown participant type; using a standard participant", "检查 type", "Check the type value");
|
|
240
|
+
const { text, icon } = label(alias || meta.alias || id, lineNo);
|
|
241
|
+
const explicit = iconRef || meta.icon;
|
|
242
|
+
const resolved = explicit ? resolveIconRef(explicit) : null;
|
|
243
|
+
if (explicit && !resolved) recordWarning(warning("IGNORED_ASSET", lang, lineNo, `未知图标 ${explicit}`, `Unknown icon ${explicit}`, "使用内置图标", "Use a supported icon"));
|
|
244
|
+
Object.assign(actor, {
|
|
245
|
+
name: text,
|
|
246
|
+
icon: resolved || icon,
|
|
247
|
+
kind: TYPES.has(kind) ? kind : "participant",
|
|
248
|
+
declared: true,
|
|
249
|
+
line: lineNo
|
|
250
|
+
});
|
|
251
|
+
if (create) pendingCreate = {
|
|
252
|
+
id,
|
|
253
|
+
line: lineNo
|
|
254
|
+
};
|
|
255
|
+
if (stack.at(-1)?.type === "box") stack.at(-1).members.push(id);
|
|
256
|
+
statements.push({
|
|
257
|
+
type: "participant",
|
|
258
|
+
id,
|
|
259
|
+
line: lineNo
|
|
260
|
+
});
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const destroy = /^destroy\s+(\S+)$/i.exec(line);
|
|
264
|
+
if (destroy) {
|
|
265
|
+
const id = destroy[1];
|
|
266
|
+
if (!byId.has(id) || pendingDestroy) lifecycleError({
|
|
267
|
+
id,
|
|
268
|
+
line: lineNo
|
|
269
|
+
}, "未知参与者或已有待销毁的参与者", "Unknown participant or already pending destruction");
|
|
270
|
+
else pendingDestroy = {
|
|
271
|
+
id,
|
|
272
|
+
line: lineNo
|
|
273
|
+
};
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const numbering = /^autonumber(?:\s+(off|[+-]?\d+(?:\.\d+)?)(?:\s+([+-]?\d+(?:\.\d+)?))?)?$/i.exec(line);
|
|
277
|
+
if (numbering) {
|
|
278
|
+
statements.push({
|
|
279
|
+
type: "numbering",
|
|
280
|
+
start: numbering[1]?.toLowerCase() === "off" ? null : Number(numbering[1] ?? 1),
|
|
281
|
+
step: Number(numbering[2] ?? 1),
|
|
282
|
+
line: lineNo
|
|
283
|
+
});
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (/^activate\s+(\S+)$/i.test(line) || /^deactivate\s+(\S+)$/i.test(line)) {
|
|
287
|
+
const [, word, id] = /^(activate|deactivate)\s+(\S+)$/i.exec(line);
|
|
288
|
+
if (!ensure(id, lineNo)) return;
|
|
289
|
+
statements.push({
|
|
290
|
+
type: word.toLowerCase(),
|
|
291
|
+
id,
|
|
292
|
+
line: lineNo
|
|
293
|
+
});
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const note = /^Note\s+(over\s+(\S+?)(?:\s*,\s*(\S+))?|left\s+of\s+(\S+)|right\s+of\s+(\S+))\s*:\s*(.*)$/i.exec(line);
|
|
297
|
+
if (note) {
|
|
298
|
+
const ids = note[2] ? [note[2], note[3]].filter(Boolean) : [note[4] || note[5]];
|
|
299
|
+
if (ids.some((id) => !ensure(id, lineNo))) return;
|
|
300
|
+
statements.push({
|
|
301
|
+
type: "note",
|
|
302
|
+
place: note[2] ? "over" : note[4] ? "left" : "right",
|
|
303
|
+
ids,
|
|
304
|
+
...label(note[6].trim(), lineNo),
|
|
305
|
+
line: lineNo
|
|
306
|
+
});
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const block = /^(alt|opt|loop|par_over|par|critical|break|rect|box|else|and|option|end)(?:\s+(.*)|$)/i.exec(line);
|
|
310
|
+
if (block) {
|
|
311
|
+
const word = block[1].toLowerCase().replace("par_over", "par");
|
|
312
|
+
let rest = (block[2] || "").trim();
|
|
313
|
+
let color, status;
|
|
314
|
+
if (word === "box" || word === "rect") {
|
|
315
|
+
color = colorPattern.exec(rest)?.[0] || /^transparent\b/i.exec(rest)?.[0];
|
|
316
|
+
const token = rest.split(/\s/, 1)[0];
|
|
317
|
+
if (!color && (roleOfColor(token) || /^(?:black|white|(?:light|dark|slate|dim)?gr[ae]y|silver)$/i.test(token))) color = token;
|
|
318
|
+
if (color) {
|
|
319
|
+
status = roleOfColor(color);
|
|
320
|
+
if (!status && !/^transparent$/i.test(color) && !ignoredColors.has(color)) ignoredColors.set(color, lineNo);
|
|
321
|
+
rest = rest.slice(color.length).trim();
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (word === "end") {
|
|
325
|
+
if (rest) errors.push(issue(lang, lineNo, "end 后面不能再写内容", "end cannot have trailing text", "这一行只写 end", "Leave only end on this line"));
|
|
326
|
+
close(lineNo);
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
const { text, icon } = label(rest, lineNo);
|
|
330
|
+
if (SECTIONS.has(word)) {
|
|
331
|
+
const open = stack.at(-1);
|
|
332
|
+
const allowed = {
|
|
333
|
+
else: ["alt"],
|
|
334
|
+
and: ["par"],
|
|
335
|
+
option: ["critical"]
|
|
336
|
+
}[word];
|
|
337
|
+
if (!open || !allowed.includes(open.keyword)) {
|
|
338
|
+
errors.push(issue(lang, lineNo, `「${word}」不能出现在当前片段里`, `"${word}" is not valid in the current fragment`, `把它放到对应的 ${allowed.join("/")} 片段中`, `Place it inside ${allowed.join("/")} before end`));
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
statements.push({
|
|
342
|
+
type: "section",
|
|
343
|
+
keyword: word,
|
|
344
|
+
text,
|
|
345
|
+
icon,
|
|
346
|
+
line: lineNo,
|
|
347
|
+
owner: open
|
|
348
|
+
});
|
|
349
|
+
open.sections.push({
|
|
350
|
+
keyword: word,
|
|
351
|
+
text,
|
|
352
|
+
icon,
|
|
353
|
+
line: lineNo
|
|
354
|
+
});
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (stack.length >= LIMITS.fragmentDepth) {
|
|
358
|
+
errors.push(issue(lang, lineNo, `组合片段最多嵌套 ${LIMITS.fragmentDepth} 层`, `Fragments nest at most ${LIMITS.fragmentDepth} levels`, "把内层片段拆到另一张图", "Move the inner fragment to another diagram"));
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const node = {
|
|
362
|
+
type: word === "box" ? "box" : word === "rect" ? "rect" : "fragment",
|
|
363
|
+
keyword: word,
|
|
364
|
+
text,
|
|
365
|
+
icon,
|
|
366
|
+
color,
|
|
367
|
+
status,
|
|
368
|
+
line: lineNo,
|
|
369
|
+
kind: word === "box" || word === "rect" ? word : "fragment",
|
|
370
|
+
sections: [],
|
|
371
|
+
members: []
|
|
372
|
+
};
|
|
373
|
+
statements.push(node);
|
|
374
|
+
stack.push(node);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const colon = Array.from(line.matchAll(/:::[\w-]+|:/g)).find((match) => match[0] === ":")?.index;
|
|
378
|
+
const hasLabel = colon !== void 0;
|
|
379
|
+
const address = hasLabel ? line.slice(0, colon) : line;
|
|
380
|
+
const match = arrowPattern.exec(address);
|
|
381
|
+
if (match?.index > 0) {
|
|
382
|
+
const arrow = ARROWS.get(match[0].toLowerCase());
|
|
383
|
+
const left = /^(.*?)\s*(\(\))?\s*(\+)?$/.exec(address.slice(0, match.index).trim());
|
|
384
|
+
const right = /^([+-])?\s*(\(\))?\s*(.*?)\s*(\+)?$/.exec(address.slice(match.index + match[0].length).trim());
|
|
385
|
+
const [, sender, centralStart, before] = left;
|
|
386
|
+
const from = actorRef(sender, lineNo);
|
|
387
|
+
const [, prefix, centralEnd, to, after] = right;
|
|
388
|
+
const signal = prefix || before || after || (to.endsWith("-") && !byId.has(to) ? "-" : "");
|
|
389
|
+
const recipient = actorRef(signal === "-" && !prefix && to.endsWith("-") ? to.slice(0, -1) : to, lineNo);
|
|
390
|
+
if (!validId(from) || !validId(recipient)) {
|
|
391
|
+
errors.push(issue(lang, lineNo, "消息的参与者写法无效", "Invalid message participants", "写成 A->>B: 消息,自调用写成 A->>A: 消息", "Write A->>B: message, or A->>A: message for a self call"));
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
if (!ensure(from, lineNo) || !ensure(recipient, lineNo)) return;
|
|
395
|
+
let create, destroy;
|
|
396
|
+
if (pendingCreate) {
|
|
397
|
+
if (pendingCreate.id === recipient) create = recipient;
|
|
398
|
+
else lifecycleError(pendingCreate, "下一条消息必须发给新参与者", "The next message must target the new participant");
|
|
399
|
+
pendingCreate = void 0;
|
|
400
|
+
} else if (pendingDestroy) {
|
|
401
|
+
if (pendingDestroy.id === from || pendingDestroy.id === recipient) destroy = pendingDestroy.id;
|
|
402
|
+
else lifecycleError(pendingDestroy, "下一条消息必须涉及待销毁的参与者", "The next message must involve the destroyed participant");
|
|
403
|
+
pendingDestroy = void 0;
|
|
404
|
+
}
|
|
405
|
+
messages += 1;
|
|
406
|
+
if (messages > LIMITS.messages) {
|
|
407
|
+
errors.push(issue(lang, lineNo, `消息超过 ${LIMITS.messages} 条`, `More than ${LIMITS.messages} messages`, `删到 ${LIMITS.messages} 条,或拆成两张图`, `Keep at most ${LIMITS.messages} messages, or split the diagram`));
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const activateOn = signal === "+" ? recipient : "";
|
|
411
|
+
const deactivateOn = signal === "-" ? from : "";
|
|
412
|
+
statements.push({
|
|
413
|
+
type: "message",
|
|
414
|
+
from,
|
|
415
|
+
to: recipient,
|
|
416
|
+
style: arrow.style,
|
|
417
|
+
arrow,
|
|
418
|
+
centralStart: Boolean(centralStart),
|
|
419
|
+
centralEnd: Boolean(centralEnd),
|
|
420
|
+
create,
|
|
421
|
+
destroy,
|
|
422
|
+
...label(hasLabel ? line.slice(colon + 1).trim() : "", lineNo),
|
|
423
|
+
line: lineNo,
|
|
424
|
+
activate: Boolean(activateOn),
|
|
425
|
+
deactivate: Boolean(deactivateOn),
|
|
426
|
+
activateOn,
|
|
427
|
+
deactivateOn
|
|
428
|
+
});
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
errors.push(issue(lang, lineNo, `无法识别的语句「${line.slice(0, 40)}」`, `Unrecognized statement: ${line.slice(0, 40)}`, "改成 participant、消息、Note、片段、rect、box 或 end", "Use participant, a message, Note, a fragment, rect, box, or end"));
|
|
432
|
+
}
|
|
433
|
+
lines.forEach((raw, index) => {
|
|
434
|
+
if (/^\s*(?:style|classDef|linkStyle)\s/i.test(raw) && look.accept(raw, firstLine + index)) return;
|
|
435
|
+
for (const part of splitLine(raw)) statement(part, firstLine + index);
|
|
436
|
+
});
|
|
437
|
+
for (const pending of [pendingCreate, pendingDestroy]) if (pending) lifecycleError(pending, "缺少后续消息", "Missing a following message");
|
|
438
|
+
for (const open of stack) errors.push(issue(lang, open.line, `「${open.keyword}」缺少 end`, `"${open.keyword}" is missing end`, `在第 ${lines.length ? firstLine + lines.length - 1 : open.line} 行后补上 end`, "Add end after the block"));
|
|
439
|
+
const depth = /* @__PURE__ */ new Map();
|
|
440
|
+
for (const item of statements) {
|
|
441
|
+
if (item.type === "activate") depth.set(item.id, (depth.get(item.id) || 0) + 1);
|
|
442
|
+
if (item.type === "message" && item.activateOn) depth.set(item.activateOn, (depth.get(item.activateOn) || 0) + 1);
|
|
443
|
+
if (item.type === "deactivate" || item.type === "message" && item.deactivateOn) {
|
|
444
|
+
const id = item.type === "deactivate" ? item.id : item.deactivateOn;
|
|
445
|
+
const next = (depth.get(id) || 0) - 1;
|
|
446
|
+
if (next < 0) errors.push(issue(lang, item.line, `「${id}」的激活条没有配对的开始`, `Activation of "${id}" has no matching start`, `在这条之前写 activate ${id},或给进入消息加 +`, `Write activate ${id} before this line, or add + to the incoming message`));
|
|
447
|
+
depth.set(id, Math.max(0, next));
|
|
448
|
+
}
|
|
449
|
+
if (item.destroy) depth.delete(item.destroy);
|
|
450
|
+
}
|
|
451
|
+
if (!participants.length) errors.push(issue(lang, firstLine, "图中没有参与者", "The diagram has no participants", "写 participant,或写一条 A->>B: 消息", "Add a participant or a message such as A->>B: text"));
|
|
452
|
+
for (const actor of participants) {
|
|
453
|
+
actor.status = look.statusOf(actor.id);
|
|
454
|
+
actor.link = menuLinks.get(actor.id) ?? look.linkOf(actor.id);
|
|
455
|
+
}
|
|
456
|
+
let edgeIndex = 0;
|
|
457
|
+
for (const item of statements) if (item.type === "message") item.status = look.edgeStatusOf(edgeIndex++);
|
|
458
|
+
errors.push(...look.diagnostics());
|
|
459
|
+
if (ignoredColors.size) {
|
|
460
|
+
const colors = [...ignoredColors.keys()].join(", ");
|
|
461
|
+
errors.push(warning("IGNORED_STYLE", lang, ignoredColors.values().next().value, `区段颜色使用中性色:${colors}`, `Region colors rendered as neutral: ${colors}`, "使用有彩度的颜色", "Use chromatic colors"));
|
|
462
|
+
}
|
|
463
|
+
return {
|
|
464
|
+
model: {
|
|
465
|
+
type: "sequence",
|
|
466
|
+
participants,
|
|
467
|
+
statements,
|
|
468
|
+
title,
|
|
469
|
+
messages
|
|
470
|
+
},
|
|
471
|
+
errors
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
var ICON_SPACE = 20;
|
|
475
|
+
var MESSAGE_PADDING = 36;
|
|
476
|
+
var MESSAGE_END_GAP = 12;
|
|
477
|
+
var iconSpace = (item) => item.icon ? ICON_SPACE : 0;
|
|
478
|
+
function layout(model, { measure, width, lang }, info = {}) {
|
|
479
|
+
const config = info.config?.sequence;
|
|
480
|
+
const mirrorActors = config?.mirrorActors !== false;
|
|
481
|
+
const errors = [];
|
|
482
|
+
const actors = model.participants.map((item) => ({ ...item }));
|
|
483
|
+
const actorAt = (id) => actors.find((item) => item.id === id);
|
|
484
|
+
const labelSize = (text, font, glyph) => {
|
|
485
|
+
const lines = wrapText(text, 168, font, measure).slice(0, LIMITS.lines);
|
|
486
|
+
return {
|
|
487
|
+
lines,
|
|
488
|
+
w: snap(Math.max(64, maxWidthOf(lines, font, measure) + 24)),
|
|
489
|
+
h: snap(Math.max(32, lines.length * 18 + 14 + (glyph ? 24 : 0)))
|
|
490
|
+
};
|
|
491
|
+
};
|
|
492
|
+
for (const actor of actors) {
|
|
493
|
+
const glyph = actor.kind !== "participant" || Boolean(actor.icon);
|
|
494
|
+
actor.head = labelSize(actor.name, FONT.title, glyph);
|
|
495
|
+
actor.foot = { ...actor.head };
|
|
496
|
+
if (wrapText(actor.name, 168, FONT.title, measure).length > LIMITS.lines) errors.push(issue(lang, actor.line, `参与者「${actor.name}」超过 ${LIMITS.lines} 行`, `Participant "${actor.name}" exceeds ${LIMITS.lines} lines`, "缩短名称,说明移到图外", "Shorten the name and move the description outside"));
|
|
497
|
+
}
|
|
498
|
+
const columnBudget = (Math.max(320, width) - 16) / (actors.length + 1);
|
|
499
|
+
const labelLimit = Math.round(Math.max(96, Math.min(200, columnBudget)));
|
|
500
|
+
const noteLimit = Math.round(Math.max(96, Math.min(180, columnBudget)));
|
|
501
|
+
const events = [];
|
|
502
|
+
const open = [];
|
|
503
|
+
const push = (event) => {
|
|
504
|
+
events.push(event);
|
|
505
|
+
};
|
|
506
|
+
let number = config?.showSequenceNumbers === true ? 1 : null, step = 1;
|
|
507
|
+
const sizeMessage = (event, limit) => {
|
|
508
|
+
const lines = event.text || event.icon ? wrapText(event.text, Math.max(1, limit - iconSpace(event)), FONT.label, measure) : [];
|
|
509
|
+
const above = (lines.length ? lines.length * 16 + 8 : 0) + (event.ordinal ? 16 : 0);
|
|
510
|
+
const createdHeight = event.create ? actorAt(event.create).head.h / 2 : 0;
|
|
511
|
+
const selfReserve = event.from === event.to ? event.ordinal ? 12 : !lines.length ? 8 : 0 : 0;
|
|
512
|
+
const lineOffset = Math.max(above + (lines.length ? 2 : 10) + selfReserve, createdHeight + 8);
|
|
513
|
+
const h = snap(Math.max(above + 20, lineOffset + createdHeight + 12));
|
|
514
|
+
Object.assign(event, {
|
|
515
|
+
lines,
|
|
516
|
+
h,
|
|
517
|
+
lineOffset,
|
|
518
|
+
textW: Math.max(maxWidthOf(lines, FONT.label, measure) + iconSpace(event), measure(event.ordinal, FONT.label))
|
|
519
|
+
});
|
|
520
|
+
};
|
|
521
|
+
for (const item of model.statements) if (item.type === "box" || item.type === "rect" || item.type === "fragment") {
|
|
522
|
+
const h = item.type === "box" ? 8 : item.type === "rect" && !item.text && !item.icon ? 8 : 28;
|
|
523
|
+
push({
|
|
524
|
+
...item,
|
|
525
|
+
kind: "open",
|
|
526
|
+
h,
|
|
527
|
+
depth: open.length
|
|
528
|
+
});
|
|
529
|
+
open.push(events.at(-1));
|
|
530
|
+
} else if (item.type === "section") push({
|
|
531
|
+
...item,
|
|
532
|
+
host: open.at(-1),
|
|
533
|
+
kind: "section",
|
|
534
|
+
h: 28
|
|
535
|
+
});
|
|
536
|
+
else if (item.type === "end") {
|
|
537
|
+
const started = open.pop();
|
|
538
|
+
push({
|
|
539
|
+
...item,
|
|
540
|
+
kind: "close",
|
|
541
|
+
h: 12,
|
|
542
|
+
depth: open.length
|
|
543
|
+
});
|
|
544
|
+
if (started) started.close = events.at(-1);
|
|
545
|
+
} else if (item.type === "note") {
|
|
546
|
+
const font = FONT.label;
|
|
547
|
+
const span = item.place === "over" ? Math.abs(actorAt(item.ids[0]).index - actorAt(item.ids.at(-1)).index) : 0;
|
|
548
|
+
const limit = Math.max(noteLimit, Math.min(180, span * 132));
|
|
549
|
+
const wrapped = wrapText(item.text, limit, font, measure);
|
|
550
|
+
if (wrapped.length > LIMITS.lines) errors.push(issue(lang, item.line, `注释超过 ${LIMITS.lines} 行`, `Note exceeds ${LIMITS.lines} lines`, "缩短注释,或拆成多条 Note", "Shorten the note or split it into several Note lines"));
|
|
551
|
+
const lines = wrapped.slice(0, LIMITS.lines);
|
|
552
|
+
push({
|
|
553
|
+
...item,
|
|
554
|
+
kind: "note",
|
|
555
|
+
lines,
|
|
556
|
+
h: snap(Math.max(lines.length, item.icon ? 1 : 0) * 16 + 16),
|
|
557
|
+
textW: maxWidthOf(lines, font, measure) + iconSpace(item)
|
|
558
|
+
});
|
|
559
|
+
} else if (item.type === "participant") push({
|
|
560
|
+
...item,
|
|
561
|
+
kind: "participant",
|
|
562
|
+
h: 0
|
|
563
|
+
});
|
|
564
|
+
else if (item.type === "activate" || item.type === "deactivate") push({
|
|
565
|
+
...item,
|
|
566
|
+
kind: item.type,
|
|
567
|
+
h: 8
|
|
568
|
+
});
|
|
569
|
+
else if (item.type === "numbering") {
|
|
570
|
+
number = item.start;
|
|
571
|
+
step = item.step;
|
|
572
|
+
} else if (item.type === "message") {
|
|
573
|
+
const ordinal = number == null ? "" : String(Number(number.toPrecision(15)));
|
|
574
|
+
if (number != null) number += step;
|
|
575
|
+
const event = {
|
|
576
|
+
...item,
|
|
577
|
+
kind: "message",
|
|
578
|
+
ordinal
|
|
579
|
+
};
|
|
580
|
+
const span = Math.abs(actorAt(item.from).index - actorAt(item.to).index);
|
|
581
|
+
sizeMessage(event, span ? Math.max(labelLimit, Math.min(200, span * 132)) : Math.min(168, labelLimit));
|
|
582
|
+
push(event);
|
|
583
|
+
}
|
|
584
|
+
if (errors.length) return {
|
|
585
|
+
width,
|
|
586
|
+
height: 0,
|
|
587
|
+
errors
|
|
588
|
+
};
|
|
589
|
+
const count = actors.length;
|
|
590
|
+
const indexOf = new Map(events.map((event, index) => [event, index]));
|
|
591
|
+
const contained = /* @__PURE__ */ new Map();
|
|
592
|
+
for (const event of events) {
|
|
593
|
+
if (event.kind !== "open" || !event.close) continue;
|
|
594
|
+
const inside = [];
|
|
595
|
+
for (let i = indexOf.get(event) + 1; i < indexOf.get(event.close); i += 1) inside.push(events[i]);
|
|
596
|
+
contained.set(event, inside);
|
|
597
|
+
}
|
|
598
|
+
const rangeOf = (ids) => {
|
|
599
|
+
const indexes = ids.map((id) => actorAt(id).index);
|
|
600
|
+
return [Math.min(...indexes), Math.max(...indexes)];
|
|
601
|
+
};
|
|
602
|
+
for (const event of events) {
|
|
603
|
+
if (event.kind !== "open") continue;
|
|
604
|
+
const seen = event.type === "box" ? [...event.members || []] : [];
|
|
605
|
+
if (event.type !== "box") {
|
|
606
|
+
for (const inner of contained.get(event) || []) if (inner.kind === "message" || inner.kind === "note") seen.push(...inner.kind === "note" ? inner.ids : [inner.from, inner.to]);
|
|
607
|
+
}
|
|
608
|
+
const ids = [...new Set(seen)];
|
|
609
|
+
if (!ids.length) {
|
|
610
|
+
event.lo = 0;
|
|
611
|
+
event.hi = count - 1;
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
[event.lo, event.hi] = rangeOf(ids);
|
|
615
|
+
}
|
|
616
|
+
if (errors.length) return {
|
|
617
|
+
width,
|
|
618
|
+
height: 0,
|
|
619
|
+
errors
|
|
620
|
+
};
|
|
621
|
+
const headHalf = actors.map((actor) => Math.max(actor.head.w, actor.foot.w) / 2);
|
|
622
|
+
const base = new Array(count + 1).fill(0);
|
|
623
|
+
const floor = new Array(count + 1).fill(0);
|
|
624
|
+
const bump = (index, value) => {
|
|
625
|
+
if (index >= 0 && index <= count) base[index] = Math.max(base[index], value);
|
|
626
|
+
};
|
|
627
|
+
const shared = (lo, hi, value) => {
|
|
628
|
+
const parts = hi - lo;
|
|
629
|
+
for (let i = lo + 1; i <= hi; i += 1) bump(i, value / parts);
|
|
630
|
+
};
|
|
631
|
+
actors.forEach((actor, i) => {
|
|
632
|
+
bump(i, headHalf[i] + 12);
|
|
633
|
+
bump(i + 1, headHalf[i] + 12);
|
|
634
|
+
floor[i] = Math.max(floor[i], headHalf[i] + 12);
|
|
635
|
+
floor[i + 1] = Math.max(floor[i + 1], headHalf[i] + 12);
|
|
636
|
+
if (i > 0) {
|
|
637
|
+
bump(i, headHalf[i - 1] + headHalf[i] + 16);
|
|
638
|
+
floor[i] = Math.max(floor[i], headHalf[i - 1] + headHalf[i] + 16);
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
for (let i = 1; i < count; i += 1) floor[i] = Math.max(floor[i], 24);
|
|
642
|
+
const activeDepths = /* @__PURE__ */ new Map();
|
|
643
|
+
const leftExtent = [...headHalf];
|
|
644
|
+
for (const event of events) {
|
|
645
|
+
const start = event.activateOn || event.type === "activate" && event.id;
|
|
646
|
+
const stop = event.deactivateOn || event.type === "deactivate" && event.id;
|
|
647
|
+
if (start) {
|
|
648
|
+
const depth = activeDepths.get(start) || 0;
|
|
649
|
+
activeDepths.set(start, depth + 1);
|
|
650
|
+
const i = actorAt(start).index;
|
|
651
|
+
leftExtent[i] = Math.max(leftExtent[i], 5 + depth * 7);
|
|
652
|
+
}
|
|
653
|
+
if (stop) activeDepths.set(stop, (activeDepths.get(stop) || 1) - 1);
|
|
654
|
+
if (event.destroy) activeDepths.delete(event.destroy);
|
|
655
|
+
}
|
|
656
|
+
for (let i = 0; i < count; i += 1) if (leftExtent[i] > headHalf[i]) bump(i, leftExtent[i] + (i > 0 ? headHalf[i - 1] + 16 : 12));
|
|
657
|
+
for (const event of events) if (event.kind === "note") {
|
|
658
|
+
if (event.place !== "over") bump(event.place === "left" ? actorAt(event.ids[0]).index : actorAt(event.ids.at(-1)).index + 1, event.textW + 28);
|
|
659
|
+
else {
|
|
660
|
+
const [lo, hi] = rangeOf(event.ids);
|
|
661
|
+
if (lo === hi) bump(hi + 1, event.textW + 44);
|
|
662
|
+
else shared(lo, hi, event.textW + 28);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
const messagePadding = (event) => MESSAGE_PADDING + (event.create ? actorAt(event.create).head.w / 2 : 0);
|
|
666
|
+
for (const event of events) if (event.kind === "message") {
|
|
667
|
+
const [lo, hi] = rangeOf([event.from, event.to]);
|
|
668
|
+
if (lo === hi) bump(lo + 1, event.textW + 60);
|
|
669
|
+
else {
|
|
670
|
+
const span = base.slice(lo + 1, hi + 1).reduce((sum, gap) => sum + gap, 0);
|
|
671
|
+
const deficit = event.textW + messagePadding(event) - span;
|
|
672
|
+
if (deficit > 0) for (let i = lo + 1; i <= hi; i++) base[i] += deficit / (hi - lo);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
const gaps = base.map((value, i) => Math.max(value, floor[i]));
|
|
676
|
+
const xsOf = () => {
|
|
677
|
+
const xs = [];
|
|
678
|
+
let cursor = gaps[0];
|
|
679
|
+
for (let i = 0; i < count; i += 1) {
|
|
680
|
+
xs.push(cursor);
|
|
681
|
+
cursor += gaps[i + 1];
|
|
682
|
+
}
|
|
683
|
+
return xs;
|
|
684
|
+
};
|
|
685
|
+
const noteBox = (event, xs) => {
|
|
686
|
+
const a = xs[actorAt(event.ids[0]).index], b = xs[actorAt(event.ids.at(-1)).index];
|
|
687
|
+
const w = snap(event.textW + 16);
|
|
688
|
+
const h = event.h - 8;
|
|
689
|
+
if (event.place === "over" && a !== b) {
|
|
690
|
+
const span = Math.max(w, Math.abs(a - b) + 24);
|
|
691
|
+
return {
|
|
692
|
+
x: (a + b - span) / 2,
|
|
693
|
+
w: span,
|
|
694
|
+
h
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
return {
|
|
698
|
+
x: event.place === "left" ? a - w - 16 : a + 16,
|
|
699
|
+
w,
|
|
700
|
+
h
|
|
701
|
+
};
|
|
702
|
+
};
|
|
703
|
+
const sectionLabel = (event) => [event.keyword, event.text].filter(Boolean).join(" ");
|
|
704
|
+
const frameLabel = (event) => event.type !== "fragment" ? event.text : `${event.keyword}${event.text ? ` [${event.text}]` : ""}`;
|
|
705
|
+
const geometryOf = (xs) => {
|
|
706
|
+
const notes = /* @__PURE__ */ new Map();
|
|
707
|
+
for (const event of events) if (event.kind === "note") notes.set(event, noteBox(event, xs));
|
|
708
|
+
const frames = /* @__PURE__ */ new Map();
|
|
709
|
+
for (let i = events.length - 1; i >= 0; i -= 1) {
|
|
710
|
+
const event = events[i];
|
|
711
|
+
if (event.kind !== "open" || event.lo == null) continue;
|
|
712
|
+
const pad = 18 + event.depth * 12;
|
|
713
|
+
let left = xs[event.lo] - (event.type === "box" ? headHalf[event.lo] + 4 : pad);
|
|
714
|
+
let right = xs[event.hi] + (event.type === "box" ? headHalf[event.hi] + 4 : pad);
|
|
715
|
+
for (const inner of contained.get(event) || []) if (inner.kind === "message") {
|
|
716
|
+
const a = xs[actorAt(inner.from).index], b = xs[actorAt(inner.to).index];
|
|
717
|
+
if (inner.create) {
|
|
718
|
+
const half = actorAt(inner.create).head.w / 2;
|
|
719
|
+
left = Math.min(left, b - half - 8);
|
|
720
|
+
right = Math.max(right, b + half + 8);
|
|
721
|
+
}
|
|
722
|
+
if (a === b) right = Math.max(right, a + 52 + inner.textW + 8);
|
|
723
|
+
else {
|
|
724
|
+
const mid = (a + b) / 2;
|
|
725
|
+
left = Math.min(left, mid - inner.textW / 2 - 4);
|
|
726
|
+
right = Math.max(right, mid + inner.textW / 2 + 4);
|
|
727
|
+
}
|
|
728
|
+
} else if (inner.kind === "note" && notes.has(inner)) {
|
|
729
|
+
const note = notes.get(inner);
|
|
730
|
+
left = Math.min(left, note.x);
|
|
731
|
+
right = Math.max(right, note.x + note.w);
|
|
732
|
+
} else if (inner.kind === "open" && frames.has(inner)) {
|
|
733
|
+
left = Math.min(left, frames.get(inner).left - 6);
|
|
734
|
+
right = Math.max(right, frames.get(inner).right + 6);
|
|
735
|
+
}
|
|
736
|
+
const labelWidths = [measure(frameLabel(event), FONT.label) + iconSpace(event), ...event.sections.map((section) => measure(sectionLabel(section), FONT.label) + iconSpace(section))];
|
|
737
|
+
right = Math.max(right, left + 20 + Math.max(...labelWidths));
|
|
738
|
+
frames.set(event, {
|
|
739
|
+
left,
|
|
740
|
+
right
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
let left = xs[0] - leftExtent[0], right = xs[count - 1] + headHalf[count - 1];
|
|
744
|
+
for (const frame of frames.values()) {
|
|
745
|
+
left = Math.min(left, frame.left);
|
|
746
|
+
right = Math.max(right, frame.right);
|
|
747
|
+
}
|
|
748
|
+
for (const note of notes.values()) {
|
|
749
|
+
left = Math.min(left, note.x);
|
|
750
|
+
right = Math.max(right, note.x + note.w);
|
|
751
|
+
}
|
|
752
|
+
for (const event of events) {
|
|
753
|
+
if (event.kind !== "message") continue;
|
|
754
|
+
const a = xs[actorAt(event.from).index], b = xs[actorAt(event.to).index];
|
|
755
|
+
if (a === b) right = Math.max(right, a + 52 + event.textW + 8);
|
|
756
|
+
else {
|
|
757
|
+
const mid = (a + b) / 2;
|
|
758
|
+
left = Math.min(left, mid - event.textW / 2 - 4);
|
|
759
|
+
right = Math.max(right, mid + event.textW / 2 + 4);
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
return {
|
|
763
|
+
notes,
|
|
764
|
+
frames,
|
|
765
|
+
left,
|
|
766
|
+
right
|
|
767
|
+
};
|
|
768
|
+
};
|
|
769
|
+
let xs = xsOf();
|
|
770
|
+
let geometry = geometryOf(xs);
|
|
771
|
+
for (let pass = 0; pass < 4; pass += 1) {
|
|
772
|
+
const slack = Math.floor(width) - 16 - (geometry.right - geometry.left);
|
|
773
|
+
if (slack < 1) break;
|
|
774
|
+
const add = Math.min(slack / (count + 1), 128);
|
|
775
|
+
if (add < 1) break;
|
|
776
|
+
for (let i = 0; i <= count; i += 1) gaps[i] += add;
|
|
777
|
+
xs = xsOf();
|
|
778
|
+
geometry = geometryOf(xs);
|
|
779
|
+
}
|
|
780
|
+
for (const event of events) if (event.kind === "message") {
|
|
781
|
+
const [lo, hi] = rangeOf([event.from, event.to]);
|
|
782
|
+
if (lo !== hi) sizeMessage(event, xs[hi] - xs[lo] - messagePadding(event) + 1e-6);
|
|
783
|
+
if (event.lines.length > LIMITS.lines) errors.push(issue(lang, event.line, `消息「${event.text}」超过 ${LIMITS.lines} 行`, `Message "${event.text}" exceeds ${LIMITS.lines} lines`, "缩短消息,说明移到图外", "Shorten the message and move details outside"));
|
|
784
|
+
}
|
|
785
|
+
if (errors.length) return {
|
|
786
|
+
width,
|
|
787
|
+
height: 0,
|
|
788
|
+
errors
|
|
789
|
+
};
|
|
790
|
+
geometry = geometryOf(xs);
|
|
791
|
+
const shift = 8 - geometry.left;
|
|
792
|
+
xs = xs.map((value) => value + shift);
|
|
793
|
+
geometry = geometryOf(xs);
|
|
794
|
+
actors.forEach((actor, i) => {
|
|
795
|
+
actor.x = xs[i];
|
|
796
|
+
});
|
|
797
|
+
for (const [event, note] of geometry.notes) event.note = note;
|
|
798
|
+
for (const [event, frame] of geometry.frames) {
|
|
799
|
+
event.left = frame.left;
|
|
800
|
+
event.right = frame.right;
|
|
801
|
+
}
|
|
802
|
+
let contentRight = geometry.right;
|
|
803
|
+
const hasBox = events.some((event) => event.type === "box");
|
|
804
|
+
const headBand = snap(8 + Math.max(36, ...actors.map((actor) => actor.head.h)) + 12);
|
|
805
|
+
const top = headBand + (hasBox ? 28 : 8);
|
|
806
|
+
let cursor = top;
|
|
807
|
+
for (const event of events) {
|
|
808
|
+
event.y = cursor;
|
|
809
|
+
cursor += event.h;
|
|
810
|
+
}
|
|
811
|
+
const foot = mirrorActors ? snap(Math.max(...actors.map((actor) => actor.foot.h)) + 12) : 0;
|
|
812
|
+
const bottom = cursor + 8;
|
|
813
|
+
const height = bottom + foot + 8;
|
|
814
|
+
for (const event of events) {
|
|
815
|
+
if (event.kind !== "open" || !event.close) continue;
|
|
816
|
+
const boxY = event.type === "box" ? headBand + 20 : event.y;
|
|
817
|
+
const boxH = (event.type === "box" ? bottom + 4 : event.close.y + event.close.h) - boxY;
|
|
818
|
+
event.box = {
|
|
819
|
+
x: event.left,
|
|
820
|
+
y: boxY,
|
|
821
|
+
w: event.right - event.left,
|
|
822
|
+
h: boxH
|
|
823
|
+
};
|
|
824
|
+
}
|
|
825
|
+
for (const event of events) if (event.note) event.note.y = event.y + 4;
|
|
826
|
+
const activations = [];
|
|
827
|
+
const active = new Map(actors.map((actor) => [actor.id, []]));
|
|
828
|
+
const messageY = (event) => event.y + event.lineOffset;
|
|
829
|
+
const creations = /* @__PURE__ */ new Map(), destructions = /* @__PURE__ */ new Map();
|
|
830
|
+
const start = (id, at) => {
|
|
831
|
+
const stack = active.get(id);
|
|
832
|
+
const bar = {
|
|
833
|
+
id,
|
|
834
|
+
x: actorAt(id).x - 5 - stack.length * 7,
|
|
835
|
+
y: at,
|
|
836
|
+
h: 0
|
|
837
|
+
};
|
|
838
|
+
stack.push(bar);
|
|
839
|
+
activations.push(bar);
|
|
840
|
+
};
|
|
841
|
+
const stop = (id, at, minHeight = 16) => {
|
|
842
|
+
const bar = active.get(id).pop();
|
|
843
|
+
if (bar) bar.h = Math.max(minHeight, at - bar.y);
|
|
844
|
+
};
|
|
845
|
+
for (const event of events) {
|
|
846
|
+
if (event.type === "activate") start(event.id, event.y + 4);
|
|
847
|
+
if (event.type === "deactivate") stop(event.id, event.y + 4);
|
|
848
|
+
if (event.kind !== "message") continue;
|
|
849
|
+
const y = messageY(event);
|
|
850
|
+
if (event.create) creations.set(event.create, y);
|
|
851
|
+
if (event.destroy) destructions.set(event.destroy, y);
|
|
852
|
+
if (event.activateOn) start(event.activateOn, y + (event.create ? actorAt(event.create).head.h / 2 : 0));
|
|
853
|
+
event.fromBar = active.get(event.from).at(-1);
|
|
854
|
+
event.toBar = active.get(event.to).at(-1);
|
|
855
|
+
if (event.deactivateOn && event.destroy !== event.deactivateOn) stop(event.deactivateOn, y + 8);
|
|
856
|
+
if (event.destroy) while (active.get(event.destroy).length) stop(event.destroy, y, 0);
|
|
857
|
+
}
|
|
858
|
+
const barsByX = [...activations].sort((a, b) => a.x - b.x);
|
|
859
|
+
const labelXOf = (box, y, width) => {
|
|
860
|
+
let x = box.x + 8;
|
|
861
|
+
for (const bar of barsByX) {
|
|
862
|
+
if (bar.y > y + 18 || bar.y + bar.h < y) continue;
|
|
863
|
+
const right = bar.x + 16;
|
|
864
|
+
if (bar.x < x + width && right > x) x = right;
|
|
865
|
+
}
|
|
866
|
+
return x;
|
|
867
|
+
};
|
|
868
|
+
for (const event of events) {
|
|
869
|
+
if (event.kind === "open" && event.box && event.type !== "box") {
|
|
870
|
+
const text = frameLabel(event);
|
|
871
|
+
if (!text && !event.icon) continue;
|
|
872
|
+
const w = measure(text, FONT.label) + iconSpace(event);
|
|
873
|
+
event.labelX = labelXOf(event.box, event.box.y, w);
|
|
874
|
+
event.box.w = Math.max(event.box.w, event.labelX + w + 12 - event.box.x);
|
|
875
|
+
contentRight = Math.max(contentRight, event.box.x + event.box.w);
|
|
876
|
+
}
|
|
877
|
+
if (event.kind === "section") {
|
|
878
|
+
const { host } = event;
|
|
879
|
+
if (!host?.box) continue;
|
|
880
|
+
const w = measure(sectionLabel(event), FONT.label) + iconSpace(event);
|
|
881
|
+
event.labelX = labelXOf(host.box, event.y, w);
|
|
882
|
+
host.box.w = Math.max(host.box.w, event.labelX + w + 12 - host.box.x);
|
|
883
|
+
contentRight = Math.max(contentRight, host.box.x + host.box.w);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
for (const actor of actors) {
|
|
887
|
+
const created = creations.get(actor.id), destroyed = destructions.get(actor.id);
|
|
888
|
+
actor.head.x = actor.x - actor.head.w / 2;
|
|
889
|
+
actor.head.y = created == null ? 8 : created - actor.head.h / 2;
|
|
890
|
+
actor.life = {
|
|
891
|
+
x: actor.x,
|
|
892
|
+
y1: created == null ? top - 4 : created + actor.head.h / 2,
|
|
893
|
+
y2: destroyed ?? bottom
|
|
894
|
+
};
|
|
895
|
+
actor.destroyed = destroyed != null;
|
|
896
|
+
if (actor.destroyed || !mirrorActors) actor.foot = null;
|
|
897
|
+
else {
|
|
898
|
+
actor.foot.x = actor.x - actor.foot.w / 2;
|
|
899
|
+
actor.foot.y = bottom + 8;
|
|
900
|
+
}
|
|
901
|
+
for (const bar of active.get(actor.id)) bar.h = Math.max(0, actor.life.y2 - bar.y);
|
|
902
|
+
}
|
|
903
|
+
const naturalWidth = snap(contentRight + 8);
|
|
904
|
+
const canvas = naturalWidth <= width ? Math.floor(width) : naturalWidth;
|
|
905
|
+
if (naturalWidth > width * 2 || actors.length > 60 || model.messages > 200) errors.push(warning("LARGE_DIAGRAM", lang, actors[0].line, "时序图较大,可滚动查看", "Large sequence diagram; scroll to view it", "可将时序图拆分", "Consider splitting the diagram"));
|
|
906
|
+
return {
|
|
907
|
+
type: "sequence",
|
|
908
|
+
width: canvas,
|
|
909
|
+
height: snap(height),
|
|
910
|
+
actors,
|
|
911
|
+
events,
|
|
912
|
+
activations,
|
|
913
|
+
errors,
|
|
914
|
+
title: model.title
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
function scene(layout, ctx) {
|
|
918
|
+
const theme = appearance(ctx), children = [];
|
|
919
|
+
const drawIcon = (icon, x, y, fill) => {
|
|
920
|
+
const [left, top, w, h] = icon.viewBox.split(/\s+/).map(Number), scale = 16 / Math.max(w, h);
|
|
921
|
+
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, {
|
|
922
|
+
...attrs,
|
|
923
|
+
...icon.set === "simple" ? { fill } : {
|
|
924
|
+
fill: "none",
|
|
925
|
+
stroke: fill,
|
|
926
|
+
"stroke-width": 1.7,
|
|
927
|
+
"stroke-linecap": "round",
|
|
928
|
+
"stroke-linejoin": "round"
|
|
929
|
+
}
|
|
930
|
+
})));
|
|
931
|
+
};
|
|
932
|
+
const addText = (parent, { x, y, lines, icon, meta, font = FONT.label, anchor = "middle", lineHeight = 16, fill = theme.fg2, underline = false }) => {
|
|
933
|
+
const rows = lines.length ? lines : [""];
|
|
934
|
+
const firstY = y - (rows.length - 1) / 2 * lineHeight;
|
|
935
|
+
const nodes = [];
|
|
936
|
+
if (icon) {
|
|
937
|
+
const middle = anchor === "middle";
|
|
938
|
+
const firstWidth = middle ? ctx.measure(rows[0], font.size, font.weight) : 0;
|
|
939
|
+
nodes.push(drawIcon(icon, middle ? x - firstWidth / 2 - (rows[0] ? 2 : 0) : x + 8, firstY, fill));
|
|
940
|
+
x += middle ? ICON_SPACE / 2 : ICON_SPACE;
|
|
941
|
+
}
|
|
942
|
+
rows.forEach((text, index) => {
|
|
943
|
+
if (!text) return;
|
|
944
|
+
const node = ctx.text(x, firstY + index * lineHeight, text, {
|
|
945
|
+
anchor,
|
|
946
|
+
size: font.size,
|
|
947
|
+
weight: font.weight,
|
|
948
|
+
fill,
|
|
949
|
+
"dominant-baseline": "central",
|
|
950
|
+
...underline ? { "text-decoration": "underline" } : {}
|
|
951
|
+
});
|
|
952
|
+
nodes.push(meta ? {
|
|
953
|
+
...node,
|
|
954
|
+
meta
|
|
955
|
+
} : node);
|
|
956
|
+
});
|
|
957
|
+
if (nodes.length) parent.push(ctx.el("g", {}, nodes));
|
|
958
|
+
};
|
|
959
|
+
const line = (x1, y1, x2, y2, attrs = {}) => ctx.line(x1, y1, x2, y2, attrs);
|
|
960
|
+
const heads = {
|
|
961
|
+
arrow: "M1 1 L8 5 L1 9 Z",
|
|
962
|
+
open: "M1 1 L8 5 L1 9",
|
|
963
|
+
cross: "M1 1 L9 9 M1 9 L9 1",
|
|
964
|
+
top: "M1 1 L8 5 H1 Z",
|
|
965
|
+
bottom: "M1 9 L8 5 H1 Z",
|
|
966
|
+
"stick-top": "M1 1 L8 5",
|
|
967
|
+
"stick-bottom": "M1 9 L8 5"
|
|
968
|
+
};
|
|
969
|
+
const markerId = (head, status) => `seq-arrow${head === "arrow" ? "" : `-${head}`}${status ? `-${status}` : ""}`;
|
|
970
|
+
const startHead = (head) => head?.replace(/top|bottom/, (side) => side === "top" ? "bottom" : "top");
|
|
971
|
+
const markers = /* @__PURE__ */ new Map();
|
|
972
|
+
for (const event of layout.events) if (event.arrow) for (const head of [event.arrow.head, startHead(event.arrow.start)]) {
|
|
973
|
+
if (!heads[head]) continue;
|
|
974
|
+
const id = markerId(head, event.status), color = theme[event.status] ?? theme.fg3;
|
|
975
|
+
if (!markers.has(id)) markers.set(id, ctx.el("marker", {
|
|
976
|
+
id,
|
|
977
|
+
viewBox: "0 0 10 10",
|
|
978
|
+
refX: head === "cross" ? 5 : 8,
|
|
979
|
+
refY: 5,
|
|
980
|
+
markerWidth: 10,
|
|
981
|
+
markerHeight: 10,
|
|
982
|
+
orient: "auto-start-reverse",
|
|
983
|
+
markerUnits: "userSpaceOnUse"
|
|
984
|
+
}, [ctx.path(heads[head], {
|
|
985
|
+
fill: heads[head].endsWith("Z") ? color : "none",
|
|
986
|
+
stroke: color,
|
|
987
|
+
"stroke-width": 1.2
|
|
988
|
+
})]));
|
|
989
|
+
}
|
|
990
|
+
if (markers.size) children.push(ctx.el("defs", {}, [...markers.values()]));
|
|
991
|
+
const glyphs = {
|
|
992
|
+
actor: "M12 1 a3 3 0 1 0 .01 0 M12 7 v7 m-6-4 h12 m-6 4 l-5 6 m5-6 l5 6",
|
|
993
|
+
boundary: "M16 3 a7 7 0 1 0 .01 0 M9 10 H3 M3 4 v12",
|
|
994
|
+
control: "M12 4 a7 7 0 1 0 .01 0 M15 1 l-4 3 4 3",
|
|
995
|
+
entity: "M12 2 a7 7 0 1 0 .01 0 M5 17 h14",
|
|
996
|
+
database: "M4 5 a8 3 0 1 0 16 0 a8 3 0 1 0-16 0 v12 a8 3 0 0 0 16 0 V5 M4 11 a8 3 0 0 0 16 0",
|
|
997
|
+
queue: "M19 3 a3 7 0 1 0 0 14 a3 7 0 1 0 0-14 H5 a3 7 0 0 0 0 14 h14",
|
|
998
|
+
collections: "M7 4 V2 h14 v13 h-3 M3 6 h14 v13 H3 Z"
|
|
999
|
+
};
|
|
1000
|
+
const sectionTone = ctx.frameTone(theme.accent), boxTone = ctx.frameTone(theme.tones[1]);
|
|
1001
|
+
const paintFrame = (event) => {
|
|
1002
|
+
if (!event.box) return;
|
|
1003
|
+
const { x, y, w, h } = event.box;
|
|
1004
|
+
const frame = [];
|
|
1005
|
+
const boxFrame = event.type === "box";
|
|
1006
|
+
const coloredRect = event.type === "rect" && event.color;
|
|
1007
|
+
const role = theme[event.status];
|
|
1008
|
+
const groupTone = role ? ctx.frameTone(role) : event.color ? theme.line : boxTone;
|
|
1009
|
+
const fill = /^transparent$/i.test(event.color) ? "none" : coloredRect ? role || theme.fg4 : event.type === "rect" ? ctx.tint(theme.bg, sectionTone, theme.soft) : boxFrame ? ctx.tint(theme.bg, event.color && !role ? theme.fg4 : groupTone, .08) : ctx.mix(theme.bg, theme.fg4, .07);
|
|
1010
|
+
const stroke = coloredRect ? "none" : event.type === "rect" ? sectionTone : boxFrame ? groupTone : theme.line;
|
|
1011
|
+
frame.push(ctx.rect(x, y, w, h, {
|
|
1012
|
+
fill,
|
|
1013
|
+
stroke,
|
|
1014
|
+
...coloredRect ? { "fill-opacity": .1 } : event.type === "rect" ? { "stroke-opacity": .45 } : {},
|
|
1015
|
+
"stroke-width": 1,
|
|
1016
|
+
rx: theme.smallRadius,
|
|
1017
|
+
...boxFrame ? { "stroke-dasharray": "5 4" } : {}
|
|
1018
|
+
}));
|
|
1019
|
+
if (boxFrame) {
|
|
1020
|
+
if (event.text || event.icon) addText(children, {
|
|
1021
|
+
x: x + w / 2,
|
|
1022
|
+
y: y - 10,
|
|
1023
|
+
lines: [event.text],
|
|
1024
|
+
icon: event.icon
|
|
1025
|
+
});
|
|
1026
|
+
} else if (event.type !== "rect" || event.text || event.icon) {
|
|
1027
|
+
const label = event.type === "rect" ? event.text : event.keyword + (event.text ? " [" + event.text + "]" : "");
|
|
1028
|
+
addText(frame, {
|
|
1029
|
+
x: event.labelX ?? x + 8,
|
|
1030
|
+
y: y + 12,
|
|
1031
|
+
lines: [label],
|
|
1032
|
+
icon: event.icon,
|
|
1033
|
+
anchor: "start"
|
|
1034
|
+
});
|
|
1035
|
+
}
|
|
1036
|
+
children.push({
|
|
1037
|
+
...ctx.el("g", {}, frame),
|
|
1038
|
+
meta: {
|
|
1039
|
+
kind: "frame",
|
|
1040
|
+
id: `sequence-frame-${event.y}`
|
|
1041
|
+
}
|
|
1042
|
+
});
|
|
1043
|
+
};
|
|
1044
|
+
for (const event of layout.events) if (event.kind === "open") paintFrame(event);
|
|
1045
|
+
for (const actor of layout.actors) children.push(line(actor.life.x, actor.life.y1, actor.life.x, actor.life.y2, {
|
|
1046
|
+
stroke: theme.line2,
|
|
1047
|
+
"stroke-width": 1
|
|
1048
|
+
}));
|
|
1049
|
+
for (const bar of layout.activations) if (bar.h > 0) children.push(ctx.rect(bar.x, bar.y, 10, bar.h, {
|
|
1050
|
+
fill: ctx.tint(theme.bg, sectionTone, .28),
|
|
1051
|
+
stroke: sectionTone,
|
|
1052
|
+
"stroke-opacity": .45,
|
|
1053
|
+
"stroke-width": 1
|
|
1054
|
+
}));
|
|
1055
|
+
const actorAt = (id) => layout.actors.find((item) => item.id === id);
|
|
1056
|
+
const creationRoutes = [];
|
|
1057
|
+
const boxes = layout.events.filter((event) => event.kind === "open" && event.type === "box").map((event) => event.box);
|
|
1058
|
+
for (const event of layout.events) {
|
|
1059
|
+
if (event.kind === "section") {
|
|
1060
|
+
const { host } = event;
|
|
1061
|
+
if (!host?.box) continue;
|
|
1062
|
+
children.push(line(host.box.x, event.y + 4, host.box.x + host.box.w, event.y + 4, {
|
|
1063
|
+
stroke: theme.line,
|
|
1064
|
+
"stroke-dasharray": "3 3"
|
|
1065
|
+
}));
|
|
1066
|
+
addText(children, {
|
|
1067
|
+
x: event.labelX ?? host.box.x + 8,
|
|
1068
|
+
y: event.y + 14,
|
|
1069
|
+
lines: [[event.keyword, event.text].filter(Boolean).join(" ")],
|
|
1070
|
+
icon: event.icon,
|
|
1071
|
+
anchor: "start"
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
if (event.kind === "note") {
|
|
1075
|
+
const { x, y, w, h } = event.note, note = [];
|
|
1076
|
+
note.push(ctx.rect(x, y, w, h, {
|
|
1077
|
+
fill: theme.surface,
|
|
1078
|
+
stroke: theme.line2,
|
|
1079
|
+
"stroke-width": 1,
|
|
1080
|
+
rx: theme.smallRadius
|
|
1081
|
+
}));
|
|
1082
|
+
const spans = event.place === "over" && event.ids[0] !== event.ids.at(-1);
|
|
1083
|
+
addText(note, {
|
|
1084
|
+
x: spans ? x + w / 2 : x + 8,
|
|
1085
|
+
y: y + h / 2,
|
|
1086
|
+
lines: event.lines,
|
|
1087
|
+
icon: event.icon,
|
|
1088
|
+
anchor: spans ? "middle" : "start"
|
|
1089
|
+
});
|
|
1090
|
+
children.push(ctx.el("g", {}, note));
|
|
1091
|
+
}
|
|
1092
|
+
if (event.kind !== "message") continue;
|
|
1093
|
+
const from = actorAt(event.from), to = actorAt(event.to);
|
|
1094
|
+
const y = event.y + event.lineOffset;
|
|
1095
|
+
const self = from === to;
|
|
1096
|
+
const dir = to.x >= from.x ? 1 : -1;
|
|
1097
|
+
const { head, start, dashed } = event.arrow;
|
|
1098
|
+
const color = theme[event.status] ?? theme.fg3;
|
|
1099
|
+
const lineAttrs = {
|
|
1100
|
+
stroke: color,
|
|
1101
|
+
"stroke-width": 1.5,
|
|
1102
|
+
"stroke-linecap": "butt",
|
|
1103
|
+
...dashed ? { "stroke-dasharray": "5 4" } : {},
|
|
1104
|
+
...start ? { "marker-start": `url(#${markerId(startHead(start), event.status)})` } : {},
|
|
1105
|
+
...head === "none" ? {} : { "marker-end": `url(#${markerId(head, event.status)})` }
|
|
1106
|
+
};
|
|
1107
|
+
const startX = event.centralStart ? from.x : event.fromBar ? event.fromBar.x + (dir > 0 ? 10 : 0) : from.x + dir * (self ? 6 : 8);
|
|
1108
|
+
const startY = self ? y - 14 : y, endY = self ? y + 4 : y;
|
|
1109
|
+
const hitsHead = event.create && endY >= to.head.y && endY <= to.head.y + to.head.h;
|
|
1110
|
+
const centralEnd = event.centralEnd && !event.create;
|
|
1111
|
+
const endX = hitsHead ? dir > 0 ? to.head.x : to.head.x + to.head.w : centralEnd || head === "cross" && event.destroy === to.id ? to.x : !event.create && event.toBar ? event.toBar.x + (self || dir < 0 ? 10 : 0) : to.x - (self ? -1 : dir) * MESSAGE_END_GAP;
|
|
1112
|
+
const tipStart = startX + (event.centralStart && start ? dir * 7 : 0);
|
|
1113
|
+
const tipEnd = endX - (centralEnd && head !== "none" ? (self ? -1 : dir) * 7 : 0);
|
|
1114
|
+
const route = self ? ctx.path(`M${tipStart} ${startY} h${40 + startX - tipStart} v18 H${tipEnd}`, {
|
|
1115
|
+
fill: "none",
|
|
1116
|
+
...lineAttrs
|
|
1117
|
+
}) : line(tipStart, y, tipEnd, y, lineAttrs);
|
|
1118
|
+
(hitsHead ? creationRoutes : children).push({
|
|
1119
|
+
...route,
|
|
1120
|
+
meta: {
|
|
1121
|
+
kind: "message",
|
|
1122
|
+
from: from.id,
|
|
1123
|
+
to: to.id
|
|
1124
|
+
}
|
|
1125
|
+
});
|
|
1126
|
+
const dot = (x, cy) => children.push(ctx.el("circle", {
|
|
1127
|
+
cx: x,
|
|
1128
|
+
cy,
|
|
1129
|
+
r: 3.5,
|
|
1130
|
+
fill: color
|
|
1131
|
+
}));
|
|
1132
|
+
if (event.centralStart) dot(startX, startY);
|
|
1133
|
+
if (centralEnd) dot(endX, endY);
|
|
1134
|
+
const x = self ? from.x + 52 : event.create ? (startX + endX) / 2 : (from.x + to.x) / 2;
|
|
1135
|
+
const labelY = self ? y - 20 - (event.lines.length - 1) * 8 : y - event.lines.length * 8 - 10;
|
|
1136
|
+
const ordinalY = y - (event.lines.length * 16 + (self ? 28 : 18));
|
|
1137
|
+
const labelLeft = self ? x : x - event.textW / 2;
|
|
1138
|
+
const labelRight = labelLeft + event.textW;
|
|
1139
|
+
const labelTop = (event.ordinal ? ordinalY : labelY - (event.lines.length - 1) * 8) - 7;
|
|
1140
|
+
const labelBottom = (event.lines.length ? labelY + (event.lines.length - 1) * 8 : ordinalY) + 7;
|
|
1141
|
+
if (event.textW && boxes.some((box) => box.y < labelBottom && box.y + box.h > labelTop && [box.x, box.x + box.w].some((edge) => labelLeft - 2 < edge && edge < labelRight + 2))) children.push(ctx.rect(labelLeft - 4, labelTop, event.textW + 8, labelBottom - labelTop, {
|
|
1142
|
+
fill: theme.bg,
|
|
1143
|
+
rx: theme.smallRadius
|
|
1144
|
+
}));
|
|
1145
|
+
const meta = {
|
|
1146
|
+
kind: "message-label",
|
|
1147
|
+
from: from.id,
|
|
1148
|
+
to: to.id
|
|
1149
|
+
};
|
|
1150
|
+
if (event.lines.length || event.icon) addText(children, {
|
|
1151
|
+
x,
|
|
1152
|
+
y: labelY,
|
|
1153
|
+
lines: event.lines,
|
|
1154
|
+
icon: event.icon,
|
|
1155
|
+
meta,
|
|
1156
|
+
anchor: self ? "start" : "middle"
|
|
1157
|
+
});
|
|
1158
|
+
if (event.ordinal) addText(children, {
|
|
1159
|
+
x,
|
|
1160
|
+
y: ordinalY,
|
|
1161
|
+
lines: [event.ordinal],
|
|
1162
|
+
meta,
|
|
1163
|
+
anchor: self ? "start" : "middle",
|
|
1164
|
+
fill: theme.fg3
|
|
1165
|
+
});
|
|
1166
|
+
if (event.destroy && !(head === "cross" && event.destroy === to.id)) {
|
|
1167
|
+
const dx = actorAt(event.destroy).x;
|
|
1168
|
+
children.push(ctx.path(`M${dx - 4} ${y - 4} l8 8 m0 -8 l-8 8`, {
|
|
1169
|
+
fill: "none",
|
|
1170
|
+
stroke: theme.fg3,
|
|
1171
|
+
"stroke-width": 1.5
|
|
1172
|
+
}));
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
for (const actor of layout.actors) for (const part of [actor.head, actor.foot].filter(Boolean)) {
|
|
1176
|
+
const group = [];
|
|
1177
|
+
const glyph = glyphs[actor.kind];
|
|
1178
|
+
const role = theme[actor.status];
|
|
1179
|
+
group.push(ctx.rect(part.x, part.y, part.w, part.h, {
|
|
1180
|
+
fill: role ? ctx.tint(theme.surface, role, .12) : theme.surface,
|
|
1181
|
+
stroke: role ? ctx.frameTone(role) : theme.line,
|
|
1182
|
+
"stroke-width": 1,
|
|
1183
|
+
rx: theme.smallRadius
|
|
1184
|
+
}));
|
|
1185
|
+
if (actor.icon) group.push(drawIcon(actor.icon, actor.x, part.y + 16, theme.fg3));
|
|
1186
|
+
else if (glyph) group.push(ctx.path(glyph, {
|
|
1187
|
+
transform: `translate(${actor.x - 12} ${part.y + 6})`,
|
|
1188
|
+
fill: "none",
|
|
1189
|
+
stroke: theme.fg3,
|
|
1190
|
+
"stroke-width": 1.3,
|
|
1191
|
+
"stroke-linecap": "round",
|
|
1192
|
+
"stroke-linejoin": "round"
|
|
1193
|
+
}));
|
|
1194
|
+
addText(group, {
|
|
1195
|
+
x: actor.x,
|
|
1196
|
+
y: part.y + part.h / 2 + (glyph || actor.icon ? 12 : 0),
|
|
1197
|
+
lines: part.lines,
|
|
1198
|
+
font: FONT.title,
|
|
1199
|
+
lineHeight: 18,
|
|
1200
|
+
fill: theme.fg1,
|
|
1201
|
+
underline: Boolean(actor.link)
|
|
1202
|
+
});
|
|
1203
|
+
const card = {
|
|
1204
|
+
...ctx.el("g", {}, group),
|
|
1205
|
+
meta: {
|
|
1206
|
+
kind: "participant",
|
|
1207
|
+
id: actor.id
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
children.push(actor.link ? ctx.link(actor.link.href, [card], actor.link) : card);
|
|
1211
|
+
}
|
|
1212
|
+
children.push(...creationRoutes);
|
|
1213
|
+
return {
|
|
1214
|
+
width: layout.width,
|
|
1215
|
+
height: layout.height,
|
|
1216
|
+
children,
|
|
1217
|
+
title: layout.title,
|
|
1218
|
+
heading: layout.title
|
|
1219
|
+
};
|
|
1220
|
+
}
|
|
1221
|
+
var implementations = { sequenceDiagram: {
|
|
1222
|
+
parse,
|
|
1223
|
+
layout,
|
|
1224
|
+
scene,
|
|
1225
|
+
acceptsPresentation: true,
|
|
1226
|
+
honoredConfig: ["sequence.mirrorActors", "sequence.showSequenceNumbers"]
|
|
1227
|
+
} };
|
|
1228
|
+
export { LIMITS, implementations, sequence_exports };
|