@x-cmd/draw 0.2.5 → 0.4.0
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/README.md +80 -166
- package/dist/lib.d.ts +27 -1
- package/dist/lib.js +39 -1
- package/dist/node/cli.js +109 -11
- package/dist/render/anno.d.ts +50 -0
- package/dist/render/anno.js +589 -0
- package/dist/render/arch.d.ts +106 -0
- package/dist/render/arch.js +1011 -0
- package/dist/render/archmap.d.ts +157 -0
- package/dist/render/archmap.js +683 -0
- package/dist/render/area.js +20 -14
- package/dist/render/bar-grouped.js +8 -5
- package/dist/render/bar-stacked.js +11 -7
- package/dist/render/bar.js +8 -3
- package/dist/render/butterfly.d.ts +3 -0
- package/dist/render/butterfly.js +355 -0
- package/dist/render/calendar.d.ts +6 -0
- package/dist/render/calendar.js +448 -0
- package/dist/render/combo.js +16 -8
- package/dist/render/compass.d.ts +6 -0
- package/dist/render/compass.js +77 -0
- package/dist/render/dispatch.js +26 -1
- package/dist/render/echarts-ssr.d.ts +51 -0
- package/dist/render/echarts-ssr.js +511 -0
- package/dist/render/gantt.d.ts +46 -0
- package/dist/render/gantt.js +415 -0
- package/dist/render/gantt2.d.ts +24 -0
- package/dist/render/gantt2.js +198 -0
- package/dist/render/hbar.js +8 -4
- package/dist/render/icons.d.ts +3 -0
- package/dist/render/icons.js +78 -0
- package/dist/render/kinds.d.ts +11 -0
- package/dist/render/kinds.js +176 -0
- package/dist/render/ladder.d.ts +21 -0
- package/dist/render/ladder.js +230 -0
- package/dist/render/layout.d.ts +46 -0
- package/dist/render/layout.js +57 -0
- package/dist/render/line-multi.js +7 -5
- package/dist/render/line.js +9 -5
- package/dist/render/maps.d.ts +46 -0
- package/dist/render/maps.js +237 -0
- package/dist/render/measure.d.ts +0 -1
- package/dist/render/measure.js +0 -1
- package/dist/render/mermaid.d.ts +30 -0
- package/dist/render/mermaid.js +277 -0
- package/dist/render/mindmap.d.ts +10 -0
- package/dist/render/mindmap.js +388 -0
- package/dist/render/orgicon.d.ts +12 -0
- package/dist/render/orgicon.js +31 -0
- package/dist/render/pie.js +1 -1
- package/dist/render/png.d.ts +21 -0
- package/dist/render/png.js +109 -0
- package/dist/render/quadrant.d.ts +6 -0
- package/dist/render/quadrant.js +201 -0
- package/dist/render/query.d.ts +2 -0
- package/dist/render/query.js +108 -1
- package/dist/render/route.d.ts +86 -0
- package/dist/render/route.js +1077 -0
- package/dist/render/scatter.js +87 -12
- package/dist/render/seq.d.ts +41 -0
- package/dist/render/seq.js +248 -0
- package/dist/render/svg.d.ts +34 -2
- package/dist/render/svg.js +159 -20
- package/dist/render/table.d.ts +30 -0
- package/dist/render/table.js +134 -0
- package/dist/render/tokei.d.ts +18 -0
- package/dist/render/tokei.js +230 -0
- package/dist/render/uc.d.ts +58 -0
- package/dist/render/uc.js +319 -0
- package/dist/render/units.d.ts +32 -0
- package/dist/render/units.js +84 -0
- package/dist/render/vendor/au.js +10 -0
- package/dist/render/vendor/br.js +10 -0
- package/dist/render/vendor/china.js +11 -0
- package/dist/render/vendor/de.js +10 -0
- package/dist/render/vendor/eu.js +10 -0
- package/dist/render/vendor/japan.js +10 -0
- package/dist/render/vendor/kr.js +10 -0
- package/dist/render/vendor/mermaid-flow-parser.js +1164 -0
- package/dist/render/vendor/mermaid-gantt-parser.js +810 -0
- package/dist/render/vendor/us-states.js +11 -0
- package/dist/render/vendor/world-geo.d.ts +22 -0
- package/dist/render/vendor/world-geo.js +9 -0
- package/dist/render/vendor/world.d.ts +13 -0
- package/dist/render/vendor/world.js +12 -0
- package/dist/specs/base.d.ts +16 -0
- package/dist/specs/base.js +34 -1
- package/dist/specs/butterfly.d.ts +46 -0
- package/dist/specs/butterfly.js +241 -0
- package/dist/specs/calendar.d.ts +90 -0
- package/dist/specs/calendar.js +296 -0
- package/dist/specs/compass.d.ts +27 -0
- package/dist/specs/compass.js +111 -0
- package/dist/specs/index.d.ts +5 -0
- package/dist/specs/index.js +5 -0
- package/dist/specs/ladder.d.ts +43 -0
- package/dist/specs/ladder.js +261 -0
- package/dist/specs/quadrant.d.ts +41 -0
- package/dist/specs/quadrant.js +200 -0
- package/dist/specs/scatter.d.ts +14 -8
- package/dist/specs/scatter.js +54 -5
- package/dist/specs/table.d.ts +24 -0
- package/dist/specs/table.js +154 -0
- package/dist/store.d.ts +1 -1
- package/dist/store.js +1 -1
- package/dist/styles/butterfly.d.ts +2 -0
- package/dist/styles/butterfly.js +74 -0
- package/dist/styles/calendar.d.ts +2 -0
- package/dist/styles/calendar.js +51 -0
- package/dist/styles/compass.d.ts +2 -0
- package/dist/styles/compass.js +19 -0
- package/dist/styles/fonts-banner.d.ts +5 -0
- package/dist/styles/fonts-banner.js +9 -0
- package/dist/styles/fonts-cjk.d.ts +2 -0
- package/dist/styles/fonts-cjk.js +6 -0
- package/dist/styles/fonts-ttf.d.ts +2 -0
- package/dist/styles/fonts-ttf.js +6 -0
- package/dist/styles/index.js +13 -0
- package/dist/styles/ladder.d.ts +3 -0
- package/dist/styles/ladder.js +66 -0
- package/dist/styles/quadrant.d.ts +2 -0
- package/dist/styles/quadrant.js +19 -0
- package/dist/styles/table.d.ts +2 -0
- package/dist/styles/table.js +12 -0
- package/dist/styles/types.d.ts +1 -0
- package/dist/styles/types.js +1 -0
- package/dist/theme.d.ts +1 -1
- package/dist/theme.js +3 -4
- package/dist/types/dagre.d.ts +39 -0
- package/dist/types/dagre.js +3 -0
- package/package.json +28 -3
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
// /draw/archmap(x-cmd-hub/draw#17/#20):网格拓扑图 — 自研网格引擎保底版。
|
|
2
|
+
// 分工:人或 AI 写①分层②排序(粗格 r0c1 或像素逃生舱,明文进 URL,读者零算术读拓扑),
|
|
3
|
+
// 引擎做③打包(auto 轨道:列宽=成员自然宽最大值,跨格补差均摊)④路由(L/Z 正交 +
|
|
4
|
+
// 端口面扩散 + 穿盒外推绕行)。输出静态 SVG,<metadata> 全量回程。
|
|
5
|
+
// 参数语法(n/e/b 字段分隔 ";",查询串原字符合法,label 可含逗号):
|
|
6
|
+
// n=<id>;<type>;<label>[;<sublabel>][;<pos>]
|
|
7
|
+
// e=<from>;<to>[;<label>[;<variant>]] variant: main(强调)|dash(虚线)|缺省常规
|
|
8
|
+
// b=<label>;<id> <id> ... 边界包裹(ids 空格分隔)
|
|
9
|
+
// pos: r<row>c<col>[w<span>][h<span>](跨度≤3)| p<x>,<y>[,<w>x<h>](像素逃生舱)
|
|
10
|
+
// 类型枚举 → 语义色:fe/be/db/cloud/sec/bus/svc 映射系列色板,ext 中性虚线框。
|
|
11
|
+
// 渲染管线:路由先算 → 边界标签四角择优(远离走线) → 边标签多候选避让(节点盒∪边界标签区)。
|
|
12
|
+
import { ApiError } from '../specs/index.js';
|
|
13
|
+
import { resolveVersion } from '../version.js';
|
|
14
|
+
import { resolveSkin } from '../styles/index.js';
|
|
15
|
+
import { esc, r2, roundedPath, sVar, segRectIntersect, svgDoc, textW } from './svg.js';
|
|
16
|
+
import { routeEdges } from './route.js';
|
|
17
|
+
import { clampInt, schemeParam } from './query.js';
|
|
18
|
+
const TYPES = ['ext', 'fe', 'be', 'db', 'cloud', 'sec', 'bus', 'svc'];
|
|
19
|
+
// 语义类型 → 系列色序号(ext 无色,走中性虚线框)
|
|
20
|
+
export const TYPE_COLOR = {
|
|
21
|
+
fe: 0, be: 1, db: 2, cloud: 3, sec: 4, bus: 5, svc: 6,
|
|
22
|
+
};
|
|
23
|
+
export const ID_RE = /^[A-Za-z0-9][A-Za-z0-9_.-]*$/;
|
|
24
|
+
export const GRID_POS_RE = /^r(\d+)c(\d+)(?:w([1-3]))?(?:h([1-3]))?$/;
|
|
25
|
+
// 负数要能写:像素位置可以是负的(球面图里的经度就是负的)
|
|
26
|
+
export const PX_POS_RE = /^p(-?\d+(?:\.\d+)?),(-?\d+(?:\.\d+)?)(?:,(\d+)x(\d+))?$/;
|
|
27
|
+
const GAP_X = 30;
|
|
28
|
+
const GAP_Y = 34;
|
|
29
|
+
const PAD = 26;
|
|
30
|
+
const MIN_CELL_W = 96;
|
|
31
|
+
const MIN_CELL_H = 40;
|
|
32
|
+
const N_EXAMPLE = 'n=api;be;API Gateway;auth;r1c0w2';
|
|
33
|
+
function bad(detail) {
|
|
34
|
+
throw new ApiError(400, `${detail} Example: ${N_EXAMPLE}`);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 切节点字段:末尾的空段丢掉(至少留 id;type;label 三段)。
|
|
38
|
+
* 这样 `n=id;type;label;;` 和 `n=id;type;label` 一样,不用作者数分号。
|
|
39
|
+
*/
|
|
40
|
+
export function splitFields(v) {
|
|
41
|
+
const f = v.split(';');
|
|
42
|
+
while (f.length > 3 && f[f.length - 1].trim() === '')
|
|
43
|
+
f.pop();
|
|
44
|
+
return f;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 图标字段:provider:名字(aws:ec2 / azure:vm / gcp:gce / org:verisign)
|
|
48
|
+
*
|
|
49
|
+
* `org:` 是公司/机构图标 —— 那一族住在私有仓的内部图库里(商标图形不能公开分发),
|
|
50
|
+
* 公开请求查不到就画组图标(见 render/orgicon.ts)。其余前缀是云服务图标,公开可用。
|
|
51
|
+
*/
|
|
52
|
+
export const ICON_FIELD_RE = /^(?:aws|azure|gcp|org):[a-z0-9][a-z0-9-]*$/;
|
|
53
|
+
export function parseNodes(raw) {
|
|
54
|
+
return raw.map((v, i) => {
|
|
55
|
+
const f = splitFields(v);
|
|
56
|
+
const where = `"n=${v}" (#${i + 1})`;
|
|
57
|
+
if (f.length < 3 || f.length > 6)
|
|
58
|
+
bad(`${where}: expected id;type;label[;sublabel][;pos][;icon] fields`);
|
|
59
|
+
const [id, type, label] = f;
|
|
60
|
+
// 图标按形状认(provider:name),拿掉之后剩下的还是「副标题 + 位置」的老规矩
|
|
61
|
+
const icon = f.slice(3).find((seg) => ICON_FIELD_RE.test(seg));
|
|
62
|
+
const rest = f.slice(3).filter((seg) => !ICON_FIELD_RE.test(seg));
|
|
63
|
+
let sub;
|
|
64
|
+
let pos;
|
|
65
|
+
if (rest.length === 1) {
|
|
66
|
+
// 一段:像格位/像素坐标 → pos,否则视作 sublabel
|
|
67
|
+
if (GRID_POS_RE.test(rest[0]) || PX_POS_RE.test(rest[0]))
|
|
68
|
+
pos = rest[0];
|
|
69
|
+
else
|
|
70
|
+
sub = rest[0];
|
|
71
|
+
}
|
|
72
|
+
else if (rest.length >= 2) {
|
|
73
|
+
sub = rest[0];
|
|
74
|
+
pos = rest[1];
|
|
75
|
+
}
|
|
76
|
+
if (!ID_RE.test(id))
|
|
77
|
+
bad(`${where}: id must be alnum/dot/dash/underscore, not starting with a digit-punct`);
|
|
78
|
+
if (!TYPES.includes(type))
|
|
79
|
+
bad(`${where}: unknown type "${type}" (one of ${TYPES.join('/')})`);
|
|
80
|
+
if (!label.trim())
|
|
81
|
+
bad(`${where}: label is required`);
|
|
82
|
+
let grid;
|
|
83
|
+
let px;
|
|
84
|
+
if (pos !== undefined && pos !== '') {
|
|
85
|
+
const g = GRID_POS_RE.exec(pos);
|
|
86
|
+
if (g)
|
|
87
|
+
grid = { row: +g[1], col: +g[2], w: g[3] ? +g[3] : 1, h: g[4] ? +g[4] : 1 };
|
|
88
|
+
else {
|
|
89
|
+
const p = PX_POS_RE.exec(pos);
|
|
90
|
+
if (!p)
|
|
91
|
+
bad(`${where}: pos must look like r0c1[w2][h2] or p40,80[,120x48]`);
|
|
92
|
+
px = { x: +p[1], y: +p[2], ...(p[3] ? { w: +p[3], h: +p[4] } : {}) };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return { id, type: type, label: label.trim(), sub: sub?.trim() || undefined, icon, grid, px };
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
export function parseEdges(raw, ids) {
|
|
99
|
+
return raw.map((v, i) => {
|
|
100
|
+
const f = v.split(';');
|
|
101
|
+
const where = `"e=${v}" (#${i + 1})`;
|
|
102
|
+
if (f.length < 2 || f.length > 4)
|
|
103
|
+
bad(`${where}: expected from;to[;label][;variant] fields`);
|
|
104
|
+
const [from, to, label, variant] = f;
|
|
105
|
+
if (!ids.has(from))
|
|
106
|
+
bad(`${where}: unknown node "${from}"`);
|
|
107
|
+
if (!ids.has(to))
|
|
108
|
+
bad(`${where}: unknown node "${to}"`);
|
|
109
|
+
let va = 'plain';
|
|
110
|
+
if (variant !== undefined && variant !== '') {
|
|
111
|
+
if (variant !== 'main' && variant !== 'dash')
|
|
112
|
+
bad(`${where}: unknown variant "${variant}" (main|dash)`);
|
|
113
|
+
va = variant;
|
|
114
|
+
}
|
|
115
|
+
return { from, to, label: label?.trim() || undefined, variant: va };
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
export function parseBoundaries(raw, ids) {
|
|
119
|
+
return raw.map((v, i) => {
|
|
120
|
+
const sep = v.indexOf(';');
|
|
121
|
+
if (sep < 1)
|
|
122
|
+
bad(`"b=${v}" (#${i + 1}): expected <label>;<id> <id> ...`);
|
|
123
|
+
const label = v.slice(0, sep).trim();
|
|
124
|
+
let rest = v.slice(sep + 1).trim();
|
|
125
|
+
// 可选的第三段:色板编号 1–10(b=名称;id id…;9)
|
|
126
|
+
let color;
|
|
127
|
+
const csep = rest.lastIndexOf(';');
|
|
128
|
+
if (csep > 0) {
|
|
129
|
+
const tail = rest.slice(csep + 1).trim();
|
|
130
|
+
if (/^\d{1,2}$/.test(tail)) {
|
|
131
|
+
const n = Number(tail);
|
|
132
|
+
if (n < 1 || n > 10)
|
|
133
|
+
bad(`"b=${v}": colour must be a palette number 1-10, got "${tail}"`);
|
|
134
|
+
color = n;
|
|
135
|
+
rest = rest.slice(0, csep).trim();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const members = rest.split(/\s+/).filter(Boolean);
|
|
139
|
+
if (!label || !members.length)
|
|
140
|
+
bad(`"b=${v}" (#${i + 1}): label and at least one member id required`);
|
|
141
|
+
for (const m of members)
|
|
142
|
+
if (!ids.has(m))
|
|
143
|
+
bad(`"b=${v}": unknown node "${m}"`);
|
|
144
|
+
return { label, ids: members, ...(color ? { color } : {}) };
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
// 自然尺寸:label 13px 加左右内边距;副标 11px;盒高 40(带副标 56)
|
|
148
|
+
export function naturalSize(n, scale = 1) {
|
|
149
|
+
const w = Math.max(MIN_CELL_W * scale, textW(n.label, 13 * scale) + 34 * scale, n.sub ? textW(n.sub, 11 * scale) + 34 * scale : 0);
|
|
150
|
+
const h = (n.sub ? 56 : 40) * scale;
|
|
151
|
+
return { w: Math.min(w, 360), h };
|
|
152
|
+
}
|
|
153
|
+
export function packGrid(nodes, gridCols, opts = {}) {
|
|
154
|
+
const gapX = opts.gapX ?? GAP_X;
|
|
155
|
+
const gapY = opts.gapY ?? GAP_Y;
|
|
156
|
+
const pad = opts.pad ?? PAD;
|
|
157
|
+
const sizeOf = opts.sizeOf ?? ((n) => naturalSize(n));
|
|
158
|
+
// 格子占用(显式先标,冲突即报);无 pos 节点自动落入首个空格(行优先,确定性,回程记录落位)
|
|
159
|
+
const cell = new Map();
|
|
160
|
+
const mark = (n) => {
|
|
161
|
+
for (let r = n.grid.row; r < n.grid.row + n.grid.h; r++)
|
|
162
|
+
for (let c = n.grid.col; c < n.grid.col + n.grid.w; c++) {
|
|
163
|
+
const k = `${r},${c}`;
|
|
164
|
+
const prev = cell.get(k);
|
|
165
|
+
if (prev)
|
|
166
|
+
bad(`nodes "${prev}" and "${n.id}" share grid cell r${r}c${c}`);
|
|
167
|
+
cell.set(k, n.id);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
for (const n of nodes)
|
|
171
|
+
if (n.grid)
|
|
172
|
+
mark(n);
|
|
173
|
+
// 没写位置的节点:落在邻居旁边(离邻居最近的空格);没邻居就按给的顺序排。
|
|
174
|
+
// grid= 给了列数 → 每行几格就按几格换行(文档里「强制分成 N 列」是这个意思)。
|
|
175
|
+
const wrap = gridCols > 0 ? gridCols : 12;
|
|
176
|
+
const cellOf = new Map();
|
|
177
|
+
for (const n of nodes)
|
|
178
|
+
if (n.grid)
|
|
179
|
+
cellOf.set(n.id, { row: n.grid.row, col: n.grid.col });
|
|
180
|
+
const freeCell = (want) => {
|
|
181
|
+
let best = null;
|
|
182
|
+
let bestD = Infinity;
|
|
183
|
+
for (let r = 0; r < 64; r++) {
|
|
184
|
+
for (let c = 0; c < wrap; c++) {
|
|
185
|
+
if (cell.has(`${r},${c}`))
|
|
186
|
+
continue;
|
|
187
|
+
// 目标格子附近的最优;没有目标就取最靠前的空格(行优先)
|
|
188
|
+
const d = want ? Math.abs(r - want.row) + Math.abs(c - want.col) : 0;
|
|
189
|
+
const key = d * 100000 + r * 100 + c;
|
|
190
|
+
if (key < bestD) {
|
|
191
|
+
bestD = key;
|
|
192
|
+
best = { row: r, col: c };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return best ?? { row: 0, col: 0 };
|
|
197
|
+
};
|
|
198
|
+
for (const n of nodes) {
|
|
199
|
+
if (n.grid || n.px)
|
|
200
|
+
continue;
|
|
201
|
+
// 邻居(已经定下位置的)格子的中位数 —— 没写位置的节点往那儿靠
|
|
202
|
+
const nb = (opts.edges ?? [])
|
|
203
|
+
.filter((e) => e.from === n.id || e.to === n.id)
|
|
204
|
+
.map((e) => cellOf.get(e.from === n.id ? e.to : e.from))
|
|
205
|
+
.filter((g) => !!g);
|
|
206
|
+
const want = nb.length
|
|
207
|
+
? {
|
|
208
|
+
row: Math.round(nb.reduce((a, g) => a + g.row, 0) / nb.length),
|
|
209
|
+
col: Math.round(nb.reduce((a, g) => a + g.col, 0) / nb.length),
|
|
210
|
+
}
|
|
211
|
+
: null;
|
|
212
|
+
const g = freeCell(want);
|
|
213
|
+
n.grid = { row: g.row, col: g.col, w: 1, h: 1 };
|
|
214
|
+
cell.set(`${g.row},${g.col}`, n.id);
|
|
215
|
+
cellOf.set(n.id, g);
|
|
216
|
+
}
|
|
217
|
+
const gridNodes = nodes.filter(n => n.grid);
|
|
218
|
+
const rows = Math.max(0, ...gridNodes.map(n => n.grid.row + n.grid.h));
|
|
219
|
+
const cols = Math.max(gridCols, ...gridNodes.map(n => n.grid.col + n.grid.w));
|
|
220
|
+
let trackX = [];
|
|
221
|
+
let trackY = [];
|
|
222
|
+
let colW = [];
|
|
223
|
+
let rowH = [];
|
|
224
|
+
if (cols > 0 && rows > 0) {
|
|
225
|
+
colW = new Array(cols).fill(sizeOf({ id: '', type: 'be', label: '' }).w);
|
|
226
|
+
rowH = new Array(rows).fill(sizeOf({ id: '', type: 'be', label: '' }).h);
|
|
227
|
+
const spans = [];
|
|
228
|
+
for (const n of gridNodes) {
|
|
229
|
+
const sz = sizeOf(n);
|
|
230
|
+
if (n.grid.w === 1)
|
|
231
|
+
colW[n.grid.col] = Math.max(colW[n.grid.col], sz.w);
|
|
232
|
+
if (n.grid.h === 1)
|
|
233
|
+
rowH[n.grid.row] = Math.max(rowH[n.grid.row], sz.h);
|
|
234
|
+
if (n.grid.w > 1 || n.grid.h > 1)
|
|
235
|
+
spans.push(n);
|
|
236
|
+
}
|
|
237
|
+
for (const n of spans) {
|
|
238
|
+
const sz = sizeOf(n);
|
|
239
|
+
const { col, row, w, h } = n.grid;
|
|
240
|
+
const needW = sz.w - (colW.slice(col, col + w).reduce((a, b) => a + b, 0) + gapX * (w - 1));
|
|
241
|
+
if (needW > 0)
|
|
242
|
+
for (let c = col; c < col + w; c++)
|
|
243
|
+
colW[c] += needW / w;
|
|
244
|
+
const needH = sz.h - (rowH.slice(row, row + h).reduce((a, b) => a + b, 0) + gapY * (h - 1));
|
|
245
|
+
if (needH > 0)
|
|
246
|
+
for (let r = row; r < row + h; r++)
|
|
247
|
+
rowH[r] += needH / h;
|
|
248
|
+
}
|
|
249
|
+
trackX = new Array(cols);
|
|
250
|
+
trackY = new Array(rows);
|
|
251
|
+
let x = pad;
|
|
252
|
+
for (let c = 0; c < cols; c++) {
|
|
253
|
+
trackX[c] = x;
|
|
254
|
+
x += colW[c] + (opts.gapAfter?.[c] ?? gapX);
|
|
255
|
+
}
|
|
256
|
+
let y = pad;
|
|
257
|
+
for (let r = 0; r < rows; r++) {
|
|
258
|
+
trackY[r] = y;
|
|
259
|
+
y += rowH[r] + gapY;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const W = cols > 0 && rows > 0 ? trackX[cols - 1] + colW[cols - 1] + pad : 0;
|
|
263
|
+
const H = cols > 0 && rows > 0 ? trackY[rows - 1] + rowH[rows - 1] + pad : 0;
|
|
264
|
+
const placed = nodes.map(n => {
|
|
265
|
+
if (n.grid) {
|
|
266
|
+
const { row, col, w, h } = n.grid;
|
|
267
|
+
const box = {
|
|
268
|
+
x: trackX[col], y: trackY[row],
|
|
269
|
+
w: trackX[col + w - 1] + colW[col + w - 1] - trackX[col],
|
|
270
|
+
h: trackY[row + h - 1] + rowH[row + h - 1] - trackY[row],
|
|
271
|
+
};
|
|
272
|
+
return { ...n, box, color: TYPE_COLOR[n.type] ?? -1 };
|
|
273
|
+
}
|
|
274
|
+
const sz = sizeOf(n);
|
|
275
|
+
return { ...n, box: { x: n.px.x, y: n.px.y, w: n.px.w ?? sz.w, h: n.px.h ?? sz.h }, color: TYPE_COLOR[n.type] ?? -1 };
|
|
276
|
+
});
|
|
277
|
+
return { placed, W, H };
|
|
278
|
+
}
|
|
279
|
+
const boxCx = (b) => b.x + b.w / 2;
|
|
280
|
+
const boxCy = (b) => b.y + b.h / 2;
|
|
281
|
+
// 端口:同面多边均匀扩散(第 i/n 条落在面长 (i+1)/(n+1) 处)
|
|
282
|
+
export function port(b, side, i, n) {
|
|
283
|
+
const len = side === 'left' || side === 'right' ? b.h : b.w;
|
|
284
|
+
const off = ((i + 1) / (n + 1) - 0.5) * len;
|
|
285
|
+
switch (side) {
|
|
286
|
+
case 'left': return [b.x, boxCy(b) + off];
|
|
287
|
+
case 'right': return [b.x + b.w, boxCy(b) + off];
|
|
288
|
+
case 'top': return [boxCx(b) + off, b.y];
|
|
289
|
+
case 'bottom': return [boxCx(b) + off, b.y + b.h];
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
export function step(p, side, d) {
|
|
293
|
+
switch (side) {
|
|
294
|
+
case 'left': return [p[0] - d, p[1]];
|
|
295
|
+
case 'right': return [p[0] + d, p[1]];
|
|
296
|
+
case 'top': return [p[0], p[1] - d];
|
|
297
|
+
case 'bottom': return [p[0], p[1] + d];
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
// 中线穿盒外推:撞到的盒子往来源方向同侧让出 16px(单次外推,网格布局下足够)
|
|
301
|
+
function nudgeMid(kind, mid, p1, p3, a, b, others) {
|
|
302
|
+
for (const o of others) {
|
|
303
|
+
if (o === a || o === b)
|
|
304
|
+
continue;
|
|
305
|
+
if (kind === 'v') {
|
|
306
|
+
const hit = mid >= o.x - 1 && mid <= o.x + o.w + 1
|
|
307
|
+
&& Math.min(p1[1], p3[1]) <= o.y + o.h && Math.max(p1[1], p3[1]) >= o.y;
|
|
308
|
+
if (hit)
|
|
309
|
+
mid = boxCx(a) < boxCx(o) ? o.x - 16 : o.x + o.w + 16;
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
const hit = mid >= o.y - 1 && mid <= o.y + o.h + 1
|
|
313
|
+
&& Math.min(p1[0], p3[0]) <= o.x + o.w && Math.max(p1[0], p3[0]) >= o.x;
|
|
314
|
+
if (hit)
|
|
315
|
+
mid = boxCy(a) < boxCy(o) ? o.y - 16 : o.y + o.h + 16;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
return mid;
|
|
319
|
+
}
|
|
320
|
+
// 折线按弧长取点(边标签锚点);t 为弧长比例
|
|
321
|
+
function polyAt(pts, t) {
|
|
322
|
+
const lens = [];
|
|
323
|
+
let total = 0;
|
|
324
|
+
for (let i = 1; i < pts.length; i++) {
|
|
325
|
+
const l = Math.abs(pts[i][0] - pts[i - 1][0]) + Math.abs(pts[i][1] - pts[i - 1][1]);
|
|
326
|
+
lens.push(l);
|
|
327
|
+
total += l;
|
|
328
|
+
}
|
|
329
|
+
let acc = 0;
|
|
330
|
+
for (let i = 0; i < lens.length; i++) {
|
|
331
|
+
if (acc + lens[i] >= total * t && lens[i] > 0) {
|
|
332
|
+
const f = (total * t - acc) / lens[i];
|
|
333
|
+
const [x1, y1] = pts[i];
|
|
334
|
+
const [x2, y2] = pts[i + 1];
|
|
335
|
+
return { p: [x1 + (x2 - x1) * f, y1 + (y2 - y1) * f], horiz: y1 === y2 };
|
|
336
|
+
}
|
|
337
|
+
acc += lens[i];
|
|
338
|
+
}
|
|
339
|
+
return { p: pts[pts.length - 1], horiz: true };
|
|
340
|
+
}
|
|
341
|
+
const rectsOverlap = (a, b) => a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
|
|
342
|
+
// 出/入面由中心差主轴决定(网格布局下单调,天然不回折);同面多边按出现序扩散
|
|
343
|
+
// 直角肘形路由(mermaid 语法糖):直线优先——同层对齐即纯直线;有落差在
|
|
344
|
+
// 中点垂直折一次(水平→垂直→水平);反向边从下方 U 形直角绕回。
|
|
345
|
+
// 线只为节点与方向折,永不因文字折(标签就近放置,不影响线形)。
|
|
346
|
+
function straightRoutes(placed, edges, dir) {
|
|
347
|
+
const byId = new Map(placed.map(p => [p.id, p]));
|
|
348
|
+
const lr = dir === 'LR';
|
|
349
|
+
return edges.map(e => {
|
|
350
|
+
const a = byId.get(e.from).box;
|
|
351
|
+
const b = byId.get(e.to).box;
|
|
352
|
+
let pts;
|
|
353
|
+
const back = lr ? boxCx(b) < boxCx(a) : boxCy(b) < boxCy(a);
|
|
354
|
+
if (back) {
|
|
355
|
+
// 反向边:出底/右缘,外圈 U 形直角绕回
|
|
356
|
+
if (lr) {
|
|
357
|
+
const loopY = Math.max(a.y + a.h, b.y + b.h) + 36;
|
|
358
|
+
pts = [[boxCx(a), a.y + a.h], [boxCx(a), loopY], [boxCx(b), loopY], [boxCx(b), b.y + b.h]];
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
const loopX = Math.max(a.x + a.w, b.x + b.w) + 36;
|
|
362
|
+
pts = [[a.x + a.w, boxCy(a)], [loopX, boxCy(a)], [loopX, boxCy(b)], [b.x + b.w, boxCy(b)]];
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
else if (lr) {
|
|
366
|
+
const x0 = a.x + a.w, y0 = boxCy(a), x1 = b.x, y1 = boxCy(b);
|
|
367
|
+
pts = Math.abs(y0 - y1) <= 14
|
|
368
|
+
? [[x0, y0], [x1, y1]]
|
|
369
|
+
: [[x0, y0], [Math.round((x0 + x1) / 2), y0], [Math.round((x0 + x1) / 2), y1], [x1, y1]];
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
const x0 = boxCx(a), y0 = a.y + a.h, x1 = boxCx(b), y1 = b.y;
|
|
373
|
+
pts = Math.abs(x0 - x1) <= 14
|
|
374
|
+
? [[x0, y0], [x1, y1]]
|
|
375
|
+
: [[x0, y0], [x0, Math.round((y0 + y1) / 2)], [x1, Math.round((y0 + y1) / 2)], [x1, y1]];
|
|
376
|
+
}
|
|
377
|
+
return {
|
|
378
|
+
e,
|
|
379
|
+
pts,
|
|
380
|
+
stroke: 'var(--d-muted)',
|
|
381
|
+
width: 1.8,
|
|
382
|
+
dash: e.variant === 'dash' ? ' stroke-dasharray="5 4"' : '',
|
|
383
|
+
marker: 'am-p',
|
|
384
|
+
};
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* 走线:几何交给布线内核(见 render/route.ts),这里只管样式。
|
|
389
|
+
*
|
|
390
|
+
* 内核那套规矩:不同源的边不共用一段、不首尾相接;端口进出一组排、间距 ≥ 9px。
|
|
391
|
+
* (旧写法进出端口分开数 → 落同一个点;竖通道只避让盒子、不看别的线 →
|
|
392
|
+
* 两条边选了同一个通道,接成一根线。见 x-cmd-hub/draw#92。)
|
|
393
|
+
*/
|
|
394
|
+
export function computeRoutes(placed, edges, bounds = [], routing, dir = 'TB') {
|
|
395
|
+
const byId = new Map(placed.map(p => [p.id, p]));
|
|
396
|
+
const ctx = {
|
|
397
|
+
nodes: placed,
|
|
398
|
+
boxes: new Map(placed.map(p => [p.id, p.box])),
|
|
399
|
+
edges,
|
|
400
|
+
bounds,
|
|
401
|
+
dir,
|
|
402
|
+
portBox: (id) => byId.get(id).box,
|
|
403
|
+
};
|
|
404
|
+
const paths = (routing ?? routeEdges)(ctx);
|
|
405
|
+
return edges.map((e, i) => {
|
|
406
|
+
const style = e.variant === 'main'
|
|
407
|
+
? { stroke: 'var(--d-ink)', width: 1.8, marker: 'am-m', dash: '' }
|
|
408
|
+
: e.variant === 'dash'
|
|
409
|
+
? { stroke: 'var(--d-faint)', width: 1.4, marker: 'am-d', dash: ' stroke-dasharray="5 4"' }
|
|
410
|
+
: { stroke: 'var(--d-muted)', width: 1.4, marker: 'am-p', dash: '' };
|
|
411
|
+
return { e, pts: paths[i] ?? [], ...style };
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
function drawRoutes(routed, interactive = false, style = 'base', byId) {
|
|
415
|
+
return routed.map(rt => {
|
|
416
|
+
const d = roundedPath(rt.pts, 10);
|
|
417
|
+
const ia = interactive ? ` data-from="${esc(rt.e.from)}" data-to="${esc(rt.e.to)}"` : '';
|
|
418
|
+
let stroke = rt.stroke;
|
|
419
|
+
let width = rt.width;
|
|
420
|
+
if (style === 'soft') {
|
|
421
|
+
const src = byId?.get(rt.e.from);
|
|
422
|
+
stroke = src && src.color >= 0 ? sVar(src.color) : rt.stroke;
|
|
423
|
+
width = Math.max(rt.width, 1.6);
|
|
424
|
+
}
|
|
425
|
+
else if (style === 'mono') {
|
|
426
|
+
stroke = 'var(--d-muted)';
|
|
427
|
+
}
|
|
428
|
+
return `<path d="${d}"${ia} fill="none" stroke="${stroke}" stroke-width="${width}"${rt.dash} marker-end="url(#${rt.marker})"/>`;
|
|
429
|
+
}).join('');
|
|
430
|
+
}
|
|
431
|
+
// 边标签多候选避让:弧长 7 档 × 线两侧(横线上/下,竖线右/左),首个不撞胜出
|
|
432
|
+
function drawEdgeLabels(routed, avoid, interactive = false) {
|
|
433
|
+
const out = [];
|
|
434
|
+
for (const rt of routed) {
|
|
435
|
+
const text = rt.e.label;
|
|
436
|
+
if (!text)
|
|
437
|
+
continue;
|
|
438
|
+
const ia = interactive ? ` data-edge-label="${esc(rt.e.from)} ${esc(rt.e.to)}"` : '';
|
|
439
|
+
const w = textW(text, 11) + 10;
|
|
440
|
+
const rectOf = (p, horiz, mirror) => horiz
|
|
441
|
+
? { x: p[0] - w / 2, y: mirror ? p[1] + 2 : p[1] - 17, w, h: 16 }
|
|
442
|
+
: { x: mirror ? p[0] - w - 4 : p[0] + 4, y: p[1] - 8, w, h: 16 };
|
|
443
|
+
let place;
|
|
444
|
+
for (const t of [0.5, 0.38, 0.62, 0.3, 0.7, 0.2, 0.8]) {
|
|
445
|
+
const at = polyAt(rt.pts, t);
|
|
446
|
+
for (const mirror of [false, true]) {
|
|
447
|
+
if (!avoid.some(r => rectsOverlap(rectOf(at.p, at.horiz, mirror), r))) {
|
|
448
|
+
place = { ...at, mirror };
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
if (place)
|
|
453
|
+
break;
|
|
454
|
+
}
|
|
455
|
+
if (!place)
|
|
456
|
+
place = { ...polyAt(rt.pts, 0.5), mirror: false };
|
|
457
|
+
const attrs = place.horiz
|
|
458
|
+
? `x="${r2(place.p[0])}" y="${r2(place.p[1] + (place.mirror ? 14 : -6))}" text-anchor="middle"`
|
|
459
|
+
: `x="${r2(place.p[0] + (place.mirror ? -6 : 6))}" y="${r2(place.p[1] + 3.5)}" text-anchor="${place.mirror ? 'end' : 'start'}"`;
|
|
460
|
+
// 底衬:标签比两个节点之间的空档还宽时会压到节点上,给个画布色的胶囊,
|
|
461
|
+
// 线从底下过、字浮在上面 —— 和 v2 的边标签一个样。
|
|
462
|
+
const tx = place.horiz ? place.p[0] : place.p[0] + (place.mirror ? -6 : 6);
|
|
463
|
+
const ty = place.horiz ? place.p[1] + (place.mirror ? 14 : -6) : place.p[1] + 3.5;
|
|
464
|
+
const chipX = place.horiz ? tx - w / 2 : place.mirror ? tx - w : tx;
|
|
465
|
+
const chipY = place.horiz ? ty - 12 : ty - 11;
|
|
466
|
+
out.push(`<rect x="${r2(chipX)}" y="${r2(chipY)}" width="${r2(w)}" height="15" rx="4" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1"/>`, `<text${ia} ${attrs} font-size="11" fill="var(--d-text)">${esc(text)}</text>`);
|
|
467
|
+
}
|
|
468
|
+
return out.join('');
|
|
469
|
+
}
|
|
470
|
+
export const ARCH_STYLES = ['base', 'mono', 'soft'];
|
|
471
|
+
// 形状帧:rect(默认)/diamond(判定)/cylinder(存储)/round(胶囊)。
|
|
472
|
+
// soft 用同色淡填充;mono 墨线;base 用中性卡+角色色条(菱形/圆柱不吃色条)
|
|
473
|
+
function nodeFrame(b, shape, style, color, dash, tint) {
|
|
474
|
+
const stroke = tint ?? (style === 'mono' ? 'var(--d-ink)' : 'var(--d-border)');
|
|
475
|
+
const fill = tint ? `${tint}" fill-opacity="0.13` : 'var(--d-card)';
|
|
476
|
+
const sw = style === 'mono' || tint ? 1.2 : 1;
|
|
477
|
+
if (shape === 'diamond') {
|
|
478
|
+
const pts = `${r2(boxCx(b))},${r2(b.y + 3)} ${r2(b.x + b.w - 3)},${r2(boxCy(b))} ${r2(boxCx(b))},${r2(b.y + b.h - 3)} ${r2(b.x + 3)},${r2(boxCy(b))}`;
|
|
479
|
+
return `<polygon points="${pts}" fill="${fill}" stroke="${stroke}" stroke-width="${sw}"${dash}/>`;
|
|
480
|
+
}
|
|
481
|
+
if (shape === 'cylinder') {
|
|
482
|
+
const ry = 7;
|
|
483
|
+
return `<path d="M${r2(b.x)} ${r2(b.y + ry)}A${r2(b.w / 2)} ${ry} 0 0 1 ${r2(b.x + b.w)} ${r2(b.y + ry)}V${r2(b.y + b.h - ry)}A${r2(b.w / 2)} ${ry} 0 0 1 ${r2(b.x)} ${r2(b.y + b.h - ry)}Z" fill="${fill}" stroke="${stroke}" stroke-width="${sw}"${dash}/>` +
|
|
484
|
+
`<ellipse cx="${r2(boxCx(b))}" cy="${r2(b.y + ry)}" rx="${r2(b.w / 2)}" ry="${ry}" fill="none" stroke="${stroke}" stroke-width="${sw}"/>`;
|
|
485
|
+
}
|
|
486
|
+
const rx = shape === 'round' ? r2(b.h / 2) : style === 'mono' ? 2 : style === 'soft' ? 14 : 8;
|
|
487
|
+
return `<rect x="${r2(b.x)}" y="${r2(b.y)}" width="${r2(b.w)}" height="${r2(b.h)}" rx="${rx}" fill="${fill}" stroke="${stroke}" stroke-width="${sw}"${dash}/>`;
|
|
488
|
+
}
|
|
489
|
+
function drawNode(n, interactive = false, degree = 0, style = 'base') {
|
|
490
|
+
const { box: b, color } = n;
|
|
491
|
+
const dash = n.type === 'ext' ? ' stroke-dasharray="5 3"' : '';
|
|
492
|
+
const tint = style === 'soft' && color >= 0 ? sVar(color) : null;
|
|
493
|
+
const frame = nodeFrame(b, n.shape ?? 'rect', style, color, dash, tint);
|
|
494
|
+
const accent = style === 'base' && color >= 0 && n.type !== 'ext' && !(n.shape === 'diamond' || n.shape === 'cylinder')
|
|
495
|
+
? `<rect x="${r2(b.x)}" y="${r2(b.y)}" width="3.5" height="${r2(b.h)}" rx="1.5" fill="${sVar(color)}"/>`
|
|
496
|
+
: '';
|
|
497
|
+
const labelY = n.sub ? boxCy(b) - 3 : boxCy(b) + 4.5;
|
|
498
|
+
const sub = n.sub
|
|
499
|
+
? `<text x="${r2(boxCx(b))}" y="${r2(boxCy(b) + 15)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${esc(n.sub)}</text>`
|
|
500
|
+
: '';
|
|
501
|
+
// interactive(explore 落地页):data-tip 悬停说明 + data-node 供点击高亮连通关系
|
|
502
|
+
const ia = interactive
|
|
503
|
+
? ` data-node="${esc(n.id)}" data-tip="${esc(`${n.label} · ${n.type}${n.sub ? ` · ${n.sub}` : ''}${degree ? ` · ${degree} link${degree > 1 ? 's' : ''}` : ''}`)}" tabindex="0"`
|
|
504
|
+
: '';
|
|
505
|
+
return (`<g${ia}>` +
|
|
506
|
+
frame +
|
|
507
|
+
accent +
|
|
508
|
+
`<text x="${r2(boxCx(b))}" y="${r2(labelY)}" text-anchor="middle" font-size="13" font-weight="500" fill="var(--d-ink)">${esc(n.label)}</text>` +
|
|
509
|
+
sub +
|
|
510
|
+
`</g>`);
|
|
511
|
+
}
|
|
512
|
+
export function boundaryBox(byId, bd) {
|
|
513
|
+
const boxes = bd.ids.map(id => byId.get(id).box);
|
|
514
|
+
const x = Math.min(...boxes.map(o => o.x)) - 14;
|
|
515
|
+
const y = Math.min(...boxes.map(o => o.y)) - 14;
|
|
516
|
+
return {
|
|
517
|
+
x, y,
|
|
518
|
+
w: Math.max(...boxes.map(o => o.x + o.w)) + 14 - x,
|
|
519
|
+
h: Math.max(...boxes.map(o => o.y + o.h)) + 14 - y,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
// 边界标签四角择优:远离边走线(权重高)与节点盒;平手偏好左上(视觉惯例)
|
|
523
|
+
function boundaryLabelCorner(box, label, routed, boxes) {
|
|
524
|
+
const w = textW(label, 10) + label.length * 1.2;
|
|
525
|
+
const cands = [
|
|
526
|
+
{ x: box.x + 12, y: box.y + 3.5, anchor: 'start' }, // TL
|
|
527
|
+
{ x: box.x + box.w - 12, y: box.y + 3.5, anchor: 'end' }, // TR
|
|
528
|
+
{ x: box.x + box.w - 12, y: box.y + box.h - 3.5, anchor: 'end' }, // BR
|
|
529
|
+
{ x: box.x + 12, y: box.y + box.h - 3.5, anchor: 'start' }, // BL
|
|
530
|
+
];
|
|
531
|
+
const rectOf = (c) => ({
|
|
532
|
+
x: c.anchor === 'start' ? c.x - 6 : c.x - w - 6,
|
|
533
|
+
y: c.y - 13,
|
|
534
|
+
w: w + 12,
|
|
535
|
+
h: 26,
|
|
536
|
+
});
|
|
537
|
+
let best = cands[0];
|
|
538
|
+
let bestScore = Infinity;
|
|
539
|
+
cands.forEach((c, idx) => {
|
|
540
|
+
const r = rectOf(c);
|
|
541
|
+
let score = idx * 0.01; // 平手序 TL < TR < BR < BL
|
|
542
|
+
for (const rt of routed) {
|
|
543
|
+
for (let i = 1; i < rt.pts.length; i++) {
|
|
544
|
+
if (segRectIntersect(rt.pts[i - 1][0], rt.pts[i - 1][1], rt.pts[i][0], rt.pts[i][1], r.x, r.y, r.w, r.h)) {
|
|
545
|
+
score += 10;
|
|
546
|
+
break;
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
for (const b of boxes)
|
|
551
|
+
if (rectsOverlap(r, b))
|
|
552
|
+
score += 5;
|
|
553
|
+
if (score < bestScore) {
|
|
554
|
+
bestScore = score;
|
|
555
|
+
best = c;
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
return best;
|
|
559
|
+
}
|
|
560
|
+
function drawBoundaries(placed, boundaries, corners, interactive = false) {
|
|
561
|
+
const byId = new Map(placed.map(p => [p.id, p]));
|
|
562
|
+
return boundaries.map((bd, i) => {
|
|
563
|
+
const box = boundaryBox(byId, bd);
|
|
564
|
+
const c = corners[i];
|
|
565
|
+
// interactive:带成员 id 清单,选中态下客户端据此判断边界是否相关(相关=含 .on 节点)
|
|
566
|
+
const ia = interactive ? ` data-boundary="${esc(bd.ids.join(' '))}"` : '';
|
|
567
|
+
return (`<g${ia}>` +
|
|
568
|
+
`<rect x="${r2(box.x)}" y="${r2(box.y)}" width="${r2(box.w)}" height="${r2(box.h)}" rx="10" fill="none" stroke="var(--d-border)" stroke-width="1.2"/>` +
|
|
569
|
+
`<text x="${r2(c.x)}" y="${r2(c.y)}" text-anchor="${c.anchor}" dominant-baseline="middle" font-size="10" font-weight="600" letter-spacing="1.2" fill="var(--d-faint)">${esc(bd.label.toUpperCase())}</text>` +
|
|
570
|
+
`</g>`);
|
|
571
|
+
}).join('');
|
|
572
|
+
}
|
|
573
|
+
const DEFS = `<defs>` +
|
|
574
|
+
`<marker id="am-p" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" fill="var(--d-muted)"/></marker>` +
|
|
575
|
+
`<marker id="am-m" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" fill="var(--d-ink)"/></marker>` +
|
|
576
|
+
`<marker id="am-d" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" fill="var(--d-faint)"/></marker>` +
|
|
577
|
+
`</defs>`;
|
|
578
|
+
/**
|
|
579
|
+
* Render an architecture map from a URL's query params (`/draw/archmap`).
|
|
580
|
+
*
|
|
581
|
+
* Nodes carry their grid cell (`r0c1`, optional `w`/`h` spans up to 3) or a
|
|
582
|
+
* pixel escape hatch (`p40,80`); the engine auto-sizes tracks, routes
|
|
583
|
+
* orthogonal edges and rejects cell double-booking. Throws ApiError(400)
|
|
584
|
+
* with a corrective example on malformed params.
|
|
585
|
+
*/
|
|
586
|
+
export function renderArchmapFromUrl(url, interactive = false) {
|
|
587
|
+
const params = url.searchParams;
|
|
588
|
+
const nRaw = params.getAll('n');
|
|
589
|
+
if (!nRaw.length)
|
|
590
|
+
bad('at least one node is required');
|
|
591
|
+
const nodes = parseNodes(nRaw);
|
|
592
|
+
const seen = new Set();
|
|
593
|
+
for (const n of nodes) {
|
|
594
|
+
if (seen.has(n.id))
|
|
595
|
+
bad(`duplicate node id "${n.id}"`);
|
|
596
|
+
seen.add(n.id);
|
|
597
|
+
}
|
|
598
|
+
const edges = parseEdges(params.getAll('e'), seen);
|
|
599
|
+
const boundaries = parseBoundaries(params.getAll('b'), seen);
|
|
600
|
+
const title = params.get('title')?.trim() || undefined;
|
|
601
|
+
const gridCols = clampInt(params, 'grid', 0, 1, 12);
|
|
602
|
+
const v = resolveVersion(params.get('v'));
|
|
603
|
+
// t= 为架构图自有风格(base/mono/soft);其余值透传 sankey 皮肤(调色板复用)
|
|
604
|
+
const tParam = params.get('t');
|
|
605
|
+
const style = tParam === 'mono' || tParam === 'soft' ? tParam : 'base';
|
|
606
|
+
const skin = resolveSkin('sankey', style === 'base' ? tParam : null, schemeParam(params));
|
|
607
|
+
return renderArchmapCore({ nodes, edges, boundaries, title, style, interactive, gridCols, v, skin,
|
|
608
|
+
minW: clampInt(params, 'w', 0, 200, 2000), minH: clampInt(params, 'h', 0, 140, 2000),
|
|
609
|
+
tagQuery: url.search });
|
|
610
|
+
}
|
|
611
|
+
export function renderArchmapCore(o) {
|
|
612
|
+
const { placed, W, H } = packGrid(o.nodes, o.gridCols);
|
|
613
|
+
// 无标题也留 30px 角标带(#44):Edit 胶囊占右上 y≈25–45,不留带会压住首行节点
|
|
614
|
+
const titleH = o.title ? 40 : 30;
|
|
615
|
+
let width = Math.max(W, 320);
|
|
616
|
+
let bodyH = H;
|
|
617
|
+
for (const p of placed) { // 像素节点可能超出轨道画布
|
|
618
|
+
width = Math.max(width, p.box.x + p.box.w + PAD);
|
|
619
|
+
bodyH = Math.max(bodyH, p.box.y + p.box.h + PAD);
|
|
620
|
+
}
|
|
621
|
+
// 分组框先算好:走线要避开它(绕行时别跑出这个框)
|
|
622
|
+
const byIdEarly = new Map(placed.map(p => [p.id, p]));
|
|
623
|
+
const bboxes = o.boundaries.map(bd => boundaryBox(byIdEarly, bd));
|
|
624
|
+
// 管线:路由 → 边界标签选角(避开走线) → 边标签避让(节点盒 ∪ 边界标签区)
|
|
625
|
+
const routed = o.edgeMode === 'straight'
|
|
626
|
+
? straightRoutes(placed, o.edges, o.straightDir ?? 'TB')
|
|
627
|
+
: computeRoutes(placed, o.edges, bboxes, o.routing);
|
|
628
|
+
// 画布要盖住走线:反向边绕到节点外面(U 形),边标签又贴在走线上 ——
|
|
629
|
+
// 只按节点盒算尺寸,那一段会被裁掉(字看不全)。
|
|
630
|
+
for (const rt of routed) {
|
|
631
|
+
for (const p of rt.pts) {
|
|
632
|
+
width = Math.max(width, p[0] + PAD);
|
|
633
|
+
bodyH = Math.max(bodyH, p[1] + PAD);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
width = Math.max(width, o.minW ?? 0);
|
|
637
|
+
const height = Math.max(bodyH + titleH, o.minH ?? 0);
|
|
638
|
+
const byId = new Map(placed.map(p => [p.id, p]));
|
|
639
|
+
const boxes = placed.map(p => p.box);
|
|
640
|
+
const corners = o.boundaries.map(bd => boundaryLabelCorner(boundaryBox(byId, bd), bd.label.toUpperCase(), routed, boxes));
|
|
641
|
+
const cornerRect = (bd, c) => {
|
|
642
|
+
const w = textW(bd.label.toUpperCase(), 10) + bd.label.length * 1.2;
|
|
643
|
+
return { x: c.anchor === 'start' ? c.x - 6 : c.x - w - 6, y: c.y - 13, w: w + 12, h: 26 };
|
|
644
|
+
};
|
|
645
|
+
const avoid = [
|
|
646
|
+
...boxes.map(b => ({ x: b.x - 2, y: b.y - 2, w: b.w + 4, h: b.h + 4 })),
|
|
647
|
+
...o.boundaries.map((bd, i) => cornerRect(bd, corners[i])),
|
|
648
|
+
];
|
|
649
|
+
const titleSvg = o.title
|
|
650
|
+
? `<text x="${PAD}" y="${PAD + 14}" font-size="18" font-weight="500" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(o.title)}</text>`
|
|
651
|
+
: '';
|
|
652
|
+
// interactive 悬停强调 + 点击高亮(1-hop 连通)样式;客户端脚本在 explore 落地页。
|
|
653
|
+
// Figma/Miro 式:悬停 = 提亮,选中 = 提亮 + 未选中淡化;无描边/无换色/无 transform
|
|
654
|
+
// (transform 会被入场动画 draw-rise 的 fill-both 终态锁死,勿用)
|
|
655
|
+
const iStyle = o.interactive
|
|
656
|
+
? `<style>#am g[data-node]{cursor:pointer;transition:filter .12s ease,opacity .15s ease}` +
|
|
657
|
+
`#am g[data-node]:focus{outline:none}` +
|
|
658
|
+
`#am g[data-node]:hover,#am g[data-node]:focus-visible{filter:brightness(1.3)}` +
|
|
659
|
+
`#am g[data-node].on{filter:brightness(1.3)}` +
|
|
660
|
+
`#am.hl g[data-node]{opacity:.45}#am.hl g[data-node].on{opacity:1}` +
|
|
661
|
+
`#am.hl g[data-boundary]{opacity:.4}#am.hl g[data-boundary].on{opacity:1}` +
|
|
662
|
+
`#am.hl path[data-from]{opacity:.25}#am.hl path[data-from].on{opacity:1}` +
|
|
663
|
+
`#am.hl text[data-edge-label]{opacity:.3}#am.hl text[data-edge-label].on{opacity:1}</style>`
|
|
664
|
+
: '';
|
|
665
|
+
const degree = new Map();
|
|
666
|
+
if (o.interactive)
|
|
667
|
+
for (const e of o.edges) {
|
|
668
|
+
degree.set(e.from, (degree.get(e.from) ?? 0) + 1);
|
|
669
|
+
degree.set(e.to, (degree.get(e.to) ?? 0) + 1);
|
|
670
|
+
}
|
|
671
|
+
const body = iStyle + DEFS
|
|
672
|
+
+ drawBoundaries(placed, o.boundaries, corners, o.interactive)
|
|
673
|
+
+ drawRoutes(routed, o.interactive, o.style, byId)
|
|
674
|
+
+ placed.map(p => drawNode(p, o.interactive, degree.get(p.id) ?? 0, o.style)).join('')
|
|
675
|
+
// 边标签画在节点之上:标签比空档宽时会伸进节点里,压在节点下面就看不见了
|
|
676
|
+
+ drawEdgeLabels(routed, avoid, o.interactive);
|
|
677
|
+
// 标题占位下移 + h 垫高的剩余空间上下均分(内容垂直居中,标题仍在顶部)
|
|
678
|
+
const dy = titleH + (height - titleH - bodyH) / 2;
|
|
679
|
+
const children = titleSvg + `<g id="am" transform="translate(0 ${r2(dy)})">` + body + `</g>`;
|
|
680
|
+
const spec = { type: 'archmap', ...(o.title ? { title: o.title } : {}), nodes: o.nodes, edges: o.edges, boundaries: o.boundaries };
|
|
681
|
+
const svg = svgDoc(width, height, 'archmap', o.v.version, o.aria ?? o.title ?? `architecture map, ${o.nodes.length} nodes`, spec, o.skin, children, o.tagQuery, o.tagKind);
|
|
682
|
+
return { svg, version: o.v.version, cacheControl: o.v.cacheControl };
|
|
683
|
+
}
|