@x-cmd/draw 0.5.3 → 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/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
|
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",
|