@x-cmd/draw 0.3.0 → 0.4.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/README.md +79 -297
- package/dist/lib.d.ts +26 -3
- package/dist/lib.js +31 -4
- package/dist/node/cli.js +1 -1
- 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 +90 -3
- package/dist/render/archmap.js +174 -102
- package/dist/render/area.js +9 -7
- package/dist/render/banner.d.ts +8 -0
- package/dist/render/banner.js +204 -0
- package/dist/render/bar-grouped.js +3 -2
- package/dist/render/bar-stacked.js +3 -2
- package/dist/render/bar.js +3 -2
- 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 +3 -2
- 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 +30 -0
- package/dist/render/echarts-ssr.js +337 -16
- package/dist/render/gantt.d.ts +11 -0
- package/dist/render/gantt.js +12 -19
- 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 +2 -2
- package/dist/render/line.js +2 -2
- package/dist/render/maps.d.ts +34 -0
- package/dist/render/maps.js +197 -2
- package/dist/render/mermaid.js +25 -6
- package/dist/render/mindmap.js +3 -2
- package/dist/render/onepage.d.ts +62 -0
- package/dist/render/onepage.js +670 -0
- package/dist/render/orgicon.d.ts +12 -0
- package/dist/render/orgicon.js +31 -0
- package/dist/render/png.js +3 -1
- 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 +100 -1
- package/dist/render/route.d.ts +86 -0
- package/dist/render/route.js +1077 -0
- package/dist/render/scatter.js +73 -7
- package/dist/render/seq.d.ts +41 -0
- package/dist/render/seq.js +248 -0
- package/dist/render/src-spec.d.ts +5 -0
- package/dist/render/src-spec.js +49 -0
- package/dist/render/svg.d.ts +16 -2
- package/dist/render/svg.js +85 -7
- 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/us-states.js +4 -6
- 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 +5 -0
- package/dist/specs/base.js +14 -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 +268 -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/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/types/dagre.d.ts +39 -0
- package/dist/types/dagre.js +3 -0
- package/package.json +17 -1
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
// /draw/uc —— 用例图:左边是参与者(火柴人),里面是用例(椭圆),外面一圈系统边界。
|
|
2
|
+
//
|
|
3
|
+
// 模型:
|
|
4
|
+
// n=id;actor|label 参与者
|
|
5
|
+
// n=id;uc|label 用例
|
|
6
|
+
// e=from;to[;label] 关联;label 写 <<include>> / <<extend>> 就是虚线依赖
|
|
7
|
+
// b=label;id id … 系统边界(用例在里面)
|
|
8
|
+
//
|
|
9
|
+
// 画法:参与者排成一列(左),用例排成一行(右),参与者和用例之间连线;
|
|
10
|
+
// 用例外面套一个系统边界框。
|
|
11
|
+
import { ApiError } from '../specs/index.js';
|
|
12
|
+
import { GRID_POS_RE, PX_POS_RE, parseBoundaries, splitFields } from './archmap.js';
|
|
13
|
+
import { resolveSkin } from '../styles/index.js';
|
|
14
|
+
import { clampInt, schemeParam } from './query.js';
|
|
15
|
+
import { esc, r2, roundedPath, svgDoc, textW } from './svg.js';
|
|
16
|
+
const PAD = 44;
|
|
17
|
+
const ACTOR_H = 92; // 火柴人 + 名字占的高度
|
|
18
|
+
const UC_H = 54; // 椭圆高
|
|
19
|
+
const COL_GAP = 78;
|
|
20
|
+
const ROW_GAP = 18;
|
|
21
|
+
const UC_ROW_GAP = 46;
|
|
22
|
+
/**
|
|
23
|
+
* 节点语法和架构图完全一致:id;类型;标签[;副标][;位置]
|
|
24
|
+
* —— 位置可省(用例图自己排),给了就按给的摆。
|
|
25
|
+
*/
|
|
26
|
+
function parseNodes(raw) {
|
|
27
|
+
return raw.map((v, i) => {
|
|
28
|
+
const f = splitFields(v).map((x) => x.trim());
|
|
29
|
+
if (f.length < 3 || f.length > 5) {
|
|
30
|
+
throw new ApiError(400, `"n" #${i + 1} must be "id;actor|label[;sub][;pos]". Example: n=caller;actor;Your app`);
|
|
31
|
+
}
|
|
32
|
+
const kind = f[1];
|
|
33
|
+
if (kind !== 'actor' && kind !== 'uc') {
|
|
34
|
+
throw new ApiError(400, `"n" #${i + 1}: kind must be "actor" or "uc", got ${JSON.stringify(kind)}`);
|
|
35
|
+
}
|
|
36
|
+
if (!f[2])
|
|
37
|
+
throw new ApiError(400, `"n" #${i + 1}: label is required`);
|
|
38
|
+
let sub;
|
|
39
|
+
let pos;
|
|
40
|
+
if (f.length === 4) {
|
|
41
|
+
if (GRID_POS_RE.test(f[3]) || PX_POS_RE.test(f[3]))
|
|
42
|
+
pos = f[3];
|
|
43
|
+
else
|
|
44
|
+
sub = f[3];
|
|
45
|
+
}
|
|
46
|
+
else if (f.length === 5) {
|
|
47
|
+
sub = f[3];
|
|
48
|
+
pos = f[4];
|
|
49
|
+
}
|
|
50
|
+
const node = { id: f[0], kind, label: f[2], ...(sub ? { sub } : {}), x: 0, y: 0, w: 0, h: 0 };
|
|
51
|
+
if (pos) {
|
|
52
|
+
const g = GRID_POS_RE.exec(pos);
|
|
53
|
+
if (g) {
|
|
54
|
+
node.grid = { row: +g[1], col: +g[2], w: g[3] ? +g[3] : 1, h: g[4] ? +g[4] : 1 };
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const px = PX_POS_RE.exec(pos);
|
|
58
|
+
if (!px)
|
|
59
|
+
throw new ApiError(400, `"n" #${i + 1}: pos must look like r0c1[w2][h2] or p40,80[,120x48]`);
|
|
60
|
+
node.px = { x: +px[1], y: +px[2], ...(px[3] ? { w: +px[3], h: +px[4] } : {}) };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return node;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function parseEdges(raw, ids) {
|
|
67
|
+
return raw.map((v, i) => {
|
|
68
|
+
const f = v.split(';').map((x) => x.trim());
|
|
69
|
+
if (f.length < 2 || !ids.has(f[0]) || !ids.has(f[1])) {
|
|
70
|
+
throw new ApiError(400, `"e" #${i + 1} must be "from;to[;label]" and both ids must exist. Example: e=caller;uc1`);
|
|
71
|
+
}
|
|
72
|
+
const label = f[2] || undefined;
|
|
73
|
+
return { from: f[0], to: f[1], label, dep: !!label && /^<<.+>>$/.test(label) };
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
/** 火柴人:头 + 身子 + 两条胳膊 + 两条腿 */
|
|
77
|
+
function drawActor(n, color, interactive = false) {
|
|
78
|
+
const cx = n.x + n.w / 2;
|
|
79
|
+
const headR = 11;
|
|
80
|
+
const headY = n.y + 4 + headR;
|
|
81
|
+
const bodyTop = headY + headR + 3;
|
|
82
|
+
const bodyBot = bodyTop + 26;
|
|
83
|
+
const armY = bodyTop + 9;
|
|
84
|
+
const labelY = n.y + n.h - 16;
|
|
85
|
+
const ia = interactive ? ` data-node="${esc(n.id)}" data-tip="${esc(`actor · ${n.label}${n.sub ? ` · ${n.sub}` : ''}`)}" tabindex="0"` : '';
|
|
86
|
+
return (`<g${ia}>` +
|
|
87
|
+
`<circle cx="${r2(cx)}" cy="${r2(headY)}" r="${headR}" fill="none" stroke="${color}" stroke-width="1.6"/>` +
|
|
88
|
+
`<path d="M${r2(cx)} ${r2(bodyTop)}L${r2(cx)} ${r2(bodyBot)}" stroke="${color}" stroke-width="1.6" fill="none"/>` +
|
|
89
|
+
`<path d="M${r2(cx - 13)} ${r2(armY)}L${r2(cx + 13)} ${r2(armY)}" stroke="${color}" stroke-width="1.6" fill="none"/>` +
|
|
90
|
+
`<path d="M${r2(cx - 11)} ${r2(bodyBot)}L${r2(cx)} ${r2(bodyTop + 12)}M${r2(cx + 11)} ${r2(bodyBot)}L${r2(cx)} ${r2(bodyTop + 12)}" stroke="${color}" stroke-width="1.6" fill="none"/>` +
|
|
91
|
+
`<text x="${r2(cx)}" y="${r2(labelY)}" text-anchor="middle" font-size="13" font-weight="600" fill="var(--d-ink)">${esc(n.label)}</text>` +
|
|
92
|
+
(n.sub ? `<text x="${r2(cx)}" y="${r2(labelY + 15)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${esc(n.sub)}</text>` : '') +
|
|
93
|
+
`</g>`);
|
|
94
|
+
}
|
|
95
|
+
/** 用例:椭圆 */
|
|
96
|
+
function drawUseCase(n, color, interactive = false) {
|
|
97
|
+
const cx = n.x + n.w / 2;
|
|
98
|
+
const cy = n.y + n.h / 2;
|
|
99
|
+
const labelY = n.sub ? cy - 2 : cy + 5;
|
|
100
|
+
const ia = interactive ? ` data-node="${esc(n.id)}" data-tip="${esc(`use case · ${n.label}${n.sub ? ` · ${n.sub}` : ''}`)}" tabindex="0"` : '';
|
|
101
|
+
return (`<g${ia}>` +
|
|
102
|
+
`<ellipse cx="${r2(cx)}" cy="${r2(cy)}" rx="${r2(n.w / 2)}" ry="${r2(n.h / 2)}" fill="${color}" fill-opacity="0.1" stroke="${color}" stroke-opacity="0.6" stroke-width="1.3"/>` +
|
|
103
|
+
`<text x="${r2(cx)}" y="${r2(labelY)}" text-anchor="middle" font-size="13" font-weight="500" fill="var(--d-ink)">${esc(n.label)}</text>` +
|
|
104
|
+
(n.sub ? `<text x="${r2(cx)}" y="${r2(labelY + 15)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${esc(n.sub)}</text>` : '') +
|
|
105
|
+
`</g>`);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* 渲染用例图。
|
|
109
|
+
*/
|
|
110
|
+
export function renderUcCore(o) {
|
|
111
|
+
const titleH = o.title ? 40 : 0;
|
|
112
|
+
const actors = o.nodes.filter((n) => n.kind === 'actor');
|
|
113
|
+
const ucs = o.nodes.filter((n) => n.kind === 'uc');
|
|
114
|
+
// 参与者一列(左),用例一行(右)
|
|
115
|
+
const actorW = Math.max(120, ...actors.map((a) => textW(a.label, 13) + 36));
|
|
116
|
+
const ucW = Math.max(150, ...ucs.map((u) => Math.max(textW(u.label, 13) + 64, u.sub ? textW(u.sub, 11) + 64 : 0)));
|
|
117
|
+
let maxY = PAD + titleH;
|
|
118
|
+
actors.forEach((a) => { a.w = actorW; });
|
|
119
|
+
// 用例排成一列,参与者在另一侧 —— 中间留出一条「脊柱」通道走线,
|
|
120
|
+
// 这样同一个参与方连多个用例时,线各走各的,不横穿别人的椭圆。
|
|
121
|
+
const ucColH = ucs.length * (UC_H + UC_ROW_GAP) - UC_ROW_GAP;
|
|
122
|
+
const actorColH = actors.length * (ACTOR_H + ROW_GAP) - ROW_GAP;
|
|
123
|
+
const colH = Math.max(ucColH, actorColH);
|
|
124
|
+
const top = PAD + titleH;
|
|
125
|
+
const ucTop = top + Math.max(0, (colH - ucColH) / 2);
|
|
126
|
+
const actorTop = top + Math.max(0, (colH - actorColH) / 2);
|
|
127
|
+
const ucX = o.actorsRight ? PAD : PAD + actorW + COL_GAP;
|
|
128
|
+
const actorX = o.actorsRight ? PAD + actorW + COL_GAP + ucW + COL_GAP : PAD;
|
|
129
|
+
ucs.forEach((u, i) => {
|
|
130
|
+
u.x = ucX;
|
|
131
|
+
u.y = ucTop + i * (UC_H + UC_ROW_GAP);
|
|
132
|
+
u.w = ucW;
|
|
133
|
+
u.h = UC_H;
|
|
134
|
+
maxY = Math.max(maxY, u.y + u.h);
|
|
135
|
+
});
|
|
136
|
+
actors.forEach((a, i) => {
|
|
137
|
+
a.x = actorX;
|
|
138
|
+
a.y = actorTop + i * (ACTOR_H + ROW_GAP);
|
|
139
|
+
a.w = actorW;
|
|
140
|
+
a.h = ACTOR_H;
|
|
141
|
+
maxY = Math.max(maxY, a.y + a.h);
|
|
142
|
+
});
|
|
143
|
+
// 给了位置的按位置摆(和架构图同一套格子)
|
|
144
|
+
const placed = [...ucs, ...actors];
|
|
145
|
+
const gridNodes = placed.filter((n) => n.grid);
|
|
146
|
+
// 脊柱:参与方和用例之间的空档正中
|
|
147
|
+
const spineX = o.actorsRight
|
|
148
|
+
? ucX + ucW + COL_GAP / 2
|
|
149
|
+
: PAD + actorW + COL_GAP / 2;
|
|
150
|
+
let width = Math.max(320, (o.actorsRight ? actorX + actorW : ucX + ucW + 30) + PAD);
|
|
151
|
+
let height = maxY + PAD;
|
|
152
|
+
if (gridNodes.length) {
|
|
153
|
+
const rows = Math.max(...gridNodes.map((n) => n.grid.row + n.grid.h));
|
|
154
|
+
const cols = Math.max(...gridNodes.map((n) => n.grid.col + n.grid.w));
|
|
155
|
+
const colX = new Array(cols).fill(0);
|
|
156
|
+
const rowY = new Array(rows).fill(0);
|
|
157
|
+
for (const n of gridNodes) {
|
|
158
|
+
colX[n.grid.col] = Math.max(colX[n.grid.col], n.kind === 'actor' ? actorW : ucW);
|
|
159
|
+
rowY[n.grid.row] = Math.max(rowY[n.grid.row], n.kind === 'actor' ? ACTOR_H : UC_H);
|
|
160
|
+
}
|
|
161
|
+
const trackX = [];
|
|
162
|
+
const trackY = [];
|
|
163
|
+
let gx = PAD;
|
|
164
|
+
for (let c = 0; c < cols; c++) {
|
|
165
|
+
trackX[c] = gx;
|
|
166
|
+
gx += colX[c] + COL_GAP;
|
|
167
|
+
}
|
|
168
|
+
let gy = PAD + titleH;
|
|
169
|
+
for (let r = 0; r < rows; r++) {
|
|
170
|
+
trackY[r] = gy;
|
|
171
|
+
gy += rowY[r] + ROW_GAP;
|
|
172
|
+
}
|
|
173
|
+
for (const n of gridNodes) {
|
|
174
|
+
const g = n.grid;
|
|
175
|
+
n.x = trackX[g.col];
|
|
176
|
+
n.y = trackY[g.row];
|
|
177
|
+
n.w = n.kind === 'actor' ? actorW : ucW;
|
|
178
|
+
n.h = n.kind === 'actor' ? ACTOR_H : UC_H;
|
|
179
|
+
width = Math.max(width, n.x + n.w + PAD);
|
|
180
|
+
maxY = Math.max(maxY, n.y + n.h);
|
|
181
|
+
}
|
|
182
|
+
// 没给位置的用例回到用例列;没给位置的参与方留在原处
|
|
183
|
+
for (const n of placed) {
|
|
184
|
+
if (n.grid)
|
|
185
|
+
continue;
|
|
186
|
+
if (n.kind === 'uc')
|
|
187
|
+
n.x = ucX;
|
|
188
|
+
}
|
|
189
|
+
height = Math.max(height, maxY + PAD);
|
|
190
|
+
}
|
|
191
|
+
// 系统边界:把用例框起来(可以有多层)
|
|
192
|
+
let boundarySvg = '';
|
|
193
|
+
for (const b of o.boundaries) {
|
|
194
|
+
const bs = b.ids.map((id) => o.nodes.find((n) => n.id === id)).filter(Boolean);
|
|
195
|
+
if (!bs.length)
|
|
196
|
+
continue;
|
|
197
|
+
const bx = Math.min(...bs.map((x) => x.x)) - 26;
|
|
198
|
+
const by = Math.min(...bs.map((x) => x.y)) - 26;
|
|
199
|
+
const bw = Math.max(...bs.map((x) => x.x + x.w)) + 26 - bx;
|
|
200
|
+
const bh = Math.max(...bs.map((x) => x.y + x.h)) + 26 - by;
|
|
201
|
+
const label = b.label.toUpperCase();
|
|
202
|
+
const lw = textW(label, 10) + b.label.length * 1.2 + 20;
|
|
203
|
+
width = Math.max(width, bx + bw + PAD);
|
|
204
|
+
height = Math.max(height, by + bh + PAD);
|
|
205
|
+
boundarySvg +=
|
|
206
|
+
`<g data-boundary="${esc(b.ids.join(' '))}">` +
|
|
207
|
+
`<rect x="${r2(bx)}" y="${r2(by)}" width="${r2(bw)}" height="${r2(bh)}" rx="14" fill="none" stroke="var(--d-border)" stroke-width="1.3" stroke-dasharray="7 5"/>` +
|
|
208
|
+
`<rect x="${r2(bx + 14)}" y="${r2(by - 9)}" width="${r2(lw)}" height="20" rx="6" fill="var(--d-card)"/>` +
|
|
209
|
+
`<text x="${r2(bx + 14 + lw / 2)}" y="${r2(by + 4)}" text-anchor="middle" font-size="10" font-weight="600" letter-spacing="1.2" fill="var(--d-faint)">${esc(label)}</text>` +
|
|
210
|
+
`</g>`;
|
|
211
|
+
}
|
|
212
|
+
// 连线:参与方 → 脊柱 → 用例,三段正交。同一个参与方连多个用例时,
|
|
213
|
+
// 每条线在参与方那边错开一点、在脊柱上各走各的一段,互不压。
|
|
214
|
+
const byId = new Map(o.nodes.map((n) => [n.id, n]));
|
|
215
|
+
const exitSeen = new Map();
|
|
216
|
+
const edges = [];
|
|
217
|
+
let labelRight = 0; // 标签往右伸出去多少(画布要留出这段)
|
|
218
|
+
for (const e of o.edges) {
|
|
219
|
+
const a = byId.get(e.from);
|
|
220
|
+
const b = byId.get(e.to);
|
|
221
|
+
const stroke = e.dep ? 'var(--d-faint)' : 'var(--d-muted)';
|
|
222
|
+
const dash = e.dep ? ' stroke-dasharray="5 4"' : '';
|
|
223
|
+
const ia = o.interactive ? ` data-from="${esc(e.from)}" data-to="${esc(e.to)}"` : '';
|
|
224
|
+
const n = (exitSeen.get(e.from) ?? 0);
|
|
225
|
+
exitSeen.set(e.from, n + 1);
|
|
226
|
+
let pts;
|
|
227
|
+
let lx = 0;
|
|
228
|
+
let ly = 0;
|
|
229
|
+
// 标签贴在哪:start = 从 lx 往右排,end = 往左排
|
|
230
|
+
let lanchor = 'start';
|
|
231
|
+
if (a.kind === 'actor' && b.kind === 'uc') {
|
|
232
|
+
const ax = o.actorsRight ? a.x - n * 9 : a.x + a.w + n * 9;
|
|
233
|
+
const ay = a.y + 30;
|
|
234
|
+
const bx = o.actorsRight ? b.x + b.w : b.x;
|
|
235
|
+
const by = b.y + b.h / 2;
|
|
236
|
+
pts = [[ax, ay], [spineX, ay], [spineX, by], [bx, by]];
|
|
237
|
+
// 标签贴在用例左边,不压在椭圆上
|
|
238
|
+
lx = bx - 6;
|
|
239
|
+
ly = by;
|
|
240
|
+
lanchor = 'end';
|
|
241
|
+
}
|
|
242
|
+
else if (a.kind === 'uc' && b.kind === 'actor') {
|
|
243
|
+
const ax = o.actorsRight ? a.x : a.x + a.w;
|
|
244
|
+
const ay = a.y + a.h / 2;
|
|
245
|
+
// 参与方在哪一侧,就从哪一侧接线(脊柱在参与方的右边还是左边)
|
|
246
|
+
const bx = o.actorsRight ? b.x - n * 9 : b.x + b.w + n * 9;
|
|
247
|
+
const by = b.y + 30;
|
|
248
|
+
pts = [[ax, ay], [spineX, ay], [spineX, by], [bx, by]];
|
|
249
|
+
lx = bx + 6;
|
|
250
|
+
ly = by;
|
|
251
|
+
lanchor = 'start';
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
// 用例之间的依赖(<<include>>):绕到用例列右边的一条通道上,
|
|
255
|
+
// 不从两个椭圆中间斜穿过去
|
|
256
|
+
const depX = ucX + ucW + 18;
|
|
257
|
+
const ax = a.x + a.w;
|
|
258
|
+
const ay = a.y + a.h / 2;
|
|
259
|
+
const bx = b.x + b.w;
|
|
260
|
+
const by = b.y + b.h / 2;
|
|
261
|
+
pts = [[ax, ay], [depX, ay], [depX, by], [bx, by]];
|
|
262
|
+
// 标签排在通道右边,一点不碰椭圆
|
|
263
|
+
lx = depX + 6;
|
|
264
|
+
ly = (ay + by) / 2;
|
|
265
|
+
lanchor = 'start';
|
|
266
|
+
}
|
|
267
|
+
edges.push(`<path d="${roundedPath(pts, 8)}" fill="none" stroke="${stroke}" stroke-width="1.3"${dash}${ia}/>`);
|
|
268
|
+
if (e.label) {
|
|
269
|
+
const w = textW(e.label, 10.5) + 12;
|
|
270
|
+
const rx = lanchor === 'end' ? lx - w : lx;
|
|
271
|
+
const tx = lx;
|
|
272
|
+
labelRight = Math.max(labelRight, rx + w);
|
|
273
|
+
edges.push(`<rect x="${r2(rx)}" y="${r2(ly - 15)}" width="${r2(w)}" height="17" rx="5" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1"/>`, `<text x="${r2(tx)}" y="${r2(ly - 3)}" text-anchor="${lanchor}" font-size="10.5" fill="var(--d-text)">${esc(e.label)}</text>`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
width = Math.max(width, labelRight + PAD);
|
|
277
|
+
const titleSvg = o.title
|
|
278
|
+
? `<text x="${PAD}" y="${PAD + 16}" font-size="18" font-weight="600" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(o.title)}</text>`
|
|
279
|
+
: '';
|
|
280
|
+
const nodes = o.nodes.map((n) => n.kind === 'actor'
|
|
281
|
+
? drawActor(n, 'var(--d-muted)', o.interactive)
|
|
282
|
+
: drawUseCase(n, 'var(--d-s2)', o.interactive)).join('');
|
|
283
|
+
const spec = { type: 'uc', title: o.title, nodes: o.nodes, edges: o.edges };
|
|
284
|
+
const svg = svgDoc(width, height, 'uc', o.version, o.title ?? `use case diagram, ${o.nodes.length} elements`, spec, o.skin, titleSvg + boundarySvg + edges.join('') + nodes, o.tagQuery, 'uc');
|
|
285
|
+
return { svg, version: o.version, cacheControl: o.cacheControl };
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* `/draw/uc` 入口:GET ?n=…&e=…&b=…
|
|
289
|
+
*/
|
|
290
|
+
export function renderUcFromUrl(url, interactive = false) {
|
|
291
|
+
const params = url.searchParams;
|
|
292
|
+
const nRaw = params.getAll('n');
|
|
293
|
+
if (!nRaw.length) {
|
|
294
|
+
throw new ApiError(400, `at least one element is required. Example: /draw/uc?n=caller;actor;Your app&n=uc1;uc;Render a chart&e=caller;uc1`);
|
|
295
|
+
}
|
|
296
|
+
const nodes = parseNodes(nRaw);
|
|
297
|
+
const ids = new Set(nodes.map((n) => n.id));
|
|
298
|
+
if (ids.size !== nodes.length)
|
|
299
|
+
throw new ApiError(400, 'duplicate element id');
|
|
300
|
+
const edges = parseEdges(params.getAll('e'), ids);
|
|
301
|
+
const boundaries = parseBoundaries(params.getAll('b'), ids);
|
|
302
|
+
const v = parseInt(params.get('v') ?? '1', 10) || 1;
|
|
303
|
+
const side = (params.get('actors') ?? 'left').toLowerCase();
|
|
304
|
+
if (side !== 'left' && side !== 'right') {
|
|
305
|
+
throw new ApiError(400, `"actors" must be "left" (default) or "right". Got: ${JSON.stringify(side)}`);
|
|
306
|
+
}
|
|
307
|
+
const skin = resolveSkin('sankey', params.get('t'), schemeParam(params));
|
|
308
|
+
return renderUcCore({
|
|
309
|
+
nodes, edges, boundaries, tagQuery: url.search,
|
|
310
|
+
actorsRight: side === 'right',
|
|
311
|
+
title: params.get('title')?.trim() || undefined,
|
|
312
|
+
interactive,
|
|
313
|
+
version: v,
|
|
314
|
+
cacheControl: 'public, max-age=300',
|
|
315
|
+
skin,
|
|
316
|
+
minW: clampInt(params, 'w', 0, 200, 2400),
|
|
317
|
+
minH: clampInt(params, 'h', 0, 140, 2400),
|
|
318
|
+
});
|
|
319
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export interface FooterItem {
|
|
2
|
+
/** 小标题 */
|
|
3
|
+
label: string;
|
|
4
|
+
/** 值(跟在 label 后面) */
|
|
5
|
+
value?: string;
|
|
6
|
+
/** 值前面的色点 */
|
|
7
|
+
color?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface TipRow {
|
|
10
|
+
/** 主文案 */
|
|
11
|
+
text: string;
|
|
12
|
+
/** 右边的小字(时间、日期) */
|
|
13
|
+
sub?: string;
|
|
14
|
+
/** 前面的小圆点颜色 */
|
|
15
|
+
color?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 页头:标题那一行的右边,挂一句摘要(多少个日程、什么口径)。
|
|
19
|
+
*/
|
|
20
|
+
export declare function headerMeta(meta: string | undefined, x: number, y: number, width: number): string;
|
|
21
|
+
/**
|
|
22
|
+
* 页脚:一行小字。
|
|
23
|
+
* 左边若干项(中间用「·」隔开),右边一句备注(来源、口径)。
|
|
24
|
+
*/
|
|
25
|
+
export declare function footerUnit(items: FooterItem[], note: string | undefined, x: number, y: number, width: number): string;
|
|
26
|
+
/**
|
|
27
|
+
* 提示单元:主体里塞不下、但又不该丢掉的明细 —— 列成一块小面板。
|
|
28
|
+
* 两列排(条目多的时候不至于拉得太高)。
|
|
29
|
+
*/
|
|
30
|
+
export declare function tipUnit(rows: TipRow[], x: number, y: number, width: number, title?: string): string;
|
|
31
|
+
/** 提示单元要占多高(画之前先问一遍,好留位置) */
|
|
32
|
+
export declare function tipHeight(rows: number, width: number, hasTitle?: boolean): number;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// 显示单元:页头 / 提示 / 页脚。
|
|
2
|
+
//
|
|
3
|
+
// 一个图除了主体,还有几块「附属信息」:
|
|
4
|
+
// header —— 抬头(标题那一行;右边可以挂一句摘要)
|
|
5
|
+
// tip —— 提示(主体里装不下的明细,列出来 —— 网格太密时塞不下的日程)
|
|
6
|
+
// footer —— 页脚(补充信息:统计、口径、来源)
|
|
7
|
+
//
|
|
8
|
+
// 先在日历上做出来(那儿最需要),以后别的图种接上就行 —— 三个单元都与图种无关。
|
|
9
|
+
import { esc, r2, textW } from './svg.js';
|
|
10
|
+
/**
|
|
11
|
+
* 页头:标题那一行的右边,挂一句摘要(多少个日程、什么口径)。
|
|
12
|
+
*/
|
|
13
|
+
export function headerMeta(meta, x, y, width) {
|
|
14
|
+
if (!meta)
|
|
15
|
+
return '';
|
|
16
|
+
return `<text x="${r2(x + width)}" y="${r2(y)}" text-anchor="end" font-size="12" fill="var(--d-muted)">${esc(meta)}</text>`;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 页脚:一行小字。
|
|
20
|
+
* 左边若干项(中间用「·」隔开),右边一句备注(来源、口径)。
|
|
21
|
+
*/
|
|
22
|
+
export function footerUnit(items, note, x, y, width) {
|
|
23
|
+
if (!items.length && !note)
|
|
24
|
+
return '';
|
|
25
|
+
const parts = [];
|
|
26
|
+
let cx = x;
|
|
27
|
+
items.forEach((it, i) => {
|
|
28
|
+
if (i > 0) {
|
|
29
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(y)}" font-size="11" fill="var(--d-faint)">·</text>`);
|
|
30
|
+
cx += 9;
|
|
31
|
+
}
|
|
32
|
+
if (it.color) {
|
|
33
|
+
parts.push(`<circle cx="${r2(cx + 3)}" cy="${r2(y - 4)}" r="3" fill="${it.color}"/>`);
|
|
34
|
+
cx += 12;
|
|
35
|
+
}
|
|
36
|
+
const label = it.value ? `${it.label} ${it.value}` : it.label;
|
|
37
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(y)}" font-size="11" fill="var(--d-muted)">${esc(label)}</text>`);
|
|
38
|
+
cx += textW(label, 11);
|
|
39
|
+
});
|
|
40
|
+
if (note) {
|
|
41
|
+
parts.push(`<text x="${r2(x + width)}" y="${r2(y)}" text-anchor="end" font-size="11" fill="var(--d-faint)">${esc(note)}</text>`);
|
|
42
|
+
}
|
|
43
|
+
return `<g class="footer">${parts.join('')}</g>`;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 提示单元:主体里塞不下、但又不该丢掉的明细 —— 列成一块小面板。
|
|
47
|
+
* 两列排(条目多的时候不至于拉得太高)。
|
|
48
|
+
*/
|
|
49
|
+
export function tipUnit(rows, x, y, width, title) {
|
|
50
|
+
if (!rows.length)
|
|
51
|
+
return '';
|
|
52
|
+
const cols = width > 520 ? 2 : 1;
|
|
53
|
+
const perCol = Math.ceil(rows.length / cols);
|
|
54
|
+
const rowH = 15;
|
|
55
|
+
const titleH = title ? 20 : 0;
|
|
56
|
+
const h = titleH + perCol * rowH + 10;
|
|
57
|
+
const colW = width / cols;
|
|
58
|
+
const parts = [];
|
|
59
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(y)}" width="${r2(width)}" height="${r2(h)}" rx="8" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1" fill-opacity="0.9"/>`);
|
|
60
|
+
if (title) {
|
|
61
|
+
parts.push(`<text x="${r2(x + 10)}" y="${r2(y + 15)}" font-size="11" font-weight="600" fill="var(--d-muted)">${esc(title)}</text>`);
|
|
62
|
+
}
|
|
63
|
+
rows.forEach((r, i) => {
|
|
64
|
+
const col = Math.floor(i / perCol);
|
|
65
|
+
const row = i % perCol;
|
|
66
|
+
const rx = x + 10 + col * colW;
|
|
67
|
+
const ry = y + titleH + 6 + row * rowH;
|
|
68
|
+
if (r.color)
|
|
69
|
+
parts.push(`<circle cx="${r2(rx + 3)}" cy="${r2(ry - 3)}" r="3" fill="${r.color}"/>`);
|
|
70
|
+
parts.push(`<text x="${r2(rx + (r.color ? 11 : 0))}" y="${r2(ry)}" font-size="11" fill="var(--d-text)">${esc(r.text)}</text>`);
|
|
71
|
+
if (r.sub) {
|
|
72
|
+
parts.push(`<text x="${r2(rx + colW - 16)}" y="${r2(ry)}" text-anchor="end" font-size="11" fill="var(--d-faint)">${esc(r.sub)}</text>`);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return `<g class="tip">${parts.join('')}</g>`;
|
|
76
|
+
}
|
|
77
|
+
/** 提示单元要占多高(画之前先问一遍,好留位置) */
|
|
78
|
+
export function tipHeight(rows, width, hasTitle = true) {
|
|
79
|
+
if (!rows)
|
|
80
|
+
return 0;
|
|
81
|
+
const cols = width > 520 ? 2 : 1;
|
|
82
|
+
const perCol = Math.ceil(rows / cols);
|
|
83
|
+
return (hasTitle ? 20 : 0) + perCol * 15 + 10;
|
|
84
|
+
}
|