@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,1186 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { statusMarks } from "../core/theme.js";
|
|
3
|
+
import { FONT, appearance, error, labelText, maxWidthOf, snap, wrapText } from "./support.js";
|
|
4
|
+
import { parseFlow } from "./graph-parse.js";
|
|
5
|
+
// src/diagram/swimlane.js
|
|
6
|
+
var swimlane_exports = /* @__PURE__ */ __exportAll({
|
|
7
|
+
implementations: () => implementations,
|
|
8
|
+
layout: () => layout,
|
|
9
|
+
scene: () => scene
|
|
10
|
+
});
|
|
11
|
+
var LIMITS = {
|
|
12
|
+
lanes: 6,
|
|
13
|
+
nodes: 30,
|
|
14
|
+
edges: 50,
|
|
15
|
+
backEdges: 3,
|
|
16
|
+
lines: 3,
|
|
17
|
+
labelWidth: 208
|
|
18
|
+
};
|
|
19
|
+
var HEADER = 36;
|
|
20
|
+
var NODE_GAP = 10;
|
|
21
|
+
var NODE_PAD_X = 28;
|
|
22
|
+
var NODE_PAD_Y = 16;
|
|
23
|
+
var LANE_GAP = 12;
|
|
24
|
+
var STEP_GAP = 24;
|
|
25
|
+
var OUTER = 4;
|
|
26
|
+
var TRACK = 12;
|
|
27
|
+
var CLEAR = 12;
|
|
28
|
+
var PORT_GAP = 12;
|
|
29
|
+
var PORT_SPREAD = 24;
|
|
30
|
+
var PORT_INSET = 10;
|
|
31
|
+
var HOP_R = 5;
|
|
32
|
+
var ZONE_PAD = 4;
|
|
33
|
+
var LABEL_GAP = 8;
|
|
34
|
+
var LABEL_EDGE = 8;
|
|
35
|
+
var MIN_LANE_TB = 44;
|
|
36
|
+
var MIN_LANE_LR = 40;
|
|
37
|
+
var MIN_STEP = 40;
|
|
38
|
+
var issue = (lang, line, zh, en, fixZh, fixEn) => error(lang, line, zh, en, fixZh, fixEn);
|
|
39
|
+
function directionOf(options, body, ctx) {
|
|
40
|
+
const declared = options.LR === true ? "LR" : options.TB === true ? "TB" : null;
|
|
41
|
+
const line = body.find((row) => /^direction\s+/i.test(row.text));
|
|
42
|
+
const written = line ? line.text.replace(/^direction\s+/i, "").trim().toUpperCase() : null;
|
|
43
|
+
const normalized = written === "TD" ? "TB" : written;
|
|
44
|
+
if (declared && normalized && declared !== normalized) return { error: issue(ctx.lang, line.line, "泳道方向声明冲突", "Conflicting swimlane directions", "只保留 swimlane LR/TB 或正文 direction 其中一种", "Keep either swimlane LR/TB or one direction statement") };
|
|
45
|
+
const direction = declared || normalized || "TB";
|
|
46
|
+
if (!["TB", "LR"].includes(direction)) return { error: issue(ctx.lang, line?.line || ctx.firstLine - 1, "泳道只支持 TB 或 LR", "Swimlanes only support TB or LR", "改成 swimlane、swimlane LR 或 direction TB/LR", "Use swimlane, swimlane LR, or direction TB/LR") };
|
|
47
|
+
return {
|
|
48
|
+
direction,
|
|
49
|
+
line
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function parse(source, info = {}) {
|
|
53
|
+
const ctx = {
|
|
54
|
+
...info,
|
|
55
|
+
lang: info.lang || "zh",
|
|
56
|
+
firstLine: info.firstLine || 1,
|
|
57
|
+
options: info.options || {}
|
|
58
|
+
};
|
|
59
|
+
const errors = [];
|
|
60
|
+
const { lang } = ctx;
|
|
61
|
+
for (const key of Object.keys(ctx.options)) if (!["LR", "TB"].includes(key)) errors.push(issue(lang, ctx.firstLine - 1, `swimlane 不支持选项「${key}」`, `Unsupported swimlane option: ${key}`, "只使用 swimlane、swimlane LR 或 swimlane TB", "Use only swimlane, swimlane LR, or swimlane TB"));
|
|
62
|
+
const rows = String(source).split("\n").map((raw, index) => ({
|
|
63
|
+
raw,
|
|
64
|
+
line: ctx.firstLine + index
|
|
65
|
+
})).filter((row) => row.raw.trim() && !row.raw.trimStart().startsWith("%%"));
|
|
66
|
+
if (rows.some((row) => /^\s*\t/.test(row.raw))) errors.push(issue(lang, rows.find((row) => /^\s*\t/.test(row.raw)).line, "缩进不能使用 Tab", "Tabs are not allowed in indentation", "把 Tab 换成空格", "Replace tabs with spaces"));
|
|
67
|
+
const body = rows.filter((row) => !/^\s*\t/.test(row.raw)).map((row) => ({
|
|
68
|
+
...row,
|
|
69
|
+
indent: row.raw.match(/^ */)[0].length,
|
|
70
|
+
text: row.raw.trim()
|
|
71
|
+
}));
|
|
72
|
+
const direction = directionOf(ctx.options, body, ctx);
|
|
73
|
+
if (direction.error) errors.push(direction.error);
|
|
74
|
+
const lanes = [];
|
|
75
|
+
const laneByName = /* @__PURE__ */ new Map();
|
|
76
|
+
const ownership = /* @__PURE__ */ new Map();
|
|
77
|
+
const statements = [];
|
|
78
|
+
let current = null;
|
|
79
|
+
let laneIndent = null;
|
|
80
|
+
for (const row of body) {
|
|
81
|
+
if (/^direction\b/i.test(row.text)) continue;
|
|
82
|
+
if (/^lane\s+/i.test(row.text)) {
|
|
83
|
+
if (row.indent !== 0) errors.push(issue(lang, row.line, "lane 必须写在顶层", "lane must be at the top level", "去掉 lane 前面的缩进", "Remove indentation before lane"));
|
|
84
|
+
const name = row.text.replace(/^lane\s+/i, "").trim();
|
|
85
|
+
if (!name || laneByName.has(name)) errors.push(issue(lang, row.line, `泳道名称无效或重复:${name || "(空)"}`, `Invalid or duplicate lane: ${name || "(empty)"}`, "给每条泳道一个不重复的名称", "Give each lane a unique name"));
|
|
86
|
+
current = {
|
|
87
|
+
id: `lane-${lanes.length}`,
|
|
88
|
+
name,
|
|
89
|
+
line: row.line
|
|
90
|
+
};
|
|
91
|
+
lanes.push(current);
|
|
92
|
+
laneByName.set(name, current);
|
|
93
|
+
laneIndent = null;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (current && row.indent > 0) {
|
|
97
|
+
if (laneIndent == null) laneIndent = row.indent;
|
|
98
|
+
if (row.indent !== laneIndent) errors.push(issue(lang, row.line, "同一泳道内的缩进不一致", "Inconsistent indentation inside a lane", `把这一行缩进改为 ${laneIndent} 个空格`, `Indent this line with ${laneIndent} spaces`));
|
|
99
|
+
statements.push(row);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (row.indent === 0) {
|
|
103
|
+
current = null;
|
|
104
|
+
statements.push(row);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
errors.push(issue(lang, row.line, "节点必须写在 lane 下面", "A node must be indented under a lane", "先声明 lane,再缩进写节点", "Declare a lane, then indent its nodes"));
|
|
108
|
+
}
|
|
109
|
+
if (!lanes.length) errors.push(issue(lang, ctx.firstLine, "图中没有泳道", "Swimlane diagram has no lanes", "添加 lane 名称和缩进的节点", "Add lane names and indented nodes"));
|
|
110
|
+
if (lanes.length > LIMITS.lanes) errors.push(issue(lang, lanes[LIMITS.lanes].line, `最多 ${LIMITS.lanes} 条泳道`, `At most ${LIMITS.lanes} lanes`, "把流程拆成两张泳道图", "Split the flow into two swimlane diagrams"));
|
|
111
|
+
const flow = parseFlow(statements.map(({ text, line }) => ({
|
|
112
|
+
text,
|
|
113
|
+
line
|
|
114
|
+
})), { direction: direction.direction || "TB" });
|
|
115
|
+
errors.push(...flow.errors.filter((error) => !/超过上限/.test(error.message)));
|
|
116
|
+
const model = flow.model;
|
|
117
|
+
const defined = /* @__PURE__ */ new Map();
|
|
118
|
+
let scanLane = null;
|
|
119
|
+
for (const row of body) {
|
|
120
|
+
if (/^lane\s+/i.test(row.text) && row.indent === 0) {
|
|
121
|
+
scanLane = lanes.find((lane) => lane.line === row.line);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (!scanLane || row.indent === 0) {
|
|
125
|
+
scanLane = null;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
for (const match of row.text.matchAll(/(?:^|[\s&])([A-Za-z_][\w-]*)\s*[\[\(\{>]/g)) {
|
|
129
|
+
const id = match[1];
|
|
130
|
+
if (defined.has(id) && defined.get(id) !== scanLane.id) errors.push(issue(lang, row.line, `节点 ${id} 重复归属`, `Node ${id} belongs to more than one lane`, "只在一条泳道里定义这个节点", "Define this node in only one lane"));
|
|
131
|
+
else defined.set(id, scanLane.id);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
const laneStatements = /* @__PURE__ */ new Set();
|
|
135
|
+
body.forEach((row) => {
|
|
136
|
+
if (row.indent > 0) laneStatements.add(row.line);
|
|
137
|
+
});
|
|
138
|
+
for (const node of model.nodes) if (!laneStatements.has(node.line)) errors.push(issue(lang, node.line, `节点 ${node.id} 没有泳道`, `Node ${node.id} has no lane`, "把节点定义缩进到对应 lane 下面", "Indent the node definition under its lane"));
|
|
139
|
+
else if (ownership.has(node.id)) errors.push(issue(lang, node.line, `节点 ${node.id} 重复归属`, `Node ${node.id} belongs to more than one lane`, "只在一条泳道里定义这个节点", "Define this node in only one lane"));
|
|
140
|
+
else {
|
|
141
|
+
const owner = [...lanes].reverse().find((lane) => lane.line < node.line);
|
|
142
|
+
ownership.set(node.id, owner?.id || null);
|
|
143
|
+
}
|
|
144
|
+
if (model.nodes.length > LIMITS.nodes) errors.push(issue(lang, model.nodes[LIMITS.nodes].line, `最多 ${LIMITS.nodes} 个节点`, `At most ${LIMITS.nodes} nodes`, "拆成总图和局部图", "Split into an overview and a detail diagram"));
|
|
145
|
+
if (model.edges.length > LIMITS.edges) errors.push(issue(lang, model.edges[LIMITS.edges].line, `最多 ${LIMITS.edges} 条连线`, `At most ${LIMITS.edges} edges`, "拆成总图和局部图", "Split into an overview and a detail diagram"));
|
|
146
|
+
return {
|
|
147
|
+
model: {
|
|
148
|
+
type: "swimlane",
|
|
149
|
+
direction: direction.direction || "TB",
|
|
150
|
+
lanes: lanes.map((lane) => ({
|
|
151
|
+
...lane,
|
|
152
|
+
nodes: model.nodes.filter((node) => ownership.get(node.id) === lane.id).map((node) => node.id)
|
|
153
|
+
})),
|
|
154
|
+
nodes: model.nodes.map((node) => ({
|
|
155
|
+
id: node.id,
|
|
156
|
+
label: labelText(node.label),
|
|
157
|
+
shape: node.shape,
|
|
158
|
+
class: node.class || null,
|
|
159
|
+
lane: ownership.get(node.id) || null,
|
|
160
|
+
line: node.line
|
|
161
|
+
})),
|
|
162
|
+
edges: model.edges.map((edge) => ({
|
|
163
|
+
id: `e${edge.seq}`,
|
|
164
|
+
from: edge.from,
|
|
165
|
+
to: edge.to,
|
|
166
|
+
label: labelText(edge.label),
|
|
167
|
+
style: edge.style,
|
|
168
|
+
line: edge.line
|
|
169
|
+
}))
|
|
170
|
+
},
|
|
171
|
+
errors
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function backEdgesOf(nodes, edges) {
|
|
175
|
+
const outgoing = new Map(nodes.map((node) => [node.id, []]));
|
|
176
|
+
edges.forEach((edge, index) => outgoing.get(edge.from)?.push({
|
|
177
|
+
edge,
|
|
178
|
+
index
|
|
179
|
+
}));
|
|
180
|
+
for (const list of outgoing.values()) list.sort((a, b) => a.index - b.index);
|
|
181
|
+
const seen = /* @__PURE__ */ new Set();
|
|
182
|
+
const stack = /* @__PURE__ */ new Set();
|
|
183
|
+
const back = [];
|
|
184
|
+
const visit = (id) => {
|
|
185
|
+
seen.add(id);
|
|
186
|
+
stack.add(id);
|
|
187
|
+
for (const item of outgoing.get(id) || []) if (stack.has(item.edge.to)) back.push(item.edge);
|
|
188
|
+
else if (!seen.has(item.edge.to)) visit(item.edge.to);
|
|
189
|
+
stack.delete(id);
|
|
190
|
+
};
|
|
191
|
+
for (const node of nodes) if (!seen.has(node.id)) visit(node.id);
|
|
192
|
+
return back;
|
|
193
|
+
}
|
|
194
|
+
function stepsOf(nodes, edges, back) {
|
|
195
|
+
const incoming = new Map(nodes.map((node) => [node.id, 0]));
|
|
196
|
+
const outgoing = new Map(nodes.map((node) => [node.id, []]));
|
|
197
|
+
for (const edge of edges) if (!back.has(edge)) {
|
|
198
|
+
incoming.set(edge.to, incoming.get(edge.to) + 1);
|
|
199
|
+
outgoing.get(edge.from).push(edge.to);
|
|
200
|
+
}
|
|
201
|
+
const step = new Map(nodes.map((node) => [node.id, 0]));
|
|
202
|
+
const queue = nodes.filter((node) => incoming.get(node.id) === 0).map((node) => node.id);
|
|
203
|
+
const seen = /* @__PURE__ */ new Set();
|
|
204
|
+
while (queue.length) {
|
|
205
|
+
const id = queue.shift();
|
|
206
|
+
if (seen.has(id)) continue;
|
|
207
|
+
seen.add(id);
|
|
208
|
+
for (const next of outgoing.get(id)) {
|
|
209
|
+
step.set(next, Math.max(step.get(next), step.get(id) + 1));
|
|
210
|
+
incoming.set(next, incoming.get(next) - 1);
|
|
211
|
+
if (incoming.get(next) === 0) queue.push(next);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return step;
|
|
215
|
+
}
|
|
216
|
+
function segments(points) {
|
|
217
|
+
const result = [];
|
|
218
|
+
for (let i = 1; i < points.length; i += 1) {
|
|
219
|
+
const [a, b] = [points[i - 1], points[i]];
|
|
220
|
+
if (a.x === b.x && a.y === b.y) continue;
|
|
221
|
+
result.push({
|
|
222
|
+
x: Math.min(a.x, b.x),
|
|
223
|
+
y: Math.min(a.y, b.y),
|
|
224
|
+
w: Math.abs(b.x - a.x),
|
|
225
|
+
h: Math.abs(b.y - a.y)
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
function simplify(points) {
|
|
231
|
+
const result = [];
|
|
232
|
+
for (const point of points) {
|
|
233
|
+
if (result.length && result[result.length - 1].x === point.x && result[result.length - 1].y === point.y) continue;
|
|
234
|
+
while (result.length > 1 && (result[result.length - 2].x === result[result.length - 1].x && result[result.length - 1].x === point.x || result[result.length - 2].y === result[result.length - 1].y && result[result.length - 1].y === point.y)) result.pop();
|
|
235
|
+
result.push(point);
|
|
236
|
+
}
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
function crossingsOf(edges) {
|
|
240
|
+
const verticals = [];
|
|
241
|
+
for (const edge of edges) for (const part of segments(edge.points)) if (part.w === 0) verticals.push({
|
|
242
|
+
edge,
|
|
243
|
+
x: part.x,
|
|
244
|
+
lo: part.y,
|
|
245
|
+
hi: part.y + part.h
|
|
246
|
+
});
|
|
247
|
+
const result = new Map(edges.map((edge) => [edge, []]));
|
|
248
|
+
for (const edge of edges) for (const part of segments(edge.points)) {
|
|
249
|
+
if (part.h !== 0) continue;
|
|
250
|
+
for (const upright of verticals) {
|
|
251
|
+
if (upright.edge === edge) continue;
|
|
252
|
+
if (!(part.x + HOP_R < upright.x && upright.x < part.x + part.w - HOP_R)) continue;
|
|
253
|
+
if (!(upright.lo + HOP_R < part.y && part.y < upright.hi - HOP_R)) continue;
|
|
254
|
+
result.get(edge).push({
|
|
255
|
+
x: upright.x,
|
|
256
|
+
y: part.y
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return result;
|
|
261
|
+
}
|
|
262
|
+
function pathData(points, hops) {
|
|
263
|
+
const parts = [`M${points[0].x} ${points[0].y}`];
|
|
264
|
+
for (let i = 1; i < points.length; i += 1) {
|
|
265
|
+
const [from, to] = [points[i - 1], points[i]];
|
|
266
|
+
if (from.y === to.y) {
|
|
267
|
+
const direction = to.x > from.x ? 1 : -1;
|
|
268
|
+
const list = hops.filter((hop) => hop.y === from.y && (hop.x - from.x) * direction > 0 && (to.x - hop.x) * direction > 0).sort((a, b) => (a.x - b.x) * direction);
|
|
269
|
+
for (const hop of list) {
|
|
270
|
+
parts.push(`L${hop.x - direction * HOP_R} ${hop.y}`);
|
|
271
|
+
parts.push(`A${HOP_R} ${HOP_R} 0 0 ${direction > 0 ? 1 : 0} ${hop.x + direction * HOP_R} ${hop.y}`);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
parts.push(`L${to.x} ${to.y}`);
|
|
275
|
+
}
|
|
276
|
+
return parts.join(" ");
|
|
277
|
+
}
|
|
278
|
+
var capacity = (tracks, floor) => Math.max(floor, tracks ? 24 + (tracks - 1) * TRACK : 0);
|
|
279
|
+
function assignTracks(runs) {
|
|
280
|
+
const tracks = [];
|
|
281
|
+
for (const run of [...runs].sort((a, b) => a.lo - b.lo || a.hi - b.hi || a.index - b.index)) {
|
|
282
|
+
let track = 0;
|
|
283
|
+
while ((tracks[track] || []).some((other) => other.lo <= run.hi && run.lo <= other.hi)) track += 1;
|
|
284
|
+
(tracks[track] ||= []).push(run);
|
|
285
|
+
run.track = track;
|
|
286
|
+
}
|
|
287
|
+
return tracks.length;
|
|
288
|
+
}
|
|
289
|
+
function findCycle(runs, orders) {
|
|
290
|
+
const next = /* @__PURE__ */ new Map();
|
|
291
|
+
for (const order of orders) {
|
|
292
|
+
if (!next.has(order.from)) next.set(order.from, []);
|
|
293
|
+
next.get(order.from).push(order);
|
|
294
|
+
}
|
|
295
|
+
const state = /* @__PURE__ */ new Map();
|
|
296
|
+
const path = [];
|
|
297
|
+
let cycle = null;
|
|
298
|
+
const visit = (run) => {
|
|
299
|
+
state.set(run, 1);
|
|
300
|
+
for (const order of next.get(run) || []) {
|
|
301
|
+
if (state.get(order.to) === 1) {
|
|
302
|
+
const start = path.findIndex((item) => item.from === order.to);
|
|
303
|
+
cycle = [...path.slice(start < 0 ? path.length : start), order];
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
if (!state.has(order.to)) {
|
|
307
|
+
path.push(order);
|
|
308
|
+
visit(order.to);
|
|
309
|
+
path.pop();
|
|
310
|
+
}
|
|
311
|
+
if (cycle) return;
|
|
312
|
+
}
|
|
313
|
+
state.set(run, 2);
|
|
314
|
+
};
|
|
315
|
+
for (const run of runs) {
|
|
316
|
+
if (!state.has(run)) visit(run);
|
|
317
|
+
if (cycle) break;
|
|
318
|
+
}
|
|
319
|
+
return cycle;
|
|
320
|
+
}
|
|
321
|
+
function layout(model, { measure, width, lang }) {
|
|
322
|
+
const horizontal = model.direction === "LR";
|
|
323
|
+
const crossOf = (node) => horizontal ? node.h : node.w;
|
|
324
|
+
const alongOf = (node) => horizontal ? node.w : node.h;
|
|
325
|
+
const point = (cross, along) => horizontal ? {
|
|
326
|
+
x: along,
|
|
327
|
+
y: cross
|
|
328
|
+
} : {
|
|
329
|
+
x: cross,
|
|
330
|
+
y: along
|
|
331
|
+
};
|
|
332
|
+
const fail = (line, zh, en, fixZh, fixEn) => ({
|
|
333
|
+
width,
|
|
334
|
+
height: 0,
|
|
335
|
+
errors: [issue(lang, line, zh, en, fixZh, fixEn)]
|
|
336
|
+
});
|
|
337
|
+
const backList = backEdgesOf(model.nodes, model.edges);
|
|
338
|
+
if (backList.length > LIMITS.backEdges) return fail(backList[LIMITS.backEdges].line, `回边最多 ${LIMITS.backEdges} 条`, `At most ${LIMITS.backEdges} back edges`, "减少回环,或拆成两张图", "Reduce loops or split into two diagrams");
|
|
339
|
+
const back = new Set(backList);
|
|
340
|
+
const stepOf = stepsOf(model.nodes, model.edges, back);
|
|
341
|
+
if ([...stepOf.values()].some((value) => !Number.isFinite(value))) return fail(model.nodes[0]?.line || 1, "步序无法确定", "Node steps cannot be resolved", "检查节点归属与连线", "Check node ownership and edges");
|
|
342
|
+
const labelOf = new Map(model.edges.map((edge) => {
|
|
343
|
+
const icon = model.mermaid && edge.icon;
|
|
344
|
+
if (!edge.label && !icon) return [edge.id, null];
|
|
345
|
+
const lines = wrapText(edge.label ?? "", 120, FONT.label, measure);
|
|
346
|
+
const w = snap(maxWidthOf(lines, FONT.label, measure) + 12 + (icon ? 18 : 0));
|
|
347
|
+
const h = snap(lines.length * 16 + 8);
|
|
348
|
+
return [edge.id, {
|
|
349
|
+
lines,
|
|
350
|
+
w,
|
|
351
|
+
h,
|
|
352
|
+
cross: horizontal ? h : w,
|
|
353
|
+
along: horizontal ? w : h
|
|
354
|
+
}];
|
|
355
|
+
}));
|
|
356
|
+
const sideLists = /* @__PURE__ */ new Map();
|
|
357
|
+
const addEnd = (id, side, edge, end) => {
|
|
358
|
+
const key = `${id}|${side}`;
|
|
359
|
+
if (!sideLists.has(key)) sideLists.set(key, []);
|
|
360
|
+
sideLists.get(key).push({
|
|
361
|
+
edge,
|
|
362
|
+
end
|
|
363
|
+
});
|
|
364
|
+
};
|
|
365
|
+
for (const edge of model.edges) {
|
|
366
|
+
const out = back.has(edge) ? -1 : 1;
|
|
367
|
+
addEnd(edge.from, out, edge, "out");
|
|
368
|
+
addEnd(edge.to, -out, edge, "in");
|
|
369
|
+
}
|
|
370
|
+
const spacingOf = (list) => {
|
|
371
|
+
if (list.length < 2) return PORT_GAP;
|
|
372
|
+
const widths = list.filter((item) => item.end === "out" && labelOf.get(item.edge.id)).map((item) => labelOf.get(item.edge.id).cross);
|
|
373
|
+
if (!widths.length) return PORT_GAP;
|
|
374
|
+
const widest = Math.max(...widths);
|
|
375
|
+
return Math.max(PORT_GAP, widths.length > 1 ? widest + LABEL_GAP : widest / 2 + LABEL_GAP);
|
|
376
|
+
};
|
|
377
|
+
const portRoom = (id) => Math.max(32, ...[-1, 1].map((side) => {
|
|
378
|
+
const list = sideLists.get(`${id}|${side}`) || [];
|
|
379
|
+
return list.length < 2 ? 0 : 20 + (list.length - 1) * spacingOf(list);
|
|
380
|
+
}));
|
|
381
|
+
const nodes = [];
|
|
382
|
+
for (const node of model.nodes) {
|
|
383
|
+
const decision = model.mermaid && node.shape === "diamond";
|
|
384
|
+
const cap = decision ? 88 : LIMITS.labelWidth;
|
|
385
|
+
const wrapped = wrapText(node.label, cap, FONT.body, measure);
|
|
386
|
+
const lines = (decision && wrapped.length > LIMITS.lines ? wrapText(node.label, LIMITS.labelWidth, FONT.body, measure) : wrapped).slice(0, LIMITS.lines + 1);
|
|
387
|
+
if (lines.length > LIMITS.lines) return fail(node.line, `节点「${node.label}」超过 ${LIMITS.lines} 行`, `Node label exceeds ${LIMITS.lines} lines: ${node.label}`, "缩短标签或把说明移到图外", "Shorten the label or move the description outside");
|
|
388
|
+
const textW = maxWidthOf(lines, FONT.body, measure) + (model.mermaid && node.icon ? 22 : 0);
|
|
389
|
+
const textH = lines.length * 20;
|
|
390
|
+
const diamond = node.shape === "diamond";
|
|
391
|
+
const room = portRoom(node.id);
|
|
392
|
+
if (decision || model.mermaid && node.shape === "circle") {
|
|
393
|
+
const diameter = snap(Math.max(room, decision ? textW + textH + 16 : Math.hypot(textW + 16, textH + 12)));
|
|
394
|
+
nodes.push({
|
|
395
|
+
...node,
|
|
396
|
+
lines,
|
|
397
|
+
diamond,
|
|
398
|
+
step: stepOf.get(node.id),
|
|
399
|
+
w: diameter,
|
|
400
|
+
h: diameter
|
|
401
|
+
});
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
const cross = (diamond ? textW * 1.7 : textW) + NODE_PAD_X;
|
|
405
|
+
const along = (diamond ? textH * 1.7 : textH) + NODE_PAD_Y;
|
|
406
|
+
nodes.push({
|
|
407
|
+
id: node.id,
|
|
408
|
+
label: node.label,
|
|
409
|
+
shape: node.shape,
|
|
410
|
+
class: node.class || null,
|
|
411
|
+
lane: node.lane,
|
|
412
|
+
line: node.line,
|
|
413
|
+
lines,
|
|
414
|
+
diamond,
|
|
415
|
+
step: stepOf.get(node.id),
|
|
416
|
+
w: snap(horizontal ? cross : Math.max(cross, room)),
|
|
417
|
+
h: snap(horizontal ? Math.max(along, room) : along)
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
if (!nodes.length) return fail(model.nodes[0]?.line || 1, "图中没有节点", "Swimlane diagram has no nodes", "在泳道下缩进写节点", "Indent nodes under a lane");
|
|
421
|
+
const maxStep = Math.max(...nodes.map((node) => node.step));
|
|
422
|
+
const laneCount = model.lanes.length;
|
|
423
|
+
const nodeById = new Map(nodes.map((node) => [node.id, node]));
|
|
424
|
+
const laneIndex = new Map(model.lanes.map((lane, index) => [lane.id, index]));
|
|
425
|
+
const lanes = model.lanes.map((lane) => ({
|
|
426
|
+
lane,
|
|
427
|
+
columns: Array.from({ length: maxStep + 1 }, () => [])
|
|
428
|
+
}));
|
|
429
|
+
for (const node of nodes) lanes[laneIndex.get(node.lane)]?.columns[node.step].push(node);
|
|
430
|
+
for (const item of lanes) {
|
|
431
|
+
item.rooms = item.columns.map((column) => column.reduce((sum, node) => sum + crossOf(node), 0) + NODE_GAP * Math.max(0, column.length - 1));
|
|
432
|
+
const title = horizontal ? 0 : measure(item.lane.name, FONT.title) + 12;
|
|
433
|
+
const groupHeadroom = horizontal ? 24 * (model.nested?.filter((group) => group.lane === item.lane.id).length || 0) : 0;
|
|
434
|
+
item.size = snap(Math.max(0, ...item.rooms, title, horizontal ? MIN_LANE_LR : MIN_LANE_TB) + 12 + groupHeadroom * 2);
|
|
435
|
+
}
|
|
436
|
+
let nominal = 0;
|
|
437
|
+
const nominalAt = lanes.map((item, index) => {
|
|
438
|
+
if (index) nominal += LANE_GAP;
|
|
439
|
+
const value = nominal;
|
|
440
|
+
nominal += item.size;
|
|
441
|
+
return value;
|
|
442
|
+
});
|
|
443
|
+
const centerOf = (node) => nominalAt[laneIndex.get(node.lane)] + node.outset + crossOf(node) / 2;
|
|
444
|
+
const sources = new Map(nodes.map((node) => [node.id, []]));
|
|
445
|
+
for (const edge of model.edges) if (!back.has(edge)) sources.get(edge.to).push(nodeById.get(edge.from));
|
|
446
|
+
const place = (item, column, index) => {
|
|
447
|
+
let cursor = Math.round((item.size - item.rooms[index]) / 2);
|
|
448
|
+
for (const node of column) {
|
|
449
|
+
node.outset = cursor;
|
|
450
|
+
cursor += crossOf(node) + NODE_GAP;
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
for (let step = 0; step <= maxStep; step += 1) for (const item of lanes) {
|
|
454
|
+
const column = item.columns[step];
|
|
455
|
+
place(item, column, step);
|
|
456
|
+
if (column.length < 2) continue;
|
|
457
|
+
const key = new Map(column.map((node) => {
|
|
458
|
+
const list = sources.get(node.id);
|
|
459
|
+
return [node, list.length ? list.reduce((sum, other) => sum + centerOf(other), 0) / list.length : centerOf(node)];
|
|
460
|
+
}));
|
|
461
|
+
column.sort((a, b) => key.get(a) - key.get(b) || nodes.indexOf(a) - nodes.indexOf(b));
|
|
462
|
+
place(item, column, step);
|
|
463
|
+
}
|
|
464
|
+
const steps = Array.from({ length: maxStep + 1 }, (_, index) => snap(Math.max(MIN_STEP, ...nodes.filter((node) => node.step === index).map(alongOf)) + 12));
|
|
465
|
+
const plans = model.edges.map((edge, index) => {
|
|
466
|
+
const from = nodeById.get(edge.from);
|
|
467
|
+
const to = nodeById.get(edge.to);
|
|
468
|
+
const isBack = back.has(edge);
|
|
469
|
+
return {
|
|
470
|
+
edge,
|
|
471
|
+
index,
|
|
472
|
+
from,
|
|
473
|
+
to,
|
|
474
|
+
isBack,
|
|
475
|
+
label: labelOf.get(edge.id),
|
|
476
|
+
laneFrom: laneIndex.get(from.lane),
|
|
477
|
+
laneTo: laneIndex.get(to.lane),
|
|
478
|
+
outBand: isBack ? from.step - 1 : from.step,
|
|
479
|
+
inBand: isBack ? to.step : to.step - 1,
|
|
480
|
+
outSide: isBack ? -1 : 1,
|
|
481
|
+
inSide: isBack ? 1 : -1,
|
|
482
|
+
port: {
|
|
483
|
+
out: 0,
|
|
484
|
+
in: 0
|
|
485
|
+
}
|
|
486
|
+
};
|
|
487
|
+
});
|
|
488
|
+
const broken = plans.find((plan) => plan.outBand < 0 || plan.inBand < 0);
|
|
489
|
+
if (broken) return fail(broken.edge.line, `连线 ${broken.edge.from} → ${broken.edge.to} 无法在泳道网格内走线`, `Edge ${broken.edge.from} → ${broken.edge.to} cannot be routed on the lane grid`, "把首行的自环拆成两个节点,或让回边指向更晚的步骤", "Split the self loop on the first row into two nodes, or point the back edge at a later step");
|
|
490
|
+
const planOf = new Map(plans.map((plan) => [plan.edge, plan]));
|
|
491
|
+
const middle = (node) => node.outset + crossOf(node) / 2;
|
|
492
|
+
const blocked = (plan, at) => nodes.some((node) => node.lane === plan.from.lane && node !== plan.from && node !== plan.to && node.step > Math.min(plan.from.step, plan.to.step) && node.step < Math.max(plan.from.step, plan.to.step) && at > node.outset - PORT_GAP && at < node.outset + crossOf(node) + PORT_GAP);
|
|
493
|
+
const aligned = (plan) => plan.laneFrom === plan.laneTo && middle(plan.from) === middle(plan.to) && !blocked(plan, middle(plan.from));
|
|
494
|
+
const groups = /* @__PURE__ */ new Map();
|
|
495
|
+
for (const [key, list] of sideLists) {
|
|
496
|
+
const node = nodeById.get(key.slice(0, key.lastIndexOf("|")));
|
|
497
|
+
const items = list.map(({ edge, end }) => ({
|
|
498
|
+
plan: planOf.get(edge),
|
|
499
|
+
end
|
|
500
|
+
}));
|
|
501
|
+
const heading = (item) => centerOf(item.end === "out" ? item.plan.to : item.plan.from);
|
|
502
|
+
items.sort((a, b) => heading(a) - heading(b) || a.plan.index - b.plan.index || (a.end === "out" ? -1 : 1));
|
|
503
|
+
let pinned = items.findIndex((item) => aligned(item.plan) && !item.plan.isBack) >= 0 ? items.findIndex((item) => aligned(item.plan) && !item.plan.isBack) : items.findIndex((item) => aligned(item.plan));
|
|
504
|
+
if (model.mermaid && pinned < 0 && items.length > 1 && items.every(({ plan, end }) => end === "out" && !plan.label && plan.laneFrom !== plan.laneTo) && new Set(items.map(({ plan }) => plan.laneTo)).size === items.length) pinned = items.findIndex(({ plan }) => Math.abs(plan.laneTo - plan.laneFrom) === 1);
|
|
505
|
+
const half = crossOf(node) / 2 - PORT_INSET;
|
|
506
|
+
const reach = pinned >= 0 ? Math.max(pinned, items.length - 1 - pinned) : (items.length - 1) / 2;
|
|
507
|
+
const spacing = items.length < 2 ? 0 : Math.round(Math.max(spacingOf(list), Math.min(PORT_SPREAD, half / reach)));
|
|
508
|
+
const anchor = pinned >= 0 ? pinned : (items.length - 1) / 2;
|
|
509
|
+
const lo = node.outset + PORT_INSET / 2;
|
|
510
|
+
const hi = node.outset + crossOf(node) - PORT_INSET / 2;
|
|
511
|
+
let start = Math.round(middle(node) - anchor * spacing);
|
|
512
|
+
if (model.mermaid && pinned >= 0 && (start < lo || start + (items.length - 1) * spacing > hi)) {
|
|
513
|
+
const room = Math.min(pinned ? (middle(node) - lo) / pinned : Infinity, pinned < items.length - 1 ? (hi - middle(node)) / (items.length - 1 - pinned) : Infinity);
|
|
514
|
+
items.forEach((item, order) => {
|
|
515
|
+
item.plan.port[item.end] = Math.round(middle(node) + (order - pinned) * Math.min(spacing, room));
|
|
516
|
+
});
|
|
517
|
+
const group = {
|
|
518
|
+
items,
|
|
519
|
+
lo,
|
|
520
|
+
hi
|
|
521
|
+
};
|
|
522
|
+
for (const item of items) groups.set(`${item.plan.index}|${item.end}`, group);
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
start = Math.min(Math.max(start, Math.ceil(lo)), Math.floor(hi - (items.length - 1) * spacing));
|
|
526
|
+
items.forEach((item, order) => {
|
|
527
|
+
item.plan.port[item.end] = start + order * spacing;
|
|
528
|
+
});
|
|
529
|
+
const group = {
|
|
530
|
+
items,
|
|
531
|
+
lo,
|
|
532
|
+
hi
|
|
533
|
+
};
|
|
534
|
+
for (const item of items) groups.set(`${item.plan.index}|${item.end}`, group);
|
|
535
|
+
}
|
|
536
|
+
const groupOf = (plan, end) => groups.get(`${plan.index}|${end}`);
|
|
537
|
+
let nearJog = () => false;
|
|
538
|
+
const shiftGroup = (group, delta) => {
|
|
539
|
+
if (!delta || group.items.some(({ plan }) => plan.kind === "straight")) return false;
|
|
540
|
+
const values = group.items.map(({ plan, end }) => plan.port[end] + delta);
|
|
541
|
+
if (values.some((value) => value < group.lo || value > group.hi)) return false;
|
|
542
|
+
if (group.items.some(({ plan, end }, index) => nearJog(plan, end, values[index]))) return false;
|
|
543
|
+
group.items.forEach(({ plan, end }, index) => {
|
|
544
|
+
plan.port[end] = values[index];
|
|
545
|
+
});
|
|
546
|
+
return true;
|
|
547
|
+
};
|
|
548
|
+
const straightable = (plan) => plan.laneFrom === plan.laneTo && plan.port.out === plan.port.in && !blocked(plan, plan.port.out);
|
|
549
|
+
for (const plan of plans) if (straightable(plan)) plan.kind = "straight";
|
|
550
|
+
for (const plan of plans) {
|
|
551
|
+
if (plan.kind || plan.laneFrom !== plan.laneTo) continue;
|
|
552
|
+
const dx = plan.port.in - plan.port.out;
|
|
553
|
+
if (Math.abs(dx) >= PORT_GAP || blocked(plan, plan.port.out)) continue;
|
|
554
|
+
if (shiftGroup(groupOf(plan, "in"), -dx) || shiftGroup(groupOf(plan, "out"), dx)) plan.kind = "straight";
|
|
555
|
+
}
|
|
556
|
+
for (const plan of plans) {
|
|
557
|
+
if (plan.kind) continue;
|
|
558
|
+
plan.kind = plan.outBand === plan.inBand ? "jump" : "bend";
|
|
559
|
+
if (plan.kind === "bend") plan.corridor = plan.laneFrom !== plan.laneTo ? Math.max(plan.laneFrom, plan.laneTo) : (plan.port.out + plan.port.in) / 2 < lanes[plan.laneFrom].size / 2 ? plan.laneFrom : plan.laneFrom + 1;
|
|
560
|
+
}
|
|
561
|
+
const corridorRuns = /* @__PURE__ */ new Map();
|
|
562
|
+
for (const plan of plans) {
|
|
563
|
+
if (plan.kind !== "bend") continue;
|
|
564
|
+
plan.rail = {
|
|
565
|
+
index: plan.index,
|
|
566
|
+
lo: Math.min(plan.outBand, plan.inBand),
|
|
567
|
+
hi: Math.max(plan.outBand, plan.inBand)
|
|
568
|
+
};
|
|
569
|
+
if (!corridorRuns.has(plan.corridor)) corridorRuns.set(plan.corridor, []);
|
|
570
|
+
corridorRuns.get(plan.corridor).push(plan.rail);
|
|
571
|
+
}
|
|
572
|
+
const gaps = Array.from({ length: laneCount + 1 }, (_, index) => capacity(assignTracks(corridorRuns.get(index) || []), index === 0 || index === laneCount ? OUTER : LANE_GAP));
|
|
573
|
+
let crossCursor = gaps[0];
|
|
574
|
+
const laneAt = lanes.map((item, index) => {
|
|
575
|
+
if (index) crossCursor += gaps[index];
|
|
576
|
+
const value = crossCursor;
|
|
577
|
+
crossCursor += item.size;
|
|
578
|
+
return value;
|
|
579
|
+
});
|
|
580
|
+
const crossTotal = crossCursor + gaps[laneCount];
|
|
581
|
+
const gapEnd = (corridor) => corridor <= 0 ? 0 : laneAt[corridor - 1] + lanes[corridor - 1].size;
|
|
582
|
+
const railAt = (plan) => gapEnd(plan.corridor) + CLEAR + plan.rail.track * TRACK;
|
|
583
|
+
const at = (plan, end) => laneAt[end === "out" ? plan.laneFrom : plan.laneTo] + plan.port[end];
|
|
584
|
+
const bandRuns = /* @__PURE__ */ new Map();
|
|
585
|
+
const addRun = (band, run) => {
|
|
586
|
+
if (!bandRuns.has(band)) bandRuns.set(band, []);
|
|
587
|
+
bandRuns.get(band).push(run);
|
|
588
|
+
};
|
|
589
|
+
for (const plan of plans) if (plan.kind === "jump") {
|
|
590
|
+
const run = {
|
|
591
|
+
plan,
|
|
592
|
+
index: plan.index,
|
|
593
|
+
ends: ["out", "in"]
|
|
594
|
+
};
|
|
595
|
+
plan.runs = {
|
|
596
|
+
out: run,
|
|
597
|
+
in: run
|
|
598
|
+
};
|
|
599
|
+
addRun(plan.outBand, run);
|
|
600
|
+
} else if (plan.kind === "bend") {
|
|
601
|
+
plan.runs = {
|
|
602
|
+
out: {
|
|
603
|
+
plan,
|
|
604
|
+
index: plan.index,
|
|
605
|
+
ends: ["out"],
|
|
606
|
+
rail: true
|
|
607
|
+
},
|
|
608
|
+
in: {
|
|
609
|
+
plan,
|
|
610
|
+
index: plan.index,
|
|
611
|
+
ends: ["in"],
|
|
612
|
+
rail: true
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
addRun(plan.outBand, plan.runs.out);
|
|
616
|
+
addRun(plan.inBand, plan.runs.in);
|
|
617
|
+
}
|
|
618
|
+
const classOf = (plan, end) => (end === "out" ? plan.outSide : plan.inSide) > 0 ? "upper" : "lower";
|
|
619
|
+
const throughs = (band) => plans.filter((plan) => plan.kind === "straight" && band >= Math.min(plan.outBand, plan.inBand) && band <= Math.max(plan.outBand, plan.inBand));
|
|
620
|
+
const stubsOf = (runs) => runs.flatMap((run) => run.ends.map((end) => ({
|
|
621
|
+
run,
|
|
622
|
+
plan: run.plan,
|
|
623
|
+
end,
|
|
624
|
+
at: at(run.plan, end),
|
|
625
|
+
cls: classOf(run.plan, end)
|
|
626
|
+
})));
|
|
627
|
+
const jogsIn = (band) => (bandRuns.get(band) || []).filter((run) => run.after).map((run) => run.jog);
|
|
628
|
+
const railsIn = (band) => plans.filter((plan) => plan.kind === "bend" && band >= plan.rail.lo && band <= plan.rail.hi).map(railAt);
|
|
629
|
+
nearJog = (plan, end, value) => jogsIn(end === "out" ? plan.outBand : plan.inBand).some((jog) => Math.abs(jog - (laneAt[end === "out" ? plan.laneFrom : plan.laneTo] + value)) < PORT_GAP);
|
|
630
|
+
const conflictsIn = (band) => {
|
|
631
|
+
const runs = bandRuns.get(band) || [];
|
|
632
|
+
const stubs = stubsOf(runs);
|
|
633
|
+
const found = [];
|
|
634
|
+
for (let i = 0; i < stubs.length; i += 1) for (let j = i + 1; j < stubs.length; j += 1) {
|
|
635
|
+
const [a, b] = [stubs[i], stubs[j]];
|
|
636
|
+
if (a.run === b.run || Math.abs(a.at - b.at) >= PORT_GAP) continue;
|
|
637
|
+
if (a.cls === b.cls) found.push({
|
|
638
|
+
kind: "apart",
|
|
639
|
+
a,
|
|
640
|
+
b
|
|
641
|
+
});
|
|
642
|
+
else found.push({
|
|
643
|
+
kind: "order",
|
|
644
|
+
upper: a.cls === "upper" ? a : b,
|
|
645
|
+
lower: a.cls === "upper" ? b : a
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
for (const through of throughs(band)) for (const stub of stubs) if (stub.plan !== through && Math.abs(stub.at - at(through, "out")) < PORT_GAP) found.push({
|
|
649
|
+
kind: "apart",
|
|
650
|
+
a: { at: at(through, "out") },
|
|
651
|
+
b: stub
|
|
652
|
+
});
|
|
653
|
+
return found;
|
|
654
|
+
};
|
|
655
|
+
const keyOf = (item) => item.kind === "order" ? `o|${item.upper.plan.index}|${item.upper.end}|${item.lower.plan.index}|${item.lower.end}` : `a|${item.a.plan?.index ?? "s"}|${item.a.end ?? ""}|${item.b.plan.index}|${item.b.end}`;
|
|
656
|
+
const moveAway = (stub, fixed) => {
|
|
657
|
+
const right = fixed + PORT_GAP - stub.at;
|
|
658
|
+
const left = fixed - PORT_GAP - stub.at;
|
|
659
|
+
return (Math.abs(right) <= Math.abs(left) ? [right, left] : [left, right]).some((delta) => shiftGroup(groupOf(stub.plan, stub.end), delta));
|
|
660
|
+
};
|
|
661
|
+
const ignored = /* @__PURE__ */ new Set();
|
|
662
|
+
const tried = /* @__PURE__ */ new Set();
|
|
663
|
+
const orderEdges = (band) => {
|
|
664
|
+
const edges = conflictsIn(band).filter((item) => item.kind === "order" && !ignored.has(keyOf(item))).map((item) => ({
|
|
665
|
+
from: item.upper.run,
|
|
666
|
+
to: item.lower.run,
|
|
667
|
+
item
|
|
668
|
+
}));
|
|
669
|
+
for (const run of bandRuns.get(band) || []) if (run.after) edges.push({
|
|
670
|
+
from: run,
|
|
671
|
+
to: run.after,
|
|
672
|
+
item: null
|
|
673
|
+
});
|
|
674
|
+
return edges;
|
|
675
|
+
};
|
|
676
|
+
const dogleg = (run, band) => {
|
|
677
|
+
const { plan } = run;
|
|
678
|
+
const upperEnd = run.ends.find((end) => classOf(plan, end) === "upper");
|
|
679
|
+
const lowerEnd = run.ends.find((end) => classOf(plan, end) === "lower");
|
|
680
|
+
if (!upperEnd || !lowerEnd) return false;
|
|
681
|
+
const [lo, hi] = [Math.min(at(plan, upperEnd), at(plan, lowerEnd)), Math.max(at(plan, upperEnd), at(plan, lowerEnd))];
|
|
682
|
+
const marks = [
|
|
683
|
+
lo,
|
|
684
|
+
hi,
|
|
685
|
+
...stubsOf(bandRuns.get(band)).map((stub) => stub.at),
|
|
686
|
+
...throughs(band).map((other) => at(other, "out")),
|
|
687
|
+
...jogsIn(band),
|
|
688
|
+
...railsIn(band)
|
|
689
|
+
].filter((value) => value >= lo && value <= hi).sort((a, b) => a - b);
|
|
690
|
+
let best = null;
|
|
691
|
+
for (let i = 1; i < marks.length; i += 1) {
|
|
692
|
+
const gap = marks[i] - marks[i - 1];
|
|
693
|
+
if (gap >= 24 && (!best || gap > best.gap)) best = {
|
|
694
|
+
gap,
|
|
695
|
+
x: Math.round((marks[i] + marks[i - 1]) / 2)
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
if (!best) return false;
|
|
699
|
+
const low = {
|
|
700
|
+
plan,
|
|
701
|
+
index: plan.index,
|
|
702
|
+
ends: [lowerEnd],
|
|
703
|
+
jog: best.x
|
|
704
|
+
};
|
|
705
|
+
const up = {
|
|
706
|
+
plan,
|
|
707
|
+
index: plan.index,
|
|
708
|
+
ends: [upperEnd],
|
|
709
|
+
jog: best.x,
|
|
710
|
+
after: low
|
|
711
|
+
};
|
|
712
|
+
const list = bandRuns.get(band);
|
|
713
|
+
list.splice(list.indexOf(run), 1, up, low);
|
|
714
|
+
plan.runs = {
|
|
715
|
+
[upperEnd]: up,
|
|
716
|
+
[lowerEnd]: low
|
|
717
|
+
};
|
|
718
|
+
return true;
|
|
719
|
+
};
|
|
720
|
+
const spread = (run) => Math.abs(at(run.plan, "out") - at(run.plan, "in"));
|
|
721
|
+
const bandsWithRuns = [...bandRuns.keys()].sort((a, b) => a - b);
|
|
722
|
+
for (let round = 0; round < 96; round += 1) {
|
|
723
|
+
let apart = null;
|
|
724
|
+
let cycle = null;
|
|
725
|
+
let cycleBand = null;
|
|
726
|
+
for (const band of bandsWithRuns) {
|
|
727
|
+
apart = conflictsIn(band).find((item) => item.kind === "apart" && !tried.has(keyOf(item)));
|
|
728
|
+
if (apart) break;
|
|
729
|
+
cycle = findCycle(bandRuns.get(band), orderEdges(band));
|
|
730
|
+
if (cycle) {
|
|
731
|
+
cycleBand = band;
|
|
732
|
+
break;
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
if (apart) {
|
|
736
|
+
tried.add(keyOf(apart));
|
|
737
|
+
if (!moveAway(apart.b, apart.a.at) && apart.a.plan) moveAway(apart.a, apart.b.at);
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
if (!cycle) break;
|
|
741
|
+
const conflicts = cycle.filter((edge) => edge.item).map((edge) => edge.item);
|
|
742
|
+
const shift = conflicts.find((item) => !tried.has(keyOf(item)));
|
|
743
|
+
if (shift) {
|
|
744
|
+
tried.add(keyOf(shift));
|
|
745
|
+
moveAway(shift.lower, shift.upper.at) || moveAway(shift.upper, shift.lower.at);
|
|
746
|
+
continue;
|
|
747
|
+
}
|
|
748
|
+
if (![...new Set(cycle.flatMap((edge) => [edge.from, edge.to]))].filter((run) => run.ends.length === 2).sort((a, b) => spread(b) - spread(a) || a.index - b.index).some((run) => dogleg(run, cycleBand))) ignored.add(keyOf(conflicts[0]));
|
|
749
|
+
}
|
|
750
|
+
const zones = [];
|
|
751
|
+
for (const plan of plans) {
|
|
752
|
+
if (!plan.label) continue;
|
|
753
|
+
const band = plan.outBand;
|
|
754
|
+
const top = !plan.isBack;
|
|
755
|
+
const center = at(plan, "out");
|
|
756
|
+
const span = plan.label.cross;
|
|
757
|
+
const m = Math.max(1, Math.ceil((8 + plan.label.along - CLEAR) / TRACK));
|
|
758
|
+
const avoid = [
|
|
759
|
+
...stubsOf(bandRuns.get(band) || []).filter((stub) => stub.plan !== plan && stub.cls === (top ? "upper" : "lower")).map((stub) => stub.at),
|
|
760
|
+
...throughs(band).filter((other) => other !== plan).map((other) => at(other, "out")),
|
|
761
|
+
...plans.filter((other) => other.kind === "bend" && other !== plan && band >= other.rail.lo && band <= other.rail.hi).map(railAt)
|
|
762
|
+
];
|
|
763
|
+
const taken = zones.filter((zone) => zone.band === band && zone.top === top);
|
|
764
|
+
const fits = (lo) => lo >= 0 && lo + span <= crossTotal && avoid.every((value) => value < lo - 2 || value > lo + span + 2) && taken.every((zone) => zone.hi + 4 <= lo || lo + span + 4 <= zone.lo);
|
|
765
|
+
const base = Math.round(center - span / 2);
|
|
766
|
+
const reach = Math.max(0, Math.floor(span / 2 - LABEL_EDGE));
|
|
767
|
+
let lo = base;
|
|
768
|
+
for (let shift = 0; shift <= reach; shift += 2) {
|
|
769
|
+
if (fits(base - shift)) {
|
|
770
|
+
lo = base - shift;
|
|
771
|
+
break;
|
|
772
|
+
}
|
|
773
|
+
if (fits(base + shift)) {
|
|
774
|
+
lo = base + shift;
|
|
775
|
+
break;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
if (model.mermaid) {
|
|
779
|
+
const beside = [center + LABEL_GAP, center - span - LABEL_GAP].find(fits);
|
|
780
|
+
if (beside != null) lo = beside;
|
|
781
|
+
}
|
|
782
|
+
zones.push({
|
|
783
|
+
plan,
|
|
784
|
+
band,
|
|
785
|
+
top,
|
|
786
|
+
m,
|
|
787
|
+
lo,
|
|
788
|
+
hi: lo + span
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
const bandTracks = /* @__PURE__ */ new Map();
|
|
792
|
+
for (let band = 0; band <= maxStep; band += 1) {
|
|
793
|
+
const runs = bandRuns.get(band) || [];
|
|
794
|
+
const tops = zones.filter((zone) => zone.band === band && zone.top);
|
|
795
|
+
const bottoms = zones.filter((zone) => zone.band === band && !zone.top);
|
|
796
|
+
if (!runs.length && !zones.some((zone) => zone.band === band)) continue;
|
|
797
|
+
const before = new Map(runs.map((run) => [run, /* @__PURE__ */ new Set()]));
|
|
798
|
+
for (const order of orderEdges(band)) before.get(order.to).add(order.from);
|
|
799
|
+
for (const run of runs) {
|
|
800
|
+
const values = run.ends.map((end) => at(run.plan, end));
|
|
801
|
+
if (run.rail) values.push(railAt(run.plan));
|
|
802
|
+
if (run.jog != null) values.push(run.jog);
|
|
803
|
+
run.lo = Math.min(...values);
|
|
804
|
+
run.hi = Math.max(...values);
|
|
805
|
+
}
|
|
806
|
+
const order = [];
|
|
807
|
+
const pending = new Set(runs);
|
|
808
|
+
const rank = (a, b) => a.lo - b.lo || a.hi - b.hi || a.index - b.index;
|
|
809
|
+
while (pending.size) {
|
|
810
|
+
const ready = [...pending].filter((run) => [...before.get(run)].every((prev) => !pending.has(prev)));
|
|
811
|
+
const pick = (ready.length ? ready : [...pending]).sort(rank)[0];
|
|
812
|
+
order.push(pick);
|
|
813
|
+
pending.delete(pick);
|
|
814
|
+
}
|
|
815
|
+
const tracks = [];
|
|
816
|
+
const occupy = (track, lo, hi, owner) => (tracks[track] ||= []).push({
|
|
817
|
+
lo,
|
|
818
|
+
hi,
|
|
819
|
+
owner
|
|
820
|
+
});
|
|
821
|
+
for (const zone of tops) for (let track = 0; track < zone.m; track += 1) occupy(track, zone.lo, zone.hi, zone.plan);
|
|
822
|
+
for (const run of order) {
|
|
823
|
+
let floor = 0;
|
|
824
|
+
for (const prev of before.get(run)) if (prev.track != null) floor = Math.max(floor, prev.track + 1);
|
|
825
|
+
for (const zone of tops) {
|
|
826
|
+
const lowerInside = run.ends.some((end) => classOf(run.plan, end) === "lower" && at(run.plan, end) >= zone.lo - 2 && at(run.plan, end) <= zone.hi + 2);
|
|
827
|
+
if (zone.plan === run.plan || lowerInside) floor = Math.max(floor, zone.m);
|
|
828
|
+
}
|
|
829
|
+
let track = floor;
|
|
830
|
+
while ((tracks[track] || []).some((item) => item.owner !== run.plan && item.lo <= run.hi && run.lo <= item.hi)) track += 1;
|
|
831
|
+
occupy(track, run.lo, run.hi, run.plan);
|
|
832
|
+
run.track = track;
|
|
833
|
+
}
|
|
834
|
+
const used = tracks.length;
|
|
835
|
+
const below = Math.max(0, ...bottoms.map((zone) => zone.m));
|
|
836
|
+
bandTracks.set(band, used + below);
|
|
837
|
+
}
|
|
838
|
+
const bands = Array.from({ length: maxStep + 1 }, (_, band) => capacity(bandTracks.get(band) || 0, STEP_GAP));
|
|
839
|
+
let alongCursor = horizontal ? snap(Math.max(HEADER, ...model.lanes.map((lane) => measure(lane.name, FONT.title) + 24))) : HEADER + 24 * (model.nested?.length || 0);
|
|
840
|
+
const alongAt = Array.from({ length: maxStep + 1 }, (_, index) => {
|
|
841
|
+
if (index) alongCursor += bands[index - 1] + steps[index - 1];
|
|
842
|
+
return alongCursor;
|
|
843
|
+
});
|
|
844
|
+
const alongTotal = alongAt[maxStep] + steps[maxStep] + (bandTracks.has(maxStep) ? bands[maxStep] : 0);
|
|
845
|
+
const canvasW = horizontal ? Math.max(snap(width), alongTotal) : crossTotal;
|
|
846
|
+
const canvasH = horizontal ? crossTotal : alongTotal;
|
|
847
|
+
const bandStart = (band) => alongAt[band] + steps[band];
|
|
848
|
+
const trackAt = (band, track) => bandStart(band) + CLEAR + track * TRACK;
|
|
849
|
+
for (const node of nodes) {
|
|
850
|
+
node.cross = laneAt[laneIndex.get(node.lane)] + node.outset;
|
|
851
|
+
node.along = alongAt[node.step] + Math.round((steps[node.step] - alongOf(node)) / 2);
|
|
852
|
+
const box = point(node.cross, node.along);
|
|
853
|
+
node.x = box.x;
|
|
854
|
+
node.y = box.y;
|
|
855
|
+
}
|
|
856
|
+
const portPoint = (node, cross, side) => {
|
|
857
|
+
const shift = Math.abs(cross - (node.cross + crossOf(node) / 2));
|
|
858
|
+
const r = crossOf(node) / 2;
|
|
859
|
+
const curve = model.mermaid && node.shape === "circle" ? r : 0;
|
|
860
|
+
const depth = node.diamond ? Math.min(alongOf(node) / 2, shift * alongOf(node) / crossOf(node)) : curve ? r - Math.sqrt(Math.max(0, r * r - shift * shift)) : 0;
|
|
861
|
+
return point(cross, Math.round(side > 0 ? node.along + alongOf(node) - depth : node.along + depth));
|
|
862
|
+
};
|
|
863
|
+
const edges = plans.map((plan) => {
|
|
864
|
+
const outCross = at(plan, "out");
|
|
865
|
+
const inCross = at(plan, "in");
|
|
866
|
+
const middle = [];
|
|
867
|
+
if (plan.kind === "jump" && plan.runs.out === plan.runs.in) {
|
|
868
|
+
const along = trackAt(plan.outBand, plan.runs.out.track);
|
|
869
|
+
middle.push(point(outCross, along), point(inCross, along));
|
|
870
|
+
} else if (plan.kind === "jump") {
|
|
871
|
+
const out = trackAt(plan.outBand, plan.runs.out.track);
|
|
872
|
+
const into = trackAt(plan.outBand, plan.runs.in.track);
|
|
873
|
+
middle.push(point(outCross, out), point(plan.runs.out.jog, out), point(plan.runs.out.jog, into), point(inCross, into));
|
|
874
|
+
} else if (plan.kind === "bend") {
|
|
875
|
+
const rail = railAt(plan);
|
|
876
|
+
const out = trackAt(plan.outBand, plan.runs.out.track);
|
|
877
|
+
const into = trackAt(plan.inBand, plan.runs.in.track);
|
|
878
|
+
middle.push(point(outCross, out), point(rail, out), point(rail, into), point(inCross, into));
|
|
879
|
+
}
|
|
880
|
+
return {
|
|
881
|
+
id: plan.edge.id,
|
|
882
|
+
from: plan.edge.from,
|
|
883
|
+
to: plan.edge.to,
|
|
884
|
+
style: plan.edge.style,
|
|
885
|
+
line: plan.edge.line,
|
|
886
|
+
back: plan.isBack,
|
|
887
|
+
points: simplify([
|
|
888
|
+
portPoint(plan.from, outCross, plan.outSide),
|
|
889
|
+
...middle,
|
|
890
|
+
portPoint(plan.to, inCross, plan.inSide)
|
|
891
|
+
]),
|
|
892
|
+
label: null
|
|
893
|
+
};
|
|
894
|
+
});
|
|
895
|
+
if (model.mermaid) for (const plan of plans) {
|
|
896
|
+
if (!plan.from.diamond || plan.to.shape !== "rect" || plan.isBack || plan.laneFrom === plan.laneTo || plan.kind !== "jump") continue;
|
|
897
|
+
const sign = Math.sign(plan.laneTo - plan.laneFrom);
|
|
898
|
+
const sourceAlong = plan.from.along + alongOf(plan.from) / 2;
|
|
899
|
+
const targetAlong = plan.to.along;
|
|
900
|
+
const sourceCross = plan.from.cross + (sign > 0 ? crossOf(plan.from) : 0);
|
|
901
|
+
const targetCross = plan.to.cross + crossOf(plan.to) / 2;
|
|
902
|
+
const boundary = Math.max(plan.laneFrom, plan.laneTo);
|
|
903
|
+
const rail = laneAt[boundary] - gaps[boundary] / 2;
|
|
904
|
+
if (targetAlong - sourceAlong <= 8 || (rail - sourceCross) * sign < 4 || (targetCross - rail) * sign < 4) continue;
|
|
905
|
+
if (!nodes.every((node) => node === plan.from || node === plan.to || node.along + alongOf(node) < sourceAlong - 4 || node.along > targetAlong + 4 || node.cross + crossOf(node) < Math.min(sourceCross, targetCross) - 4 || node.cross > Math.max(sourceCross, targetCross) + 4)) continue;
|
|
906
|
+
const route = [
|
|
907
|
+
point(sourceCross, sourceAlong),
|
|
908
|
+
point(rail, sourceAlong),
|
|
909
|
+
point(rail, targetAlong - 8),
|
|
910
|
+
point(targetCross, targetAlong - 8),
|
|
911
|
+
point(targetCross, targetAlong)
|
|
912
|
+
];
|
|
913
|
+
const shared = (a, b) => a.w === 0 && b.w === 0 && a.x === b.x && Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y) > 4 || a.h === 0 && b.h === 0 && a.y === b.y && Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x) > 4;
|
|
914
|
+
if (edges.some((edge, index) => index !== plan.index && segments(edge.points).some((b) => segments(route).some((a) => shared(a, b))))) continue;
|
|
915
|
+
edges[plan.index].points = route;
|
|
916
|
+
plan.sideBranch = {
|
|
917
|
+
sourceAlong,
|
|
918
|
+
rail
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
const hops = crossingsOf(edges);
|
|
922
|
+
for (const edge of edges) {
|
|
923
|
+
edge.hops = hops.get(edge) || [];
|
|
924
|
+
edge.d = pathData(edge.points, edge.hops);
|
|
925
|
+
}
|
|
926
|
+
for (const zone of zones) {
|
|
927
|
+
const { label } = zone.plan;
|
|
928
|
+
const along = zone.top ? bandStart(zone.band) + ZONE_PAD : bandStart(zone.band) + bands[zone.band] - ZONE_PAD - label.along;
|
|
929
|
+
const box = zone.plan.sideBranch ? point(zone.plan.sideBranch.rail - label.cross / 2, zone.plan.sideBranch.sourceAlong + LABEL_GAP) : point(zone.lo, along);
|
|
930
|
+
edges[zone.plan.index].label = {
|
|
931
|
+
x: box.x,
|
|
932
|
+
y: box.y,
|
|
933
|
+
w: label.w,
|
|
934
|
+
h: label.h,
|
|
935
|
+
lines: label.lines
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
const laneGeometry = model.lanes.map((lane, index) => horizontal ? {
|
|
939
|
+
id: lane.id,
|
|
940
|
+
name: lane.name,
|
|
941
|
+
line: lane.line,
|
|
942
|
+
nodes: lane.nodes,
|
|
943
|
+
x: 0,
|
|
944
|
+
y: laneAt[index],
|
|
945
|
+
w: canvasW,
|
|
946
|
+
h: lanes[index].size
|
|
947
|
+
} : {
|
|
948
|
+
id: lane.id,
|
|
949
|
+
name: lane.name,
|
|
950
|
+
line: lane.line,
|
|
951
|
+
nodes: lane.nodes,
|
|
952
|
+
x: laneAt[index],
|
|
953
|
+
y: 0,
|
|
954
|
+
w: lanes[index].size,
|
|
955
|
+
h: canvasH
|
|
956
|
+
});
|
|
957
|
+
if (model.reverse) {
|
|
958
|
+
const axis = horizontal ? "x" : "y", size = horizontal ? "w" : "h";
|
|
959
|
+
const extent = horizontal ? canvasW : canvasH;
|
|
960
|
+
for (const box of [...nodes, ...edges.map((edge) => edge.label).filter(Boolean)]) box[axis] = extent - box[axis] - box[size];
|
|
961
|
+
for (const edge of edges) {
|
|
962
|
+
for (const point of [...edge.points, ...edge.hops]) point[axis] = extent - point[axis];
|
|
963
|
+
edge.d = pathData(edge.points, edge.hops);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
return {
|
|
967
|
+
type: "swimlane",
|
|
968
|
+
direction: model.direction,
|
|
969
|
+
width: canvasW,
|
|
970
|
+
height: canvasH,
|
|
971
|
+
lanes: laneGeometry,
|
|
972
|
+
nodes,
|
|
973
|
+
edges,
|
|
974
|
+
errors: [],
|
|
975
|
+
model
|
|
976
|
+
};
|
|
977
|
+
}
|
|
978
|
+
function iconAt(ctx, icon, x, y, color) {
|
|
979
|
+
const [left, top, w, h] = icon.viewBox.split(/\s+/).map(Number), scale = 14 / Math.max(w, h);
|
|
980
|
+
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, {
|
|
981
|
+
...attrs,
|
|
982
|
+
...icon.set === "simple" ? { fill: color } : {
|
|
983
|
+
fill: "none",
|
|
984
|
+
stroke: color,
|
|
985
|
+
"stroke-width": 1.6,
|
|
986
|
+
"stroke-linecap": "round",
|
|
987
|
+
"stroke-linejoin": "round"
|
|
988
|
+
}
|
|
989
|
+
})));
|
|
990
|
+
}
|
|
991
|
+
var linked = (ctx, item, drawing) => item?.link ? ctx.link(item.link.href, [drawing], item.link) : drawing;
|
|
992
|
+
function scene(layout, ctx) {
|
|
993
|
+
const model = layout.model;
|
|
994
|
+
const mermaid = model?.mermaid;
|
|
995
|
+
const flowEdges = new Map(model?.edges.map((edge) => [edge.id, edge]));
|
|
996
|
+
const flowNodes = new Map(mermaid ? model.nodes.map((node) => [node.id, node]) : []);
|
|
997
|
+
const appearance$1 = appearance(ctx);
|
|
998
|
+
const palette = Array.from({ length: 6 }, (_, index) => ctx.frameTone(ctx.color(`tone${index + 1}`)));
|
|
999
|
+
const marker = "swimlane-arrow";
|
|
1000
|
+
const children = [ctx.el("defs", {}, [ctx.el("marker", {
|
|
1001
|
+
id: marker,
|
|
1002
|
+
viewBox: "0 0 8 8",
|
|
1003
|
+
refX: 7,
|
|
1004
|
+
refY: 4,
|
|
1005
|
+
markerWidth: 8,
|
|
1006
|
+
markerHeight: 8,
|
|
1007
|
+
markerUnits: "userSpaceOnUse",
|
|
1008
|
+
orient: mermaid ? "auto-start-reverse" : "auto"
|
|
1009
|
+
}, [ctx.path("M0 0 L8 4 L0 8 Z", { fill: appearance$1.fg4 })])])];
|
|
1010
|
+
for (const [index, lane] of layout.lanes.entries()) {
|
|
1011
|
+
const authored = mermaid && model.lanes[index];
|
|
1012
|
+
const color = appearance$1[authored?.class] || palette[index % palette.length];
|
|
1013
|
+
const group = ctx.el("g", {}, [ctx.rect(lane.x, lane.y, lane.w, lane.h, {
|
|
1014
|
+
fill: ctx.tint(appearance$1.bg, color, .07),
|
|
1015
|
+
stroke: ctx.tint(appearance$1.bg, color, .45),
|
|
1016
|
+
"stroke-width": 1,
|
|
1017
|
+
rx: appearance$1.radius
|
|
1018
|
+
}), ctx.text(layout.direction === "LR" ? model?.reverse ? lane.w - 12 : 12 : lane.x + lane.w / 2, layout.direction === "LR" ? lane.y + lane.h / 2 : model?.reverse ? lane.h - 18 : 18, lane.name, {
|
|
1019
|
+
size: FONT.title.size,
|
|
1020
|
+
weight: FONT.title.weight,
|
|
1021
|
+
fill: appearance$1.fg1,
|
|
1022
|
+
anchor: layout.direction === "LR" ? model?.reverse ? "end" : "start" : "middle",
|
|
1023
|
+
"dominant-baseline": "central"
|
|
1024
|
+
})]);
|
|
1025
|
+
group.meta = {
|
|
1026
|
+
lane: lane.id,
|
|
1027
|
+
...mermaid ? {
|
|
1028
|
+
kind: "frame",
|
|
1029
|
+
id: lane.id
|
|
1030
|
+
} : {}
|
|
1031
|
+
};
|
|
1032
|
+
children.push(linked(ctx, authored, group));
|
|
1033
|
+
}
|
|
1034
|
+
if (mermaid) {
|
|
1035
|
+
const nodes = new Map(layout.nodes.map((node) => [node.id, node]));
|
|
1036
|
+
const groups = model.nested.filter((group) => group.nodes.length);
|
|
1037
|
+
for (const [index, group] of groups.entries()) {
|
|
1038
|
+
const members = group.nodes.map((id) => nodes.get(id));
|
|
1039
|
+
const lane = layout.lanes.find((item) => item.id === group.lane);
|
|
1040
|
+
const x = Math.min(...members.map((node) => node.x)) - 4;
|
|
1041
|
+
const titleY = Math.min(...members.map((node) => node.y)) - 14 - groups.slice(index + 1).filter((item) => item.lane === group.lane).length * 20;
|
|
1042
|
+
const y = Math.max(lane.y + 2, titleY - 8);
|
|
1043
|
+
const titleWidth = ctx.measure(group.title, FONT.label.size, 600) + 12;
|
|
1044
|
+
const w = Math.min(lane.x + lane.w - x - 2, Math.max(titleWidth, ...members.map((node) => node.x + node.w + 4 - x)));
|
|
1045
|
+
const h = Math.max(...members.map((node) => node.y + node.h + 4 - y));
|
|
1046
|
+
children.push(ctx.rect(x, y, w, h, {
|
|
1047
|
+
fill: appearance$1.surface2,
|
|
1048
|
+
stroke: appearance$1.line2,
|
|
1049
|
+
rx: appearance$1.smallRadius,
|
|
1050
|
+
meta: {
|
|
1051
|
+
kind: "frame",
|
|
1052
|
+
id: group.id
|
|
1053
|
+
}
|
|
1054
|
+
}));
|
|
1055
|
+
children.push(ctx.text(x + 6, y + 9, group.title, {
|
|
1056
|
+
size: FONT.label.size,
|
|
1057
|
+
weight: 600,
|
|
1058
|
+
anchor: "start",
|
|
1059
|
+
"dominant-baseline": "central",
|
|
1060
|
+
fill: appearance$1.fg2
|
|
1061
|
+
}));
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
for (const edge of layout.edges) {
|
|
1065
|
+
const flow = flowEdges.get(edge.id);
|
|
1066
|
+
if (mermaid && edge.style === "invisible") continue;
|
|
1067
|
+
children.push(linked(ctx, flow, ctx.path(edge.d, {
|
|
1068
|
+
fill: "none",
|
|
1069
|
+
stroke: mermaid && appearance$1[flow.status] || appearance$1.fg4,
|
|
1070
|
+
"stroke-width": mermaid && edge.style === "bold" ? 2.5 : 1.25,
|
|
1071
|
+
"stroke-linejoin": "round",
|
|
1072
|
+
...edge.style === "dashed" ? { "stroke-dasharray": "5 4" } : {},
|
|
1073
|
+
...!mermaid || flow.endArrow ? { "marker-end": `url(#${marker})` } : {},
|
|
1074
|
+
...mermaid && flow.startArrow ? { "marker-start": `url(#${marker})` } : {},
|
|
1075
|
+
meta: {
|
|
1076
|
+
edge: edge.id,
|
|
1077
|
+
back: edge.back,
|
|
1078
|
+
...mermaid ? {
|
|
1079
|
+
kind: "edge",
|
|
1080
|
+
from: edge.from,
|
|
1081
|
+
to: edge.to,
|
|
1082
|
+
...flow.status ? { status: flow.status } : {}
|
|
1083
|
+
} : {}
|
|
1084
|
+
}
|
|
1085
|
+
})));
|
|
1086
|
+
}
|
|
1087
|
+
for (const node of layout.nodes) {
|
|
1088
|
+
const color = palette[Math.max(0, layout.lanes.findIndex((lane) => lane.id === node.lane)) % palette.length];
|
|
1089
|
+
const stroke = node.class && appearance$1[node.class] ? appearance$1[node.class] : ctx.tint(appearance$1.bg, color, .4);
|
|
1090
|
+
const shape = mermaid && node.shape === "circle" ? ctx.shape("circle", {
|
|
1091
|
+
cx: node.x + node.w / 2,
|
|
1092
|
+
cy: node.y + node.h / 2,
|
|
1093
|
+
r: node.w / 2,
|
|
1094
|
+
fill: appearance$1.node,
|
|
1095
|
+
stroke,
|
|
1096
|
+
"stroke-width": 1
|
|
1097
|
+
}) : node.shape === "diamond" ? ctx.shape("polygon", {
|
|
1098
|
+
points: `${node.x + node.w / 2},${node.y} ${node.x + node.w},${node.y + node.h / 2} ${node.x + node.w / 2},${node.y + node.h} ${node.x},${node.y + node.h / 2}`,
|
|
1099
|
+
fill: appearance$1.node,
|
|
1100
|
+
stroke,
|
|
1101
|
+
"stroke-width": 1
|
|
1102
|
+
}, {
|
|
1103
|
+
node: node.id,
|
|
1104
|
+
shape: node.shape,
|
|
1105
|
+
...node.class ? { status: node.class } : {}
|
|
1106
|
+
}) : ctx.rect(node.x, node.y, node.w, node.h, {
|
|
1107
|
+
fill: appearance$1.node,
|
|
1108
|
+
stroke,
|
|
1109
|
+
"stroke-width": 1,
|
|
1110
|
+
rx: mermaid && node.shape === "stadium" ? node.h / 2 : mermaid && node.shape === "rect" ? 0 : appearance$1.smallRadius,
|
|
1111
|
+
meta: {
|
|
1112
|
+
node: node.id,
|
|
1113
|
+
shape: node.shape,
|
|
1114
|
+
...node.class ? { status: node.class } : {}
|
|
1115
|
+
}
|
|
1116
|
+
});
|
|
1117
|
+
if (mermaid) shape.meta = {
|
|
1118
|
+
...shape.meta,
|
|
1119
|
+
kind: "node",
|
|
1120
|
+
id: node.id
|
|
1121
|
+
};
|
|
1122
|
+
const mark = statusMarks[node.class];
|
|
1123
|
+
const authored = flowNodes.get(node.id), icon = authored?.icon;
|
|
1124
|
+
const glyphs = [];
|
|
1125
|
+
if (icon) {
|
|
1126
|
+
const width = Math.max(0, ...node.lines.map((line) => ctx.measure(line, FONT.body.size, FONT.body.weight)));
|
|
1127
|
+
const x = node.x + node.w / 2 - (width / 2 + 2), y = node.y + node.h / 2;
|
|
1128
|
+
glyphs.push(iconAt(ctx, icon, x, y, stroke));
|
|
1129
|
+
}
|
|
1130
|
+
children.push(linked(ctx, authored, ctx.el("g", {}, [
|
|
1131
|
+
shape,
|
|
1132
|
+
...glyphs,
|
|
1133
|
+
...node.lines.map((line, index) => ctx.text(node.x + node.w / 2 + (icon ? 11 : 0), node.y + node.h / 2 + (index - (node.lines.length - 1) / 2) * 20, line, {
|
|
1134
|
+
size: FONT.body.size,
|
|
1135
|
+
fill: appearance$1.fg1,
|
|
1136
|
+
anchor: "middle",
|
|
1137
|
+
"dominant-baseline": "central",
|
|
1138
|
+
meta: { node: node.id }
|
|
1139
|
+
})),
|
|
1140
|
+
...mark ? [ctx.text(node.x + 8, node.y + 9, mark, {
|
|
1141
|
+
size: 12,
|
|
1142
|
+
weight: 600,
|
|
1143
|
+
fill: stroke,
|
|
1144
|
+
anchor: "middle",
|
|
1145
|
+
"dominant-baseline": "central",
|
|
1146
|
+
meta: {
|
|
1147
|
+
node: node.id,
|
|
1148
|
+
status: node.class
|
|
1149
|
+
}
|
|
1150
|
+
})] : []
|
|
1151
|
+
])));
|
|
1152
|
+
}
|
|
1153
|
+
for (const edge of layout.edges.filter((item) => item.label)) {
|
|
1154
|
+
const authored = mermaid && flowEdges.get(edge.id), icon = authored?.icon;
|
|
1155
|
+
const group = ctx.el("g", {}, [
|
|
1156
|
+
ctx.rect(edge.label.x, edge.label.y, edge.label.w, edge.label.h, { fill: appearance$1.surface }),
|
|
1157
|
+
...icon ? [iconAt(ctx, icon, edge.label.x + 12, edge.label.y + edge.label.h / 2, appearance$1.fg2)] : [],
|
|
1158
|
+
...edge.label.lines.map((line, index) => ctx.text(edge.label.x + edge.label.w / 2 + (icon ? 9 : 0), edge.label.y + edge.label.h / 2 + (index - (edge.label.lines.length - 1) / 2) * 16, line, {
|
|
1159
|
+
size: FONT.label.size,
|
|
1160
|
+
fill: appearance$1.fg2,
|
|
1161
|
+
anchor: "middle",
|
|
1162
|
+
"dominant-baseline": "central"
|
|
1163
|
+
}))
|
|
1164
|
+
]);
|
|
1165
|
+
group.meta = {
|
|
1166
|
+
label: edge.id,
|
|
1167
|
+
...mermaid ? {
|
|
1168
|
+
kind: "edge-label-group",
|
|
1169
|
+
from: edge.from,
|
|
1170
|
+
to: edge.to
|
|
1171
|
+
} : {}
|
|
1172
|
+
};
|
|
1173
|
+
children.push(linked(ctx, authored, group));
|
|
1174
|
+
}
|
|
1175
|
+
return {
|
|
1176
|
+
width: layout.width,
|
|
1177
|
+
height: layout.height,
|
|
1178
|
+
children
|
|
1179
|
+
};
|
|
1180
|
+
}
|
|
1181
|
+
var implementations = { swimlane: {
|
|
1182
|
+
parse,
|
|
1183
|
+
layout,
|
|
1184
|
+
scene
|
|
1185
|
+
} };
|
|
1186
|
+
export { implementations, layout, scene, swimlane_exports };
|