@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,30 @@
|
|
|
1
|
+
import type { TableSpec } from '../specs/table.js';
|
|
2
|
+
import { type RenderOpts } from './svg.js';
|
|
3
|
+
/** 一列。 */
|
|
4
|
+
export interface TableCol {
|
|
5
|
+
name: string;
|
|
6
|
+
align: 'left' | 'right';
|
|
7
|
+
/** 列宽(px) */
|
|
8
|
+
w: number;
|
|
9
|
+
}
|
|
10
|
+
/** 渲染用的网格。 */
|
|
11
|
+
export interface TableGrid {
|
|
12
|
+
title?: string;
|
|
13
|
+
cols: TableCol[];
|
|
14
|
+
rows: string[][];
|
|
15
|
+
/** 自然宽(px) */
|
|
16
|
+
width: number;
|
|
17
|
+
/** 自然高(px) */
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 规格 → 可渲染的网格。
|
|
22
|
+
*/
|
|
23
|
+
export declare function tableToGrid(spec: TableSpec): TableGrid;
|
|
24
|
+
/**
|
|
25
|
+
* 渲染表格。
|
|
26
|
+
*
|
|
27
|
+
* @param spec 规格(嵌进 <metadata> —— I5:输出里带着它渲染自的那份数据)
|
|
28
|
+
* @param o 渲染选项
|
|
29
|
+
*/
|
|
30
|
+
export declare function renderTable(spec: TableSpec, o: RenderOpts): string;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// 表格渲染器:扁平的 {cols, rows} → SVG。
|
|
2
|
+
//
|
|
3
|
+
// 排版用 draw 自己的那一套(svgDoc / textW / skin),所以同一页里表格与其它图
|
|
4
|
+
// 长得是一家人 —— 这是 x-cmd/draw#24 明确要求的:「表头和单元格的对齐要跟 draw
|
|
5
|
+
// 的其它 kind 用同一套排版办法,不然同一张表在两个地方会长得不一样」。
|
|
6
|
+
//
|
|
7
|
+
// 规格自包含(见 src/specs/table.ts):多维表格仓(x-cmd/table)是另一个服务,
|
|
8
|
+
// 两边共享的是 cols=/rows= 这个约定,不是代码。
|
|
9
|
+
import { ApiError } from '../specs/index.js';
|
|
10
|
+
import { esc, svgDoc, textW } from './svg.js';
|
|
11
|
+
/** 单元格左右内边距。 */
|
|
12
|
+
const PAD_X = 10;
|
|
13
|
+
/** 行高。 */
|
|
14
|
+
const ROW_H = 28;
|
|
15
|
+
/** 表头字号。 */
|
|
16
|
+
const HEAD_FS = 13;
|
|
17
|
+
/** 数据字号。 */
|
|
18
|
+
const BODY_FS = 14;
|
|
19
|
+
/** 画布外边距。 */
|
|
20
|
+
const PAD = 24;
|
|
21
|
+
/**
|
|
22
|
+
* 顶边那一条留给标题与 @X-CMD 角标(角标的基线固定在 38)。
|
|
23
|
+
* 表头从它下面开始 —— 不然窄表格的最后一列表头会被角标压住。
|
|
24
|
+
*/
|
|
25
|
+
const TOP_LINE_Y = 38;
|
|
26
|
+
const CONTENT_TOP = TOP_LINE_Y + 20;
|
|
27
|
+
/** 列宽的上下限。 */
|
|
28
|
+
const MIN_COL_W = 72;
|
|
29
|
+
const MAX_COL_W = 320;
|
|
30
|
+
/** 整列都是数字 → 右对齐(与 Airtable 一致)。 */
|
|
31
|
+
function columnNumeric(rows, index) {
|
|
32
|
+
let seen = false;
|
|
33
|
+
for (const row of rows) {
|
|
34
|
+
const v = row[index];
|
|
35
|
+
if (v === undefined || v === '')
|
|
36
|
+
continue;
|
|
37
|
+
if (!Number.isFinite(Number(v)))
|
|
38
|
+
return false;
|
|
39
|
+
seen = true;
|
|
40
|
+
}
|
|
41
|
+
return seen;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 规格 → 可渲染的网格。
|
|
45
|
+
*/
|
|
46
|
+
export function tableToGrid(spec) {
|
|
47
|
+
if (!spec.cols.length && !spec.rows.length) {
|
|
48
|
+
throw new ApiError(400, 'A table needs data. Try ?cols=Service,Errors&rows=api:12;worker:9');
|
|
49
|
+
}
|
|
50
|
+
const width = Math.max(spec.cols.length, ...spec.rows.map((r) => r.length), 0);
|
|
51
|
+
const names = [];
|
|
52
|
+
for (let i = 0; i < width; i++)
|
|
53
|
+
names.push(spec.cols[i] ?? String(i + 1));
|
|
54
|
+
const cols = names.map((name, i) => {
|
|
55
|
+
let w = textW(name, HEAD_FS);
|
|
56
|
+
for (const row of spec.rows) {
|
|
57
|
+
const cw = textW(row[i] ?? '', BODY_FS);
|
|
58
|
+
if (cw > w)
|
|
59
|
+
w = cw;
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
name,
|
|
63
|
+
align: columnNumeric(spec.rows, i) ? 'right' : 'left',
|
|
64
|
+
w: Math.min(MAX_COL_W, Math.max(MIN_COL_W, Math.ceil(w) + PAD_X * 2)),
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
const rows = spec.rows.map((r) => {
|
|
68
|
+
const out = [];
|
|
69
|
+
for (let i = 0; i < width; i++)
|
|
70
|
+
out.push(r[i] ?? '');
|
|
71
|
+
return out;
|
|
72
|
+
});
|
|
73
|
+
const grid = {
|
|
74
|
+
cols,
|
|
75
|
+
rows,
|
|
76
|
+
width: Math.ceil(cols.reduce((a, c) => a + c.w, 0) + PAD * 2),
|
|
77
|
+
height: Math.ceil(CONTENT_TOP + 34 + rows.length * ROW_H + PAD),
|
|
78
|
+
};
|
|
79
|
+
if (spec.title)
|
|
80
|
+
grid.title = spec.title;
|
|
81
|
+
return grid;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 渲染表格。
|
|
85
|
+
*
|
|
86
|
+
* @param spec 规格(嵌进 <metadata> —— I5:输出里带着它渲染自的那份数据)
|
|
87
|
+
* @param o 渲染选项
|
|
88
|
+
*/
|
|
89
|
+
export function renderTable(spec, o) {
|
|
90
|
+
const grid = tableToGrid(spec);
|
|
91
|
+
const width = o.width || grid.width;
|
|
92
|
+
const height = o.height || grid.height;
|
|
93
|
+
// 画布比自然宽度窄时,列宽等比压缩(文字放不下就溢出,宁可溢出也不要糊在一起)
|
|
94
|
+
const natural = grid.width - PAD * 2;
|
|
95
|
+
const scale = natural > 0 ? Math.min(1, (width - PAD * 2) / natural) : 1;
|
|
96
|
+
const widths = grid.cols.map((c) => c.w * scale);
|
|
97
|
+
const parts = [];
|
|
98
|
+
// 标题与角标共用顶边那一条(角标的基线固定在 38)
|
|
99
|
+
if (grid.title) {
|
|
100
|
+
parts.push(`<text x="${PAD}" y="${TOP_LINE_Y}" font-size="18" font-weight="500" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(grid.title)}</text>`);
|
|
101
|
+
}
|
|
102
|
+
// 表头
|
|
103
|
+
let y = CONTENT_TOP;
|
|
104
|
+
let x = PAD;
|
|
105
|
+
const headY = y + 22;
|
|
106
|
+
for (let i = 0; i < grid.cols.length; i++) {
|
|
107
|
+
const c = grid.cols[i];
|
|
108
|
+
const anchor = c.align === 'right' ? 'end' : 'start';
|
|
109
|
+
const tx = c.align === 'right' ? x + widths[i] - PAD_X : x + PAD_X;
|
|
110
|
+
parts.push(`<text x="${tx}" y="${headY}" font-size="${HEAD_FS}" font-weight="600" fill="var(--d-muted)" text-anchor="${anchor}">${esc(c.name)}</text>`);
|
|
111
|
+
x += widths[i];
|
|
112
|
+
}
|
|
113
|
+
parts.push(`<line x1="${PAD}" y1="${headY + 8}" x2="${width - PAD}" y2="${headY + 8}" stroke="var(--d-border)" stroke-width="1"/>`);
|
|
114
|
+
y = headY + 8;
|
|
115
|
+
// 数据行
|
|
116
|
+
for (let r = 0; r < grid.rows.length; r++) {
|
|
117
|
+
const row = grid.rows[r];
|
|
118
|
+
const rowY = y + r * ROW_H;
|
|
119
|
+
if (r % 2 === 1) {
|
|
120
|
+
parts.push(`<rect x="${PAD}" y="${rowY}" width="${width - PAD * 2}" height="${ROW_H}" fill="var(--d-grid)" opacity="0.35"/>`);
|
|
121
|
+
}
|
|
122
|
+
let cx = PAD;
|
|
123
|
+
for (let i = 0; i < grid.cols.length; i++) {
|
|
124
|
+
const c = grid.cols[i];
|
|
125
|
+
const anchor = c.align === 'right' ? 'end' : 'start';
|
|
126
|
+
const tx = c.align === 'right' ? cx + widths[i] - PAD_X : cx + PAD_X;
|
|
127
|
+
parts.push(`<text x="${tx}" y="${rowY + ROW_H / 2 + 5}" font-size="${BODY_FS}" fill="var(--d-ink)" text-anchor="${anchor}">${esc(row[i] ?? '')}</text>`);
|
|
128
|
+
cx += widths[i];
|
|
129
|
+
}
|
|
130
|
+
parts.push(`<line x1="${PAD}" y1="${rowY + ROW_H}" x2="${width - PAD}" y2="${rowY + ROW_H}" stroke="var(--d-grid)" stroke-width="1"/>`);
|
|
131
|
+
}
|
|
132
|
+
const aria = grid.title ? `${grid.title} — table` : 'table';
|
|
133
|
+
return svgDoc(width, height, 'table', o.version, aria, spec, o.skin, parts.join(''));
|
|
134
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { MultiSpec } from '../specs/index.js';
|
|
2
|
+
import { type DrawResult } from './query.js';
|
|
3
|
+
/**
|
|
4
|
+
* 解析 tokei 的 JSON,转成堆叠柱要用的多系列 spec。
|
|
5
|
+
*
|
|
6
|
+
* 接受 tokei 的完整输出(含每个文件的 reports / children)——这里只取每门语言的
|
|
7
|
+
* 三项合计;也接受裁剪过的 {"TypeScript":{"code":…,"comments":…,"blanks":…}}。
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseTokei(raw: string): MultiSpec;
|
|
10
|
+
/** tokei 的数据:三项计数 + 文件数(有的话) */
|
|
11
|
+
export interface TokeiData {
|
|
12
|
+
spec: MultiSpec;
|
|
13
|
+
/** 每种语言的文件数;输入里没有文件信息时是 null */
|
|
14
|
+
files: number[] | null;
|
|
15
|
+
}
|
|
16
|
+
/** 解析 tokei 的 JSON(整份或裁剪过的),顺带把文件数取出来 */
|
|
17
|
+
export declare function parseTokeiData(raw: string): TokeiData;
|
|
18
|
+
export declare function renderTokeiFromUrl(url: URL, body?: string): DrawResult;
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// /draw/tokei —— 代码统计图:把 tokei 的统计结果画成「代码 / 注释 / 空行」三段堆叠柱。
|
|
2
|
+
//
|
|
3
|
+
// 输入是 tokei --output json 的输出(整份或裁剪过的子集都行):
|
|
4
|
+
// {"TypeScript":{"code":1200,"comments":300,"blanks":150}, ...}
|
|
5
|
+
// 每种语言一根柱,三段自下而上:代码、注释、空行。
|
|
6
|
+
import { ApiError } from '../specs/index.js';
|
|
7
|
+
import { renderBarStacked } from './bar-stacked.js';
|
|
8
|
+
import { renderCombo } from './combo.js';
|
|
9
|
+
import { renderButterfly } from './butterfly.js';
|
|
10
|
+
import { resolveSkin } from '../styles/index.js';
|
|
11
|
+
import { clampInt, schemeParam } from './query.js';
|
|
12
|
+
const EXAMPLE = 'd={"TypeScript":{"code":1200,"comments":300,"blanks":150}}';
|
|
13
|
+
const total = (c) => c.code + c.comments + c.blanks;
|
|
14
|
+
const num = (v) => typeof v === 'number' && Number.isFinite(v) && v > 0 ? Math.round(v) : 0;
|
|
15
|
+
/**
|
|
16
|
+
* 解析 tokei 的 JSON,转成堆叠柱要用的多系列 spec。
|
|
17
|
+
*
|
|
18
|
+
* 接受 tokei 的完整输出(含每个文件的 reports / children)——这里只取每门语言的
|
|
19
|
+
* 三项合计;也接受裁剪过的 {"TypeScript":{"code":…,"comments":…,"blanks":…}}。
|
|
20
|
+
*/
|
|
21
|
+
export function parseTokei(raw) {
|
|
22
|
+
if (!raw || !raw.trim()) {
|
|
23
|
+
throw new ApiError(400, `Missing data. Use ?${EXAMPLE}`);
|
|
24
|
+
}
|
|
25
|
+
let obj;
|
|
26
|
+
try {
|
|
27
|
+
obj = JSON.parse(raw);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
throw new ApiError(400, `"d" is not valid JSON (remember to URL-encode it). Example: ?${EXAMPLE}`);
|
|
31
|
+
}
|
|
32
|
+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
|
|
33
|
+
throw new ApiError(400, `"d" must be an object of language → {code, comments, blanks}. Example: ?${EXAMPLE}`);
|
|
34
|
+
}
|
|
35
|
+
const rows = [];
|
|
36
|
+
for (const [name, v] of Object.entries(obj)) {
|
|
37
|
+
if (name === 'Total')
|
|
38
|
+
continue; // 合计行不画成一根柱
|
|
39
|
+
if (!v || typeof v !== 'object' || Array.isArray(v))
|
|
40
|
+
continue;
|
|
41
|
+
const r = v;
|
|
42
|
+
const c = { code: num(r.code), comments: num(r.comments), blanks: num(r.blanks), files: num(r.files) };
|
|
43
|
+
if (total(c) === 0)
|
|
44
|
+
continue;
|
|
45
|
+
rows.push([name, c]);
|
|
46
|
+
}
|
|
47
|
+
if (!rows.length) {
|
|
48
|
+
throw new ApiError(400, `No language with any code in "d". Example: ?${EXAMPLE}`);
|
|
49
|
+
}
|
|
50
|
+
// 总量大的在前 —— 柱状图从大到小读起来最顺
|
|
51
|
+
rows.sort((a, b) => total(b[1]) - total(a[1]));
|
|
52
|
+
const labels = rows.map(([name]) => name);
|
|
53
|
+
const values = (k) => rows.map(([, c]) => c[k]);
|
|
54
|
+
return {
|
|
55
|
+
labels,
|
|
56
|
+
series: [
|
|
57
|
+
{ name: 'code', values: values('code') },
|
|
58
|
+
{ name: 'comments', values: values('comments') },
|
|
59
|
+
{ name: 'blanks', values: values('blanks') },
|
|
60
|
+
],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* 一门语言的文件数:
|
|
65
|
+
* - 直接写了 files(裁剪过的 JSON 可以自己填)
|
|
66
|
+
* - tokei 整份输出里是 reports(每个文件一条),老版本是 children(一个对象)
|
|
67
|
+
*/
|
|
68
|
+
function fileCountOf(r) {
|
|
69
|
+
if (typeof r.files === 'number' && Number.isFinite(r.files))
|
|
70
|
+
return Math.max(0, Math.round(r.files));
|
|
71
|
+
const reports = r.reports;
|
|
72
|
+
if (Array.isArray(reports))
|
|
73
|
+
return reports.length;
|
|
74
|
+
const kids = r.children;
|
|
75
|
+
if (kids && typeof kids === 'object' && !Array.isArray(kids))
|
|
76
|
+
return Object.keys(kids).length;
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
79
|
+
/** 解析 tokei 的 JSON(整份或裁剪过的),顺带把文件数取出来 */
|
|
80
|
+
export function parseTokeiData(raw) {
|
|
81
|
+
const spec = parseTokei(raw);
|
|
82
|
+
let obj;
|
|
83
|
+
try {
|
|
84
|
+
obj = JSON.parse(raw);
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
return { spec, files: null };
|
|
88
|
+
}
|
|
89
|
+
const rows = [];
|
|
90
|
+
let any = false;
|
|
91
|
+
for (const [name, v] of Object.entries(obj)) {
|
|
92
|
+
if (name === 'Total' || !v || typeof v !== 'object' || Array.isArray(v))
|
|
93
|
+
continue;
|
|
94
|
+
const r = v;
|
|
95
|
+
const n = fileCountOf(r);
|
|
96
|
+
if (n > 0)
|
|
97
|
+
any = true;
|
|
98
|
+
rows.push([name, n]);
|
|
99
|
+
}
|
|
100
|
+
if (!any)
|
|
101
|
+
return { spec, files: null };
|
|
102
|
+
// 跟着柱的顺序排(柱按总量从大到小)
|
|
103
|
+
const byName = new Map(rows);
|
|
104
|
+
const files = spec.labels.map((l) => byName.get(l) ?? 0);
|
|
105
|
+
return { spec, files };
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* /draw/tokei 入口:GET ?d=<tokei json>,或 POST(body 就是 tokei 的 json)。
|
|
109
|
+
*/
|
|
110
|
+
/**
|
|
111
|
+
* property 形态:langs=TypeScript:4820:1180:940,JavaScript:1687:298:71
|
|
112
|
+
* 每项是 语言:代码:注释:空行,后两个可省。主推这个 —— 短,而且一眼能读。
|
|
113
|
+
*/
|
|
114
|
+
function parseLangs(raw) {
|
|
115
|
+
const out = {};
|
|
116
|
+
for (const item of raw.split(',')) {
|
|
117
|
+
const s = item.trim();
|
|
118
|
+
if (!s)
|
|
119
|
+
continue;
|
|
120
|
+
const f = s.split(':').map((x) => x.trim());
|
|
121
|
+
if (f.length < 2) {
|
|
122
|
+
throw new ApiError(400, `"langs" must be "lang:code[:comments[:blanks][:files]]" per language, comma separated. Example: langs=TypeScript:4820:1180:940:45,Shell:640:120:88:6`);
|
|
123
|
+
}
|
|
124
|
+
const name = f[0];
|
|
125
|
+
if (!name) {
|
|
126
|
+
throw new ApiError(400, `"langs": language name is missing. Example: langs=TypeScript:4820:1180:940`);
|
|
127
|
+
}
|
|
128
|
+
const nums = f.slice(1).map((x) => Number(x));
|
|
129
|
+
if (nums.some((n) => !Number.isFinite(n) || n < 0)) {
|
|
130
|
+
throw new ApiError(400, `"langs": counts must be numbers ≥ 0. Example: langs=TypeScript:4820:1180:940`);
|
|
131
|
+
}
|
|
132
|
+
out[name] = {
|
|
133
|
+
code: Math.round(nums[0]),
|
|
134
|
+
comments: Math.round(nums[1] ?? 0),
|
|
135
|
+
blanks: Math.round(nums[2] ?? 0),
|
|
136
|
+
files: Math.round(nums[3] ?? 0),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
if (!Object.keys(out).length) {
|
|
140
|
+
throw new ApiError(400, `"langs" is empty. Example: langs=TypeScript:4820:1180:940`);
|
|
141
|
+
}
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
export function renderTokeiFromUrl(url, body) {
|
|
145
|
+
const params = url.searchParams;
|
|
146
|
+
const langs = params.get('langs');
|
|
147
|
+
const raw = langs !== null && langs !== ''
|
|
148
|
+
? JSON.stringify(parseLangs(langs))
|
|
149
|
+
: (body ?? params.get('d') ?? '');
|
|
150
|
+
const { spec, files } = parseTokeiData(raw);
|
|
151
|
+
const title = params.get('title')?.trim();
|
|
152
|
+
const unit = params.get('unit')?.trim();
|
|
153
|
+
const layout = (params.get('layout') ?? 'bar').toLowerCase();
|
|
154
|
+
if (!['bar', 'butterfly', 'files', 'tb'].includes(layout)) {
|
|
155
|
+
throw new ApiError(400, `"layout" must be one of bar, butterfly, files, tb. Got: ${JSON.stringify(layout)}`);
|
|
156
|
+
}
|
|
157
|
+
const butterflyLayout = layout === 'butterfly' || layout === 'files' || layout === 'tb';
|
|
158
|
+
// 双翼的摆法用双翼自己的风格家族(t=track/grid/scale,缺省是干净的);
|
|
159
|
+
// 柱状用柱状家族。两边同一张图,t= 的意思也就一致了。
|
|
160
|
+
const skin = resolveSkin(butterflyLayout ? 'butterfly' : 'bar', params.get('t'), schemeParam(params));
|
|
161
|
+
const o = {
|
|
162
|
+
width: clampInt(params, 'w', 760, 200, 2400),
|
|
163
|
+
height: clampInt(params, 'h', 420, 140, 2000),
|
|
164
|
+
version: 1,
|
|
165
|
+
skin,
|
|
166
|
+
};
|
|
167
|
+
const withTitle = { ...spec, ...(title ? { title } : {}), ...(unit ? { unit } : {}) };
|
|
168
|
+
const svg = layout === 'butterfly' || layout === 'files' || layout === 'tb'
|
|
169
|
+
? renderTokeiButterfly(withTitle, files, layout, o)
|
|
170
|
+
: renderTokeiBar(withTitle, files, o);
|
|
171
|
+
return { svg, version: o.version, cacheControl: 'public, max-age=300' };
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 柱状:三项堆叠的柱,文件数有的话叠一条自己的标尺的折线
|
|
175
|
+
* (文件数和行数不是一个量级 —— 放同一根轴上会骗人)。
|
|
176
|
+
*/
|
|
177
|
+
function renderTokeiBar(spec, files, o) {
|
|
178
|
+
if (!files)
|
|
179
|
+
return renderBarStacked(spec, o);
|
|
180
|
+
const combo = {
|
|
181
|
+
labels: spec.labels,
|
|
182
|
+
stack: true,
|
|
183
|
+
series: [
|
|
184
|
+
...spec.series.map((s) => ({ ...s, kind: 'bar' })),
|
|
185
|
+
{ name: 'files', values: files, kind: 'line' },
|
|
186
|
+
],
|
|
187
|
+
};
|
|
188
|
+
return renderCombo(combo, { ...o, skin: resolveSkin('combo', null, 'light') });
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* 双翼(#55 的做法):数据往左右(或上下)展开。
|
|
192
|
+
* butterfly —— 左代码、右注释+空行(同一个量级,长度可比),文件数叠一条折线
|
|
193
|
+
* files —— 左文件数、右三项行数(两侧各一套标尺)
|
|
194
|
+
* tb —— butterfly 站起来
|
|
195
|
+
*/
|
|
196
|
+
function renderTokeiButterfly(spec, files, layout, o) {
|
|
197
|
+
const [code, comments, blanks] = spec.series;
|
|
198
|
+
const tb = layout === 'tb';
|
|
199
|
+
const bfly = {
|
|
200
|
+
labels: spec.labels,
|
|
201
|
+
dir: tb ? 'tb' : 'lr',
|
|
202
|
+
...(spec.title ? { title: spec.title } : {}),
|
|
203
|
+
};
|
|
204
|
+
if (layout === 'files') {
|
|
205
|
+
// 左:文件数;右:三项行数(两侧各一套标尺 —— 单位不一样)
|
|
206
|
+
bfly.scale = 'each';
|
|
207
|
+
bfly.left = { name: 'files', values: files ?? spec.labels.map(() => 0) };
|
|
208
|
+
bfly.right = {
|
|
209
|
+
segments: [
|
|
210
|
+
{ name: 'code', values: code.values },
|
|
211
|
+
{ name: 'comments', values: comments.values },
|
|
212
|
+
{ name: 'blanks', values: blanks.values },
|
|
213
|
+
],
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
// 左:代码;右:注释+空行(同一个量级,共用标尺,长度可比)
|
|
218
|
+
bfly.left = { name: 'code', values: code.values };
|
|
219
|
+
bfly.right = {
|
|
220
|
+
segments: [
|
|
221
|
+
{ name: 'comments', values: comments.values },
|
|
222
|
+
{ name: 'blanks', values: blanks.values },
|
|
223
|
+
],
|
|
224
|
+
};
|
|
225
|
+
// 文件数叠在左侧(自己的标尺)
|
|
226
|
+
if (files)
|
|
227
|
+
bfly.line = { name: 'files', values: files, side: 'left' };
|
|
228
|
+
}
|
|
229
|
+
return renderButterfly(bfly, o);
|
|
230
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ArchBoundary } from './archmap.js';
|
|
2
|
+
import { resolveSkin } from '../styles/index.js';
|
|
3
|
+
import { type DrawResult } from './query.js';
|
|
4
|
+
interface UcNode {
|
|
5
|
+
id: string;
|
|
6
|
+
kind: 'actor' | 'uc';
|
|
7
|
+
label: string;
|
|
8
|
+
sub?: string;
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
w: number;
|
|
12
|
+
h: number;
|
|
13
|
+
/** 位置:和架构图同一套(不写就自己排) */
|
|
14
|
+
grid?: {
|
|
15
|
+
row: number;
|
|
16
|
+
col: number;
|
|
17
|
+
w: number;
|
|
18
|
+
h: number;
|
|
19
|
+
};
|
|
20
|
+
px?: {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
w?: number;
|
|
24
|
+
h?: number;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
interface UcEdge {
|
|
28
|
+
from: string;
|
|
29
|
+
to: string;
|
|
30
|
+
label?: string;
|
|
31
|
+
dep: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface UcOpts {
|
|
34
|
+
nodes: UcNode[];
|
|
35
|
+
edges: UcEdge[];
|
|
36
|
+
/** b= 画出来的边界(可以有多层) */
|
|
37
|
+
boundaries: ArchBoundary[];
|
|
38
|
+
/** 原始 URL 查询串:角标直链 explore 落地页用 */
|
|
39
|
+
tagQuery?: string;
|
|
40
|
+
/** 参与方摆在右边(默认在左边) */
|
|
41
|
+
actorsRight?: boolean;
|
|
42
|
+
title?: string;
|
|
43
|
+
interactive: boolean;
|
|
44
|
+
version: number;
|
|
45
|
+
cacheControl: string;
|
|
46
|
+
skin: ReturnType<typeof resolveSkin>;
|
|
47
|
+
minW?: number;
|
|
48
|
+
minH?: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 渲染用例图。
|
|
52
|
+
*/
|
|
53
|
+
export declare function renderUcCore(o: UcOpts): DrawResult;
|
|
54
|
+
/**
|
|
55
|
+
* `/draw/uc` 入口:GET ?n=…&e=…&b=…
|
|
56
|
+
*/
|
|
57
|
+
export declare function renderUcFromUrl(url: URL, interactive?: boolean): DrawResult;
|
|
58
|
+
export {};
|