@fengwk/kchart 0.0.0-stage → 0.1.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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE.md +11 -0
- package/README.md +68 -2
- package/README.zh-CN.md +69 -0
- package/dist/LICENSE +21 -0
- package/dist/NOTICE.md +11 -0
- package/dist/assets/ICONS-LICENSE.txt +53 -0
- package/dist/assets/maps/LICENSE.md +75 -0
- package/dist/assets/maps/METADATA.md +9 -0
- package/dist/assets/maps/REGIONS.md +28 -0
- package/dist/assets/maps/us-states.geojson +1 -0
- package/dist/assets/maps/world.geojson +1 -0
- package/dist/esm/_virtual/_kchart-families.js +48 -0
- package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
- package/dist/esm/assets/icons.js +11 -0
- package/dist/esm/assets/maps/us-states.js +3 -0
- package/dist/esm/assets/maps/world.js +3 -0
- package/dist/esm/assets/themes/claude.js +69 -0
- package/dist/esm/assets/themes/cloudmusic.js +62 -0
- package/dist/esm/assets/themes/kanagawa.js +67 -0
- package/dist/esm/assets/themes/obsidian.js +67 -0
- package/dist/esm/assets/themes/paper.js +61 -0
- package/dist/esm/assets/themes/terminal.js +60 -0
- package/dist/esm/assets/themes/tokyo.js +62 -0
- package/dist/esm/chart/basic.js +1313 -0
- package/dist/esm/chart/catalog.js +476 -0
- package/dist/esm/chart/composition.js +844 -0
- package/dist/esm/chart/context.js +189 -0
- package/dist/esm/chart/distribution.js +442 -0
- package/dist/esm/chart/fields.js +67 -0
- package/dist/esm/chart/maps.js +563 -0
- package/dist/esm/chart/mindmap.js +629 -0
- package/dist/esm/chart/multivariate.js +1229 -0
- package/dist/esm/chart/registry.js +108 -0
- package/dist/esm/chart/relations.js +949 -0
- package/dist/esm/chart/spec.js +1415 -0
- package/dist/esm/chart/stat-compare.js +302 -0
- package/dist/esm/chart/stat-trend.js +181 -0
- package/dist/esm/chart/statistics.js +1007 -0
- package/dist/esm/chart/unit-variants.js +645 -0
- package/dist/esm/chart/variants.js +552 -0
- package/dist/esm/core/color.js +197 -0
- package/dist/esm/core/context.js +256 -0
- package/dist/esm/core/diagnostics.js +23 -0
- package/dist/esm/core/format.js +167 -0
- package/dist/esm/core/geometry.js +130 -0
- package/dist/esm/core/icon-data.js +5 -0
- package/dist/esm/core/icons.js +173 -0
- package/dist/esm/core/scale.js +112 -0
- package/dist/esm/core/scene.js +234 -0
- package/dist/esm/core/svg.js +89 -0
- package/dist/esm/core/text.js +184 -0
- package/dist/esm/core/theme.js +318 -0
- package/dist/esm/diagram/agentflow.js +596 -0
- package/dist/esm/diagram/architecture.js +729 -0
- package/dist/esm/diagram/block.js +988 -0
- package/dist/esm/diagram/blocks.js +687 -0
- package/dist/esm/diagram/c4.js +1002 -0
- package/dist/esm/diagram/class.js +1158 -0
- package/dist/esm/diagram/cynefin.js +749 -0
- package/dist/esm/diagram/emphasis.js +7 -0
- package/dist/esm/diagram/er.js +657 -0
- package/dist/esm/diagram/eventmodeling.js +948 -0
- package/dist/esm/diagram/gantt-date.js +323 -0
- package/dist/esm/diagram/gantt.js +661 -0
- package/dist/esm/diagram/gitgraph.js +472 -0
- package/dist/esm/diagram/graph-layout.js +4046 -0
- package/dist/esm/diagram/graph-parse.js +1366 -0
- package/dist/esm/diagram/graph-route.js +490 -0
- package/dist/esm/diagram/graph.js +553 -0
- package/dist/esm/diagram/ishikawa.js +496 -0
- package/dist/esm/diagram/journey.js +512 -0
- package/dist/esm/diagram/kanban.js +280 -0
- package/dist/esm/diagram/label.js +56 -0
- package/dist/esm/diagram/layers.js +385 -0
- package/dist/esm/diagram/map-label.js +44 -0
- package/dist/esm/diagram/mindmap.js +277 -0
- package/dist/esm/diagram/numeric-composition.js +78 -0
- package/dist/esm/diagram/numeric-model.js +86 -0
- package/dist/esm/diagram/numeric-parse.js +205 -0
- package/dist/esm/diagram/numeric-radar.js +355 -0
- package/dist/esm/diagram/numeric-sankey.js +201 -0
- package/dist/esm/diagram/numeric-xy.js +422 -0
- package/dist/esm/diagram/packet.js +175 -0
- package/dist/esm/diagram/presentation.js +252 -0
- package/dist/esm/diagram/quadrant.js +297 -0
- package/dist/esm/diagram/railroad-parse.js +465 -0
- package/dist/esm/diagram/railroad.js +294 -0
- package/dist/esm/diagram/registry.js +296 -0
- package/dist/esm/diagram/relations.js +966 -0
- package/dist/esm/diagram/requirement.js +341 -0
- package/dist/esm/diagram/sequence.js +1228 -0
- package/dist/esm/diagram/shapes.js +366 -0
- package/dist/esm/diagram/source.js +538 -0
- package/dist/esm/diagram/support.js +153 -0
- package/dist/esm/diagram/swimlane-mermaid.js +140 -0
- package/dist/esm/diagram/swimlane.js +1186 -0
- package/dist/esm/diagram/time.js +748 -0
- package/dist/esm/diagram/timeline.js +376 -0
- package/dist/esm/diagram/tree-family-icon.js +22 -0
- package/dist/esm/diagram/treeview.js +293 -0
- package/dist/esm/diagram/usecase.js +852 -0
- package/dist/esm/diagram/venn.js +1038 -0
- package/dist/esm/diagram/wardley.js +838 -0
- package/dist/esm/index.d.ts +133 -0
- package/dist/esm/index.js +92 -0
- package/dist/esm/interaction.js +561 -0
- package/dist/esm/mermaid.d.ts +28 -0
- package/dist/esm/mermaid.js +70 -0
- package/dist/esm/node.d.ts +68 -0
- package/dist/esm/node.js +373 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/page-api.js +65 -0
- package/dist/esm/runtime.js +352 -0
- package/dist/esm/src/families.js +209 -0
- package/dist/page-api.js +1 -0
- package/dist/units/chart/basic.js +1 -0
- package/dist/units/chart/catalog.js +1 -0
- package/dist/units/chart/composition.js +1 -0
- package/dist/units/chart/context.js +1 -0
- package/dist/units/chart/distribution.js +1 -0
- package/dist/units/chart/fields.js +1 -0
- package/dist/units/chart/maps.js +1 -0
- package/dist/units/chart/mindmap.js +1 -0
- package/dist/units/chart/multivariate.js +1 -0
- package/dist/units/chart/registry.js +1 -0
- package/dist/units/chart/relations.js +1 -0
- package/dist/units/chart/spec.js +3 -0
- package/dist/units/chart/stat-compare.js +1 -0
- package/dist/units/chart/stat-trend.js +1 -0
- package/dist/units/chart/statistics.js +1 -0
- package/dist/units/chart/unit-variants.js +1 -0
- package/dist/units/chart/variants.js +1 -0
- package/dist/units/core/color.js +1 -0
- package/dist/units/core/context.js +1 -0
- package/dist/units/core/diagnostics.js +1 -0
- package/dist/units/core/format.js +1 -0
- package/dist/units/core/geometry.js +1 -0
- package/dist/units/core/icon-data.js +1 -0
- package/dist/units/core/icons.js +1 -0
- package/dist/units/core/scale.js +1 -0
- package/dist/units/core/scene.js +1 -0
- package/dist/units/core/svg.js +1 -0
- package/dist/units/core/text.js +2 -0
- package/dist/units/core/theme.js +1 -0
- package/dist/units/diagram/agentflow.js +12 -0
- package/dist/units/diagram/architecture.js +4 -0
- package/dist/units/diagram/block.js +2 -0
- package/dist/units/diagram/blocks.js +2 -0
- package/dist/units/diagram/c4.js +6 -0
- package/dist/units/diagram/class.js +5 -0
- package/dist/units/diagram/cynefin.js +4 -0
- package/dist/units/diagram/emphasis.js +1 -0
- package/dist/units/diagram/er.js +9 -0
- package/dist/units/diagram/eventmodeling.js +5 -0
- package/dist/units/diagram/gantt-date.js +1 -0
- package/dist/units/diagram/gantt.js +3 -0
- package/dist/units/diagram/gitgraph.js +2 -0
- package/dist/units/diagram/graph-layout.js +4 -0
- package/dist/units/diagram/graph-parse.js +9 -0
- package/dist/units/diagram/graph-route.js +1 -0
- package/dist/units/diagram/graph.js +1 -0
- package/dist/units/diagram/ishikawa.js +2 -0
- package/dist/units/diagram/journey.js +5 -0
- package/dist/units/diagram/kanban.js +2 -0
- package/dist/units/diagram/label.js +4 -0
- package/dist/units/diagram/layers.js +1 -0
- package/dist/units/diagram/map-label.js +1 -0
- package/dist/units/diagram/mindmap.js +2 -0
- package/dist/units/diagram/numeric-composition.js +1 -0
- package/dist/units/diagram/numeric-model.js +1 -0
- package/dist/units/diagram/numeric-parse.js +2 -0
- package/dist/units/diagram/numeric-radar.js +3 -0
- package/dist/units/diagram/numeric-sankey.js +2 -0
- package/dist/units/diagram/numeric-xy.js +2 -0
- package/dist/units/diagram/packet.js +2 -0
- package/dist/units/diagram/presentation.js +2 -0
- package/dist/units/diagram/quadrant.js +2 -0
- package/dist/units/diagram/railroad-parse.js +4 -0
- package/dist/units/diagram/railroad.js +1 -0
- package/dist/units/diagram/registry.js +2 -0
- package/dist/units/diagram/relations.js +3 -0
- package/dist/units/diagram/requirement.js +3 -0
- package/dist/units/diagram/sequence.js +2 -0
- package/dist/units/diagram/shapes.js +60 -0
- package/dist/units/diagram/source.js +20 -0
- package/dist/units/diagram/support.js +3 -0
- package/dist/units/diagram/swimlane-mermaid.js +3 -0
- package/dist/units/diagram/swimlane.js +2 -0
- package/dist/units/diagram/time.js +3 -0
- package/dist/units/diagram/timeline.js +2 -0
- package/dist/units/diagram/tree-family-icon.js +1 -0
- package/dist/units/diagram/treeview.js +2 -0
- package/dist/units/diagram/usecase.js +3 -0
- package/dist/units/diagram/venn.js +2 -0
- package/dist/units/diagram/wardley.js +4 -0
- package/dist/units/interaction.js +1 -0
- package/dist/units/runtime.js +1 -0
- package/dist/units.json +712 -0
- package/package.json +30 -4
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { createContext } from "./context.js";
|
|
3
|
+
import { el, escapeXml, hash, validateAttribute, validateLink, validateTag } from "./scene.js";
|
|
4
|
+
// src/core/svg.js
|
|
5
|
+
var svg_exports = /* @__PURE__ */ __exportAll({ renderScene: () => renderScene });
|
|
6
|
+
function serializeElement(node, prefix) {
|
|
7
|
+
validateTag(node?.tag);
|
|
8
|
+
const attrs = { ...node.attrs || {} };
|
|
9
|
+
for (const key of Object.keys(attrs)) validateAttribute(key, {
|
|
10
|
+
serialize: true,
|
|
11
|
+
tag: node.tag
|
|
12
|
+
});
|
|
13
|
+
if (node.tag === "a") validateLink(attrs);
|
|
14
|
+
if (attrs.id) attrs.id = `${prefix}-${attrs.id}`;
|
|
15
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
16
|
+
if (typeof value === "number" && !Number.isFinite(value)) throw new TypeError(`Invalid SVG geometry: ${key}`);
|
|
17
|
+
if ([
|
|
18
|
+
"d",
|
|
19
|
+
"points",
|
|
20
|
+
"transform"
|
|
21
|
+
].includes(key) && /NaN|Infinity/.test(String(value))) throw new TypeError(`Invalid SVG geometry: ${key}`);
|
|
22
|
+
if ([
|
|
23
|
+
"width",
|
|
24
|
+
"height",
|
|
25
|
+
"r",
|
|
26
|
+
"rx",
|
|
27
|
+
"ry"
|
|
28
|
+
].includes(key) && Number(value) < 0) throw new TypeError(`Negative SVG dimension: ${key}`);
|
|
29
|
+
}
|
|
30
|
+
for (const [key, value] of Object.entries(attrs)) if (typeof value === "string" && key !== "href" && key !== "target" && key !== "rel") {
|
|
31
|
+
if ((value.match(/url\([^)]*\)/gi) || []).some((ref) => !/^url\(\s*#[\w.-]+\s*\)$/i.test(ref))) throw new TypeError(`Unsafe SVG resource reference: ${key}`);
|
|
32
|
+
attrs[key] = value.replace(/url\(\s*#([\w.-]+)\s*\)/g, `url(#${prefix}-$1)`);
|
|
33
|
+
}
|
|
34
|
+
for (const [key, value] of Object.entries(node.meta || {})) if (value != null) attrs[`data-kc-${key}`] = Array.isArray(value) || typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
35
|
+
const attrText = Object.entries(attrs).map(([key, value]) => {
|
|
36
|
+
validateAttribute(key, {
|
|
37
|
+
serialize: true,
|
|
38
|
+
generated: true,
|
|
39
|
+
tag: node.tag
|
|
40
|
+
});
|
|
41
|
+
return ` ${key}="${escapeXml(value)}"`;
|
|
42
|
+
}).join("");
|
|
43
|
+
const children = (node.children || []).map((child) => serializeElement(child, prefix)).join("");
|
|
44
|
+
const text = node.text == null ? "" : escapeXml(node.text);
|
|
45
|
+
return `<${node.tag}${attrText}>${text}${children}</${node.tag}>`;
|
|
46
|
+
}
|
|
47
|
+
function renderScene(scene, options = {}) {
|
|
48
|
+
if (!scene || !Number.isFinite(scene.width) || !Number.isFinite(scene.height) || scene.width <= 0 || scene.height <= 0 || !Array.isArray(scene.children)) throw new TypeError("Invalid KChart scene");
|
|
49
|
+
const prefix = String(options.prefix || `kc-${hash(JSON.stringify(scene))}`).replace(/[^A-Za-z0-9_-]/g, "-");
|
|
50
|
+
const title = scene.title || "Chart", titleId = `${prefix}-title`;
|
|
51
|
+
const desc = scene.description ? `<desc>${escapeXml(scene.description)}</desc>` : "";
|
|
52
|
+
let nodes = scene.children, height = scene.height;
|
|
53
|
+
const ctx = createContext({
|
|
54
|
+
...options,
|
|
55
|
+
width: scene.width
|
|
56
|
+
});
|
|
57
|
+
if (options.frame) {
|
|
58
|
+
const header = [], footer = [];
|
|
59
|
+
let top = 16;
|
|
60
|
+
const paragraph = (target, value, y, size, weight = 400) => {
|
|
61
|
+
if (!value) return y;
|
|
62
|
+
const lines = ctx.wrap(value, Math.max(24, scene.width - 32), size, weight), step = size + 6;
|
|
63
|
+
lines.forEach((line, i) => target.push(ctx.text(16, y + size + i * step, line, {
|
|
64
|
+
size,
|
|
65
|
+
weight,
|
|
66
|
+
fill: weight > 400 ? ctx.color("fg") : ctx.color("muted")
|
|
67
|
+
})));
|
|
68
|
+
return y + lines.length * step + 6;
|
|
69
|
+
};
|
|
70
|
+
top = paragraph(header, scene.heading, top, 16, 650);
|
|
71
|
+
top = paragraph(header, scene.subtitle, top, 12);
|
|
72
|
+
if (header.length) top += 8;
|
|
73
|
+
let bottom = top + scene.height + 12;
|
|
74
|
+
bottom = paragraph(footer, scene.source, bottom, 12);
|
|
75
|
+
bottom = paragraph(footer, scene.note, bottom, 12);
|
|
76
|
+
height = bottom + 12;
|
|
77
|
+
nodes = [
|
|
78
|
+
ctx.rect(0, 0, scene.width, height, { fill: ctx.color("bg") }),
|
|
79
|
+
...header,
|
|
80
|
+
el("g", { transform: `translate(0 ${top})` }, scene.children),
|
|
81
|
+
...footer
|
|
82
|
+
];
|
|
83
|
+
}
|
|
84
|
+
const body = nodes.map((child) => serializeElement(child, prefix)).join("");
|
|
85
|
+
const anchor = scene.anchor && Number.isFinite(scene.anchor.x) && Number.isFinite(scene.anchor.y) ? ` data-kchart-anchor-x="${scene.anchor.x}" data-kchart-anchor-y="${scene.anchor.y}"` : "";
|
|
86
|
+
const font = escapeXml(ctx.fontFamily);
|
|
87
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${scene.width}" height="${height}" viewBox="0 0 ${scene.width} ${height}"${anchor} font-family="${font}" role="img" aria-labelledby="${titleId}"><title id="${titleId}">${escapeXml(title)}</title>${desc}${body}</svg>`;
|
|
88
|
+
}
|
|
89
|
+
export { renderScene, svg_exports };
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
// src/core/text.js
|
|
3
|
+
var text_exports = /* @__PURE__ */ __exportAll({
|
|
4
|
+
fallbackMeasure: () => fallbackMeasure,
|
|
5
|
+
wrapText: () => wrapText
|
|
6
|
+
});
|
|
7
|
+
var isCJK = (ch) => /[\u2E80-\u9FFF\uF900-\uFAFF]/.test(ch);
|
|
8
|
+
var isCJKPunct = (ch) => /[\u3000-\u303F\uFF00-\uFFEF]/.test(ch);
|
|
9
|
+
function fallbackMeasure(text, size = 12, weight = 400) {
|
|
10
|
+
return [...String(text ?? "")].reduce((sum, ch) => sum + (/^[\u2e80-\u9fff\uf900-\ufaff\u3000-\u30ff\uac00-\ud7af]$/u.test(ch) ? 1 : .56) * size * (weight >= 600 ? 1.04 : 1), 0);
|
|
11
|
+
}
|
|
12
|
+
var NO_LINE_START = /* @__PURE__ */ new Set([..."))]]】」』〉》,,。、;;::!!??…%%"]);
|
|
13
|
+
var NO_LINE_END = /* @__PURE__ */ new Set([..."(([[【「『〈《"]);
|
|
14
|
+
function applyKinsoku(lines, start = 0) {
|
|
15
|
+
for (let i = start + 1; i < lines.length; i += 1) {
|
|
16
|
+
let guard = 0;
|
|
17
|
+
while (lines[i] && NO_LINE_START.has([...lines[i]][0]) && guard++ < 8) {
|
|
18
|
+
const chars = [...lines[i]];
|
|
19
|
+
lines[i - 1] += chars.shift();
|
|
20
|
+
lines[i] = chars.join("");
|
|
21
|
+
}
|
|
22
|
+
if (!lines[i]) {
|
|
23
|
+
lines.splice(i--, 1);
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
guard = 0;
|
|
27
|
+
while (lines[i - 1] && NO_LINE_END.has([...lines[i - 1]].at(-1)) && guard++ < 8) {
|
|
28
|
+
const chars = [...lines[i - 1]];
|
|
29
|
+
if (chars.length <= 1) break;
|
|
30
|
+
lines[i] = chars.pop() + lines[i];
|
|
31
|
+
lines[i - 1] = chars.join("");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return lines;
|
|
35
|
+
}
|
|
36
|
+
var segmenter = null;
|
|
37
|
+
var segmenterChecked = false;
|
|
38
|
+
function wordBreaker() {
|
|
39
|
+
if (!segmenterChecked) {
|
|
40
|
+
segmenterChecked = true;
|
|
41
|
+
try {
|
|
42
|
+
segmenter = typeof Intl !== "undefined" && typeof Intl.Segmenter === "function" ? new Intl.Segmenter("zh", { granularity: "word" }) : null;
|
|
43
|
+
} catch {
|
|
44
|
+
segmenter = null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return segmenter;
|
|
48
|
+
}
|
|
49
|
+
function wrapTokens(paragraph) {
|
|
50
|
+
const tokens = [];
|
|
51
|
+
let latin = "";
|
|
52
|
+
const flush = () => {
|
|
53
|
+
if (latin) {
|
|
54
|
+
tokens.push(latin);
|
|
55
|
+
latin = "";
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const breaker = wordBreaker();
|
|
59
|
+
if (!breaker) {
|
|
60
|
+
for (const ch of paragraph) if (/\s/.test(ch)) {
|
|
61
|
+
flush();
|
|
62
|
+
tokens.push(" ");
|
|
63
|
+
} else if (isCJK(ch) || isCJKPunct(ch)) {
|
|
64
|
+
flush();
|
|
65
|
+
tokens.push(ch);
|
|
66
|
+
} else latin += ch;
|
|
67
|
+
flush();
|
|
68
|
+
return tokens;
|
|
69
|
+
}
|
|
70
|
+
for (const part of breaker.segment(paragraph)) {
|
|
71
|
+
const value = part.segment;
|
|
72
|
+
if (/^\s+$/.test(value)) {
|
|
73
|
+
flush();
|
|
74
|
+
tokens.push(" ");
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const chars = [...value];
|
|
78
|
+
if (!chars.some((ch) => isCJK(ch) || isCJKPunct(ch))) {
|
|
79
|
+
latin += value;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
flush();
|
|
83
|
+
if (part.isWordLike && chars.length > 1 && chars.every(isCJK)) tokens.push(value);
|
|
84
|
+
else tokens.push(...chars);
|
|
85
|
+
}
|
|
86
|
+
flush();
|
|
87
|
+
for (let i = 1; i < tokens.length; i += 1) {
|
|
88
|
+
if ([...tokens[i]].length !== 1 || !isCJK(tokens[i]) || !isCJK([...tokens[i - 1]].at(-1))) continue;
|
|
89
|
+
tokens.splice(i - 1, 2, tokens[i - 1] + tokens[i]);
|
|
90
|
+
i -= 1;
|
|
91
|
+
}
|
|
92
|
+
for (let i = 0; i < tokens.length - 1; i += 1) {
|
|
93
|
+
if (!/^[\d.,%]+$/.test(tokens[i]) || !/\d/.test(tokens[i])) continue;
|
|
94
|
+
const gap = tokens[i + 1] === " " ? 1 : 0, next = tokens[i + 1 + gap];
|
|
95
|
+
if (next && isCJK(next[0]) && !isCJKPunct(next[0])) tokens.splice(i, 2 + gap, `${tokens[i]}${gap ? " " : ""}${next}`);
|
|
96
|
+
}
|
|
97
|
+
return tokens;
|
|
98
|
+
}
|
|
99
|
+
var WRAP_TOLERANCE = .5;
|
|
100
|
+
function wrapAt(text, maxWidth, font, measure) {
|
|
101
|
+
const lines = [];
|
|
102
|
+
const fontSpec = {
|
|
103
|
+
size: Number(font.size) || 13,
|
|
104
|
+
weight: Number(font.weight) || 400
|
|
105
|
+
};
|
|
106
|
+
for (const paragraph of String(text ?? "").split("\n")) {
|
|
107
|
+
if (!paragraph) {
|
|
108
|
+
lines.push("");
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
const start = lines.length, widths = /* @__PURE__ */ new Map();
|
|
112
|
+
const widthOf = (token) => {
|
|
113
|
+
if (!widths.has(token)) widths.set(token, measure(token, fontSpec));
|
|
114
|
+
return widths.get(token);
|
|
115
|
+
};
|
|
116
|
+
const lineWidth = (tokens) => tokens.reduce((sum, token) => sum + widthOf(token), 0);
|
|
117
|
+
const tooWide = (piece) => widthOf(piece) > maxWidth + WRAP_TOLERANCE;
|
|
118
|
+
const tokens = wrapTokens(paragraph).flatMap((token) => token === " " || !tooWide(token) ? [token] : token.split(/(?<=[-/])/).flatMap((piece) => tooWide(piece) ? [...piece] : [piece]));
|
|
119
|
+
const wrapped = [[]];
|
|
120
|
+
let currentWidth = 0;
|
|
121
|
+
for (const token of tokens) {
|
|
122
|
+
const current = wrapped.at(-1);
|
|
123
|
+
if (token === " ") {
|
|
124
|
+
if (current.length) {
|
|
125
|
+
current.push(" ");
|
|
126
|
+
currentWidth += widthOf(" ");
|
|
127
|
+
}
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (currentWidth + widthOf(token) <= maxWidth + WRAP_TOLERANCE) {
|
|
131
|
+
current.push(token);
|
|
132
|
+
currentWidth += widthOf(token);
|
|
133
|
+
} else {
|
|
134
|
+
wrapped.push([token]);
|
|
135
|
+
currentWidth = widthOf(token);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
for (let i = wrapped.length - 1; i > 0; i -= 1) {
|
|
139
|
+
let guard = 0;
|
|
140
|
+
while (guard < 4 && [...wrapped[i].join("").trim()].length <= 2 && wrapped[i - 1].filter((token) => token !== " ").length > 1) {
|
|
141
|
+
let at = wrapped[i - 1].length - 1;
|
|
142
|
+
while (at >= 0 && wrapped[i - 1][at] === " ") at -= 1;
|
|
143
|
+
if (at < 0 || !isCJK([...wrapped[i - 1][at]][0])) break;
|
|
144
|
+
const merged = [...wrapped[i - 1].slice(at), ...wrapped[i]];
|
|
145
|
+
if (lineWidth(merged) > maxWidth + WRAP_TOLERANCE) break;
|
|
146
|
+
wrapped[i] = merged;
|
|
147
|
+
wrapped[i - 1] = wrapped[i - 1].slice(0, at);
|
|
148
|
+
guard += 1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
for (const line of wrapped) lines.push(line.join("").trimEnd());
|
|
152
|
+
if (lines.length === start) lines.push("");
|
|
153
|
+
applyKinsoku(lines, start);
|
|
154
|
+
}
|
|
155
|
+
return lines.length ? lines : [""];
|
|
156
|
+
}
|
|
157
|
+
function wrapText(text, maxWidth, font = {}, measure = (value, f) => fallbackMeasure(value, f.size, f.weight), { bounded = false } = {}) {
|
|
158
|
+
if (!bounded) return wrapAt(text, maxWidth, font, measure);
|
|
159
|
+
const fontSpec = {
|
|
160
|
+
size: Number(font.size) || 13,
|
|
161
|
+
weight: Number(font.weight) || 400
|
|
162
|
+
};
|
|
163
|
+
const measured = /* @__PURE__ */ new Map();
|
|
164
|
+
const widthOf = (value) => {
|
|
165
|
+
if (!measured.has(value)) measured.set(value, measure(value, fontSpec));
|
|
166
|
+
return measured.get(value);
|
|
167
|
+
};
|
|
168
|
+
const widest = (lines) => Math.max(...lines.map(widthOf));
|
|
169
|
+
let budget = maxWidth;
|
|
170
|
+
let best = wrapAt(text, budget, fontSpec, widthOf);
|
|
171
|
+
let bestWidth = widest(best);
|
|
172
|
+
let lineWidth = bestWidth;
|
|
173
|
+
while (lineWidth > maxWidth + WRAP_TOLERANCE && budget > fontSpec.size) {
|
|
174
|
+
budget = Math.max(fontSpec.size, budget - Math.ceil(lineWidth - maxWidth));
|
|
175
|
+
const lines = wrapAt(text, budget, fontSpec, widthOf);
|
|
176
|
+
lineWidth = widest(lines);
|
|
177
|
+
if (lineWidth < bestWidth) {
|
|
178
|
+
best = lines;
|
|
179
|
+
bestWidth = lineWidth;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return best;
|
|
183
|
+
}
|
|
184
|
+
export { fallbackMeasure, text_exports, wrapText };
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import claude_default from "../assets/themes/claude.js";
|
|
3
|
+
import cloudmusic_default from "../assets/themes/cloudmusic.js";
|
|
4
|
+
import kanagawa_default from "../assets/themes/kanagawa.js";
|
|
5
|
+
import obsidian_default from "../assets/themes/obsidian.js";
|
|
6
|
+
import paper_default from "../assets/themes/paper.js";
|
|
7
|
+
import terminal_default from "../assets/themes/terminal.js";
|
|
8
|
+
import tokyo_default from "../assets/themes/tokyo.js";
|
|
9
|
+
import { contrast, deltaE2000, isHex, simulateCvd } from "./color.js";
|
|
10
|
+
// src/core/theme.js
|
|
11
|
+
var theme_exports = /* @__PURE__ */ __exportAll({
|
|
12
|
+
DEFAULT_THEME: () => DEFAULT_THEME,
|
|
13
|
+
FONT_FAMILY: () => FONT_FAMILY,
|
|
14
|
+
defaultStyle: () => defaultStyle,
|
|
15
|
+
paletteOf: () => paletteOf,
|
|
16
|
+
resolveThemeConfig: () => resolveThemeConfig,
|
|
17
|
+
statusMarks: () => statusMarks,
|
|
18
|
+
theme: () => theme,
|
|
19
|
+
themes: () => themes,
|
|
20
|
+
validateThemeSnapshot: () => validateThemeSnapshot
|
|
21
|
+
});
|
|
22
|
+
var themeFiles = /* #__PURE__ */ Object.assign({
|
|
23
|
+
"../../assets/themes/claude.json": claude_default,
|
|
24
|
+
"../../assets/themes/cloudmusic.json": cloudmusic_default,
|
|
25
|
+
"../../assets/themes/kanagawa.json": kanagawa_default,
|
|
26
|
+
"../../assets/themes/obsidian.json": obsidian_default,
|
|
27
|
+
"../../assets/themes/paper.json": paper_default,
|
|
28
|
+
"../../assets/themes/terminal.json": terminal_default,
|
|
29
|
+
"../../assets/themes/tokyo.json": tokyo_default
|
|
30
|
+
});
|
|
31
|
+
var moduleData = Object.freeze(Object.fromEntries(Object.keys(themeFiles).sort().map((path) => [path.slice(path.lastIndexOf("/") + 1, -5), themeFiles[path]])));
|
|
32
|
+
var DEFAULT_THEME = "claude";
|
|
33
|
+
var FONT_FAMILY = "system-ui, -apple-system, BlinkMacSystemFont, \"Segoe UI\", \"Noto Sans SC\", \"Microsoft YaHei\", sans-serif";
|
|
34
|
+
var statusMarks = Object.freeze({
|
|
35
|
+
new: "+",
|
|
36
|
+
info: "~",
|
|
37
|
+
ok: "=",
|
|
38
|
+
warn: "!",
|
|
39
|
+
danger: "×",
|
|
40
|
+
ext: "↗"
|
|
41
|
+
});
|
|
42
|
+
var textKeys = [
|
|
43
|
+
"fg1",
|
|
44
|
+
"fg2",
|
|
45
|
+
"fg3",
|
|
46
|
+
"fg4"
|
|
47
|
+
];
|
|
48
|
+
var statusKeys = Object.keys(statusMarks);
|
|
49
|
+
var backgrounds = [
|
|
50
|
+
"bg",
|
|
51
|
+
"surface",
|
|
52
|
+
"surface2"
|
|
53
|
+
];
|
|
54
|
+
var colorKeys = [
|
|
55
|
+
...backgrounds,
|
|
56
|
+
"line",
|
|
57
|
+
"line2",
|
|
58
|
+
...textKeys,
|
|
59
|
+
"accent",
|
|
60
|
+
...statusKeys
|
|
61
|
+
];
|
|
62
|
+
var optionalColors = ["frameInk", "tint"];
|
|
63
|
+
var labels = [
|
|
64
|
+
"name",
|
|
65
|
+
"en",
|
|
66
|
+
"desc"
|
|
67
|
+
];
|
|
68
|
+
var snapshotKeys = [
|
|
69
|
+
"id",
|
|
70
|
+
"scheme",
|
|
71
|
+
"fontFamily",
|
|
72
|
+
"tones",
|
|
73
|
+
...colorKeys,
|
|
74
|
+
...optionalColors,
|
|
75
|
+
...labels,
|
|
76
|
+
"style",
|
|
77
|
+
"diagram"
|
|
78
|
+
];
|
|
79
|
+
var configKeys = [
|
|
80
|
+
...snapshotKeys,
|
|
81
|
+
"extends",
|
|
82
|
+
"order"
|
|
83
|
+
];
|
|
84
|
+
var snapshots = /* @__PURE__ */ new WeakSet();
|
|
85
|
+
var idPattern = /^[a-z][a-z0-9-]{0,47}$/;
|
|
86
|
+
var reservedId = (id) => /^(?:custom-|ui-)/.test(id);
|
|
87
|
+
var isRecord = (value) => value && typeof value === "object" && !Array.isArray(value);
|
|
88
|
+
var ranges = (min, max) => (value) => Number.isFinite(value) && value >= min && value <= max;
|
|
89
|
+
var oneOf = (...choices) => (value) => choices.includes(value);
|
|
90
|
+
var textRoles = oneOf(...textKeys, "accent", ...statusKeys, ...Array.from({ length: 6 }, (_, i) => `tone${i + 1}`));
|
|
91
|
+
var localFonts = (value) => typeof value === "string" && value.length <= 512 && !/[\u0000-\u001f\u007f]/.test(value) && value.split(",").every((part) => /^(?:[\p{L}\p{M}\p{N}_-][\p{L}\p{M}\p{N} _-]*|"[\p{L}\p{M}\p{N} &_-]+"|'[\p{L}\p{M}\p{N} &_-]+')$/u.test(part.replace(/^ +| +$/g, "")));
|
|
92
|
+
var diagramFields = {
|
|
93
|
+
radius: ranges(0, 24),
|
|
94
|
+
smallRadius: ranges(0, 24),
|
|
95
|
+
soft: ranges(0, .25),
|
|
96
|
+
nodeBackground: oneOf(...backgrounds)
|
|
97
|
+
};
|
|
98
|
+
var styleFields = {
|
|
99
|
+
...diagramFields,
|
|
100
|
+
blockBackground: oneOf(...backgrounds, "transparent"),
|
|
101
|
+
codeBackground: oneOf(...backgrounds, "node"),
|
|
102
|
+
displayFont: (value) => value === "sans" || localFonts(value),
|
|
103
|
+
displayWeight: (value) => Number.isInteger(value) && ranges(100, 900)(value),
|
|
104
|
+
numberFont: oneOf("sans", "mono"),
|
|
105
|
+
numberStyle: oneOf("decimal", "decimal-leading-zero"),
|
|
106
|
+
numberColor: textRoles,
|
|
107
|
+
listMarker: textRoles
|
|
108
|
+
};
|
|
109
|
+
var defaultStyle = {
|
|
110
|
+
radius: 6,
|
|
111
|
+
smallRadius: 4,
|
|
112
|
+
soft: .13,
|
|
113
|
+
nodeBackground: "surface2",
|
|
114
|
+
blockBackground: "surface",
|
|
115
|
+
codeBackground: "node",
|
|
116
|
+
displayFont: "sans",
|
|
117
|
+
displayWeight: 600,
|
|
118
|
+
numberFont: "sans",
|
|
119
|
+
numberStyle: "decimal",
|
|
120
|
+
numberColor: "fg4",
|
|
121
|
+
listMarker: "fg4"
|
|
122
|
+
};
|
|
123
|
+
function checkKeys(value, allowed, path) {
|
|
124
|
+
if (!isRecord(value)) throw new TypeError(`${path} 必须是对象`);
|
|
125
|
+
for (const key of Object.keys(value)) if (!allowed.includes(key)) throw new TypeError(`未知主题字段 ${path}.${key}`);
|
|
126
|
+
}
|
|
127
|
+
function checkStyle(value, fields, path) {
|
|
128
|
+
checkKeys(value, Object.keys(fields), path);
|
|
129
|
+
for (const [key, item] of Object.entries(value)) if (!fields[key](item)) throw new TypeError(`主题 ${path}.${key} 取值无效`);
|
|
130
|
+
}
|
|
131
|
+
var pick = (object, keys) => Object.fromEntries(keys.filter((key) => Object.hasOwn(object, key)).map((key) => [key, object[key]]));
|
|
132
|
+
var frozen = (key, value) => {
|
|
133
|
+
if (key === "tones") return Object.freeze([...value]);
|
|
134
|
+
if (key === "tint") return Object.freeze({
|
|
135
|
+
hue: value.hue,
|
|
136
|
+
chroma: value.chroma
|
|
137
|
+
});
|
|
138
|
+
if (key === "style" || key === "diagram") return Object.freeze(pick(value, Object.keys(key === "style" ? styleFields : diagramFields)));
|
|
139
|
+
return value;
|
|
140
|
+
};
|
|
141
|
+
function snapshot(source) {
|
|
142
|
+
const result = Object.freeze(Object.fromEntries(snapshotKeys.filter((key) => Object.hasOwn(source, key) && source[key] !== void 0).map((key) => [key, frozen(key, source[key])])));
|
|
143
|
+
snapshots.add(result);
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
function validate(theme) {
|
|
147
|
+
if (!isRecord(theme)) throw new TypeError("主题必须是对象");
|
|
148
|
+
if (theme.id != null && (typeof theme.id !== "string" || !/^[a-z][a-z0-9-]*$/.test(theme.id))) throw new TypeError("主题 id 非法");
|
|
149
|
+
if (!["light", "dark"].includes(theme.scheme)) throw new TypeError("主题 scheme 必须是 light 或 dark");
|
|
150
|
+
if (typeof theme.fontFamily !== "string" || !theme.fontFamily.trim() || /[\u0000-\u001f\u007f<>;]/.test(theme.fontFamily)) throw new TypeError("主题 fontFamily 非法");
|
|
151
|
+
if (theme.style !== void 0) checkStyle(theme.style, styleFields, "style");
|
|
152
|
+
if (theme.diagram !== void 0) checkStyle(theme.diagram, diagramFields, "diagram");
|
|
153
|
+
if (!Array.isArray(theme.tones) || theme.tones.length !== 6) throw new TypeError("主题 tones 必须包含 6 个颜色");
|
|
154
|
+
for (const key of colorKeys) if (!isHex(theme[key])) throw new TypeError(`主题 ${key} 必须是 #RRGGBB 颜色`);
|
|
155
|
+
for (const hex of theme.tones) if (!isHex(hex)) throw new TypeError("主题 tones 包含非法颜色");
|
|
156
|
+
if (theme.frameInk !== void 0 && (!isHex(theme.frameInk) || contrast(theme.frameInk, theme.surface) < 3)) throw new TypeError("主题 frameInk 必须是对 surface 有至少 3:1 对比度的 #RRGGBB 颜色");
|
|
157
|
+
if (theme.tint !== void 0) {
|
|
158
|
+
const { hue, chroma } = theme.tint || {};
|
|
159
|
+
if (!isRecord(theme.tint) || Object.keys(theme.tint).some((key) => !["hue", "chroma"].includes(key)) || !Number.isFinite(hue) || hue < 0 || hue >= 360 || !ranges(1, 2)(chroma)) throw new TypeError("主题 tint 必须是 { hue: 0–360, chroma: 1–2 }");
|
|
160
|
+
}
|
|
161
|
+
for (let i = 0; i < theme.tones.length; i += 1) {
|
|
162
|
+
if (contrast(theme.tones[i], theme.surface) < 3) throw new TypeError(`主题 tones[${i}] 对 surface 对比度不足 3`);
|
|
163
|
+
for (let j = i + 1; j < theme.tones.length; j += 1) {
|
|
164
|
+
if (deltaE2000(theme.tones[i], theme.tones[j]) < 12) throw new TypeError(`主题 tones[${i}] 与 tones[${j}] 区分度不足 12`);
|
|
165
|
+
if (i < 3 && j < 3) {
|
|
166
|
+
if (deltaE2000(theme.tones[i], theme.tones[j]) < 15) throw new TypeError(`主题 tones[${i}] 与 tones[${j}] 正常视觉区分度不足 15`);
|
|
167
|
+
for (const cvd of ["protan", "deutan"]) if (deltaE2000(simulateCvd(theme.tones[i], cvd), simulateCvd(theme.tones[j], cvd)) < 15) throw new TypeError(`主题 tones[${i}] 与 tones[${j}] 在 ${cvd} 色觉下区分度不足 15`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
for (const key of ["accent", ...statusKeys]) if (contrast(theme[key], theme.surface) < 3) throw new TypeError(`主题 ${key} 对 surface 对比度不足 3`);
|
|
172
|
+
for (const text of ["fg1", "fg3"]) for (const background of ["bg", "surface"]) if (contrast(theme[text], theme[background]) < 4.5) throw new TypeError(`主题 ${text} 对 ${background} 对比度不足 4.5`);
|
|
173
|
+
return theme;
|
|
174
|
+
}
|
|
175
|
+
var colorOf = (theme, role) => /^tone[1-6]$/.test(role) ? theme.tones[Number(role.at(-1)) - 1] : theme[role];
|
|
176
|
+
function readable(theme, role, on, minimum = 4.5) {
|
|
177
|
+
for (const bg of on) if (contrast(colorOf(theme, role), bg === "print.bg" ? "#ffffff" : theme[bg]) < minimum) throw new TypeError(`主题 ${role} 对 ${bg} 对比度不足 ${minimum}`);
|
|
178
|
+
}
|
|
179
|
+
function checkReadingColors(theme) {
|
|
180
|
+
const page = theme.scheme === "light" ? ["bg", "print.bg"] : ["bg"];
|
|
181
|
+
for (const role of [...textKeys, "accent"]) readable(theme, role, [
|
|
182
|
+
...page,
|
|
183
|
+
"surface",
|
|
184
|
+
"surface2"
|
|
185
|
+
]);
|
|
186
|
+
for (const role of statusKeys) readable(theme, role, [...page, "surface"]);
|
|
187
|
+
readable(theme, theme.style.numberColor, page);
|
|
188
|
+
readable(theme, theme.style.listMarker, page, 3);
|
|
189
|
+
}
|
|
190
|
+
function resolveConfig(config, base, id) {
|
|
191
|
+
if (Object.hasOwn(config, "style")) checkStyle(config.style, styleFields, "style");
|
|
192
|
+
if (Object.hasOwn(config, "diagram") && config.diagram !== null) checkStyle(config.diagram, diagramFields, "diagram");
|
|
193
|
+
const style = {
|
|
194
|
+
...defaultStyle,
|
|
195
|
+
...base?.style,
|
|
196
|
+
...config.style
|
|
197
|
+
};
|
|
198
|
+
const diagram = config.diagram === null ? {} : {
|
|
199
|
+
...base?.diagram,
|
|
200
|
+
...config.diagram
|
|
201
|
+
};
|
|
202
|
+
const resolved = {
|
|
203
|
+
fontFamily: FONT_FAMILY,
|
|
204
|
+
...base,
|
|
205
|
+
...pick(config, snapshotKeys),
|
|
206
|
+
id,
|
|
207
|
+
style
|
|
208
|
+
};
|
|
209
|
+
for (const key of labels) resolved[key] = config[key] ?? (key === "desc" ? "" : config.name ?? config.en ?? id ?? "Custom");
|
|
210
|
+
for (const key of optionalColors) if (resolved[key] === null) delete resolved[key];
|
|
211
|
+
if (Object.keys(diagram).length) resolved.diagram = diagram;
|
|
212
|
+
else delete resolved.diagram;
|
|
213
|
+
checkFileTheme(resolved);
|
|
214
|
+
checkReadingColors(resolved);
|
|
215
|
+
for (const key of [...colorKeys, "frameInk"]) if (resolved[key]) resolved[key] = resolved[key].toLowerCase();
|
|
216
|
+
resolved.tones = resolved.tones.map((hex) => hex.toLowerCase());
|
|
217
|
+
return snapshot(resolved);
|
|
218
|
+
}
|
|
219
|
+
function checkConfig(config, path) {
|
|
220
|
+
checkKeys(config, configKeys, path);
|
|
221
|
+
for (const key of ["id", "extends"]) if (Object.hasOwn(config, key) && (typeof config[key] !== "string" || !idPattern.test(config[key]) || reservedId(config[key]))) throw new TypeError(`主题 ${key} 必须是有效且不保留的主题 ID`);
|
|
222
|
+
if (Object.hasOwn(config, "order") && (!Number.isSafeInteger(config.order) || config.order < 0)) throw new TypeError("主题 order 必须是非负整数");
|
|
223
|
+
checkLabels(config);
|
|
224
|
+
}
|
|
225
|
+
function checkLabels(config) {
|
|
226
|
+
for (const key of labels) if (Object.hasOwn(config, key) && (typeof config[key] !== "string" || config[key].length > 240 || /[\u0000-\u001f\u007f]/.test(config[key]) || key !== "desc" && !config[key].trim())) throw new TypeError(`主题 ${key} 必须是有效的纯文本`);
|
|
227
|
+
}
|
|
228
|
+
function builtins(configs) {
|
|
229
|
+
const resolved = /* @__PURE__ */ new Map(), visiting = /* @__PURE__ */ new Set();
|
|
230
|
+
const load = (id) => {
|
|
231
|
+
if (!Object.hasOwn(configs, id)) throw new TypeError(`未知继承主题:${id}`);
|
|
232
|
+
if (resolved.has(id)) return resolved.get(id);
|
|
233
|
+
if (visiting.has(id)) throw new TypeError(`主题继承存在循环:${[...visiting, id].join(" → ")}`);
|
|
234
|
+
const config = configs[id];
|
|
235
|
+
checkConfig(config, `${id}.json`);
|
|
236
|
+
if (config.id !== id) throw new TypeError(`主题 id 必须和文件名一致:${id}.json`);
|
|
237
|
+
if (labels.some((key) => !config[key]?.trim())) throw new TypeError("内置主题必须声明 name、en 和 desc");
|
|
238
|
+
visiting.add(id);
|
|
239
|
+
const parent = config.extends ?? (id === "paper" ? null : id === "claude" ? "paper" : "claude");
|
|
240
|
+
const result = resolveConfig(config, parent === null ? null : load(parent), id);
|
|
241
|
+
resolved.set(id, result);
|
|
242
|
+
visiting.delete(id);
|
|
243
|
+
return result;
|
|
244
|
+
};
|
|
245
|
+
const ids = Object.keys(configs);
|
|
246
|
+
for (const id of ids) try {
|
|
247
|
+
load(id);
|
|
248
|
+
} catch (cause) {
|
|
249
|
+
throw new TypeError(`内置主题 ${id}.json:${cause.message}`, { cause });
|
|
250
|
+
}
|
|
251
|
+
if (!resolved.has("claude") || resolved.get("paper")?.scheme !== "light") throw new TypeError("缺少默认主题或浅色打印基线 Paper");
|
|
252
|
+
ids.sort((a, b) => (configs[a].order ?? Infinity) - (configs[b].order ?? Infinity) || (a < b ? -1 : 1));
|
|
253
|
+
return Object.freeze(Object.fromEntries(ids.map((id) => [id, resolved.get(id)])));
|
|
254
|
+
}
|
|
255
|
+
var themes = builtins(moduleData);
|
|
256
|
+
function resolveThemeConfig(config) {
|
|
257
|
+
checkConfig(config, "theme");
|
|
258
|
+
if (Object.hasOwn(themes, config.id)) throw new TypeError(`主题 id 与内置主题冲突:${config.id}`);
|
|
259
|
+
const parent = config.extends ?? "claude";
|
|
260
|
+
if (!Object.hasOwn(themes, parent)) throw new TypeError(`未知继承主题:${parent}`);
|
|
261
|
+
return resolveConfig(config, themes[parent], config.id);
|
|
262
|
+
}
|
|
263
|
+
function checkFileTheme(config) {
|
|
264
|
+
if (!localFonts(config.fontFamily)) throw new TypeError("主题 fontFamily 必须是本地字体名列表");
|
|
265
|
+
validate(config);
|
|
266
|
+
}
|
|
267
|
+
function validateThemeSnapshot(config) {
|
|
268
|
+
checkKeys(config, snapshotKeys, "theme");
|
|
269
|
+
if (!config.style || Object.keys(defaultStyle).some((key) => !Object.hasOwn(config.style, key)) || labels.some((key) => !Object.hasOwn(config, key))) throw new TypeError("内嵌主题样式或名称不完整");
|
|
270
|
+
checkLabels(config);
|
|
271
|
+
checkFileTheme(config);
|
|
272
|
+
return snapshot(config);
|
|
273
|
+
}
|
|
274
|
+
function theme(idOrObject = DEFAULT_THEME, { media = "screen" } = {}) {
|
|
275
|
+
if (!["screen", "print"].includes(media)) throw new TypeError("media 必须是 screen 或 print");
|
|
276
|
+
let source;
|
|
277
|
+
if (typeof idOrObject === "string") {
|
|
278
|
+
if (!Object.hasOwn(themes, idOrObject)) throw new TypeError(`未知主题:${idOrObject}`);
|
|
279
|
+
source = themes[idOrObject];
|
|
280
|
+
} else source = snapshots.has(idOrObject) ? idOrObject : snapshot(validate(idOrObject));
|
|
281
|
+
if (media !== "print") return source;
|
|
282
|
+
if (source.scheme === "dark") {
|
|
283
|
+
const paper = themes.paper;
|
|
284
|
+
const resolved = {
|
|
285
|
+
...source,
|
|
286
|
+
scheme: "light",
|
|
287
|
+
tones: [...paper.tones]
|
|
288
|
+
};
|
|
289
|
+
for (const key of colorKeys) resolved[key] = paper[key];
|
|
290
|
+
for (const key of optionalColors) if (paper[key] !== void 0) resolved[key] = paper[key];
|
|
291
|
+
else delete resolved[key];
|
|
292
|
+
return snapshot(resolved);
|
|
293
|
+
}
|
|
294
|
+
return snapshot({
|
|
295
|
+
...source,
|
|
296
|
+
bg: "#ffffff"
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
function paletteOf(resolvedTheme) {
|
|
300
|
+
const [tone1, tone2, tone3, tone4, tone5, tone6] = resolvedTheme.tones;
|
|
301
|
+
return Object.freeze({
|
|
302
|
+
ink: tone1,
|
|
303
|
+
accent: tone2,
|
|
304
|
+
tone1,
|
|
305
|
+
tone2,
|
|
306
|
+
tone3,
|
|
307
|
+
tone4,
|
|
308
|
+
tone5,
|
|
309
|
+
tone6,
|
|
310
|
+
fg: resolvedTheme.fg1,
|
|
311
|
+
muted: resolvedTheme.fg3,
|
|
312
|
+
bg: resolvedTheme.surface,
|
|
313
|
+
grid: resolvedTheme.line,
|
|
314
|
+
axis: resolvedTheme.line2
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
Object.freeze(defaultStyle);
|
|
318
|
+
export { DEFAULT_THEME, FONT_FAMILY, defaultStyle, paletteOf, resolveThemeConfig, statusMarks, theme, theme_exports, themes, validateThemeSnapshot };
|