@klarkxy/dsh-font 0.1.3 → 0.2.0
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 +4 -2
- package/lib/client.inner.cjs +238 -274
- package/lib/client.inner.cjs.map +1 -1
- package/lib/client.js +238 -274
- package/lib/contracts.d.ts +3 -6
- package/lib/contracts.js +5 -7
- package/lib/contracts.js.map +1 -1
- package/package.json +12 -9
package/README.md
CHANGED
|
@@ -21,12 +21,14 @@ Open **Plugins → Font**:
|
|
|
21
21
|
- **Interface font** and **Code font**: pick a preset, type any font name, or click **Load system fonts** to choose from fonts installed on this computer.
|
|
22
22
|
- **Conversation font size**: 10–22 px, kept in sync with **Settings → General**.
|
|
23
23
|
|
|
24
|
-
Changes apply immediately. Choose **Default** to go back to the DSH font.
|
|
24
|
+
Changes apply immediately and stay in sync with DSH settings. Choose **Default** to go back to the DSH font. **Reset all to defaults** resets the two font choices and conversation size on this page.
|
|
25
|
+
|
|
26
|
+
Code font applies to code blocks and inline code. Terminal font and size are independent; adjust them in **Settings → General**.
|
|
25
27
|
|
|
26
28
|
## Limits
|
|
27
29
|
|
|
28
30
|
- **Load system fonts** works only in Chromium-based browsers and asks for permission.
|
|
29
|
-
- Font choices
|
|
31
|
+
- Font choices and conversation size are saved by DSH. Fonts must be available on each computer where you open the GUI.
|
|
30
32
|
- No fonts are bundled; the chosen font must be installed on the computer.
|
|
31
33
|
|
|
32
34
|
## License
|
package/docs/README.zh-CN.md
CHANGED
|
@@ -21,12 +21,14 @@ dsh plugin --profile web add @klarkxy/dsh-font
|
|
|
21
21
|
- **界面字体**和**代码字体**:选预设、手动输入字体名,或点「读取系统字体」从本机已装字体中选择。
|
|
22
22
|
- **会话字号**:10–22 px,与「设置 → 通用」中的字号同步。
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
改动立即生效,并与 DSH 设置保持同步。选择「默认」即可恢复 DSH 原字体。「全部恢复默认」会重置本页的两项字体和会话字号。
|
|
25
|
+
|
|
26
|
+
代码字体作用于代码块和行内代码。终端字体与字号独立设置,请在「设置 → 通用」中调整。
|
|
25
27
|
|
|
26
28
|
## 限制
|
|
27
29
|
|
|
28
30
|
- 「读取系统字体」只在 Chromium 系浏览器可用,首次使用需要授权。
|
|
29
|
-
-
|
|
31
|
+
- 字体选择和会话字号由 DSH 保存;在不同电脑打开界面时,各台电脑都需要安装所选字体。
|
|
30
32
|
- 插件不附带字体文件,所选字体必须已安装在本机。
|
|
31
33
|
|
|
32
34
|
## 许可证
|
package/lib/client.inner.cjs
CHANGED
|
@@ -2,6 +2,231 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let _deepseek_ai_dsh_client_ui_primitives = require("@deepseek-ai/dsh-client-ui-primitives");
|
|
3
3
|
let react = require("react");
|
|
4
4
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
5
|
+
//#region src/contracts.ts
|
|
6
|
+
/** Browser-safe contracts for @klarkxy/dsh-font: settings shape, font presets,
|
|
7
|
+
* normalization, the CSS-variable application core, and the local store.
|
|
8
|
+
* No React, no cordis — every export runs in Node tests and the browser. */
|
|
9
|
+
const PLUGIN_NAME = "@klarkxy/dsh-font";
|
|
10
|
+
/** Legacy browser-local font choices, retained as the one-time migration backup.
|
|
11
|
+
* The active panel reads and writes the host's role-specific preferences. */
|
|
12
|
+
const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
13
|
+
function defaultFontSettings() {
|
|
14
|
+
return {
|
|
15
|
+
uiFont: "",
|
|
16
|
+
codeFont: ""
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** A raw font-family string is placed into a single CSS declaration via
|
|
20
|
+
* CSSStyleDeclaration.setProperty, which cannot escape the declaration — but
|
|
21
|
+
* trimming control characters and the CSS delimiters keeps pasted garbage from
|
|
22
|
+
* producing a silently-dead value. */
|
|
23
|
+
function normalizeFontFamily(value) {
|
|
24
|
+
if (typeof value !== "string") return "";
|
|
25
|
+
return value.replace(/[\u0000-\u001F\u007F;{}<>]/g, "").trim().slice(0, 300);
|
|
26
|
+
}
|
|
27
|
+
function parseFontSettings(raw) {
|
|
28
|
+
let source = raw;
|
|
29
|
+
if (typeof source === "string") try {
|
|
30
|
+
source = JSON.parse(source);
|
|
31
|
+
} catch {
|
|
32
|
+
return defaultFontSettings();
|
|
33
|
+
}
|
|
34
|
+
if (!source || typeof source !== "object" || Array.isArray(source)) return defaultFontSettings();
|
|
35
|
+
const record = source;
|
|
36
|
+
return {
|
|
37
|
+
uiFont: normalizeFontFamily(record.uiFont),
|
|
38
|
+
codeFont: normalizeFontFamily(record.codeFont)
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function serializeFontSettings(settings) {
|
|
42
|
+
return JSON.stringify({
|
|
43
|
+
uiFont: normalizeFontFamily(settings.uiFont),
|
|
44
|
+
codeFont: normalizeFontFamily(settings.codeFont)
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const UI_FONT_PRESETS = [
|
|
48
|
+
{
|
|
49
|
+
id: "yahei",
|
|
50
|
+
label: {
|
|
51
|
+
zh: "微软雅黑",
|
|
52
|
+
en: "Microsoft YaHei"
|
|
53
|
+
},
|
|
54
|
+
value: "\"Microsoft YaHei\", \"PingFang SC\", sans-serif"
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
id: "pingfang",
|
|
58
|
+
label: {
|
|
59
|
+
zh: "苹方",
|
|
60
|
+
en: "PingFang SC"
|
|
61
|
+
},
|
|
62
|
+
value: "\"PingFang SC\", -apple-system, sans-serif"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
id: "simsun",
|
|
66
|
+
label: {
|
|
67
|
+
zh: "宋体",
|
|
68
|
+
en: "SimSun"
|
|
69
|
+
},
|
|
70
|
+
value: "\"SimSun\", \"Songti SC\", serif"
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
id: "kaiti",
|
|
74
|
+
label: {
|
|
75
|
+
zh: "楷体",
|
|
76
|
+
en: "KaiTi"
|
|
77
|
+
},
|
|
78
|
+
value: "\"KaiTi\", \"Kaiti SC\", \"STKaiti\", serif"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
id: "source-han-sans",
|
|
82
|
+
label: {
|
|
83
|
+
zh: "思源黑体",
|
|
84
|
+
en: "Source Han Sans"
|
|
85
|
+
},
|
|
86
|
+
value: "\"Source Han Sans SC\", \"Noto Sans CJK SC\", \"Noto Sans SC\", sans-serif"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
id: "source-han-serif",
|
|
90
|
+
label: {
|
|
91
|
+
zh: "思源宋体",
|
|
92
|
+
en: "Source Han Serif"
|
|
93
|
+
},
|
|
94
|
+
value: "\"Source Han Serif SC\", \"Noto Serif CJK SC\", \"Noto Serif SC\", serif"
|
|
95
|
+
}
|
|
96
|
+
];
|
|
97
|
+
const CODE_FONT_PRESETS = [
|
|
98
|
+
{
|
|
99
|
+
id: "cascadia",
|
|
100
|
+
label: {
|
|
101
|
+
zh: "Cascadia",
|
|
102
|
+
en: "Cascadia"
|
|
103
|
+
},
|
|
104
|
+
value: "\"Cascadia Mono\", \"Cascadia Code\", monospace"
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
id: "jetbrains-mono",
|
|
108
|
+
label: {
|
|
109
|
+
zh: "JetBrains Mono",
|
|
110
|
+
en: "JetBrains Mono"
|
|
111
|
+
},
|
|
112
|
+
value: "\"JetBrains Mono\", monospace"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
id: "fira-code",
|
|
116
|
+
label: {
|
|
117
|
+
zh: "Fira Code",
|
|
118
|
+
en: "Fira Code"
|
|
119
|
+
},
|
|
120
|
+
value: "\"Fira Code\", monospace"
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
id: "consolas",
|
|
124
|
+
label: {
|
|
125
|
+
zh: "Consolas",
|
|
126
|
+
en: "Consolas"
|
|
127
|
+
},
|
|
128
|
+
value: "Consolas, monospace"
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
id: "sarasa",
|
|
132
|
+
label: {
|
|
133
|
+
zh: "更纱黑体",
|
|
134
|
+
en: "Sarasa Mono"
|
|
135
|
+
},
|
|
136
|
+
value: "\"Sarasa Mono SC\", \"Sarasa Term SC\", monospace"
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
id: "maple-mono",
|
|
140
|
+
label: {
|
|
141
|
+
zh: "Maple Mono",
|
|
142
|
+
en: "Maple Mono"
|
|
143
|
+
},
|
|
144
|
+
value: "\"Maple Mono NF CN\", \"Maple Mono\", monospace"
|
|
145
|
+
}
|
|
146
|
+
];
|
|
147
|
+
function presetFor(value, presets) {
|
|
148
|
+
const comparable = (stack) => stack.split(",").map((name) => name.trim().replace(/^["']|["']$/g, "")).join(",");
|
|
149
|
+
return presets.find((preset) => comparable(preset.value) === comparable(value));
|
|
150
|
+
}
|
|
151
|
+
/** Build the committed stack for a family picked from the system font list.
|
|
152
|
+
* The family name comes from the OS (Local Font Access API); quotes and
|
|
153
|
+
* backslashes are stripped so it can be safely re-quoted as one family. */
|
|
154
|
+
function fontStackFor(family, generic) {
|
|
155
|
+
const cleaned = normalizeFontFamily(family).replace(/["\\]/g, "");
|
|
156
|
+
if (!cleaned) return generic;
|
|
157
|
+
return `"${cleaned}", ${generic}`;
|
|
158
|
+
}
|
|
159
|
+
/** Distinct, normalized, family-sorted entries out of a queryLocalFonts()
|
|
160
|
+
* result (entries carry `family` and `fullName`; duplicates per style are
|
|
161
|
+
* expected). */
|
|
162
|
+
function uniqueSystemFonts(fonts) {
|
|
163
|
+
const byFamily = /* @__PURE__ */ new Map();
|
|
164
|
+
for (const item of fonts) {
|
|
165
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) continue;
|
|
166
|
+
const record = item;
|
|
167
|
+
const family = normalizeFontFamily(record.family).replace(/["\\]/g, "");
|
|
168
|
+
if (!family || byFamily.has(family)) continue;
|
|
169
|
+
const fullName = normalizeFontFamily(record.fullName).replace(/["\\]/g, "");
|
|
170
|
+
byFamily.set(family, fullName && fullName !== family && !fullName.startsWith(`${family} `) ? fullName : "");
|
|
171
|
+
}
|
|
172
|
+
return [...byFamily.entries()].map(([family, fullName]) => ({
|
|
173
|
+
family,
|
|
174
|
+
fullName
|
|
175
|
+
})).sort((a, b) => a.family.localeCompare(b.family));
|
|
176
|
+
}
|
|
177
|
+
/** Picker label: the UI locale's recognizable name leads, the alias stays in
|
|
178
|
+
* parentheses because the canonical family is still what a custom input needs. */
|
|
179
|
+
function systemFontLabel(item, locale) {
|
|
180
|
+
if (!item.fullName) return item.family;
|
|
181
|
+
return locale === "zh" ? `${item.fullName}(${item.family})` : `${item.family}(${item.fullName})`;
|
|
182
|
+
}
|
|
183
|
+
/** Browser-local persistence. A fully-default setting removes the key so a
|
|
184
|
+
* reset leaves no residue; storage failures (private mode, quota) degrade to
|
|
185
|
+
* in-memory behaviour rather than breaking the panel. */
|
|
186
|
+
function createFontSettingsStore(storage) {
|
|
187
|
+
let current = defaultFontSettings();
|
|
188
|
+
let pendingPersistence = false;
|
|
189
|
+
return {
|
|
190
|
+
load() {
|
|
191
|
+
if (storage && !pendingPersistence) try {
|
|
192
|
+
current = parseFontSettings(storage.getItem(STORAGE_KEY));
|
|
193
|
+
} catch {}
|
|
194
|
+
return { ...current };
|
|
195
|
+
},
|
|
196
|
+
save(next) {
|
|
197
|
+
current = parseFontSettings(next);
|
|
198
|
+
if (!storage) return;
|
|
199
|
+
pendingPersistence = true;
|
|
200
|
+
try {
|
|
201
|
+
if (!current.uiFont && !current.codeFont) storage.removeItem(STORAGE_KEY);
|
|
202
|
+
else storage.setItem(STORAGE_KEY, serializeFontSettings(current));
|
|
203
|
+
pendingPersistence = false;
|
|
204
|
+
} catch {}
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/preferences.ts
|
|
210
|
+
const MIGRATION_KEY = "klarkxy.dsh-font.host-preferences.v1";
|
|
211
|
+
/** Preserve the old selection as a backup, but never replay it over native
|
|
212
|
+
* preferences on subsequent loads. Each role migrates independently. */
|
|
213
|
+
function migrateFontPreferences(theme, store, storage) {
|
|
214
|
+
try {
|
|
215
|
+
if (storage?.getItem("klarkxy.dsh-font.host-preferences.v1")) return;
|
|
216
|
+
} catch {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const old = store.load();
|
|
220
|
+
const families = theme.getTheme().fontFamilies;
|
|
221
|
+
for (const [role, value] of [
|
|
222
|
+
["text", old.uiFont],
|
|
223
|
+
["code", old.codeFont],
|
|
224
|
+
["terminal", old.codeFont]
|
|
225
|
+
]) if (!families[role] && normalizeFontFamily(value)) theme.setFontFamily(role, value);
|
|
226
|
+
try {
|
|
227
|
+
storage?.setItem(MIGRATION_KEY, "1");
|
|
228
|
+
} catch {}
|
|
229
|
+
}
|
|
5
230
|
Object.values({
|
|
6
231
|
/** Surfaces, from the page up to the topmost nested card. */
|
|
7
232
|
surface: [
|
|
@@ -398,254 +623,6 @@ ${scope} {
|
|
|
398
623
|
`.replace(/^& (\.dsh-ui-[a-z0-9-]+)(?![+>~])/gm, (_match, className) => `&${className}, & ${className}`);
|
|
399
624
|
}
|
|
400
625
|
//#endregion
|
|
401
|
-
//#region src/contracts.ts
|
|
402
|
-
/** Browser-safe contracts for @klarkxy/dsh-font: settings shape, font presets,
|
|
403
|
-
* normalization, the CSS-variable application core, and the local store.
|
|
404
|
-
* No React, no cordis — every export runs in Node tests and the browser. */
|
|
405
|
-
const PLUGIN_NAME = "@klarkxy/dsh-font";
|
|
406
|
-
/** localStorage key. Fonts are a property of the rendering environment (each
|
|
407
|
-
* browser/machine has its own installed fonts), so they persist browser-local,
|
|
408
|
-
* exactly like the host's own GUI preferences (dsh-client-store, ui-theme
|
|
409
|
-
* notes). The conversation font size is NOT stored here: the official
|
|
410
|
-
* `ctx.theme.setFontSize()` entry persists it host-side already. */
|
|
411
|
-
const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
412
|
-
/** DSH rc.2 defines these font stacks on :root. Root inline overrides also
|
|
413
|
-
* reach derived Markdown, brand and terminal font tokens. */
|
|
414
|
-
const UI_FONT_VARIABLE = "--dsw-font-family";
|
|
415
|
-
const CODE_FONT_VARIABLE = "--ds-font-family-code";
|
|
416
|
-
function defaultFontSettings() {
|
|
417
|
-
return {
|
|
418
|
-
uiFont: "",
|
|
419
|
-
codeFont: ""
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
/** A raw font-family string is placed into a single CSS declaration via
|
|
423
|
-
* CSSStyleDeclaration.setProperty, which cannot escape the declaration — but
|
|
424
|
-
* trimming control characters and the CSS delimiters keeps pasted garbage from
|
|
425
|
-
* producing a silently-dead value. */
|
|
426
|
-
function normalizeFontFamily(value) {
|
|
427
|
-
if (typeof value !== "string") return "";
|
|
428
|
-
return value.replace(/[\u0000-\u001F\u007F;{}<>]/g, "").trim().slice(0, 300);
|
|
429
|
-
}
|
|
430
|
-
function parseFontSettings(raw) {
|
|
431
|
-
let source = raw;
|
|
432
|
-
if (typeof source === "string") try {
|
|
433
|
-
source = JSON.parse(source);
|
|
434
|
-
} catch {
|
|
435
|
-
return defaultFontSettings();
|
|
436
|
-
}
|
|
437
|
-
if (!source || typeof source !== "object" || Array.isArray(source)) return defaultFontSettings();
|
|
438
|
-
const record = source;
|
|
439
|
-
return {
|
|
440
|
-
uiFont: normalizeFontFamily(record.uiFont),
|
|
441
|
-
codeFont: normalizeFontFamily(record.codeFont)
|
|
442
|
-
};
|
|
443
|
-
}
|
|
444
|
-
function serializeFontSettings(settings) {
|
|
445
|
-
return JSON.stringify({
|
|
446
|
-
uiFont: normalizeFontFamily(settings.uiFont),
|
|
447
|
-
codeFont: normalizeFontFamily(settings.codeFont)
|
|
448
|
-
});
|
|
449
|
-
}
|
|
450
|
-
const UI_FONT_PRESETS = [
|
|
451
|
-
{
|
|
452
|
-
id: "yahei",
|
|
453
|
-
label: {
|
|
454
|
-
zh: "微软雅黑",
|
|
455
|
-
en: "Microsoft YaHei"
|
|
456
|
-
},
|
|
457
|
-
value: "\"Microsoft YaHei\", \"PingFang SC\", sans-serif"
|
|
458
|
-
},
|
|
459
|
-
{
|
|
460
|
-
id: "pingfang",
|
|
461
|
-
label: {
|
|
462
|
-
zh: "苹方",
|
|
463
|
-
en: "PingFang SC"
|
|
464
|
-
},
|
|
465
|
-
value: "\"PingFang SC\", -apple-system, sans-serif"
|
|
466
|
-
},
|
|
467
|
-
{
|
|
468
|
-
id: "simsun",
|
|
469
|
-
label: {
|
|
470
|
-
zh: "宋体",
|
|
471
|
-
en: "SimSun"
|
|
472
|
-
},
|
|
473
|
-
value: "\"SimSun\", \"Songti SC\", serif"
|
|
474
|
-
},
|
|
475
|
-
{
|
|
476
|
-
id: "kaiti",
|
|
477
|
-
label: {
|
|
478
|
-
zh: "楷体",
|
|
479
|
-
en: "KaiTi"
|
|
480
|
-
},
|
|
481
|
-
value: "\"KaiTi\", \"Kaiti SC\", \"STKaiti\", serif"
|
|
482
|
-
},
|
|
483
|
-
{
|
|
484
|
-
id: "source-han-sans",
|
|
485
|
-
label: {
|
|
486
|
-
zh: "思源黑体",
|
|
487
|
-
en: "Source Han Sans"
|
|
488
|
-
},
|
|
489
|
-
value: "\"Source Han Sans SC\", \"Noto Sans CJK SC\", \"Noto Sans SC\", sans-serif"
|
|
490
|
-
},
|
|
491
|
-
{
|
|
492
|
-
id: "source-han-serif",
|
|
493
|
-
label: {
|
|
494
|
-
zh: "思源宋体",
|
|
495
|
-
en: "Source Han Serif"
|
|
496
|
-
},
|
|
497
|
-
value: "\"Source Han Serif SC\", \"Noto Serif CJK SC\", \"Noto Serif SC\", serif"
|
|
498
|
-
}
|
|
499
|
-
];
|
|
500
|
-
const CODE_FONT_PRESETS = [
|
|
501
|
-
{
|
|
502
|
-
id: "cascadia",
|
|
503
|
-
label: {
|
|
504
|
-
zh: "Cascadia",
|
|
505
|
-
en: "Cascadia"
|
|
506
|
-
},
|
|
507
|
-
value: "\"Cascadia Mono\", \"Cascadia Code\", monospace"
|
|
508
|
-
},
|
|
509
|
-
{
|
|
510
|
-
id: "jetbrains-mono",
|
|
511
|
-
label: {
|
|
512
|
-
zh: "JetBrains Mono",
|
|
513
|
-
en: "JetBrains Mono"
|
|
514
|
-
},
|
|
515
|
-
value: "\"JetBrains Mono\", monospace"
|
|
516
|
-
},
|
|
517
|
-
{
|
|
518
|
-
id: "fira-code",
|
|
519
|
-
label: {
|
|
520
|
-
zh: "Fira Code",
|
|
521
|
-
en: "Fira Code"
|
|
522
|
-
},
|
|
523
|
-
value: "\"Fira Code\", monospace"
|
|
524
|
-
},
|
|
525
|
-
{
|
|
526
|
-
id: "consolas",
|
|
527
|
-
label: {
|
|
528
|
-
zh: "Consolas",
|
|
529
|
-
en: "Consolas"
|
|
530
|
-
},
|
|
531
|
-
value: "Consolas, monospace"
|
|
532
|
-
},
|
|
533
|
-
{
|
|
534
|
-
id: "sarasa",
|
|
535
|
-
label: {
|
|
536
|
-
zh: "更纱黑体",
|
|
537
|
-
en: "Sarasa Mono"
|
|
538
|
-
},
|
|
539
|
-
value: "\"Sarasa Mono SC\", \"Sarasa Term SC\", monospace"
|
|
540
|
-
},
|
|
541
|
-
{
|
|
542
|
-
id: "maple-mono",
|
|
543
|
-
label: {
|
|
544
|
-
zh: "Maple Mono",
|
|
545
|
-
en: "Maple Mono"
|
|
546
|
-
},
|
|
547
|
-
value: "\"Maple Mono NF CN\", \"Maple Mono\", monospace"
|
|
548
|
-
}
|
|
549
|
-
];
|
|
550
|
-
function presetFor(value, presets) {
|
|
551
|
-
return presets.find((preset) => preset.value === value);
|
|
552
|
-
}
|
|
553
|
-
/** Build the committed stack for a family picked from the system font list.
|
|
554
|
-
* The family name comes from the OS (Local Font Access API); quotes and
|
|
555
|
-
* backslashes are stripped so it can be safely re-quoted as one family. */
|
|
556
|
-
function fontStackFor(family, generic) {
|
|
557
|
-
const cleaned = normalizeFontFamily(family).replace(/["\\]/g, "");
|
|
558
|
-
if (!cleaned) return generic;
|
|
559
|
-
return `"${cleaned}", ${generic}`;
|
|
560
|
-
}
|
|
561
|
-
/** Distinct, normalized, family-sorted entries out of a queryLocalFonts()
|
|
562
|
-
* result (entries carry `family` and `fullName`; duplicates per style are
|
|
563
|
-
* expected). */
|
|
564
|
-
function uniqueSystemFonts(fonts) {
|
|
565
|
-
const byFamily = /* @__PURE__ */ new Map();
|
|
566
|
-
for (const item of fonts) {
|
|
567
|
-
if (!item || typeof item !== "object" || Array.isArray(item)) continue;
|
|
568
|
-
const record = item;
|
|
569
|
-
const family = normalizeFontFamily(record.family).replace(/["\\]/g, "");
|
|
570
|
-
if (!family || byFamily.has(family)) continue;
|
|
571
|
-
const fullName = normalizeFontFamily(record.fullName).replace(/["\\]/g, "");
|
|
572
|
-
byFamily.set(family, fullName && fullName !== family && !fullName.startsWith(`${family} `) ? fullName : "");
|
|
573
|
-
}
|
|
574
|
-
return [...byFamily.entries()].map(([family, fullName]) => ({
|
|
575
|
-
family,
|
|
576
|
-
fullName
|
|
577
|
-
})).sort((a, b) => a.family.localeCompare(b.family));
|
|
578
|
-
}
|
|
579
|
-
/** Picker label: the UI locale's recognizable name leads, the alias stays in
|
|
580
|
-
* parentheses because the canonical family is still what a custom input needs. */
|
|
581
|
-
function systemFontLabel(item, locale) {
|
|
582
|
-
if (!item.fullName) return item.family;
|
|
583
|
-
return locale === "zh" ? `${item.fullName}(${item.family})` : `${item.family}(${item.fullName})`;
|
|
584
|
-
}
|
|
585
|
-
/** Root inline overrides preserve the existing local persistence/unload
|
|
586
|
-
* boundary. Save the underlying value (including !important) on first write
|
|
587
|
-
* or when the host replaces a value, and never retract somebody else's write. */
|
|
588
|
-
function createFontSettingsOverride(style) {
|
|
589
|
-
const owned = /* @__PURE__ */ new Map();
|
|
590
|
-
function release(name) {
|
|
591
|
-
const entry = owned.get(name);
|
|
592
|
-
if (!entry) return;
|
|
593
|
-
if (style.getPropertyValue(name) === entry.value && style.getPropertyPriority(name) === "") {
|
|
594
|
-
if (entry.previous) style.setProperty(name, entry.previous, entry.priority);
|
|
595
|
-
else style.removeProperty(name);
|
|
596
|
-
}
|
|
597
|
-
owned.delete(name);
|
|
598
|
-
}
|
|
599
|
-
return {
|
|
600
|
-
apply(settings) {
|
|
601
|
-
for (const [name, value] of [[UI_FONT_VARIABLE, settings.uiFont], [CODE_FONT_VARIABLE, settings.codeFont]]) {
|
|
602
|
-
if (!value) {
|
|
603
|
-
release(name);
|
|
604
|
-
continue;
|
|
605
|
-
}
|
|
606
|
-
const current = style.getPropertyValue(name);
|
|
607
|
-
const priority = style.getPropertyPriority(name);
|
|
608
|
-
const entry = owned.get(name);
|
|
609
|
-
if (!entry || current !== entry.value || priority !== "") owned.set(name, {
|
|
610
|
-
previous: current,
|
|
611
|
-
priority,
|
|
612
|
-
value: ""
|
|
613
|
-
});
|
|
614
|
-
style.setProperty(name, value);
|
|
615
|
-
owned.get(name).value = style.getPropertyValue(name);
|
|
616
|
-
}
|
|
617
|
-
},
|
|
618
|
-
dispose() {
|
|
619
|
-
for (const name of [...owned.keys()]) release(name);
|
|
620
|
-
}
|
|
621
|
-
};
|
|
622
|
-
}
|
|
623
|
-
/** Browser-local persistence. A fully-default setting removes the key so a
|
|
624
|
-
* reset leaves no residue; storage failures (private mode, quota) degrade to
|
|
625
|
-
* in-memory behaviour rather than breaking the panel. */
|
|
626
|
-
function createFontSettingsStore(storage) {
|
|
627
|
-
let current = defaultFontSettings();
|
|
628
|
-
let pendingPersistence = false;
|
|
629
|
-
return {
|
|
630
|
-
load() {
|
|
631
|
-
if (storage && !pendingPersistence) try {
|
|
632
|
-
current = parseFontSettings(storage.getItem(STORAGE_KEY));
|
|
633
|
-
} catch {}
|
|
634
|
-
return { ...current };
|
|
635
|
-
},
|
|
636
|
-
save(next) {
|
|
637
|
-
current = parseFontSettings(next);
|
|
638
|
-
if (!storage) return;
|
|
639
|
-
pendingPersistence = true;
|
|
640
|
-
try {
|
|
641
|
-
if (!current.uiFont && !current.codeFont) storage.removeItem(STORAGE_KEY);
|
|
642
|
-
else storage.setItem(STORAGE_KEY, serializeFontSettings(current));
|
|
643
|
-
pendingPersistence = false;
|
|
644
|
-
} catch {}
|
|
645
|
-
}
|
|
646
|
-
};
|
|
647
|
-
}
|
|
648
|
-
//#endregion
|
|
649
626
|
//#region src/client.tsx
|
|
650
627
|
const name = "dsh-font-client";
|
|
651
628
|
const inject = [
|
|
@@ -664,7 +641,7 @@ const COPY = {
|
|
|
664
641
|
customUiPlaceholder: "例如 \"Microsoft YaHei\", sans-serif",
|
|
665
642
|
customCodePlaceholder: "例如 \"JetBrains Mono\", monospace",
|
|
666
643
|
uiHelp: "作用于整个界面的文字。自定义时输入完整的 font-family 字体栈,留空恢复默认。",
|
|
667
|
-
codeHelp: "
|
|
644
|
+
codeHelp: "作用于代码块与行内代码。终端字体和字号可在「设置 → 通用」中单独调整。",
|
|
668
645
|
sizeHelp: "与「设置 → 通用」中的字号同步,范围 10–22。",
|
|
669
646
|
loadFonts: "读取系统字体",
|
|
670
647
|
reloadFonts: "重新读取系统字体",
|
|
@@ -687,7 +664,7 @@ const COPY = {
|
|
|
687
664
|
customUiPlaceholder: "e.g. \"Microsoft YaHei\", sans-serif",
|
|
688
665
|
customCodePlaceholder: "e.g. \"JetBrains Mono\", monospace",
|
|
689
666
|
uiHelp: "Applies to all interface text. For a custom value enter a full font-family stack; empty restores the default.",
|
|
690
|
-
codeHelp: "
|
|
667
|
+
codeHelp: "Applies to code blocks and inline code. Adjust terminal font and size separately in Settings → General.",
|
|
691
668
|
sizeHelp: "Synced with Settings → General; 10–22 px.",
|
|
692
669
|
loadFonts: "Load system fonts",
|
|
693
670
|
reloadFonts: "Reload system fonts",
|
|
@@ -720,7 +697,7 @@ function useLocale(client) {
|
|
|
720
697
|
return (0, react.useSyncExternalStore)((fn) => client.locale.subscribe(fn), getSnapshot, getSnapshot);
|
|
721
698
|
}
|
|
722
699
|
function useFontSize(client) {
|
|
723
|
-
return (0, react.useSyncExternalStore)((fn) => client.on("theme/change", () => fn()), () => client.theme.getTheme().
|
|
700
|
+
return (0, react.useSyncExternalStore)((fn) => client.on("theme/change", () => fn()), () => client.theme.getTheme().fontSizes.text, () => client.theme.getTheme().fontSizes.text);
|
|
724
701
|
}
|
|
725
702
|
function presetMode(value, presets) {
|
|
726
703
|
return presetFor(value, presets)?.id ?? (value ? "custom" : "");
|
|
@@ -805,7 +782,11 @@ function FontField(props) {
|
|
|
805
782
|
function FontSettingsPanel(props) {
|
|
806
783
|
const locale = useLocale(props.client);
|
|
807
784
|
const text = COPY[locale];
|
|
808
|
-
const
|
|
785
|
+
const families = (0, react.useSyncExternalStore)((fn) => props.client.on("theme/change", () => fn()), () => props.client.theme.getTheme().fontFamilies, () => props.client.theme.getTheme().fontFamilies);
|
|
786
|
+
const settings = {
|
|
787
|
+
uiFont: families.text,
|
|
788
|
+
codeFont: families.code
|
|
789
|
+
};
|
|
809
790
|
const fontSize = useFontSize(props.client);
|
|
810
791
|
const [fontList, setFontList] = (0, react.useState)({
|
|
811
792
|
state: "idle",
|
|
@@ -839,13 +820,8 @@ function FontSettingsPanel(props) {
|
|
|
839
820
|
}
|
|
840
821
|
}
|
|
841
822
|
function update(patch) {
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
codeFont: normalizeFontFamily(patch.codeFont ?? settings.codeFont)
|
|
845
|
-
};
|
|
846
|
-
props.store.save(next);
|
|
847
|
-
props.applySettings(next);
|
|
848
|
-
setSettings(next);
|
|
823
|
+
if (patch.uiFont !== void 0) props.client.theme.setFontFamily("text", normalizeFontFamily(patch.uiFont));
|
|
824
|
+
if (patch.codeFont !== void 0) props.client.theme.setFontFamily("code", normalizeFontFamily(patch.codeFont));
|
|
849
825
|
}
|
|
850
826
|
const uiName = presetFor(settings.uiFont, UI_FONT_PRESETS)?.label[locale] ?? (settings.uiFont || text.defaultName);
|
|
851
827
|
const codeName = presetFor(settings.codeFont, CODE_FONT_PRESETS)?.label[locale] ?? (settings.codeFont || text.defaultName);
|
|
@@ -901,7 +877,7 @@ function FontSettingsPanel(props) {
|
|
|
901
877
|
onChange: (event) => {
|
|
902
878
|
const px = Number(event.target.value);
|
|
903
879
|
if (Number.isInteger(px) && px >= 10 && px <= 22 && px !== fontSize) try {
|
|
904
|
-
props.client.theme.setFontSize(px);
|
|
880
|
+
props.client.theme.setFontSize("text", px);
|
|
905
881
|
} catch {}
|
|
906
882
|
},
|
|
907
883
|
children: sizes.map((px) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("option", {
|
|
@@ -933,7 +909,7 @@ function FontSettingsPanel(props) {
|
|
|
933
909
|
uiFont: "",
|
|
934
910
|
codeFont: ""
|
|
935
911
|
});
|
|
936
|
-
if (fontSize !== 14) props.client.theme.setFontSize(14);
|
|
912
|
+
if (fontSize !== 14) props.client.theme.setFontSize("text", 14);
|
|
937
913
|
},
|
|
938
914
|
children: text.reset
|
|
939
915
|
})]
|
|
@@ -959,7 +935,7 @@ function FontSettingsPanel(props) {
|
|
|
959
935
|
function apply(ctx) {
|
|
960
936
|
const client = ctx;
|
|
961
937
|
const store = createFontSettingsStore(readStorage());
|
|
962
|
-
|
|
938
|
+
migrateFontPreferences(client.theme, store, readStorage());
|
|
963
939
|
ctx.effect(() => {
|
|
964
940
|
if (typeof document === "undefined") return () => {};
|
|
965
941
|
const style = document.createElement("style");
|
|
@@ -968,23 +944,11 @@ function apply(ctx) {
|
|
|
968
944
|
document.head.appendChild(style);
|
|
969
945
|
return () => style.remove();
|
|
970
946
|
}, "font.styles");
|
|
971
|
-
ctx.effect(() => {
|
|
972
|
-
if (typeof document === "undefined") return () => {};
|
|
973
|
-
const override = createFontSettingsOverride(document.documentElement.style);
|
|
974
|
-
applySettings = (next) => override.apply(next);
|
|
975
|
-
applySettings(store.load());
|
|
976
|
-
return () => {
|
|
977
|
-
override.dispose();
|
|
978
|
-
applySettings = () => {};
|
|
979
|
-
};
|
|
980
|
-
}, "font.apply");
|
|
981
947
|
ctx.effect(() => client.slots.inject("plugins.bundle.config", () => client.slots.register({
|
|
982
948
|
name: "plugins.bundle.config",
|
|
983
949
|
key: PLUGIN_NAME
|
|
984
950
|
}, (slotProps) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FontSettingsPanel, {
|
|
985
951
|
client,
|
|
986
|
-
store,
|
|
987
|
-
applySettings: (next) => applySettings(next),
|
|
988
952
|
view: slotProps?.view
|
|
989
953
|
}))), "font.settings");
|
|
990
954
|
}
|