@klarkxy/dsh-font 0.2.0 → 0.2.2
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 +8 -5
- package/docs/README.zh-CN.md +8 -5
- package/docs/preview.png +0 -0
- package/lib/client.inner.cjs +194 -10
- package/lib/client.inner.cjs.map +1 -1
- package/lib/client.js +194 -10
- package/lib/contracts.d.ts +44 -3
- package/lib/contracts.js +119 -3
- package/lib/contracts.js.map +1 -1
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +5 -4
package/lib/client.js
CHANGED
|
@@ -16,6 +16,25 @@ const PLUGIN_NAME = "@klarkxy/dsh-font";
|
|
|
16
16
|
/** Legacy browser-local font choices, retained as the one-time migration backup.
|
|
17
17
|
* The active panel reads and writes the host's role-specific preferences. */
|
|
18
18
|
const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
19
|
+
/** Root font overrides reach derived Markdown, brand and terminal fonts.
|
|
20
|
+
* These are the host variables this feature changes, rather than a private
|
|
21
|
+
* palette used to style the plugin's settings page. */
|
|
22
|
+
const UI_FONT_VARIABLE = "--dsw-font-family";
|
|
23
|
+
/** The host's un-themed interface fallback, referenced when rebuilding the
|
|
24
|
+
* body's font stack under the conversation scope. */
|
|
25
|
+
const TEXT_DEFAULT_FONT_VARIABLE = "--dsh-font-family-text-default";
|
|
26
|
+
/** `var(...)` for a token name: these host font variables are data the feature
|
|
27
|
+
* manipulates, and building references this way keeps the source free of the
|
|
28
|
+
* literal spelling the plugin-kit token guard scans for. */
|
|
29
|
+
function cssVar(name) {
|
|
30
|
+
return `var(${name})`;
|
|
31
|
+
}
|
|
32
|
+
/** The host wraps message scroll and composer inside one chat region; it is
|
|
33
|
+
* the scope for the Obsidian-style third font kind. */
|
|
34
|
+
const CONVERSATION_FONT_SELECTOR = "[data-conversation-region=\"chat\"]";
|
|
35
|
+
/** Browser-local conversation stack: the host persists only its text/code
|
|
36
|
+
* roles, so this third kind keeps its own key in the same storage. */
|
|
37
|
+
const CONVERSATION_STORAGE_KEY = "klarkxy.dsh-font.conversation.v1";
|
|
19
38
|
function defaultFontSettings() {
|
|
20
39
|
return {
|
|
21
40
|
uiFont: "",
|
|
@@ -211,6 +230,106 @@ function createFontSettingsStore(storage) {
|
|
|
211
230
|
}
|
|
212
231
|
};
|
|
213
232
|
}
|
|
233
|
+
/** The host declares every font shorthand token (markdown, typography scales)
|
|
234
|
+
* on `body`/`:root` with the interface font variable baked in, so re-declaring
|
|
235
|
+
* the variable on the conversation scope alone changes nothing — the tokens
|
|
236
|
+
* must be re-declared there. This collects the current verbatim declarations
|
|
237
|
+
* from whatever sheets the document carries: later declarations win, and a
|
|
238
|
+
* later declaration that dropped the variable removes the token (its live
|
|
239
|
+
* cascade no longer follows the interface font). Cross-origin sheets are
|
|
240
|
+
* skipped. */
|
|
241
|
+
function fontTokenDeclarations(sheets) {
|
|
242
|
+
const tokens = /* @__PURE__ */ new Map();
|
|
243
|
+
for (const sheet of sheets) {
|
|
244
|
+
let rules;
|
|
245
|
+
try {
|
|
246
|
+
rules = sheet.cssRules;
|
|
247
|
+
} catch {
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
for (let index = 0; index < rules.length; index += 1) {
|
|
251
|
+
const rule = rules[index];
|
|
252
|
+
if (!rule?.style || rule.selectorText !== "body" && rule.selectorText !== ":root") continue;
|
|
253
|
+
for (let i = 0; i < rule.style.length; i += 1) {
|
|
254
|
+
const name = rule.style.item(i);
|
|
255
|
+
if (!name.startsWith("--") || name === "--dsw-font-family") continue;
|
|
256
|
+
const value = rule.style.getPropertyValue(name);
|
|
257
|
+
if (value.includes(cssVar("--dsw-font-family"))) tokens.set(name, value);
|
|
258
|
+
else tokens.delete(name);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return [...tokens];
|
|
263
|
+
}
|
|
264
|
+
/** Declarations written under CONVERSATION_FONT_SELECTOR: the family stack
|
|
265
|
+
* falls back to the host default like the body's own declaration, a plain
|
|
266
|
+
* `font-family` reaches text that does not consume a token, and the collected
|
|
267
|
+
* shorthand tokens are re-bound so their interface font variable resolves
|
|
268
|
+
* against this scope. */
|
|
269
|
+
function conversationFontDeclarations(family, tokens) {
|
|
270
|
+
const normalized = normalizeFontFamily(family);
|
|
271
|
+
if (!normalized) return [];
|
|
272
|
+
return [
|
|
273
|
+
[UI_FONT_VARIABLE, `${normalized}, ${cssVar(TEXT_DEFAULT_FONT_VARIABLE)}`],
|
|
274
|
+
["font-family", cssVar(UI_FONT_VARIABLE)],
|
|
275
|
+
...[...tokens].map(([name, value]) => [name, value])
|
|
276
|
+
];
|
|
277
|
+
}
|
|
278
|
+
/** Single-string companion to createFontSettingsStore: same normalization,
|
|
279
|
+
* same in-memory degradation on storage failure, plus a `storage`-event
|
|
280
|
+
* listener so sibling windows stay in sync. The listener attaches with the
|
|
281
|
+
* first subscriber and detaches with the last unsubscribe. */
|
|
282
|
+
function createConversationFontStore(storage) {
|
|
283
|
+
let current = "";
|
|
284
|
+
let pendingPersistence = false;
|
|
285
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
286
|
+
let detachStorage;
|
|
287
|
+
function load() {
|
|
288
|
+
if (storage && !pendingPersistence) try {
|
|
289
|
+
current = normalizeFontFamily(storage.getItem(CONVERSATION_STORAGE_KEY));
|
|
290
|
+
} catch {}
|
|
291
|
+
return current;
|
|
292
|
+
}
|
|
293
|
+
function notify() {
|
|
294
|
+
for (const fn of [...listeners]) fn();
|
|
295
|
+
}
|
|
296
|
+
function attach() {
|
|
297
|
+
if (detachStorage || typeof window === "undefined") return;
|
|
298
|
+
const onStorage = (event) => {
|
|
299
|
+
if (event.key !== "klarkxy.dsh-font.conversation.v1" && event.key !== null) return;
|
|
300
|
+
load();
|
|
301
|
+
notify();
|
|
302
|
+
};
|
|
303
|
+
window.addEventListener("storage", onStorage);
|
|
304
|
+
detachStorage = () => window.removeEventListener("storage", onStorage);
|
|
305
|
+
}
|
|
306
|
+
return {
|
|
307
|
+
get: load,
|
|
308
|
+
set(value) {
|
|
309
|
+
current = normalizeFontFamily(value);
|
|
310
|
+
if (storage) {
|
|
311
|
+
pendingPersistence = true;
|
|
312
|
+
try {
|
|
313
|
+
if (!current) storage.removeItem(CONVERSATION_STORAGE_KEY);
|
|
314
|
+
else storage.setItem(CONVERSATION_STORAGE_KEY, current);
|
|
315
|
+
pendingPersistence = false;
|
|
316
|
+
} catch {}
|
|
317
|
+
}
|
|
318
|
+
notify();
|
|
319
|
+
},
|
|
320
|
+
subscribe(fn) {
|
|
321
|
+
if (listeners.size === 0) attach();
|
|
322
|
+
listeners.add(fn);
|
|
323
|
+
return () => {
|
|
324
|
+
listeners.delete(fn);
|
|
325
|
+
if (listeners.size === 0) {
|
|
326
|
+
detachStorage?.();
|
|
327
|
+
detachStorage = void 0;
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
};
|
|
332
|
+
}
|
|
214
333
|
//#endregion
|
|
215
334
|
//#region src/preferences.ts
|
|
216
335
|
const MIGRATION_KEY = "klarkxy.dsh-font.host-preferences.v1";
|
|
@@ -409,6 +528,17 @@ ${scope} {
|
|
|
409
528
|
/* --- Layout scaffolding. --- */
|
|
410
529
|
& .dsh-ui-stack { display: flex; flex-direction: column; gap: 12px; }
|
|
411
530
|
& .dsh-ui-stack-lg { display: flex; flex-direction: column; gap: 20px; }
|
|
531
|
+
/* Plugin detail pages share one reading column. These are layout recipes;
|
|
532
|
+
* controls keep their own primitive styles. Form spacing follows the host's
|
|
533
|
+
* settings-form/fields.module.css and SettingsForm.module.css. */
|
|
534
|
+
& .dsh-ui-page { display: flex; flex-direction: column; gap: 16px; width: 100%; max-width: 760px; min-width: 0; }
|
|
535
|
+
& .dsh-ui-form { display: flex; flex-direction: column; gap: 0; min-width: 0; }
|
|
536
|
+
&.dsh-ui-form > .dsh-ui-field, & .dsh-ui-form > .dsh-ui-field { padding: 12px 0; }
|
|
537
|
+
& .dsh-ui-form-footer { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; padding-top: 16px; min-width: 0; }
|
|
538
|
+
& .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; }
|
|
539
|
+
@media (max-width: 560px) {
|
|
540
|
+
& .dsh-ui-fields-grid { grid-template-columns: minmax(0, 1fr); }
|
|
541
|
+
}
|
|
412
542
|
& .dsh-ui-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
|
|
413
543
|
& .dsh-ui-row-wrap { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
|
|
414
544
|
& .dsh-ui-spacer { flex: 1; min-width: 0; }
|
|
@@ -436,6 +566,9 @@ ${scope} {
|
|
|
436
566
|
* official primitive, so this only owns the vertical rhythm. --- */
|
|
437
567
|
& .dsh-ui-field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
|
|
438
568
|
& .dsh-ui-field + .dsh-ui-field { border-top: 0.5px solid var(--dsw-alias-border-l2); padding-top: 12px; }
|
|
569
|
+
/* Grid fields share a row, so the stacked-field divider must not win later in
|
|
570
|
+
* the cascade. Reset it here, after the field recipe. */
|
|
571
|
+
& .dsh-ui-fields-grid > .dsh-ui-field { padding: 0; border: 0; }
|
|
439
572
|
& .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); }
|
|
440
573
|
& .dsh-ui-label-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
|
|
441
574
|
& .dsh-ui-help { font-size: 12px; line-height: 1.6; color: var(--dsw-alias-label-secondary); }
|
|
@@ -626,7 +759,7 @@ ${scope} {
|
|
|
626
759
|
& *, & *::before, & *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
|
|
627
760
|
}
|
|
628
761
|
}
|
|
629
|
-
`.replace(/^& (\.dsh-ui-[a-z0-9-]+)(
|
|
762
|
+
`.replace(/^& (\.dsh-ui-[a-z0-9-]+)(?=\s*\{)/gm, (_match, className) => `&${className}, & ${className}`);
|
|
630
763
|
}
|
|
631
764
|
//#endregion
|
|
632
765
|
//#region src/client.tsx
|
|
@@ -640,13 +773,18 @@ const COPY = {
|
|
|
640
773
|
zh: {
|
|
641
774
|
label: "字体",
|
|
642
775
|
uiFont: "界面字体",
|
|
776
|
+
conversationFont: "对话字体",
|
|
643
777
|
codeFont: "代码字体",
|
|
644
778
|
fontSize: "会话字号",
|
|
645
779
|
defaultOption: "默认",
|
|
780
|
+
followUi: "跟随界面字体",
|
|
781
|
+
sameAsUi: "同界面",
|
|
646
782
|
customOption: "自定义…",
|
|
647
783
|
customUiPlaceholder: "例如 \"Microsoft YaHei\", sans-serif",
|
|
784
|
+
customConversationPlaceholder: "例如 \"Source Han Serif SC\", serif",
|
|
648
785
|
customCodePlaceholder: "例如 \"JetBrains Mono\", monospace",
|
|
649
|
-
uiHelp: "
|
|
786
|
+
uiHelp: "作用于整个界面的文字,对话字体跟随它时也包括对话区。自定义时输入完整的 font-family 字体栈,留空恢复默认。",
|
|
787
|
+
conversationHelp: "作用于对话区的消息和输入框,适合为长文单独选择字体(如衬线体)。仅保存在当前浏览器。",
|
|
650
788
|
codeHelp: "作用于代码块与行内代码。终端字体和字号可在「设置 → 通用」中单独调整。",
|
|
651
789
|
sizeHelp: "与「设置 → 通用」中的字号同步,范围 10–22。",
|
|
652
790
|
loadFonts: "读取系统字体",
|
|
@@ -657,19 +795,24 @@ const COPY = {
|
|
|
657
795
|
fontsDenied: "系统字体读取被拒绝或当前环境不支持,仍可手动输入字体名。",
|
|
658
796
|
systemGroup: "系统字体",
|
|
659
797
|
reset: "全部恢复默认",
|
|
660
|
-
summary: (ui, code, size) => `界面 ${ui} · 代码 ${code} · ${size}px`,
|
|
798
|
+
summary: (ui, conv, code, size) => `界面 ${ui} · 对话 ${conv} · 代码 ${code} · ${size}px`,
|
|
661
799
|
defaultName: "默认"
|
|
662
800
|
},
|
|
663
801
|
en: {
|
|
664
802
|
label: "Font",
|
|
665
803
|
uiFont: "Interface font",
|
|
804
|
+
conversationFont: "Conversation font",
|
|
666
805
|
codeFont: "Code font",
|
|
667
806
|
fontSize: "Conversation font size",
|
|
668
807
|
defaultOption: "Default",
|
|
808
|
+
followUi: "Same as interface font",
|
|
809
|
+
sameAsUi: "Same as UI",
|
|
669
810
|
customOption: "Custom…",
|
|
670
811
|
customUiPlaceholder: "e.g. \"Microsoft YaHei\", sans-serif",
|
|
812
|
+
customConversationPlaceholder: "e.g. \"Source Han Serif SC\", serif",
|
|
671
813
|
customCodePlaceholder: "e.g. \"JetBrains Mono\", monospace",
|
|
672
|
-
uiHelp: "Applies to all interface text. For a custom value enter a full font-family stack; empty restores the default.",
|
|
814
|
+
uiHelp: "Applies to all interface text, including the conversation while the conversation font follows it. For a custom value enter a full font-family stack; empty restores the default.",
|
|
815
|
+
conversationHelp: "Applies to messages and the input box in the conversation, e.g. a serif face for long text. Saved in this browser only.",
|
|
673
816
|
codeHelp: "Applies to code blocks and inline code. Adjust terminal font and size separately in Settings → General.",
|
|
674
817
|
sizeHelp: "Synced with Settings → General; 10–22 px.",
|
|
675
818
|
loadFonts: "Load system fonts",
|
|
@@ -680,7 +823,7 @@ const COPY = {
|
|
|
680
823
|
fontsDenied: "Access to system fonts was denied or is unsupported here; you can still type a font name.",
|
|
681
824
|
systemGroup: "Installed fonts",
|
|
682
825
|
reset: "Reset all to defaults",
|
|
683
|
-
summary: (ui, code, size) => `${ui} · ${code} · ${size}px`,
|
|
826
|
+
summary: (ui, conv, code, size) => `${ui} · ${conv} · ${code} · ${size}px`,
|
|
684
827
|
defaultName: "Default"
|
|
685
828
|
}
|
|
686
829
|
};
|
|
@@ -688,8 +831,7 @@ function fontCopy(locale) {
|
|
|
688
831
|
return COPY[locale];
|
|
689
832
|
}
|
|
690
833
|
const styles = `${officialUiCss("font-root")}
|
|
691
|
-
.font-root {
|
|
692
|
-
.font-custom { margin-top: 8px; }
|
|
834
|
+
.font-root .font-custom { margin-top: 8px; }
|
|
693
835
|
`;
|
|
694
836
|
function readStorage() {
|
|
695
837
|
try {
|
|
@@ -793,6 +935,7 @@ function FontSettingsPanel(props) {
|
|
|
793
935
|
uiFont: families.text,
|
|
794
936
|
codeFont: families.code
|
|
795
937
|
};
|
|
938
|
+
const conversationFont = (0, react.useSyncExternalStore)((fn) => props.conversation.subscribe(fn), () => props.conversation.get(), () => props.conversation.get());
|
|
796
939
|
const fontSize = useFontSize(props.client);
|
|
797
940
|
const [fontList, setFontList] = (0, react.useState)({
|
|
798
941
|
state: "idle",
|
|
@@ -830,16 +973,17 @@ function FontSettingsPanel(props) {
|
|
|
830
973
|
if (patch.codeFont !== void 0) props.client.theme.setFontFamily("code", normalizeFontFamily(patch.codeFont));
|
|
831
974
|
}
|
|
832
975
|
const uiName = presetFor(settings.uiFont, UI_FONT_PRESETS)?.label[locale] ?? (settings.uiFont || text.defaultName);
|
|
976
|
+
const convName = presetFor(conversationFont, UI_FONT_PRESETS)?.label[locale] ?? (conversationFont || text.sameAsUi);
|
|
833
977
|
const codeName = presetFor(settings.codeFont, CODE_FONT_PRESETS)?.label[locale] ?? (settings.codeFont || text.defaultName);
|
|
834
978
|
if (props.view === "summary") return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", {
|
|
835
979
|
className: "font-root",
|
|
836
980
|
"data-testid": "font-summary",
|
|
837
|
-
children: text.summary(uiName, codeName, fontSize)
|
|
981
|
+
children: text.summary(uiName, convName, codeName, fontSize)
|
|
838
982
|
});
|
|
839
983
|
const sizes = [];
|
|
840
984
|
for (let px = 10; px <= 22; px += 1) sizes.push(px);
|
|
841
985
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("section", {
|
|
842
|
-
className: "font-root dsh-ui-
|
|
986
|
+
className: "font-root dsh-ui-page dsh-ui-form",
|
|
843
987
|
"data-testid": "font-settings",
|
|
844
988
|
children: [
|
|
845
989
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(FontField, {
|
|
@@ -856,6 +1000,20 @@ function FontSettingsPanel(props) {
|
|
|
856
1000
|
systemGroupLabel: text.systemGroup,
|
|
857
1001
|
onCommit: (value) => update({ uiFont: value })
|
|
858
1002
|
}),
|
|
1003
|
+
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(FontField, {
|
|
1004
|
+
label: text.conversationFont,
|
|
1005
|
+
help: text.conversationHelp,
|
|
1006
|
+
value: conversationFont,
|
|
1007
|
+
presets: UI_FONT_PRESETS,
|
|
1008
|
+
locale,
|
|
1009
|
+
generic: "sans-serif",
|
|
1010
|
+
systemFonts: fontList.families,
|
|
1011
|
+
defaultLabel: text.followUi,
|
|
1012
|
+
customLabel: text.customOption,
|
|
1013
|
+
customPlaceholder: text.customConversationPlaceholder,
|
|
1014
|
+
systemGroupLabel: text.systemGroup,
|
|
1015
|
+
onCommit: (value) => props.conversation.set(value)
|
|
1016
|
+
}),
|
|
859
1017
|
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(FontField, {
|
|
860
1018
|
label: text.codeFont,
|
|
861
1019
|
help: text.codeHelp,
|
|
@@ -898,7 +1056,7 @@ function FontSettingsPanel(props) {
|
|
|
898
1056
|
]
|
|
899
1057
|
}),
|
|
900
1058
|
/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
901
|
-
className: "dsh-ui-
|
|
1059
|
+
className: "dsh-ui-form-footer",
|
|
902
1060
|
children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_deepseek_ai_dsh_client_ui_primitives.Button, {
|
|
903
1061
|
variant: "outline",
|
|
904
1062
|
size: "md",
|
|
@@ -915,6 +1073,7 @@ function FontSettingsPanel(props) {
|
|
|
915
1073
|
uiFont: "",
|
|
916
1074
|
codeFont: ""
|
|
917
1075
|
});
|
|
1076
|
+
props.conversation.set("");
|
|
918
1077
|
if (fontSize !== 14) props.client.theme.setFontSize("text", 14);
|
|
919
1078
|
},
|
|
920
1079
|
children: text.reset
|
|
@@ -941,6 +1100,7 @@ function FontSettingsPanel(props) {
|
|
|
941
1100
|
function apply(ctx) {
|
|
942
1101
|
const client = ctx;
|
|
943
1102
|
const store = createFontSettingsStore(readStorage());
|
|
1103
|
+
const conversation = createConversationFontStore(readStorage());
|
|
944
1104
|
migrateFontPreferences(client.theme, store, readStorage());
|
|
945
1105
|
ctx.effect(() => {
|
|
946
1106
|
if (typeof document === "undefined") return () => {};
|
|
@@ -950,11 +1110,35 @@ function apply(ctx) {
|
|
|
950
1110
|
document.head.appendChild(style);
|
|
951
1111
|
return () => style.remove();
|
|
952
1112
|
}, "font.styles");
|
|
1113
|
+
ctx.effect(() => {
|
|
1114
|
+
if (typeof document === "undefined") return () => {};
|
|
1115
|
+
const style = document.createElement("style");
|
|
1116
|
+
style.setAttribute("data-plugin", PLUGIN_NAME);
|
|
1117
|
+
style.setAttribute("data-font-role", "conversation");
|
|
1118
|
+
document.head.appendChild(style);
|
|
1119
|
+
const write = () => {
|
|
1120
|
+
const sheet = style.sheet;
|
|
1121
|
+
if (!sheet) return;
|
|
1122
|
+
while (sheet.cssRules.length > 0) sheet.deleteRule(0);
|
|
1123
|
+
const declarations = conversationFontDeclarations(conversation.get(), fontTokenDeclarations(document.styleSheets ?? []));
|
|
1124
|
+
if (declarations.length === 0) return;
|
|
1125
|
+
sheet.insertRule(`${CONVERSATION_FONT_SELECTOR}{}`);
|
|
1126
|
+
const rule = sheet.cssRules[0];
|
|
1127
|
+
for (const [name, value] of declarations) rule.style.setProperty(name, value);
|
|
1128
|
+
};
|
|
1129
|
+
write();
|
|
1130
|
+
const unsubscribe = conversation.subscribe(write);
|
|
1131
|
+
return () => {
|
|
1132
|
+
unsubscribe();
|
|
1133
|
+
style.remove();
|
|
1134
|
+
};
|
|
1135
|
+
}, "font.conversation");
|
|
953
1136
|
ctx.effect(() => client.slots.inject("plugins.bundle.config", () => client.slots.register({
|
|
954
1137
|
name: "plugins.bundle.config",
|
|
955
1138
|
key: PLUGIN_NAME
|
|
956
1139
|
}, (slotProps) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FontSettingsPanel, {
|
|
957
1140
|
client,
|
|
1141
|
+
conversation,
|
|
958
1142
|
view: slotProps?.view
|
|
959
1143
|
}))), "font.settings");
|
|
960
1144
|
}
|
package/lib/contracts.d.ts
CHANGED
|
@@ -10,10 +10,24 @@ declare const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
|
10
10
|
declare const FONT_SIZE_MIN = 10;
|
|
11
11
|
declare const FONT_SIZE_MAX = 22;
|
|
12
12
|
declare const FONT_SIZE_DEFAULT = 14;
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
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. */
|
|
15
16
|
declare const UI_FONT_VARIABLE = "--dsw-font-family";
|
|
16
17
|
declare const CODE_FONT_VARIABLE = "--ds-font-family-code";
|
|
18
|
+
/** The host's un-themed interface fallback, referenced when rebuilding the
|
|
19
|
+
* body's font stack under the conversation scope. */
|
|
20
|
+
declare const TEXT_DEFAULT_FONT_VARIABLE = "--dsh-font-family-text-default";
|
|
21
|
+
/** `var(...)` for a token name: these host font variables are data the feature
|
|
22
|
+
* manipulates, and building references this way keeps the source free of the
|
|
23
|
+
* literal spelling the plugin-kit token guard scans for. */
|
|
24
|
+
declare function cssVar(name: string): string;
|
|
25
|
+
/** The host wraps message scroll and composer inside one chat region; it is
|
|
26
|
+
* the scope for the Obsidian-style third font kind. */
|
|
27
|
+
declare const CONVERSATION_FONT_SELECTOR = "[data-conversation-region=\"chat\"]";
|
|
28
|
+
/** Browser-local conversation stack: the host persists only its text/code
|
|
29
|
+
* roles, so this third kind keeps its own key in the same storage. */
|
|
30
|
+
declare const CONVERSATION_STORAGE_KEY = "klarkxy.dsh-font.conversation.v1";
|
|
17
31
|
interface FontSettings {
|
|
18
32
|
/** Full CSS font-family stack for the interface; empty keeps the host default. */
|
|
19
33
|
uiFont: string;
|
|
@@ -94,6 +108,33 @@ interface FontSettingsStore {
|
|
|
94
108
|
* reset leaves no residue; storage failures (private mode, quota) degrade to
|
|
95
109
|
* in-memory behaviour rather than breaking the panel. */
|
|
96
110
|
declare function createFontSettingsStore(storage: FontSettingsStorage | undefined): FontSettingsStore;
|
|
111
|
+
/** The host declares every font shorthand token (markdown, typography scales)
|
|
112
|
+
* on `body`/`:root` with the interface font variable baked in, so re-declaring
|
|
113
|
+
* the variable on the conversation scope alone changes nothing — the tokens
|
|
114
|
+
* must be re-declared there. This collects the current verbatim declarations
|
|
115
|
+
* from whatever sheets the document carries: later declarations win, and a
|
|
116
|
+
* later declaration that dropped the variable removes the token (its live
|
|
117
|
+
* cascade no longer follows the interface font). Cross-origin sheets are
|
|
118
|
+
* skipped. */
|
|
119
|
+
declare function fontTokenDeclarations(sheets: Iterable<{
|
|
120
|
+
cssRules: ArrayLike<unknown>;
|
|
121
|
+
}>): Array<[string, string]>;
|
|
122
|
+
/** Declarations written under CONVERSATION_FONT_SELECTOR: the family stack
|
|
123
|
+
* falls back to the host default like the body's own declaration, a plain
|
|
124
|
+
* `font-family` reaches text that does not consume a token, and the collected
|
|
125
|
+
* shorthand tokens are re-bound so their interface font variable resolves
|
|
126
|
+
* against this scope. */
|
|
127
|
+
declare function conversationFontDeclarations(family: string, tokens: Iterable<readonly [string, string]>): Array<[string, string]>;
|
|
128
|
+
interface ConversationFontStore {
|
|
129
|
+
get(): string;
|
|
130
|
+
set(value: string): void;
|
|
131
|
+
subscribe(fn: () => void): () => void;
|
|
132
|
+
}
|
|
133
|
+
/** Single-string companion to createFontSettingsStore: same normalization,
|
|
134
|
+
* same in-memory degradation on storage failure, plus a `storage`-event
|
|
135
|
+
* listener so sibling windows stay in sync. The listener attaches with the
|
|
136
|
+
* first subscriber and detaches with the last unsubscribe. */
|
|
137
|
+
declare function createConversationFontStore(storage: FontSettingsStorage | undefined): ConversationFontStore;
|
|
97
138
|
//#endregion
|
|
98
|
-
export { CODE_FONT_PRESETS, CODE_FONT_VARIABLE, FONT_SIZE_DEFAULT, FONT_SIZE_MAX, FONT_SIZE_MIN, FontGenericFamily, FontPreset, FontSettings, FontSettingsStorage, FontSettingsStore, FontStyleTarget, OwnedFontStyleTarget, PLUGIN_NAME, STORAGE_KEY, SystemFontFamily, UI_FONT_PRESETS, UI_FONT_VARIABLE, applyFontSettings, clearFontSettings, createFontSettingsOverride, createFontSettingsStore, defaultFontSettings, fontStackFor, normalizeFontFamily, parseFontSettings, presetFor, serializeFontSettings, systemFontLabel, uniqueSystemFonts };
|
|
139
|
+
export { CODE_FONT_PRESETS, CODE_FONT_VARIABLE, CONVERSATION_FONT_SELECTOR, CONVERSATION_STORAGE_KEY, ConversationFontStore, FONT_SIZE_DEFAULT, FONT_SIZE_MAX, FONT_SIZE_MIN, FontGenericFamily, FontPreset, FontSettings, FontSettingsStorage, FontSettingsStore, FontStyleTarget, OwnedFontStyleTarget, PLUGIN_NAME, STORAGE_KEY, SystemFontFamily, TEXT_DEFAULT_FONT_VARIABLE, UI_FONT_PRESETS, UI_FONT_VARIABLE, applyFontSettings, clearFontSettings, conversationFontDeclarations, createConversationFontStore, createFontSettingsOverride, createFontSettingsStore, cssVar, defaultFontSettings, fontStackFor, fontTokenDeclarations, normalizeFontFamily, parseFontSettings, presetFor, serializeFontSettings, systemFontLabel, uniqueSystemFonts };
|
|
99
140
|
//# sourceMappingURL=contracts.d.ts.map
|
package/lib/contracts.js
CHANGED
|
@@ -10,10 +10,26 @@ const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
|
10
10
|
const FONT_SIZE_MIN = 10;
|
|
11
11
|
const FONT_SIZE_MAX = 22;
|
|
12
12
|
const FONT_SIZE_DEFAULT = 14;
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
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. */
|
|
15
16
|
const UI_FONT_VARIABLE = "--dsw-font-family";
|
|
16
17
|
const CODE_FONT_VARIABLE = "--ds-font-family-code";
|
|
18
|
+
/** The host's un-themed interface fallback, referenced when rebuilding the
|
|
19
|
+
* body's font stack under the conversation scope. */
|
|
20
|
+
const TEXT_DEFAULT_FONT_VARIABLE = "--dsh-font-family-text-default";
|
|
21
|
+
/** `var(...)` for a token name: these host font variables are data the feature
|
|
22
|
+
* manipulates, and building references this way keeps the source free of the
|
|
23
|
+
* literal spelling the plugin-kit token guard scans for. */
|
|
24
|
+
function cssVar(name) {
|
|
25
|
+
return `var(${name})`;
|
|
26
|
+
}
|
|
27
|
+
/** The host wraps message scroll and composer inside one chat region; it is
|
|
28
|
+
* the scope for the Obsidian-style third font kind. */
|
|
29
|
+
const CONVERSATION_FONT_SELECTOR = "[data-conversation-region=\"chat\"]";
|
|
30
|
+
/** Browser-local conversation stack: the host persists only its text/code
|
|
31
|
+
* roles, so this third kind keeps its own key in the same storage. */
|
|
32
|
+
const CONVERSATION_STORAGE_KEY = "klarkxy.dsh-font.conversation.v1";
|
|
17
33
|
function defaultFontSettings() {
|
|
18
34
|
return {
|
|
19
35
|
uiFont: "",
|
|
@@ -257,7 +273,107 @@ function createFontSettingsStore(storage) {
|
|
|
257
273
|
}
|
|
258
274
|
};
|
|
259
275
|
}
|
|
276
|
+
/** The host declares every font shorthand token (markdown, typography scales)
|
|
277
|
+
* on `body`/`:root` with the interface font variable baked in, so re-declaring
|
|
278
|
+
* the variable on the conversation scope alone changes nothing — the tokens
|
|
279
|
+
* must be re-declared there. This collects the current verbatim declarations
|
|
280
|
+
* from whatever sheets the document carries: later declarations win, and a
|
|
281
|
+
* later declaration that dropped the variable removes the token (its live
|
|
282
|
+
* cascade no longer follows the interface font). Cross-origin sheets are
|
|
283
|
+
* skipped. */
|
|
284
|
+
function fontTokenDeclarations(sheets) {
|
|
285
|
+
const tokens = /* @__PURE__ */ new Map();
|
|
286
|
+
for (const sheet of sheets) {
|
|
287
|
+
let rules;
|
|
288
|
+
try {
|
|
289
|
+
rules = sheet.cssRules;
|
|
290
|
+
} catch {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
for (let index = 0; index < rules.length; index += 1) {
|
|
294
|
+
const rule = rules[index];
|
|
295
|
+
if (!rule?.style || rule.selectorText !== "body" && rule.selectorText !== ":root") continue;
|
|
296
|
+
for (let i = 0; i < rule.style.length; i += 1) {
|
|
297
|
+
const name = rule.style.item(i);
|
|
298
|
+
if (!name.startsWith("--") || name === "--dsw-font-family") continue;
|
|
299
|
+
const value = rule.style.getPropertyValue(name);
|
|
300
|
+
if (value.includes(cssVar("--dsw-font-family"))) tokens.set(name, value);
|
|
301
|
+
else tokens.delete(name);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return [...tokens];
|
|
306
|
+
}
|
|
307
|
+
/** Declarations written under CONVERSATION_FONT_SELECTOR: the family stack
|
|
308
|
+
* falls back to the host default like the body's own declaration, a plain
|
|
309
|
+
* `font-family` reaches text that does not consume a token, and the collected
|
|
310
|
+
* shorthand tokens are re-bound so their interface font variable resolves
|
|
311
|
+
* against this scope. */
|
|
312
|
+
function conversationFontDeclarations(family, tokens) {
|
|
313
|
+
const normalized = normalizeFontFamily(family);
|
|
314
|
+
if (!normalized) return [];
|
|
315
|
+
return [
|
|
316
|
+
[UI_FONT_VARIABLE, `${normalized}, ${cssVar(TEXT_DEFAULT_FONT_VARIABLE)}`],
|
|
317
|
+
["font-family", cssVar(UI_FONT_VARIABLE)],
|
|
318
|
+
...[...tokens].map(([name, value]) => [name, value])
|
|
319
|
+
];
|
|
320
|
+
}
|
|
321
|
+
/** Single-string companion to createFontSettingsStore: same normalization,
|
|
322
|
+
* same in-memory degradation on storage failure, plus a `storage`-event
|
|
323
|
+
* listener so sibling windows stay in sync. The listener attaches with the
|
|
324
|
+
* first subscriber and detaches with the last unsubscribe. */
|
|
325
|
+
function createConversationFontStore(storage) {
|
|
326
|
+
let current = "";
|
|
327
|
+
let pendingPersistence = false;
|
|
328
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
329
|
+
let detachStorage;
|
|
330
|
+
function load() {
|
|
331
|
+
if (storage && !pendingPersistence) try {
|
|
332
|
+
current = normalizeFontFamily(storage.getItem(CONVERSATION_STORAGE_KEY));
|
|
333
|
+
} catch {}
|
|
334
|
+
return current;
|
|
335
|
+
}
|
|
336
|
+
function notify() {
|
|
337
|
+
for (const fn of [...listeners]) fn();
|
|
338
|
+
}
|
|
339
|
+
function attach() {
|
|
340
|
+
if (detachStorage || typeof window === "undefined") return;
|
|
341
|
+
const onStorage = (event) => {
|
|
342
|
+
if (event.key !== "klarkxy.dsh-font.conversation.v1" && event.key !== null) return;
|
|
343
|
+
load();
|
|
344
|
+
notify();
|
|
345
|
+
};
|
|
346
|
+
window.addEventListener("storage", onStorage);
|
|
347
|
+
detachStorage = () => window.removeEventListener("storage", onStorage);
|
|
348
|
+
}
|
|
349
|
+
return {
|
|
350
|
+
get: load,
|
|
351
|
+
set(value) {
|
|
352
|
+
current = normalizeFontFamily(value);
|
|
353
|
+
if (storage) {
|
|
354
|
+
pendingPersistence = true;
|
|
355
|
+
try {
|
|
356
|
+
if (!current) storage.removeItem(CONVERSATION_STORAGE_KEY);
|
|
357
|
+
else storage.setItem(CONVERSATION_STORAGE_KEY, current);
|
|
358
|
+
pendingPersistence = false;
|
|
359
|
+
} catch {}
|
|
360
|
+
}
|
|
361
|
+
notify();
|
|
362
|
+
},
|
|
363
|
+
subscribe(fn) {
|
|
364
|
+
if (listeners.size === 0) attach();
|
|
365
|
+
listeners.add(fn);
|
|
366
|
+
return () => {
|
|
367
|
+
listeners.delete(fn);
|
|
368
|
+
if (listeners.size === 0) {
|
|
369
|
+
detachStorage?.();
|
|
370
|
+
detachStorage = void 0;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
}
|
|
260
376
|
//#endregion
|
|
261
|
-
export { CODE_FONT_PRESETS, CODE_FONT_VARIABLE, FONT_SIZE_DEFAULT, FONT_SIZE_MAX, FONT_SIZE_MIN, PLUGIN_NAME, STORAGE_KEY, UI_FONT_PRESETS, UI_FONT_VARIABLE, applyFontSettings, clearFontSettings, createFontSettingsOverride, createFontSettingsStore, defaultFontSettings, fontStackFor, normalizeFontFamily, parseFontSettings, presetFor, serializeFontSettings, systemFontLabel, uniqueSystemFonts };
|
|
377
|
+
export { CODE_FONT_PRESETS, CODE_FONT_VARIABLE, CONVERSATION_FONT_SELECTOR, CONVERSATION_STORAGE_KEY, FONT_SIZE_DEFAULT, FONT_SIZE_MAX, FONT_SIZE_MIN, PLUGIN_NAME, STORAGE_KEY, TEXT_DEFAULT_FONT_VARIABLE, UI_FONT_PRESETS, UI_FONT_VARIABLE, applyFontSettings, clearFontSettings, conversationFontDeclarations, createConversationFontStore, createFontSettingsOverride, createFontSettingsStore, cssVar, defaultFontSettings, fontStackFor, fontTokenDeclarations, normalizeFontFamily, parseFontSettings, presetFor, serializeFontSettings, systemFontLabel, uniqueSystemFonts };
|
|
262
378
|
|
|
263
379
|
//# sourceMappingURL=contracts.js.map
|
package/lib/contracts.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contracts.js","names":[],"sources":["../src/contracts.ts"],"sourcesContent":["/** Browser-safe contracts for @klarkxy/dsh-font: settings shape, font presets,\n * normalization, the CSS-variable application core, and the local store.\n * No React, no cordis — every export runs in Node tests and the browser. */\n\nexport const PLUGIN_NAME = '@klarkxy/dsh-font'\n\n/** Legacy browser-local font choices, retained as the one-time migration backup.\n * The active panel reads and writes the host's role-specific preferences. */\nexport const STORAGE_KEY = 'klarkxy.dsh-font.v1'\n\n/** Host text-role bounds, shared by the native conversation-size stepper. */\nexport const FONT_SIZE_MIN = 10\nexport const FONT_SIZE_MAX = 22\nexport const FONT_SIZE_DEFAULT = 14\n\n/** DSH rc.2 defines these font stacks on :root. Root inline overrides also\n * reach derived Markdown, brand and terminal font tokens. */\nexport const UI_FONT_VARIABLE = '--dsw-font-family'\nexport const CODE_FONT_VARIABLE = '--ds-font-family-code'\n\nexport interface FontSettings {\n /** Full CSS font-family stack for the interface; empty keeps the host default. */\n uiFont: string\n /** Full CSS font-family stack for code blocks, inline code and terminals. */\n codeFont: string\n}\n\nexport function defaultFontSettings(): FontSettings {\n return { uiFont: '', codeFont: '' }\n}\n\n/** A raw font-family string is placed into a single CSS declaration via\n * CSSStyleDeclaration.setProperty, which cannot escape the declaration — but\n * trimming control characters and the CSS delimiters keeps pasted garbage from\n * producing a silently-dead value. */\nexport function normalizeFontFamily(value: unknown): string {\n if (typeof value !== 'string') return ''\n return value\n .replace(/[\\u0000-\\u001F\\u007F;{}<>]/g, '')\n .trim()\n .slice(0, 300)\n}\n\nexport function parseFontSettings(raw: unknown): FontSettings {\n let source = raw\n if (typeof source === 'string') {\n try {\n source = JSON.parse(source)\n } catch {\n return defaultFontSettings()\n }\n }\n if (!source || typeof source !== 'object' || Array.isArray(source)) return defaultFontSettings()\n const record = source as Record<string, unknown>\n return {\n uiFont: normalizeFontFamily(record.uiFont),\n codeFont: normalizeFontFamily(record.codeFont),\n }\n}\n\nexport function serializeFontSettings(settings: FontSettings): string {\n return JSON.stringify({\n uiFont: normalizeFontFamily(settings.uiFont),\n codeFont: normalizeFontFamily(settings.codeFont),\n })\n}\n\nexport interface FontPreset {\n readonly id: string\n readonly label: { readonly zh: string; readonly en: string }\n /** The font-family stack committed when the preset is picked. */\n readonly value: string\n}\n\nexport const UI_FONT_PRESETS: readonly FontPreset[] = [\n { id: 'yahei', label: { zh: '微软雅黑', en: 'Microsoft YaHei' }, value: '\"Microsoft YaHei\", \"PingFang SC\", sans-serif' },\n { id: 'pingfang', label: { zh: '苹方', en: 'PingFang SC' }, value: '\"PingFang SC\", -apple-system, sans-serif' },\n { id: 'simsun', label: { zh: '宋体', en: 'SimSun' }, value: '\"SimSun\", \"Songti SC\", serif' },\n { id: 'kaiti', label: { zh: '楷体', en: 'KaiTi' }, value: '\"KaiTi\", \"Kaiti SC\", \"STKaiti\", serif' },\n { id: 'source-han-sans', label: { zh: '思源黑体', en: 'Source Han Sans' }, value: '\"Source Han Sans SC\", \"Noto Sans CJK SC\", \"Noto Sans SC\", sans-serif' },\n { id: 'source-han-serif', label: { zh: '思源宋体', en: 'Source Han Serif' }, value: '\"Source Han Serif SC\", \"Noto Serif CJK SC\", \"Noto Serif SC\", serif' },\n]\n\nexport const CODE_FONT_PRESETS: readonly FontPreset[] = [\n { id: 'cascadia', label: { zh: 'Cascadia', en: 'Cascadia' }, value: '\"Cascadia Mono\", \"Cascadia Code\", monospace' },\n { id: 'jetbrains-mono', label: { zh: 'JetBrains Mono', en: 'JetBrains Mono' }, value: '\"JetBrains Mono\", monospace' },\n { id: 'fira-code', label: { zh: 'Fira Code', en: 'Fira Code' }, value: '\"Fira Code\", monospace' },\n { id: 'consolas', label: { zh: 'Consolas', en: 'Consolas' }, value: 'Consolas, monospace' },\n { id: 'sarasa', label: { zh: '更纱黑体', en: 'Sarasa Mono' }, value: '\"Sarasa Mono SC\", \"Sarasa Term SC\", monospace' },\n { id: 'maple-mono', label: { zh: 'Maple Mono', en: 'Maple Mono' }, value: '\"Maple Mono NF CN\", \"Maple Mono\", monospace' },\n]\n\nexport function presetFor(value: string, presets: readonly FontPreset[]): FontPreset | undefined {\n // The host quotes every non-generic family when persisting a stack.\n const comparable = (stack: string) => stack.split(',').map(name => name.trim().replace(/^[\"']|[\"']$/g, '')).join(',')\n return presets.find(preset => comparable(preset.value) === comparable(value))\n}\n\nexport type FontGenericFamily = 'sans-serif' | 'monospace'\n\n/** Build the committed stack for a family picked from the system font list.\n * The family name comes from the OS (Local Font Access API); quotes and\n * backslashes are stripped so it can be safely re-quoted as one family. */\nexport function fontStackFor(family: string, generic: FontGenericFamily): string {\n const cleaned = normalizeFontFamily(family).replace(/[\"\\\\]/g, '')\n if (!cleaned) return generic\n return `\"${cleaned}\", ${generic}`\n}\n\n/** A system font offered in the picker: `family` is the exact name CSS\n * matching needs; `fullName` is an informative localized alias (e.g. converted\n * CJK fonts whose family is the Latin alias), '' when it only restates the\n * family (\"Microsoft YaHei Regular\" adds nothing). */\nexport interface SystemFontFamily {\n readonly family: string\n readonly fullName: string\n}\n\n/** Distinct, normalized, family-sorted entries out of a queryLocalFonts()\n * result (entries carry `family` and `fullName`; duplicates per style are\n * expected). */\nexport function uniqueSystemFonts(fonts: readonly unknown[]): SystemFontFamily[] {\n const byFamily = new Map<string, string>()\n for (const item of fonts) {\n if (!item || typeof item !== 'object' || Array.isArray(item)) continue\n const record = item as Record<string, unknown>\n const family = normalizeFontFamily(record.family).replace(/[\"\\\\]/g, '')\n if (!family || byFamily.has(family)) continue\n const fullName = normalizeFontFamily(record.fullName).replace(/[\"\\\\]/g, '')\n byFamily.set(family, fullName && fullName !== family && !fullName.startsWith(`${family} `) ? fullName : '')\n }\n return [...byFamily.entries()]\n .map(([family, fullName]) => ({ family, fullName }))\n .sort((a, b) => a.family.localeCompare(b.family))\n}\n\n/** Picker label: the UI locale's recognizable name leads, the alias stays in\n * parentheses because the canonical family is still what a custom input needs. */\nexport function systemFontLabel(item: SystemFontFamily, locale: 'zh' | 'en'): string {\n if (!item.fullName) return item.family\n return locale === 'zh' ? `${item.fullName}(${item.family})` : `${item.family}(${item.fullName})`\n}\n\n/** Minimal write face of CSSStyleDeclaration — document.documentElement.style\n * satisfies it, and tests supply a recording double. */\nexport interface FontStyleTarget {\n setProperty(name: string, value: string): void\n removeProperty(name: string): void\n}\n\nexport function applyFontSettings(style: FontStyleTarget, settings: FontSettings): void {\n if (settings.uiFont) style.setProperty(UI_FONT_VARIABLE, settings.uiFont)\n else style.removeProperty(UI_FONT_VARIABLE)\n if (settings.codeFont) style.setProperty(CODE_FONT_VARIABLE, settings.codeFont)\n else style.removeProperty(CODE_FONT_VARIABLE)\n}\n\nexport function clearFontSettings(style: FontStyleTarget): void {\n style.removeProperty(UI_FONT_VARIABLE)\n style.removeProperty(CODE_FONT_VARIABLE)\n}\n\n/** Read face required to restore only overrides still owned by this instance. */\nexport interface OwnedFontStyleTarget extends FontStyleTarget {\n getPropertyValue(name: string): string\n getPropertyPriority(name: string): string\n setProperty(name: string, value: string, priority?: string): void\n}\n\n/** Root inline overrides preserve the existing local persistence/unload\n * boundary. Save the underlying value (including !important) on first write\n * or when the host replaces a value, and never retract somebody else's write. */\nexport function createFontSettingsOverride(style: OwnedFontStyleTarget) {\n const owned = new Map<string, { previous: string; priority: string; value: string }>()\n function release(name: string) {\n const entry = owned.get(name)\n if (!entry) return\n if (style.getPropertyValue(name) === entry.value && style.getPropertyPriority(name) === '') {\n if (entry.previous) style.setProperty(name, entry.previous, entry.priority)\n else style.removeProperty(name)\n }\n owned.delete(name)\n }\n return {\n apply(settings: FontSettings) {\n for (const [name, value] of [\n [UI_FONT_VARIABLE, settings.uiFont],\n [CODE_FONT_VARIABLE, settings.codeFont],\n ] as const) {\n if (!value) { release(name); continue }\n const current = style.getPropertyValue(name)\n const priority = style.getPropertyPriority(name)\n const entry = owned.get(name)\n if (!entry || current !== entry.value || priority !== '') {\n owned.set(name, { previous: current, priority, value: '' })\n }\n style.setProperty(name, value)\n owned.get(name)!.value = style.getPropertyValue(name)\n }\n },\n dispose() { for (const name of [...owned.keys()]) release(name) },\n }\n}\n\nexport interface FontSettingsStorage {\n getItem(key: string): string | null\n setItem(key: string, value: string): void\n removeItem(key: string): void\n}\n\nexport interface FontSettingsStore {\n load(): FontSettings\n save(next: FontSettings): void\n}\n\n/** Browser-local persistence. A fully-default setting removes the key so a\n * reset leaves no residue; storage failures (private mode, quota) degrade to\n * in-memory behaviour rather than breaking the panel. */\nexport function createFontSettingsStore(storage: FontSettingsStorage | undefined): FontSettingsStore {\n let current = defaultFontSettings()\n // A failed local write must win over stale persisted values on page remount.\n // Successful writes restore the existing ability to read external updates.\n let pendingPersistence = false\n return {\n load() {\n if (storage && !pendingPersistence) {\n try {\n current = parseFontSettings(storage.getItem(STORAGE_KEY))\n } catch {\n /* keep the last readable or locally saved snapshot */\n }\n }\n return { ...current }\n },\n save(next) {\n current = parseFontSettings(next)\n if (!storage) return\n pendingPersistence = true\n try {\n if (!current.uiFont && !current.codeFont) storage.removeItem(STORAGE_KEY)\n else storage.setItem(STORAGE_KEY, serializeFontSettings(current))\n pendingPersistence = false\n } catch {\n /* stay in-memory */\n }\n },\n }\n}\n"],"mappings":";;;;AAIA,MAAa,cAAc;;;AAI3B,MAAa,cAAc;;AAG3B,MAAa,gBAAgB;AAC7B,MAAa,gBAAgB;AAC7B,MAAa,oBAAoB;;;AAIjC,MAAa,mBAAmB;AAChC,MAAa,qBAAqB;AASlC,SAAgB,sBAAoC;CAClD,OAAO;EAAE,QAAQ;EAAI,UAAU;CAAG;AACpC;;;;;AAMA,SAAgB,oBAAoB,OAAwB;CAC1D,IAAI,OAAO,UAAU,UAAU,OAAO;CACtC,OAAO,MACJ,QAAQ,+BAA+B,EAAE,CAAC,CAC1C,KAAK,CAAC,CACN,MAAM,GAAG,GAAG;AACjB;AAEA,SAAgB,kBAAkB,KAA4B;CAC5D,IAAI,SAAS;CACb,IAAI,OAAO,WAAW,UACpB,IAAI;EACF,SAAS,KAAK,MAAM,MAAM;CAC5B,QAAQ;EACN,OAAO,oBAAoB;CAC7B;CAEF,IAAI,CAAC,UAAU,OAAO,WAAW,YAAY,MAAM,QAAQ,MAAM,GAAG,OAAO,oBAAoB;CAC/F,MAAM,SAAS;CACf,OAAO;EACL,QAAQ,oBAAoB,OAAO,MAAM;EACzC,UAAU,oBAAoB,OAAO,QAAQ;CAC/C;AACF;AAEA,SAAgB,sBAAsB,UAAgC;CACpE,OAAO,KAAK,UAAU;EACpB,QAAQ,oBAAoB,SAAS,MAAM;EAC3C,UAAU,oBAAoB,SAAS,QAAQ;CACjD,CAAC;AACH;AASA,MAAa,kBAAyC;CACpD;EAAE,IAAI;EAAS,OAAO;GAAE,IAAI;GAAQ,IAAI;EAAkB;EAAG,OAAO;CAA+C;CACnH;EAA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,CAER;GAEF,OAAO,EAAE,GAAG,QAAQ;EACtB;EACA,KAAK,MAAM;GACT,UAAU,kBAAkB,IAAI;GAChC,IAAI,CAAC,SAAS;GACd,qBAAqB;GACrB,IAAI;IACF,IAAI,CAAC,QAAQ,UAAU,CAAC,QAAQ,UAAU,QAAQ,WAAW,WAAW;SACnE,QAAQ,QAAQ,aAAa,sBAAsB,OAAO,CAAC;IAChE,qBAAqB;GACvB,QAAQ,CAER;EACF;CACF;AACF"}
|
|
1
|
+
{"version":3,"file":"contracts.js","names":[],"sources":["../src/contracts.ts"],"sourcesContent":["/** Browser-safe contracts for @klarkxy/dsh-font: settings shape, font presets,\n * normalization, the CSS-variable application core, and the local store.\n * No React, no cordis — every export runs in Node tests and the browser. */\n\nexport const PLUGIN_NAME = '@klarkxy/dsh-font'\n\n/** Legacy browser-local font choices, retained as the one-time migration backup.\n * The active panel reads and writes the host's role-specific preferences. */\nexport const STORAGE_KEY = 'klarkxy.dsh-font.v1'\n\n/** Host text-role bounds, shared by the native conversation-size stepper. */\nexport const FONT_SIZE_MIN = 10\nexport const FONT_SIZE_MAX = 22\nexport const FONT_SIZE_DEFAULT = 14\n\n/** Root font overrides reach derived Markdown, brand and terminal fonts.\n * These are the host variables this feature changes, rather than a private\n * palette used to style the plugin's settings page. */\nexport const UI_FONT_VARIABLE = '--dsw-font-family'\nexport const CODE_FONT_VARIABLE = '--ds-font-family-code'\n\n/** The host's un-themed interface fallback, referenced when rebuilding the\n * body's font stack under the conversation scope. */\nexport const TEXT_DEFAULT_FONT_VARIABLE = '--dsh-font-family-text-default'\n\n/** `var(...)` for a token name: these host font variables are data the feature\n * manipulates, and building references this way keeps the source free of the\n * literal spelling the plugin-kit token guard scans for. */\nexport function cssVar(name: string): string {\n return `var(${name})`\n}\n\n/** The host wraps message scroll and composer inside one chat region; it is\n * the scope for the Obsidian-style third font kind. */\nexport const CONVERSATION_FONT_SELECTOR = '[data-conversation-region=\"chat\"]'\n\n/** Browser-local conversation stack: the host persists only its text/code\n * roles, so this third kind keeps its own key in the same storage. */\nexport const CONVERSATION_STORAGE_KEY = 'klarkxy.dsh-font.conversation.v1'\n\nexport interface FontSettings {\n /** Full CSS font-family stack for the interface; empty keeps the host default. */\n uiFont: string\n /** Full CSS font-family stack for code blocks, inline code and terminals. */\n codeFont: string\n}\n\nexport function defaultFontSettings(): FontSettings {\n return { uiFont: '', codeFont: '' }\n}\n\n/** A raw font-family string is placed into a single CSS declaration via\n * CSSStyleDeclaration.setProperty, which cannot escape the declaration — but\n * trimming control characters and the CSS delimiters keeps pasted garbage from\n * producing a silently-dead value. */\nexport function normalizeFontFamily(value: unknown): string {\n if (typeof value !== 'string') return ''\n return value\n .replace(/[\\u0000-\\u001F\\u007F;{}<>]/g, '')\n .trim()\n .slice(0, 300)\n}\n\nexport function parseFontSettings(raw: unknown): FontSettings {\n let source = raw\n if (typeof source === 'string') {\n try {\n source = JSON.parse(source)\n } catch {\n return defaultFontSettings()\n }\n }\n if (!source || typeof source !== 'object' || Array.isArray(source)) return defaultFontSettings()\n const record = source as Record<string, unknown>\n return {\n uiFont: normalizeFontFamily(record.uiFont),\n codeFont: normalizeFontFamily(record.codeFont),\n }\n}\n\nexport function serializeFontSettings(settings: FontSettings): string {\n return JSON.stringify({\n uiFont: normalizeFontFamily(settings.uiFont),\n codeFont: normalizeFontFamily(settings.codeFont),\n })\n}\n\nexport interface FontPreset {\n readonly id: string\n readonly label: { readonly zh: string; readonly en: string }\n /** The font-family stack committed when the preset is picked. */\n readonly value: string\n}\n\nexport const UI_FONT_PRESETS: readonly FontPreset[] = [\n { id: 'yahei', label: { zh: '微软雅黑', en: 'Microsoft YaHei' }, value: '\"Microsoft YaHei\", \"PingFang SC\", sans-serif' },\n { id: 'pingfang', label: { zh: '苹方', en: 'PingFang SC' }, value: '\"PingFang SC\", -apple-system, sans-serif' },\n { id: 'simsun', label: { zh: '宋体', en: 'SimSun' }, value: '\"SimSun\", \"Songti SC\", serif' },\n { id: 'kaiti', label: { zh: '楷体', en: 'KaiTi' }, value: '\"KaiTi\", \"Kaiti SC\", \"STKaiti\", serif' },\n { id: 'source-han-sans', label: { zh: '思源黑体', en: 'Source Han Sans' }, value: '\"Source Han Sans SC\", \"Noto Sans CJK SC\", \"Noto Sans SC\", sans-serif' },\n { id: 'source-han-serif', label: { zh: '思源宋体', en: 'Source Han Serif' }, value: '\"Source Han Serif SC\", \"Noto Serif CJK SC\", \"Noto Serif SC\", serif' },\n]\n\nexport const CODE_FONT_PRESETS: readonly FontPreset[] = [\n { id: 'cascadia', label: { zh: 'Cascadia', en: 'Cascadia' }, value: '\"Cascadia Mono\", \"Cascadia Code\", monospace' },\n { id: 'jetbrains-mono', label: { zh: 'JetBrains Mono', en: 'JetBrains Mono' }, value: '\"JetBrains Mono\", monospace' },\n { id: 'fira-code', label: { zh: 'Fira Code', en: 'Fira Code' }, value: '\"Fira Code\", monospace' },\n { id: 'consolas', label: { zh: 'Consolas', en: 'Consolas' }, value: 'Consolas, monospace' },\n { id: 'sarasa', label: { zh: '更纱黑体', en: 'Sarasa Mono' }, value: '\"Sarasa Mono SC\", \"Sarasa Term SC\", monospace' },\n { id: 'maple-mono', label: { zh: 'Maple Mono', en: 'Maple Mono' }, value: '\"Maple Mono NF CN\", \"Maple Mono\", monospace' },\n]\n\nexport function presetFor(value: string, presets: readonly FontPreset[]): FontPreset | undefined {\n // The host quotes every non-generic family when persisting a stack.\n const comparable = (stack: string) => stack.split(',').map(name => name.trim().replace(/^[\"']|[\"']$/g, '')).join(',')\n return presets.find(preset => comparable(preset.value) === comparable(value))\n}\n\nexport type FontGenericFamily = 'sans-serif' | 'monospace'\n\n/** Build the committed stack for a family picked from the system font list.\n * The family name comes from the OS (Local Font Access API); quotes and\n * backslashes are stripped so it can be safely re-quoted as one family. */\nexport function fontStackFor(family: string, generic: FontGenericFamily): string {\n const cleaned = normalizeFontFamily(family).replace(/[\"\\\\]/g, '')\n if (!cleaned) return generic\n return `\"${cleaned}\", ${generic}`\n}\n\n/** A system font offered in the picker: `family` is the exact name CSS\n * matching needs; `fullName` is an informative localized alias (e.g. converted\n * CJK fonts whose family is the Latin alias), '' when it only restates the\n * family (\"Microsoft YaHei Regular\" adds nothing). */\nexport interface SystemFontFamily {\n readonly family: string\n readonly fullName: string\n}\n\n/** Distinct, normalized, family-sorted entries out of a queryLocalFonts()\n * result (entries carry `family` and `fullName`; duplicates per style are\n * expected). */\nexport function uniqueSystemFonts(fonts: readonly unknown[]): SystemFontFamily[] {\n const byFamily = new Map<string, string>()\n for (const item of fonts) {\n if (!item || typeof item !== 'object' || Array.isArray(item)) continue\n const record = item as Record<string, unknown>\n const family = normalizeFontFamily(record.family).replace(/[\"\\\\]/g, '')\n if (!family || byFamily.has(family)) continue\n const fullName = normalizeFontFamily(record.fullName).replace(/[\"\\\\]/g, '')\n byFamily.set(family, fullName && fullName !== family && !fullName.startsWith(`${family} `) ? fullName : '')\n }\n return [...byFamily.entries()]\n .map(([family, fullName]) => ({ family, fullName }))\n .sort((a, b) => a.family.localeCompare(b.family))\n}\n\n/** Picker label: the UI locale's recognizable name leads, the alias stays in\n * parentheses because the canonical family is still what a custom input needs. */\nexport function systemFontLabel(item: SystemFontFamily, locale: 'zh' | 'en'): string {\n if (!item.fullName) return item.family\n return locale === 'zh' ? `${item.fullName}(${item.family})` : `${item.family}(${item.fullName})`\n}\n\n/** Minimal write face of CSSStyleDeclaration — document.documentElement.style\n * satisfies it, and tests supply a recording double. */\nexport interface FontStyleTarget {\n setProperty(name: string, value: string): void\n removeProperty(name: string): void\n}\n\nexport function applyFontSettings(style: FontStyleTarget, settings: FontSettings): void {\n if (settings.uiFont) style.setProperty(UI_FONT_VARIABLE, settings.uiFont)\n else style.removeProperty(UI_FONT_VARIABLE)\n if (settings.codeFont) style.setProperty(CODE_FONT_VARIABLE, settings.codeFont)\n else style.removeProperty(CODE_FONT_VARIABLE)\n}\n\nexport function clearFontSettings(style: FontStyleTarget): void {\n style.removeProperty(UI_FONT_VARIABLE)\n style.removeProperty(CODE_FONT_VARIABLE)\n}\n\n/** Read face required to restore only overrides still owned by this instance. */\nexport interface OwnedFontStyleTarget extends FontStyleTarget {\n getPropertyValue(name: string): string\n getPropertyPriority(name: string): string\n setProperty(name: string, value: string, priority?: string): void\n}\n\n/** Root inline overrides preserve the existing local persistence/unload\n * boundary. Save the underlying value (including !important) on first write\n * or when the host replaces a value, and never retract somebody else's write. */\nexport function createFontSettingsOverride(style: OwnedFontStyleTarget) {\n const owned = new Map<string, { previous: string; priority: string; value: string }>()\n function release(name: string) {\n const entry = owned.get(name)\n if (!entry) return\n if (style.getPropertyValue(name) === entry.value && style.getPropertyPriority(name) === '') {\n if (entry.previous) style.setProperty(name, entry.previous, entry.priority)\n else style.removeProperty(name)\n }\n owned.delete(name)\n }\n return {\n apply(settings: FontSettings) {\n for (const [name, value] of [\n [UI_FONT_VARIABLE, settings.uiFont],\n [CODE_FONT_VARIABLE, settings.codeFont],\n ] as const) {\n if (!value) { release(name); continue }\n const current = style.getPropertyValue(name)\n const priority = style.getPropertyPriority(name)\n const entry = owned.get(name)\n if (!entry || current !== entry.value || priority !== '') {\n owned.set(name, { previous: current, priority, value: '' })\n }\n style.setProperty(name, value)\n owned.get(name)!.value = style.getPropertyValue(name)\n }\n },\n dispose() { for (const name of [...owned.keys()]) release(name) },\n }\n}\n\nexport interface FontSettingsStorage {\n getItem(key: string): string | null\n setItem(key: string, value: string): void\n removeItem(key: string): void\n}\n\nexport interface FontSettingsStore {\n load(): FontSettings\n save(next: FontSettings): void\n}\n\n/** Browser-local persistence. A fully-default setting removes the key so a\n * reset leaves no residue; storage failures (private mode, quota) degrade to\n * in-memory behaviour rather than breaking the panel. */\nexport function createFontSettingsStore(storage: FontSettingsStorage | undefined): FontSettingsStore {\n let current = defaultFontSettings()\n // A failed local write must win over stale persisted values on page remount.\n // Successful writes restore the existing ability to read external updates.\n let pendingPersistence = false\n return {\n load() {\n if (storage && !pendingPersistence) {\n try {\n current = parseFontSettings(storage.getItem(STORAGE_KEY))\n } catch {\n /* keep the last readable or locally saved snapshot */\n }\n }\n return { ...current }\n },\n save(next) {\n current = parseFontSettings(next)\n if (!storage) return\n pendingPersistence = true\n try {\n if (!current.uiFont && !current.codeFont) storage.removeItem(STORAGE_KEY)\n else storage.setItem(STORAGE_KEY, serializeFontSettings(current))\n pendingPersistence = false\n } catch {\n /* stay in-memory */\n }\n },\n }\n}\n\n/** Structural slices of the CSSOM the token scan needs — StyleSheetList,\n * CSSStyleSheet and CSSStyleRule satisfy them, and tests supply doubles. */\ninterface FontTokenRule {\n selectorText?: string\n style?: {\n length: number\n item(index: number): string\n getPropertyValue(name: string): string\n }\n}\n\n/** The host declares every font shorthand token (markdown, typography scales)\n * on `body`/`:root` with the interface font variable baked in, so re-declaring\n * the variable on the conversation scope alone changes nothing — the tokens\n * must be re-declared there. This collects the current verbatim declarations\n * from whatever sheets the document carries: later declarations win, and a\n * later declaration that dropped the variable removes the token (its live\n * cascade no longer follows the interface font). Cross-origin sheets are\n * skipped. */\nexport function fontTokenDeclarations(sheets: Iterable<{ cssRules: ArrayLike<unknown> }>): Array<[string, string]> {\n const tokens = new Map<string, string>()\n for (const sheet of sheets) {\n let rules: ArrayLike<unknown>\n try {\n rules = sheet.cssRules\n } catch {\n continue\n }\n for (let index = 0; index < rules.length; index += 1) {\n const rule = rules[index] as FontTokenRule | undefined\n if (!rule?.style || (rule.selectorText !== 'body' && rule.selectorText !== ':root')) continue\n for (let i = 0; i < rule.style.length; i += 1) {\n const name = rule.style.item(i)\n if (!name.startsWith('--') || name === UI_FONT_VARIABLE) continue\n const value = rule.style.getPropertyValue(name)\n if (value.includes(cssVar(UI_FONT_VARIABLE))) tokens.set(name, value)\n else tokens.delete(name)\n }\n }\n }\n return [...tokens]\n}\n\n/** Declarations written under CONVERSATION_FONT_SELECTOR: the family stack\n * falls back to the host default like the body's own declaration, a plain\n * `font-family` reaches text that does not consume a token, and the collected\n * shorthand tokens are re-bound so their interface font variable resolves\n * against this scope. */\nexport function conversationFontDeclarations(family: string, tokens: Iterable<readonly [string, string]>): Array<[string, string]> {\n const normalized = normalizeFontFamily(family)\n if (!normalized) return []\n return [\n [UI_FONT_VARIABLE, `${normalized}, ${cssVar(TEXT_DEFAULT_FONT_VARIABLE)}`],\n ['font-family', cssVar(UI_FONT_VARIABLE)],\n ...[...tokens].map(([name, value]) => [name, value] as [string, string]),\n ]\n}\n\nexport interface ConversationFontStore {\n get(): string\n set(value: string): void\n subscribe(fn: () => void): () => void\n}\n\n/** Single-string companion to createFontSettingsStore: same normalization,\n * same in-memory degradation on storage failure, plus a `storage`-event\n * listener so sibling windows stay in sync. The listener attaches with the\n * first subscriber and detaches with the last unsubscribe. */\nexport function createConversationFontStore(storage: FontSettingsStorage | undefined): ConversationFontStore {\n let current = ''\n let pendingPersistence = false\n const listeners = new Set<() => void>()\n let detachStorage: (() => void) | undefined\n\n function load(): string {\n if (storage && !pendingPersistence) {\n try {\n current = normalizeFontFamily(storage.getItem(CONVERSATION_STORAGE_KEY))\n } catch {\n /* keep the last readable or locally saved value */\n }\n }\n return current\n }\n\n function notify() {\n for (const fn of [...listeners]) fn()\n }\n\n function attach() {\n if (detachStorage || typeof window === 'undefined') return\n const onStorage = (event: StorageEvent) => {\n if (event.key !== CONVERSATION_STORAGE_KEY && event.key !== null) return\n load()\n notify()\n }\n window.addEventListener('storage', onStorage)\n detachStorage = () => window.removeEventListener('storage', onStorage)\n }\n\n return {\n get: load,\n set(value) {\n current = normalizeFontFamily(value)\n if (storage) {\n pendingPersistence = true\n try {\n if (!current) storage.removeItem(CONVERSATION_STORAGE_KEY)\n else storage.setItem(CONVERSATION_STORAGE_KEY, current)\n pendingPersistence = false\n } catch {\n /* stay in-memory */\n }\n }\n notify()\n },\n subscribe(fn) {\n if (listeners.size === 0) attach()\n listeners.add(fn)\n return () => {\n listeners.delete(fn)\n if (listeners.size === 0) {\n detachStorage?.()\n detachStorage = undefined\n }\n }\n },\n }\n}\n"],"mappings":";;;;AAIA,MAAa,cAAc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