@x-cmd/draw 0.5.2 → 0.5.4
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/dist/draw-client.iife.js +22 -22
- package/dist/render/calendar.js +28 -11
- package/dist/render/source-theme.d.ts +3 -1
- package/dist/render/source-theme.js +4 -2
- package/dist/render/wavedrom.js +143 -6
- package/package.json +1 -1
package/dist/render/calendar.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { dayIso, daysInMonth, firstWeekday, isoDay, isoParts, isoWeekday } from '../specs/calendar.js';
|
|
2
2
|
import { TAB, card } from '../theme.js';
|
|
3
|
-
import { esc, r2, svgDoc, textW, titleBlock, topLegend, trunc } from './svg.js';
|
|
3
|
+
import { esc, r2, svgDoc, TAG_RESERVE, textW, titleBlock, topLegend, trunc } from './svg.js';
|
|
4
4
|
import { footerUnit, headerMeta, tipHeight, tipUnit } from './units.js';
|
|
5
5
|
const PAD = 24;
|
|
6
6
|
const HEAD_FS = 12;
|
|
@@ -48,12 +48,17 @@ function fitText(s, maxW, fs) {
|
|
|
48
48
|
t = t.slice(0, -1);
|
|
49
49
|
return `<title>${esc(s)}</title>${esc(t)}…`;
|
|
50
50
|
}
|
|
51
|
-
/**
|
|
51
|
+
/**
|
|
52
|
+
* 按像素宽度折行(最多 maxLines 行)—— 挤的格子里文字不能串到隔壁格,也不能截掉一半。
|
|
53
|
+
*
|
|
54
|
+
* 返回的是**可以直接塞进 SVG 的片段**(已转义;放不下的那一行带 `<title>`)——
|
|
55
|
+
* 调用方不要再转义一次,不然 `<title>` 会变成字面量。
|
|
56
|
+
*/
|
|
52
57
|
function wrapLines(s, maxW, fs, maxLines = 2) {
|
|
53
58
|
if (!s)
|
|
54
59
|
return [''];
|
|
55
60
|
if (textW(s, fs) <= maxW)
|
|
56
|
-
return [s];
|
|
61
|
+
return [esc(s)];
|
|
57
62
|
const words = s.split(/\s+/).filter(Boolean);
|
|
58
63
|
const lines = [];
|
|
59
64
|
let line = '';
|
|
@@ -71,7 +76,7 @@ function wrapLines(s, maxW, fs, maxLines = 2) {
|
|
|
71
76
|
if (line && lines.length < maxLines)
|
|
72
77
|
lines.push(line);
|
|
73
78
|
// 还是太长 ⇒ 最后一行再截(但带上 title,悬停能看全)
|
|
74
|
-
return lines.map((l) => (textW(l, fs) > maxW ? fitText(l, maxW, fs) : l));
|
|
79
|
+
return lines.map((l) => (textW(l, fs) > maxW ? fitText(l, maxW, fs) : esc(l)));
|
|
75
80
|
}
|
|
76
81
|
/** 某一天是休息日吗(默认:周末) */
|
|
77
82
|
function isRest(ctx, date) {
|
|
@@ -155,9 +160,10 @@ function monthGrid(ctx, y, m, x, y0, w, h, dense = false, headY) {
|
|
|
155
160
|
}
|
|
156
161
|
const fill = today ? '#ffffff' : holiday ? 'var(--d-mark)' : rest ? 'var(--d-muted)' : 'var(--d-ink)';
|
|
157
162
|
parts.push(`<text x="${r2(nx)}" y="${r2(ny)}" text-anchor="end" font-size="${NUM_FS}" font-weight="${today || holiday ? 600 : 400}" fill="${fill}" style="${TAB}">${dayNum}</text>`);
|
|
158
|
-
// 假日的名字
|
|
163
|
+
// 假日的名字(密集的格子放不下 ⇒ 不写:半截名字比没有更糟,
|
|
164
|
+
// 假日在**日期号**上已经是红的,名字在下面的备注里)
|
|
159
165
|
const dl = ctx.dayOf(iso)?.label;
|
|
160
|
-
if (dl) {
|
|
166
|
+
if (dl && !dense) {
|
|
161
167
|
const dfill = holiday ? 'var(--d-mark)' : 'var(--d-text)';
|
|
162
168
|
parts.push(`<text x="${r2(cx + 6)}" y="${r2(cy + 16)}" font-size="${EV_FS}" fill="${dfill}">${fitText(dl, cw - 30, EV_FS)}</text>`);
|
|
163
169
|
}
|
|
@@ -182,7 +188,7 @@ function monthGrid(ctx, y, m, x, y0, w, h, dense = false, headY) {
|
|
|
182
188
|
parts.push(`<circle cx="${r2(cx + 9)}" cy="${r2(ey - 4)}" r="3" fill="${ctx.colorOf(it.kind)}"/>`);
|
|
183
189
|
// 月历不写时间(苹果的月视图也不写)—— 格子窄,时间会把名字挤没
|
|
184
190
|
lines.forEach((ln, li) => {
|
|
185
|
-
parts.push(`<text x="${r2(cx + 16)}" y="${r2(ey + li * 13)}" font-size="${EV_FS}" fill="var(--d-text)">${
|
|
191
|
+
parts.push(`<text x="${r2(cx + 16)}" y="${r2(ey + li * 13)}" font-size="${EV_FS}" fill="var(--d-text)">${ln}</text>`);
|
|
186
192
|
});
|
|
187
193
|
ey += lines.length * 13 + 2;
|
|
188
194
|
}
|
|
@@ -262,7 +268,13 @@ function timeGrid(ctx, days, periods, x, y0, w, h, dense = false) {
|
|
|
262
268
|
const by = y0 + r * ch + 3;
|
|
263
269
|
const bw = cw - 6;
|
|
264
270
|
const bh = Math.max(16, ch - 6);
|
|
265
|
-
parts.push(`<rect x="${r2(bx)}" y="${r2(by)}" width="${r2(bw)}" height="${r2(bh)}" rx="6" fill="${ctx.colorOf(it.kind)}" fill-opacity="0.14"/>`, `<rect x="${r2(bx)}" y="${r2(by)}" width="3" height="${r2(bh)}" rx="1.5" fill="${ctx.colorOf(it.kind)}"
|
|
271
|
+
parts.push(`<rect x="${r2(bx)}" y="${r2(by)}" width="${r2(bw)}" height="${r2(bh)}" rx="6" fill="${ctx.colorOf(it.kind)}" fill-opacity="0.14"/>`, `<rect x="${r2(bx)}" y="${r2(by)}" width="3" height="${r2(bh)}" rx="1.5" fill="${ctx.colorOf(it.kind)}"/>`);
|
|
272
|
+
// 名字**折行**而不是截断(截一半等于给错信息):格子放得下几行就折几行
|
|
273
|
+
const room = bw - 14;
|
|
274
|
+
const maxLines = Math.max(1, Math.min(2, Math.floor((bh - 8) / 13)));
|
|
275
|
+
wrapLines(it.label, room, EV_FS, maxLines).forEach((ln, li) => {
|
|
276
|
+
parts.push(`<text x="${r2(bx + 8)}" y="${r2(by + 14 + li * 13)}" font-size="${EV_FS}" fill="var(--d-ink)">${ln}</text>`);
|
|
277
|
+
});
|
|
266
278
|
}
|
|
267
279
|
}
|
|
268
280
|
return parts.join('');
|
|
@@ -288,7 +300,12 @@ function planBoard(ctx, start, days, x, y0, w, rowH) {
|
|
|
288
300
|
rows.forEach((it, r) => {
|
|
289
301
|
const ry = y0 + r * rowH;
|
|
290
302
|
parts.push(`<line x1="${r2(x)}" y1="${r2(ry)}" x2="${r2(x + w)}" y2="${r2(ry)}" stroke="var(--d-grid)" stroke-width="1"/>`);
|
|
291
|
-
|
|
303
|
+
// 名字折行(行高够就折两行;截一半等于给错信息)
|
|
304
|
+
const lines = wrapLines(it.label, labelW - 10, 13, Math.max(1, Math.min(2, Math.floor(rowH / 16))));
|
|
305
|
+
const ty = ry + rowH / 2 - (lines.length - 1) * 7 + 4;
|
|
306
|
+
lines.forEach((ln, li) => {
|
|
307
|
+
parts.push(`<text x="${r2(x + 4)}" y="${r2(ty + li * 15)}" font-size="13" fill="var(--d-ink)">${ln}</text>`);
|
|
308
|
+
});
|
|
292
309
|
const s = isoDay(it.date);
|
|
293
310
|
const e = isoDay(it.until ?? it.date);
|
|
294
311
|
const bx = x + labelW + (s - d0) * per;
|
|
@@ -430,8 +447,8 @@ export function renderCalendar(spec, o) {
|
|
|
430
447
|
const nDays = spec.days.length;
|
|
431
448
|
const nNotes = spec.notes.length;
|
|
432
449
|
const meta = [nItems ? `${nItems} 个日程` : '', nNotes ? `${nNotes} 条备注` : ''].filter(Boolean).join(' · ');
|
|
433
|
-
// 右边让开 Edit @X-CMD 角标(
|
|
434
|
-
const badgeRoom = Math.min(
|
|
450
|
+
// 右边让开 Edit @X-CMD 角标(它占右上角那一块 —— 标题 18px 时角标有 139px 宽)
|
|
451
|
+
const badgeRoom = Math.min(TAG_RESERVE, bodyW * 0.45);
|
|
435
452
|
const titleSvg = titleBlock(title) + headerMeta(meta, PAD, 38, bodyW - badgeRoom);
|
|
436
453
|
// 图例:出现过的类别(坐底部一行 —— 类别多的时候放标题那行会压住标题)
|
|
437
454
|
const legendSvg = legendItems.length
|
|
@@ -26,8 +26,10 @@ export declare const SERIF_STACK = "'Times New Roman',Times,Georgia,serif";
|
|
|
26
26
|
* 垫一层底色 —— 图是独立的一张图,得有底,不然在白底页面上看不见深色的线。
|
|
27
27
|
*
|
|
28
28
|
* 底色垫在**最下面**:插到 <svg> 开头(第一个子元素之前)。
|
|
29
|
+
*
|
|
30
|
+
* @param y 画布左上角的 y(viewBox 的原点不是 0 时,底要从那儿开始铺)
|
|
29
31
|
*/
|
|
30
|
-
export declare function withBackground(svg: string, theme: SvgTheme, w: number, h: number): string;
|
|
32
|
+
export declare function withBackground(svg: string, theme: SvgTheme, w: number, h: number, y?: number): string;
|
|
31
33
|
/**
|
|
32
34
|
* 库里写死的颜色换成我们这套。
|
|
33
35
|
*
|
|
@@ -36,9 +36,11 @@ const r2 = (v) => Math.round(v * 100) / 100;
|
|
|
36
36
|
* 垫一层底色 —— 图是独立的一张图,得有底,不然在白底页面上看不见深色的线。
|
|
37
37
|
*
|
|
38
38
|
* 底色垫在**最下面**:插到 <svg> 开头(第一个子元素之前)。
|
|
39
|
+
*
|
|
40
|
+
* @param y 画布左上角的 y(viewBox 的原点不是 0 时,底要从那儿开始铺)
|
|
39
41
|
*/
|
|
40
|
-
export function withBackground(svg, theme, w, h) {
|
|
41
|
-
const rect = `<rect x="0" y="
|
|
42
|
+
export function withBackground(svg, theme, w, h, y = 0) {
|
|
43
|
+
const rect = `<rect x="0" y="${r2(y)}" width="${r2(w)}" height="${r2(h)}" fill="${theme.bg}"/>`;
|
|
42
44
|
const at = svg.indexOf('>');
|
|
43
45
|
if (at < 0)
|
|
44
46
|
return svg;
|
package/dist/render/wavedrom.js
CHANGED
|
@@ -74,10 +74,10 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
74
74
|
// 结构色换掉(波形的数据色不动):#000 是笔画/文字,#fff 是波形块的底
|
|
75
75
|
out = out.replace(/<style([^>]*)>([\s\S]*?)<\/style>/g, (_m, attrs, css) => {
|
|
76
76
|
let themed = css
|
|
77
|
-
.replace(/stroke
|
|
78
|
-
.replace(/color
|
|
79
|
-
.replace(/fill
|
|
80
|
-
.replace(/fill
|
|
77
|
+
.replace(/stroke:\s*#000\b/g, `stroke:${theme.ink}`)
|
|
78
|
+
.replace(/color:\s*#000\b/g, `color:${theme.ink}`)
|
|
79
|
+
.replace(/fill:\s*#000\b/g, `fill:${theme.ink}`)
|
|
80
|
+
.replace(/fill:\s*#fff;/g, `fill:${theme.panel};`);
|
|
81
81
|
// 文字没写 fill(默认黑)—— 给一条
|
|
82
82
|
themed = themed.replace(/text\{/, `text{fill:${theme.ink};`);
|
|
83
83
|
// 皮肤那个深蓝(信号名用它)在暗色底上太沉 —— 换成站上的强调色
|
|
@@ -88,6 +88,18 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
88
88
|
// wavedrom 的画法全写在 CSS 类里(.s1/.s2/…)—— 样式块会被出图那道工序剥掉,
|
|
89
89
|
// 所以先内联到元素上:图就自足了(没有 CSS),浏览器和 PNG 一个样。
|
|
90
90
|
out = inlineCss(out);
|
|
91
|
+
// 库还有一批评色**不写在样式表里**,是写死在属性/内联样式里的(#188):
|
|
92
|
+
// - 分段模拟波的折线:style="fill:none;stroke:#000;…"(render-piece-wise.js 里写死)
|
|
93
|
+
// - 位域图的格线:<g stroke="black" …>
|
|
94
|
+
// 这些是**结构色**,得跟主题走 —— 不然暗底上是纯黑,等于没画。
|
|
95
|
+
out = out
|
|
96
|
+
.replace(/stroke:\s*#000\b/gi, `stroke:${theme.ink}`)
|
|
97
|
+
.replace(/\bstroke="black"/gi, `stroke="${theme.line}"`)
|
|
98
|
+
.replace(/\bfill="black"/gi, `fill="${theme.ink}"`)
|
|
99
|
+
.replace(/\bcolor="black"/gi, `color="${theme.ink}"`);
|
|
100
|
+
// 没写填色的文字(位域图的位号/字段名):库靠样式表里的 text{} 填黑 ——
|
|
101
|
+
// 服务端没有那张样式表(样式表是浏览器那边注入的)⇒ 字是 SVG 默认的黑 ⇒ 暗底上等于没有。
|
|
102
|
+
out = out.replace(/<text\b([^>]*)>/g, (m, attrs) => /\bfill\s*[:=]/.test(attrs) ? m : `<text${attrs} fill="${theme.ink}">`);
|
|
91
103
|
// 数据块(head/body/tail)的颜色:皮肤的一个颜色类会**同时**用在
|
|
92
104
|
// 「波形的 0 块」和「数据块」上(.s7 是白底)—— 波形 0 块在暗色底上要换成卡片色,
|
|
93
105
|
// 但数据块得留住自己的颜色(官方演示里 head/body/tail 是彩色的)。
|
|
@@ -105,9 +117,134 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
105
117
|
if (scheme === 'dark')
|
|
106
118
|
out = out.replace(/#0041c4/g, theme.accent);
|
|
107
119
|
out = withFont(out);
|
|
108
|
-
|
|
109
|
-
|
|
120
|
+
// 分段模拟波(pw):画布要撑到能装下整条路径 —— 见 fitPieceWise()
|
|
121
|
+
const fitted = fitPieceWise(out, size);
|
|
122
|
+
out = fitted.svg;
|
|
123
|
+
out = withBackground(out, theme, fitted.w, fitted.h);
|
|
124
|
+
out = setSize(out, fitted.w, fitted.h);
|
|
110
125
|
out = ensureSvgNs(out);
|
|
111
126
|
out = ensureViewBox(out);
|
|
127
|
+
// viewBox 跟着画布走(内容整体往下挪了 dy ⇒ 画布要长出那一截)
|
|
128
|
+
out = out.replace(/viewBox="[^"]*"/, `viewBox="0 0 ${fitted.w} ${fitted.h}"`);
|
|
129
|
+
if (fitted.dy)
|
|
130
|
+
out = shiftContent(out, fitted.dy);
|
|
112
131
|
return out;
|
|
113
132
|
}
|
|
133
|
+
/** 每条路径命令吃几个数 */
|
|
134
|
+
const CMD_ARGS = { m: 2, l: 2, t: 2, h: 1, v: 1, q: 4, c: 6, s: 4, a: 7 };
|
|
135
|
+
/**
|
|
136
|
+
* 路径的坐标范围(相对它的父组)。
|
|
137
|
+
*
|
|
138
|
+
* 只求「画到哪里」,不求画得长什么样 ⇒ 曲线拿控制点当极值(略大,宁多勿少)。
|
|
139
|
+
*/
|
|
140
|
+
function pathExtent(d) {
|
|
141
|
+
const toks = d.match(/[a-zA-Z]|-?\d*\.?\d+(?:e[-+]?\d+)?/g) ?? [];
|
|
142
|
+
let x = 0;
|
|
143
|
+
let y = 0;
|
|
144
|
+
let x0 = 0;
|
|
145
|
+
let y0 = 0;
|
|
146
|
+
let x1 = 0;
|
|
147
|
+
let y1 = 0;
|
|
148
|
+
let seen = false;
|
|
149
|
+
const grow = (px, py) => {
|
|
150
|
+
if (!seen) {
|
|
151
|
+
x0 = x1 = px;
|
|
152
|
+
y0 = y1 = py;
|
|
153
|
+
seen = true;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
x0 = Math.min(x0, px);
|
|
157
|
+
x1 = Math.max(x1, px);
|
|
158
|
+
y0 = Math.min(y0, py);
|
|
159
|
+
y1 = Math.max(y1, py);
|
|
160
|
+
};
|
|
161
|
+
let cmd = '';
|
|
162
|
+
let i = 0;
|
|
163
|
+
while (i < toks.length) {
|
|
164
|
+
if (/[a-zA-Z]/.test(toks[i])) {
|
|
165
|
+
cmd = toks[i].toLowerCase();
|
|
166
|
+
i++;
|
|
167
|
+
}
|
|
168
|
+
const n = CMD_ARGS[cmd];
|
|
169
|
+
if (n === undefined)
|
|
170
|
+
break;
|
|
171
|
+
const args = toks.slice(i, i + n).map(Number);
|
|
172
|
+
if (args.length < n || args.some(Number.isNaN))
|
|
173
|
+
break;
|
|
174
|
+
i += n;
|
|
175
|
+
if (cmd === 'm') {
|
|
176
|
+
x = args[0];
|
|
177
|
+
y = args[1];
|
|
178
|
+
grow(x, y);
|
|
179
|
+
}
|
|
180
|
+
else if (cmd === 'l' || cmd === 't') {
|
|
181
|
+
x += args[0];
|
|
182
|
+
y += args[1];
|
|
183
|
+
grow(x, y);
|
|
184
|
+
}
|
|
185
|
+
else if (cmd === 'h') {
|
|
186
|
+
x += args[0];
|
|
187
|
+
grow(x, y);
|
|
188
|
+
}
|
|
189
|
+
else if (cmd === 'v') {
|
|
190
|
+
y += args[0];
|
|
191
|
+
grow(x, y);
|
|
192
|
+
}
|
|
193
|
+
else if (cmd === 'q' || cmd === 's') {
|
|
194
|
+
grow(x + args[0], y + args[1]);
|
|
195
|
+
x += args[2];
|
|
196
|
+
y += args[3];
|
|
197
|
+
grow(x, y);
|
|
198
|
+
}
|
|
199
|
+
else if (cmd === 'c') {
|
|
200
|
+
grow(x + args[0], y + args[1]);
|
|
201
|
+
grow(x + args[2], y + args[3]);
|
|
202
|
+
x += args[4];
|
|
203
|
+
y += args[5];
|
|
204
|
+
grow(x, y);
|
|
205
|
+
}
|
|
206
|
+
else if (cmd === 'a') {
|
|
207
|
+
x += args[5];
|
|
208
|
+
y += args[6];
|
|
209
|
+
grow(x, y);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return seen ? { x0, y0, x1, y1 } : null;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 分段模拟波(pw):把画布撑到能装下整条路径。
|
|
216
|
+
*
|
|
217
|
+
* 库把 d 路径按「两格宽、一格高」缩放后画在 lane 的起点,但 lane 的宽度是按
|
|
218
|
+
* wave 的**字符个数**算的 —— pw 没有字符 ⇒ lane 只有一格宽 ⇒ 路径大半落在
|
|
219
|
+
* 画布外(库自己的短处:render-piece-wise.js 只管画,不管留位)。
|
|
220
|
+
*
|
|
221
|
+
* 我们不改库的算法,只把画布撑开:路径画到哪,画布就到哪。
|
|
222
|
+
*/
|
|
223
|
+
function fitPieceWise(svg, size) {
|
|
224
|
+
// pw 的路径长这样:<g transform="translate(0,Y)"><path style="fill:none;stroke:…;stroke-width:1px;" d="…"/>
|
|
225
|
+
const m = /<g transform="translate\(0,(-?[\d.]+)\)">\s*<path[^>]*?stroke-width:1px[^>]*?d="([^"]+)"/.exec(svg);
|
|
226
|
+
if (!m)
|
|
227
|
+
return { svg, w: size.w, h: size.h, dy: 0 };
|
|
228
|
+
const ext = pathExtent(m[2]);
|
|
229
|
+
if (!ext)
|
|
230
|
+
return { svg, w: size.w, h: size.h, dy: 0 };
|
|
231
|
+
// lane 组自己的位移(标签列占多宽,全看它)
|
|
232
|
+
const lane = /<g transform="translate\((-?[\d.]+),(-?[\d.]+)\)" id="lanes/.exec(svg);
|
|
233
|
+
const lx = lane ? Number(lane[1]) : 0;
|
|
234
|
+
const ly = lane ? Number(lane[2]) : 0;
|
|
235
|
+
const gy = Number(m[1]);
|
|
236
|
+
const ax1 = lx + ext.x1;
|
|
237
|
+
const ay0 = ly + gy + ext.y0;
|
|
238
|
+
const ay1 = ly + gy + ext.y1;
|
|
239
|
+
// 路径可能跑到 y=0 之上 ⇒ 内容整体往下挪 dy,画布跟着长出那一截
|
|
240
|
+
const dy = Math.max(0, -ay0);
|
|
241
|
+
const w = Math.max(size.w, Math.ceil(ax1 + 6));
|
|
242
|
+
const h = Math.max(size.h, Math.ceil(ay1 + 4 + dy));
|
|
243
|
+
return { svg, w, h, dy };
|
|
244
|
+
}
|
|
245
|
+
/** 画布往上挪了 dy ⇒ 内容整体往下挪,不然上面那截看不见 */
|
|
246
|
+
function shiftContent(svg, dy) {
|
|
247
|
+
return svg
|
|
248
|
+
.replace(/<g id="(waves_[^"]*)">/, `<g transform="translate(0,${dy})" id="$1">`)
|
|
249
|
+
.replace(/<g id="(groups_[^"]*)">/, `<g transform="translate(0,${dy})" id="$1">`);
|
|
250
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.4",
|
|
4
4
|
"description": "SVG chart rendering as a service, a library, and a CLI — pie/bar/hbar/line/area/radar/gauge/scatter/combo/sankey/qr, same params everywhere",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|