@klarkxy/dsh-font 0.1.3 → 0.2.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 +4 -2
- package/docs/README.zh-CN.md +5 -3
- package/lib/client.inner.cjs +256 -279
- package/lib/client.inner.cjs.map +1 -1
- package/lib/client.js +256 -279
- package/lib/contracts.d.ts +6 -8
- package/lib/contracts.js +8 -9
- package/lib/contracts.js.map +1 -1
- package/package.json +12 -9
package/lib/client.js
CHANGED
|
@@ -8,6 +8,231 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
8
8
|
let _deepseek_ai_dsh_client_ui_primitives = require("@deepseek-ai/dsh-client-ui-primitives");
|
|
9
9
|
let react = require("react");
|
|
10
10
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
11
|
+
//#region src/contracts.ts
|
|
12
|
+
/** Browser-safe contracts for @klarkxy/dsh-font: settings shape, font presets,
|
|
13
|
+
* normalization, the CSS-variable application core, and the local store.
|
|
14
|
+
* No React, no cordis — every export runs in Node tests and the browser. */
|
|
15
|
+
const PLUGIN_NAME = "@klarkxy/dsh-font";
|
|
16
|
+
/** Legacy browser-local font choices, retained as the one-time migration backup.
|
|
17
|
+
* The active panel reads and writes the host's role-specific preferences. */
|
|
18
|
+
const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
19
|
+
function defaultFontSettings() {
|
|
20
|
+
return {
|
|
21
|
+
uiFont: "",
|
|
22
|
+
codeFont: ""
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/** A raw font-family string is placed into a single CSS declaration via
|
|
26
|
+
* CSSStyleDeclaration.setProperty, which cannot escape the declaration — but
|
|
27
|
+
* trimming control characters and the CSS delimiters keeps pasted garbage from
|
|
28
|
+
* producing a silently-dead value. */
|
|
29
|
+
function normalizeFontFamily(value) {
|
|
30
|
+
if (typeof value !== "string") return "";
|
|
31
|
+
return value.replace(/[\u0000-\u001F\u007F;{}<>]/g, "").trim().slice(0, 300);
|
|
32
|
+
}
|
|
33
|
+
function parseFontSettings(raw) {
|
|
34
|
+
let source = raw;
|
|
35
|
+
if (typeof source === "string") try {
|
|
36
|
+
source = JSON.parse(source);
|
|
37
|
+
} catch {
|
|
38
|
+
return defaultFontSettings();
|
|
39
|
+
}
|
|
40
|
+
if (!source || typeof source !== "object" || Array.isArray(source)) return defaultFontSettings();
|
|
41
|
+
const record = source;
|
|
42
|
+
return {
|
|
43
|
+
uiFont: normalizeFontFamily(record.uiFont),
|
|
44
|
+
codeFont: normalizeFontFamily(record.codeFont)
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function serializeFontSettings(settings) {
|
|
48
|
+
return JSON.stringify({
|
|
49
|
+
uiFont: normalizeFontFamily(settings.uiFont),
|
|
50
|
+
codeFont: normalizeFontFamily(settings.codeFont)
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const UI_FONT_PRESETS = [
|
|
54
|
+
{
|
|
55
|
+
id: "yahei",
|
|
56
|
+
label: {
|
|
57
|
+
zh: "微软雅黑",
|
|
58
|
+
en: "Microsoft YaHei"
|
|
59
|
+
},
|
|
60
|
+
value: "\"Microsoft YaHei\", \"PingFang SC\", sans-serif"
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
id: "pingfang",
|
|
64
|
+
label: {
|
|
65
|
+
zh: "苹方",
|
|
66
|
+
en: "PingFang SC"
|
|
67
|
+
},
|
|
68
|
+
value: "\"PingFang SC\", -apple-system, sans-serif"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: "simsun",
|
|
72
|
+
label: {
|
|
73
|
+
zh: "宋体",
|
|
74
|
+
en: "SimSun"
|
|
75
|
+
},
|
|
76
|
+
value: "\"SimSun\", \"Songti SC\", serif"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: "kaiti",
|
|
80
|
+
label: {
|
|
81
|
+
zh: "楷体",
|
|
82
|
+
en: "KaiTi"
|
|
83
|
+
},
|
|
84
|
+
value: "\"KaiTi\", \"Kaiti SC\", \"STKaiti\", serif"
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
id: "source-han-sans",
|
|
88
|
+
label: {
|
|
89
|
+
zh: "思源黑体",
|
|
90
|
+
en: "Source Han Sans"
|
|
91
|
+
},
|
|
92
|
+
value: "\"Source Han Sans SC\", \"Noto Sans CJK SC\", \"Noto Sans SC\", sans-serif"
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
id: "source-han-serif",
|
|
96
|
+
label: {
|
|
97
|
+
zh: "思源宋体",
|
|
98
|
+
en: "Source Han Serif"
|
|
99
|
+
},
|
|
100
|
+
value: "\"Source Han Serif SC\", \"Noto Serif CJK SC\", \"Noto Serif SC\", serif"
|
|
101
|
+
}
|
|
102
|
+
];
|
|
103
|
+
const CODE_FONT_PRESETS = [
|
|
104
|
+
{
|
|
105
|
+
id: "cascadia",
|
|
106
|
+
label: {
|
|
107
|
+
zh: "Cascadia",
|
|
108
|
+
en: "Cascadia"
|
|
109
|
+
},
|
|
110
|
+
value: "\"Cascadia Mono\", \"Cascadia Code\", monospace"
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
id: "jetbrains-mono",
|
|
114
|
+
label: {
|
|
115
|
+
zh: "JetBrains Mono",
|
|
116
|
+
en: "JetBrains Mono"
|
|
117
|
+
},
|
|
118
|
+
value: "\"JetBrains Mono\", monospace"
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
id: "fira-code",
|
|
122
|
+
label: {
|
|
123
|
+
zh: "Fira Code",
|
|
124
|
+
en: "Fira Code"
|
|
125
|
+
},
|
|
126
|
+
value: "\"Fira Code\", monospace"
|
|
127
|
+
},
|
|
128
|
+
{
|
|
129
|
+
id: "consolas",
|
|
130
|
+
label: {
|
|
131
|
+
zh: "Consolas",
|
|
132
|
+
en: "Consolas"
|
|
133
|
+
},
|
|
134
|
+
value: "Consolas, monospace"
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
id: "sarasa",
|
|
138
|
+
label: {
|
|
139
|
+
zh: "更纱黑体",
|
|
140
|
+
en: "Sarasa Mono"
|
|
141
|
+
},
|
|
142
|
+
value: "\"Sarasa Mono SC\", \"Sarasa Term SC\", monospace"
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
id: "maple-mono",
|
|
146
|
+
label: {
|
|
147
|
+
zh: "Maple Mono",
|
|
148
|
+
en: "Maple Mono"
|
|
149
|
+
},
|
|
150
|
+
value: "\"Maple Mono NF CN\", \"Maple Mono\", monospace"
|
|
151
|
+
}
|
|
152
|
+
];
|
|
153
|
+
function presetFor(value, presets) {
|
|
154
|
+
const comparable = (stack) => stack.split(",").map((name) => name.trim().replace(/^["']|["']$/g, "")).join(",");
|
|
155
|
+
return presets.find((preset) => comparable(preset.value) === comparable(value));
|
|
156
|
+
}
|
|
157
|
+
/** Build the committed stack for a family picked from the system font list.
|
|
158
|
+
* The family name comes from the OS (Local Font Access API); quotes and
|
|
159
|
+
* backslashes are stripped so it can be safely re-quoted as one family. */
|
|
160
|
+
function fontStackFor(family, generic) {
|
|
161
|
+
const cleaned = normalizeFontFamily(family).replace(/["\\]/g, "");
|
|
162
|
+
if (!cleaned) return generic;
|
|
163
|
+
return `"${cleaned}", ${generic}`;
|
|
164
|
+
}
|
|
165
|
+
/** Distinct, normalized, family-sorted entries out of a queryLocalFonts()
|
|
166
|
+
* result (entries carry `family` and `fullName`; duplicates per style are
|
|
167
|
+
* expected). */
|
|
168
|
+
function uniqueSystemFonts(fonts) {
|
|
169
|
+
const byFamily = /* @__PURE__ */ new Map();
|
|
170
|
+
for (const item of fonts) {
|
|
171
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) continue;
|
|
172
|
+
const record = item;
|
|
173
|
+
const family = normalizeFontFamily(record.family).replace(/["\\]/g, "");
|
|
174
|
+
if (!family || byFamily.has(family)) continue;
|
|
175
|
+
const fullName = normalizeFontFamily(record.fullName).replace(/["\\]/g, "");
|
|
176
|
+
byFamily.set(family, fullName && fullName !== family && !fullName.startsWith(`${family} `) ? fullName : "");
|
|
177
|
+
}
|
|
178
|
+
return [...byFamily.entries()].map(([family, fullName]) => ({
|
|
179
|
+
family,
|
|
180
|
+
fullName
|
|
181
|
+
})).sort((a, b) => a.family.localeCompare(b.family));
|
|
182
|
+
}
|
|
183
|
+
/** Picker label: the UI locale's recognizable name leads, the alias stays in
|
|
184
|
+
* parentheses because the canonical family is still what a custom input needs. */
|
|
185
|
+
function systemFontLabel(item, locale) {
|
|
186
|
+
if (!item.fullName) return item.family;
|
|
187
|
+
return locale === "zh" ? `${item.fullName}(${item.family})` : `${item.family}(${item.fullName})`;
|
|
188
|
+
}
|
|
189
|
+
/** Browser-local persistence. A fully-default setting removes the key so a
|
|
190
|
+
* reset leaves no residue; storage failures (private mode, quota) degrade to
|
|
191
|
+
* in-memory behaviour rather than breaking the panel. */
|
|
192
|
+
function createFontSettingsStore(storage) {
|
|
193
|
+
let current = defaultFontSettings();
|
|
194
|
+
let pendingPersistence = false;
|
|
195
|
+
return {
|
|
196
|
+
load() {
|
|
197
|
+
if (storage && !pendingPersistence) try {
|
|
198
|
+
current = parseFontSettings(storage.getItem(STORAGE_KEY));
|
|
199
|
+
} catch {}
|
|
200
|
+
return { ...current };
|
|
201
|
+
},
|
|
202
|
+
save(next) {
|
|
203
|
+
current = parseFontSettings(next);
|
|
204
|
+
if (!storage) return;
|
|
205
|
+
pendingPersistence = true;
|
|
206
|
+
try {
|
|
207
|
+
if (!current.uiFont && !current.codeFont) storage.removeItem(STORAGE_KEY);
|
|
208
|
+
else storage.setItem(STORAGE_KEY, serializeFontSettings(current));
|
|
209
|
+
pendingPersistence = false;
|
|
210
|
+
} catch {}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/preferences.ts
|
|
216
|
+
const MIGRATION_KEY = "klarkxy.dsh-font.host-preferences.v1";
|
|
217
|
+
/** Preserve the old selection as a backup, but never replay it over native
|
|
218
|
+
* preferences on subsequent loads. Each role migrates independently. */
|
|
219
|
+
function migrateFontPreferences(theme, store, storage) {
|
|
220
|
+
try {
|
|
221
|
+
if (storage?.getItem("klarkxy.dsh-font.host-preferences.v1")) return;
|
|
222
|
+
} catch {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const old = store.load();
|
|
226
|
+
const families = theme.getTheme().fontFamilies;
|
|
227
|
+
for (const [role, value] of [
|
|
228
|
+
["text", old.uiFont],
|
|
229
|
+
["code", old.codeFont],
|
|
230
|
+
["terminal", old.codeFont]
|
|
231
|
+
]) if (!families[role] && normalizeFontFamily(value)) theme.setFontFamily(role, value);
|
|
232
|
+
try {
|
|
233
|
+
storage?.setItem(MIGRATION_KEY, "1");
|
|
234
|
+
} catch {}
|
|
235
|
+
}
|
|
11
236
|
Object.values({
|
|
12
237
|
/** Surfaces, from the page up to the topmost nested card. */
|
|
13
238
|
surface: [
|
|
@@ -184,6 +409,17 @@ ${scope} {
|
|
|
184
409
|
/* --- Layout scaffolding. --- */
|
|
185
410
|
& .dsh-ui-stack { display: flex; flex-direction: column; gap: 12px; }
|
|
186
411
|
& .dsh-ui-stack-lg { display: flex; flex-direction: column; gap: 20px; }
|
|
412
|
+
/* Plugin detail pages share one reading column. These are layout recipes;
|
|
413
|
+
* controls keep their own primitive styles. Form spacing follows the host's
|
|
414
|
+
* settings-form/fields.module.css and SettingsForm.module.css. */
|
|
415
|
+
& .dsh-ui-page { display: flex; flex-direction: column; gap: 16px; width: 100%; max-width: 760px; min-width: 0; }
|
|
416
|
+
& .dsh-ui-form { display: flex; flex-direction: column; gap: 0; min-width: 0; }
|
|
417
|
+
&.dsh-ui-form > .dsh-ui-field, & .dsh-ui-form > .dsh-ui-field { padding: 12px 0; }
|
|
418
|
+
& .dsh-ui-form-footer { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; padding-top: 16px; min-width: 0; }
|
|
419
|
+
& .dsh-ui-fields-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px 20px; min-width: 0; margin: 0; padding: 0; border: 0; }
|
|
420
|
+
@media (max-width: 560px) {
|
|
421
|
+
& .dsh-ui-fields-grid { grid-template-columns: minmax(0, 1fr); }
|
|
422
|
+
}
|
|
187
423
|
& .dsh-ui-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
|
|
188
424
|
& .dsh-ui-row-wrap { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
|
|
189
425
|
& .dsh-ui-spacer { flex: 1; min-width: 0; }
|
|
@@ -211,6 +447,9 @@ ${scope} {
|
|
|
211
447
|
* official primitive, so this only owns the vertical rhythm. --- */
|
|
212
448
|
& .dsh-ui-field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
|
|
213
449
|
& .dsh-ui-field + .dsh-ui-field { border-top: 0.5px solid var(--dsw-alias-border-l2); padding-top: 12px; }
|
|
450
|
+
/* Grid fields share a row, so the stacked-field divider must not win later in
|
|
451
|
+
* the cascade. Reset it here, after the field recipe. */
|
|
452
|
+
& .dsh-ui-fields-grid > .dsh-ui-field { padding: 0; border: 0; }
|
|
214
453
|
& .dsh-ui-label { display: flex; align-items: center; gap: 6px; font-size: 13px; line-height: 20px; font-weight: 500; color: var(--dsw-alias-label-primary); }
|
|
215
454
|
& .dsh-ui-label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
|
|
216
455
|
& .dsh-ui-help { font-size: 12px; line-height: 1.6; color: var(--dsw-alias-label-secondary); }
|
|
@@ -401,255 +640,7 @@ ${scope} {
|
|
|
401
640
|
& *, & *::before, & *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
|
|
402
641
|
}
|
|
403
642
|
}
|
|
404
|
-
`.replace(/^& (\.dsh-ui-[a-z0-9-]+)(
|
|
405
|
-
}
|
|
406
|
-
//#endregion
|
|
407
|
-
//#region src/contracts.ts
|
|
408
|
-
/** Browser-safe contracts for @klarkxy/dsh-font: settings shape, font presets,
|
|
409
|
-
* normalization, the CSS-variable application core, and the local store.
|
|
410
|
-
* No React, no cordis — every export runs in Node tests and the browser. */
|
|
411
|
-
const PLUGIN_NAME = "@klarkxy/dsh-font";
|
|
412
|
-
/** localStorage key. Fonts are a property of the rendering environment (each
|
|
413
|
-
* browser/machine has its own installed fonts), so they persist browser-local,
|
|
414
|
-
* exactly like the host's own GUI preferences (dsh-client-store, ui-theme
|
|
415
|
-
* notes). The conversation font size is NOT stored here: the official
|
|
416
|
-
* `ctx.theme.setFontSize()` entry persists it host-side already. */
|
|
417
|
-
const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
418
|
-
/** DSH rc.2 defines these font stacks on :root. Root inline overrides also
|
|
419
|
-
* reach derived Markdown, brand and terminal font tokens. */
|
|
420
|
-
const UI_FONT_VARIABLE = "--dsw-font-family";
|
|
421
|
-
const CODE_FONT_VARIABLE = "--ds-font-family-code";
|
|
422
|
-
function defaultFontSettings() {
|
|
423
|
-
return {
|
|
424
|
-
uiFont: "",
|
|
425
|
-
codeFont: ""
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
/** A raw font-family string is placed into a single CSS declaration via
|
|
429
|
-
* CSSStyleDeclaration.setProperty, which cannot escape the declaration — but
|
|
430
|
-
* trimming control characters and the CSS delimiters keeps pasted garbage from
|
|
431
|
-
* producing a silently-dead value. */
|
|
432
|
-
function normalizeFontFamily(value) {
|
|
433
|
-
if (typeof value !== "string") return "";
|
|
434
|
-
return value.replace(/[\u0000-\u001F\u007F;{}<>]/g, "").trim().slice(0, 300);
|
|
435
|
-
}
|
|
436
|
-
function parseFontSettings(raw) {
|
|
437
|
-
let source = raw;
|
|
438
|
-
if (typeof source === "string") try {
|
|
439
|
-
source = JSON.parse(source);
|
|
440
|
-
} catch {
|
|
441
|
-
return defaultFontSettings();
|
|
442
|
-
}
|
|
443
|
-
if (!source || typeof source !== "object" || Array.isArray(source)) return defaultFontSettings();
|
|
444
|
-
const record = source;
|
|
445
|
-
return {
|
|
446
|
-
uiFont: normalizeFontFamily(record.uiFont),
|
|
447
|
-
codeFont: normalizeFontFamily(record.codeFont)
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
function serializeFontSettings(settings) {
|
|
451
|
-
return JSON.stringify({
|
|
452
|
-
uiFont: normalizeFontFamily(settings.uiFont),
|
|
453
|
-
codeFont: normalizeFontFamily(settings.codeFont)
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
const UI_FONT_PRESETS = [
|
|
457
|
-
{
|
|
458
|
-
id: "yahei",
|
|
459
|
-
label: {
|
|
460
|
-
zh: "微软雅黑",
|
|
461
|
-
en: "Microsoft YaHei"
|
|
462
|
-
},
|
|
463
|
-
value: "\"Microsoft YaHei\", \"PingFang SC\", sans-serif"
|
|
464
|
-
},
|
|
465
|
-
{
|
|
466
|
-
id: "pingfang",
|
|
467
|
-
label: {
|
|
468
|
-
zh: "苹方",
|
|
469
|
-
en: "PingFang SC"
|
|
470
|
-
},
|
|
471
|
-
value: "\"PingFang SC\", -apple-system, sans-serif"
|
|
472
|
-
},
|
|
473
|
-
{
|
|
474
|
-
id: "simsun",
|
|
475
|
-
label: {
|
|
476
|
-
zh: "宋体",
|
|
477
|
-
en: "SimSun"
|
|
478
|
-
},
|
|
479
|
-
value: "\"SimSun\", \"Songti SC\", serif"
|
|
480
|
-
},
|
|
481
|
-
{
|
|
482
|
-
id: "kaiti",
|
|
483
|
-
label: {
|
|
484
|
-
zh: "楷体",
|
|
485
|
-
en: "KaiTi"
|
|
486
|
-
},
|
|
487
|
-
value: "\"KaiTi\", \"Kaiti SC\", \"STKaiti\", serif"
|
|
488
|
-
},
|
|
489
|
-
{
|
|
490
|
-
id: "source-han-sans",
|
|
491
|
-
label: {
|
|
492
|
-
zh: "思源黑体",
|
|
493
|
-
en: "Source Han Sans"
|
|
494
|
-
},
|
|
495
|
-
value: "\"Source Han Sans SC\", \"Noto Sans CJK SC\", \"Noto Sans SC\", sans-serif"
|
|
496
|
-
},
|
|
497
|
-
{
|
|
498
|
-
id: "source-han-serif",
|
|
499
|
-
label: {
|
|
500
|
-
zh: "思源宋体",
|
|
501
|
-
en: "Source Han Serif"
|
|
502
|
-
},
|
|
503
|
-
value: "\"Source Han Serif SC\", \"Noto Serif CJK SC\", \"Noto Serif SC\", serif"
|
|
504
|
-
}
|
|
505
|
-
];
|
|
506
|
-
const CODE_FONT_PRESETS = [
|
|
507
|
-
{
|
|
508
|
-
id: "cascadia",
|
|
509
|
-
label: {
|
|
510
|
-
zh: "Cascadia",
|
|
511
|
-
en: "Cascadia"
|
|
512
|
-
},
|
|
513
|
-
value: "\"Cascadia Mono\", \"Cascadia Code\", monospace"
|
|
514
|
-
},
|
|
515
|
-
{
|
|
516
|
-
id: "jetbrains-mono",
|
|
517
|
-
label: {
|
|
518
|
-
zh: "JetBrains Mono",
|
|
519
|
-
en: "JetBrains Mono"
|
|
520
|
-
},
|
|
521
|
-
value: "\"JetBrains Mono\", monospace"
|
|
522
|
-
},
|
|
523
|
-
{
|
|
524
|
-
id: "fira-code",
|
|
525
|
-
label: {
|
|
526
|
-
zh: "Fira Code",
|
|
527
|
-
en: "Fira Code"
|
|
528
|
-
},
|
|
529
|
-
value: "\"Fira Code\", monospace"
|
|
530
|
-
},
|
|
531
|
-
{
|
|
532
|
-
id: "consolas",
|
|
533
|
-
label: {
|
|
534
|
-
zh: "Consolas",
|
|
535
|
-
en: "Consolas"
|
|
536
|
-
},
|
|
537
|
-
value: "Consolas, monospace"
|
|
538
|
-
},
|
|
539
|
-
{
|
|
540
|
-
id: "sarasa",
|
|
541
|
-
label: {
|
|
542
|
-
zh: "更纱黑体",
|
|
543
|
-
en: "Sarasa Mono"
|
|
544
|
-
},
|
|
545
|
-
value: "\"Sarasa Mono SC\", \"Sarasa Term SC\", monospace"
|
|
546
|
-
},
|
|
547
|
-
{
|
|
548
|
-
id: "maple-mono",
|
|
549
|
-
label: {
|
|
550
|
-
zh: "Maple Mono",
|
|
551
|
-
en: "Maple Mono"
|
|
552
|
-
},
|
|
553
|
-
value: "\"Maple Mono NF CN\", \"Maple Mono\", monospace"
|
|
554
|
-
}
|
|
555
|
-
];
|
|
556
|
-
function presetFor(value, presets) {
|
|
557
|
-
return presets.find((preset) => preset.value === value);
|
|
558
|
-
}
|
|
559
|
-
/** Build the committed stack for a family picked from the system font list.
|
|
560
|
-
* The family name comes from the OS (Local Font Access API); quotes and
|
|
561
|
-
* backslashes are stripped so it can be safely re-quoted as one family. */
|
|
562
|
-
function fontStackFor(family, generic) {
|
|
563
|
-
const cleaned = normalizeFontFamily(family).replace(/["\\]/g, "");
|
|
564
|
-
if (!cleaned) return generic;
|
|
565
|
-
return `"${cleaned}", ${generic}`;
|
|
566
|
-
}
|
|
567
|
-
/** Distinct, normalized, family-sorted entries out of a queryLocalFonts()
|
|
568
|
-
* result (entries carry `family` and `fullName`; duplicates per style are
|
|
569
|
-
* expected). */
|
|
570
|
-
function uniqueSystemFonts(fonts) {
|
|
571
|
-
const byFamily = /* @__PURE__ */ new Map();
|
|
572
|
-
for (const item of fonts) {
|
|
573
|
-
if (!item || typeof item !== "object" || Array.isArray(item)) continue;
|
|
574
|
-
const record = item;
|
|
575
|
-
const family = normalizeFontFamily(record.family).replace(/["\\]/g, "");
|
|
576
|
-
if (!family || byFamily.has(family)) continue;
|
|
577
|
-
const fullName = normalizeFontFamily(record.fullName).replace(/["\\]/g, "");
|
|
578
|
-
byFamily.set(family, fullName && fullName !== family && !fullName.startsWith(`${family} `) ? fullName : "");
|
|
579
|
-
}
|
|
580
|
-
return [...byFamily.entries()].map(([family, fullName]) => ({
|
|
581
|
-
family,
|
|
582
|
-
fullName
|
|
583
|
-
})).sort((a, b) => a.family.localeCompare(b.family));
|
|
584
|
-
}
|
|
585
|
-
/** Picker label: the UI locale's recognizable name leads, the alias stays in
|
|
586
|
-
* parentheses because the canonical family is still what a custom input needs. */
|
|
587
|
-
function systemFontLabel(item, locale) {
|
|
588
|
-
if (!item.fullName) return item.family;
|
|
589
|
-
return locale === "zh" ? `${item.fullName}(${item.family})` : `${item.family}(${item.fullName})`;
|
|
590
|
-
}
|
|
591
|
-
/** Root inline overrides preserve the existing local persistence/unload
|
|
592
|
-
* boundary. Save the underlying value (including !important) on first write
|
|
593
|
-
* or when the host replaces a value, and never retract somebody else's write. */
|
|
594
|
-
function createFontSettingsOverride(style) {
|
|
595
|
-
const owned = /* @__PURE__ */ new Map();
|
|
596
|
-
function release(name) {
|
|
597
|
-
const entry = owned.get(name);
|
|
598
|
-
if (!entry) return;
|
|
599
|
-
if (style.getPropertyValue(name) === entry.value && style.getPropertyPriority(name) === "") {
|
|
600
|
-
if (entry.previous) style.setProperty(name, entry.previous, entry.priority);
|
|
601
|
-
else style.removeProperty(name);
|
|
602
|
-
}
|
|
603
|
-
owned.delete(name);
|
|
604
|
-
}
|
|
605
|
-
return {
|
|
606
|
-
apply(settings) {
|
|
607
|
-
for (const [name, value] of [[UI_FONT_VARIABLE, settings.uiFont], [CODE_FONT_VARIABLE, settings.codeFont]]) {
|
|
608
|
-
if (!value) {
|
|
609
|
-
release(name);
|
|
610
|
-
continue;
|
|
611
|
-
}
|
|
612
|
-
const current = style.getPropertyValue(name);
|
|
613
|
-
const priority = style.getPropertyPriority(name);
|
|
614
|
-
const entry = owned.get(name);
|
|
615
|
-
if (!entry || current !== entry.value || priority !== "") owned.set(name, {
|
|
616
|
-
previous: current,
|
|
617
|
-
priority,
|
|
618
|
-
value: ""
|
|
619
|
-
});
|
|
620
|
-
style.setProperty(name, value);
|
|
621
|
-
owned.get(name).value = style.getPropertyValue(name);
|
|
622
|
-
}
|
|
623
|
-
},
|
|
624
|
-
dispose() {
|
|
625
|
-
for (const name of [...owned.keys()]) release(name);
|
|
626
|
-
}
|
|
627
|
-
};
|
|
628
|
-
}
|
|
629
|
-
/** Browser-local persistence. A fully-default setting removes the key so a
|
|
630
|
-
* reset leaves no residue; storage failures (private mode, quota) degrade to
|
|
631
|
-
* in-memory behaviour rather than breaking the panel. */
|
|
632
|
-
function createFontSettingsStore(storage) {
|
|
633
|
-
let current = defaultFontSettings();
|
|
634
|
-
let pendingPersistence = false;
|
|
635
|
-
return {
|
|
636
|
-
load() {
|
|
637
|
-
if (storage && !pendingPersistence) try {
|
|
638
|
-
current = parseFontSettings(storage.getItem(STORAGE_KEY));
|
|
639
|
-
} catch {}
|
|
640
|
-
return { ...current };
|
|
641
|
-
},
|
|
642
|
-
save(next) {
|
|
643
|
-
current = parseFontSettings(next);
|
|
644
|
-
if (!storage) return;
|
|
645
|
-
pendingPersistence = true;
|
|
646
|
-
try {
|
|
647
|
-
if (!current.uiFont && !current.codeFont) storage.removeItem(STORAGE_KEY);
|
|
648
|
-
else storage.setItem(STORAGE_KEY, serializeFontSettings(current));
|
|
649
|
-
pendingPersistence = false;
|
|
650
|
-
} catch {}
|
|
651
|
-
}
|
|
652
|
-
};
|
|
643
|
+
`.replace(/^& (\.dsh-ui-[a-z0-9-]+)(?=\s*\{)/gm, (_match, className) => `&${className}, & ${className}`);
|
|
653
644
|
}
|
|
654
645
|
//#endregion
|
|
655
646
|
//#region src/client.tsx
|
|
@@ -670,7 +661,7 @@ const COPY = {
|
|
|
670
661
|
customUiPlaceholder: "例如 \"Microsoft YaHei\", sans-serif",
|
|
671
662
|
customCodePlaceholder: "例如 \"JetBrains Mono\", monospace",
|
|
672
663
|
uiHelp: "作用于整个界面的文字。自定义时输入完整的 font-family 字体栈,留空恢复默认。",
|
|
673
|
-
codeHelp: "
|
|
664
|
+
codeHelp: "作用于代码块与行内代码。终端字体和字号可在「设置 → 通用」中单独调整。",
|
|
674
665
|
sizeHelp: "与「设置 → 通用」中的字号同步,范围 10–22。",
|
|
675
666
|
loadFonts: "读取系统字体",
|
|
676
667
|
reloadFonts: "重新读取系统字体",
|
|
@@ -693,7 +684,7 @@ const COPY = {
|
|
|
693
684
|
customUiPlaceholder: "e.g. \"Microsoft YaHei\", sans-serif",
|
|
694
685
|
customCodePlaceholder: "e.g. \"JetBrains Mono\", monospace",
|
|
695
686
|
uiHelp: "Applies to all interface text. For a custom value enter a full font-family stack; empty restores the default.",
|
|
696
|
-
codeHelp: "
|
|
687
|
+
codeHelp: "Applies to code blocks and inline code. Adjust terminal font and size separately in Settings → General.",
|
|
697
688
|
sizeHelp: "Synced with Settings → General; 10–22 px.",
|
|
698
689
|
loadFonts: "Load system fonts",
|
|
699
690
|
reloadFonts: "Reload system fonts",
|
|
@@ -711,8 +702,7 @@ function fontCopy(locale) {
|
|
|
711
702
|
return COPY[locale];
|
|
712
703
|
}
|
|
713
704
|
const styles = `${officialUiCss("font-root")}
|
|
714
|
-
.font-root {
|
|
715
|
-
.font-custom { margin-top: 8px; }
|
|
705
|
+
.font-root .font-custom { margin-top: 8px; }
|
|
716
706
|
`;
|
|
717
707
|
function readStorage() {
|
|
718
708
|
try {
|
|
@@ -726,7 +716,7 @@ function useLocale(client) {
|
|
|
726
716
|
return (0, react.useSyncExternalStore)((fn) => client.locale.subscribe(fn), getSnapshot, getSnapshot);
|
|
727
717
|
}
|
|
728
718
|
function useFontSize(client) {
|
|
729
|
-
return (0, react.useSyncExternalStore)((fn) => client.on("theme/change", () => fn()), () => client.theme.getTheme().
|
|
719
|
+
return (0, react.useSyncExternalStore)((fn) => client.on("theme/change", () => fn()), () => client.theme.getTheme().fontSizes.text, () => client.theme.getTheme().fontSizes.text);
|
|
730
720
|
}
|
|
731
721
|
function presetMode(value, presets) {
|
|
732
722
|
return presetFor(value, presets)?.id ?? (value ? "custom" : "");
|
|
@@ -811,7 +801,11 @@ function FontField(props) {
|
|
|
811
801
|
function FontSettingsPanel(props) {
|
|
812
802
|
const locale = useLocale(props.client);
|
|
813
803
|
const text = COPY[locale];
|
|
814
|
-
const
|
|
804
|
+
const families = (0, react.useSyncExternalStore)((fn) => props.client.on("theme/change", () => fn()), () => props.client.theme.getTheme().fontFamilies, () => props.client.theme.getTheme().fontFamilies);
|
|
805
|
+
const settings = {
|
|
806
|
+
uiFont: families.text,
|
|
807
|
+
codeFont: families.code
|
|
808
|
+
};
|
|
815
809
|
const fontSize = useFontSize(props.client);
|
|
816
810
|
const [fontList, setFontList] = (0, react.useState)({
|
|
817
811
|
state: "idle",
|
|
@@ -845,13 +839,8 @@ function FontSettingsPanel(props) {
|
|
|
845
839
|
}
|
|
846
840
|
}
|
|
847
841
|
function update(patch) {
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
codeFont: normalizeFontFamily(patch.codeFont ?? settings.codeFont)
|
|
851
|
-
};
|
|
852
|
-
props.store.save(next);
|
|
853
|
-
props.applySettings(next);
|
|
854
|
-
setSettings(next);
|
|
842
|
+
if (patch.uiFont !== void 0) props.client.theme.setFontFamily("text", normalizeFontFamily(patch.uiFont));
|
|
843
|
+
if (patch.codeFont !== void 0) props.client.theme.setFontFamily("code", normalizeFontFamily(patch.codeFont));
|
|
855
844
|
}
|
|
856
845
|
const uiName = presetFor(settings.uiFont, UI_FONT_PRESETS)?.label[locale] ?? (settings.uiFont || text.defaultName);
|
|
857
846
|
const codeName = presetFor(settings.codeFont, CODE_FONT_PRESETS)?.label[locale] ?? (settings.codeFont || text.defaultName);
|
|
@@ -863,7 +852,7 @@ function FontSettingsPanel(props) {
|
|
|
863
852
|
const sizes = [];
|
|
864
853
|
for (let px = 10; px <= 22; px += 1) sizes.push(px);
|
|
865
854
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
|
|
866
|
-
className: "font-root dsh-ui-
|
|
855
|
+
className: "font-root dsh-ui-page dsh-ui-form",
|
|
867
856
|
"data-testid": "font-settings",
|
|
868
857
|
children: [
|
|
869
858
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(FontField, {
|
|
@@ -907,7 +896,7 @@ function FontSettingsPanel(props) {
|
|
|
907
896
|
onChange: (event) => {
|
|
908
897
|
const px = Number(event.target.value);
|
|
909
898
|
if (Number.isInteger(px) && px >= 10 && px <= 22 && px !== fontSize) try {
|
|
910
|
-
props.client.theme.setFontSize(px);
|
|
899
|
+
props.client.theme.setFontSize("text", px);
|
|
911
900
|
} catch {}
|
|
912
901
|
},
|
|
913
902
|
children: sizes.map((px) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("option", {
|
|
@@ -922,7 +911,7 @@ function FontSettingsPanel(props) {
|
|
|
922
911
|
]
|
|
923
912
|
}),
|
|
924
913
|
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
925
|
-
className: "dsh-ui-
|
|
914
|
+
className: "dsh-ui-form-footer",
|
|
926
915
|
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Button, {
|
|
927
916
|
variant: "outline",
|
|
928
917
|
size: "md",
|
|
@@ -939,7 +928,7 @@ function FontSettingsPanel(props) {
|
|
|
939
928
|
uiFont: "",
|
|
940
929
|
codeFont: ""
|
|
941
930
|
});
|
|
942
|
-
if (fontSize !== 14) props.client.theme.setFontSize(14);
|
|
931
|
+
if (fontSize !== 14) props.client.theme.setFontSize("text", 14);
|
|
943
932
|
},
|
|
944
933
|
children: text.reset
|
|
945
934
|
})]
|
|
@@ -965,7 +954,7 @@ function FontSettingsPanel(props) {
|
|
|
965
954
|
function apply(ctx) {
|
|
966
955
|
const client = ctx;
|
|
967
956
|
const store = createFontSettingsStore(readStorage());
|
|
968
|
-
|
|
957
|
+
migrateFontPreferences(client.theme, store, readStorage());
|
|
969
958
|
ctx.effect(() => {
|
|
970
959
|
if (typeof document === "undefined") return () => {};
|
|
971
960
|
const style = document.createElement("style");
|
|
@@ -974,23 +963,11 @@ function apply(ctx) {
|
|
|
974
963
|
document.head.appendChild(style);
|
|
975
964
|
return () => style.remove();
|
|
976
965
|
}, "font.styles");
|
|
977
|
-
ctx.effect(() => {
|
|
978
|
-
if (typeof document === "undefined") return () => {};
|
|
979
|
-
const override = createFontSettingsOverride(document.documentElement.style);
|
|
980
|
-
applySettings = (next) => override.apply(next);
|
|
981
|
-
applySettings(store.load());
|
|
982
|
-
return () => {
|
|
983
|
-
override.dispose();
|
|
984
|
-
applySettings = () => {};
|
|
985
|
-
};
|
|
986
|
-
}, "font.apply");
|
|
987
966
|
ctx.effect(() => client.slots.inject("plugins.bundle.config", () => client.slots.register({
|
|
988
967
|
name: "plugins.bundle.config",
|
|
989
968
|
key: PLUGIN_NAME
|
|
990
969
|
}, (slotProps) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FontSettingsPanel, {
|
|
991
970
|
client,
|
|
992
|
-
store,
|
|
993
|
-
applySettings: (next) => applySettings(next),
|
|
994
971
|
view: slotProps?.view
|
|
995
972
|
}))), "font.settings");
|
|
996
973
|
}
|
package/lib/contracts.d.ts
CHANGED
|
@@ -3,18 +3,16 @@
|
|
|
3
3
|
* normalization, the CSS-variable application core, and the local store.
|
|
4
4
|
* No React, no cordis — every export runs in Node tests and the browser. */
|
|
5
5
|
declare const PLUGIN_NAME = "@klarkxy/dsh-font";
|
|
6
|
-
/**
|
|
7
|
-
*
|
|
8
|
-
* exactly like the host's own GUI preferences (dsh-client-store, ui-theme
|
|
9
|
-
* notes). The conversation font size is NOT stored here: the official
|
|
10
|
-
* `ctx.theme.setFontSize()` entry persists it host-side already. */
|
|
6
|
+
/** Legacy browser-local font choices, retained as the one-time migration backup.
|
|
7
|
+
* The active panel reads and writes the host's role-specific preferences. */
|
|
11
8
|
declare const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
12
|
-
/**
|
|
9
|
+
/** Host text-role bounds, shared by the native conversation-size stepper. */
|
|
13
10
|
declare const FONT_SIZE_MIN = 10;
|
|
14
11
|
declare const FONT_SIZE_MAX = 22;
|
|
15
12
|
declare const FONT_SIZE_DEFAULT = 14;
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
13
|
+
/** Root font overrides reach derived Markdown, brand and terminal fonts.
|
|
14
|
+
* These are the host variables this feature changes, rather than a private
|
|
15
|
+
* palette used to style the plugin's settings page. */
|
|
18
16
|
declare const UI_FONT_VARIABLE = "--dsw-font-family";
|
|
19
17
|
declare const CODE_FONT_VARIABLE = "--ds-font-family-code";
|
|
20
18
|
interface FontSettings {
|