@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/contracts.js
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
|
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
|
const STORAGE_KEY = "klarkxy.dsh-font.v1";
|
|
12
|
-
/**
|
|
9
|
+
/** Host text-role bounds, shared by the native conversation-size stepper. */
|
|
13
10
|
const FONT_SIZE_MIN = 10;
|
|
14
11
|
const FONT_SIZE_MAX = 22;
|
|
15
12
|
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
|
const UI_FONT_VARIABLE = "--dsw-font-family";
|
|
19
17
|
const CODE_FONT_VARIABLE = "--ds-font-family-code";
|
|
20
18
|
function defaultFontSettings() {
|
|
@@ -152,7 +150,8 @@ const CODE_FONT_PRESETS = [
|
|
|
152
150
|
}
|
|
153
151
|
];
|
|
154
152
|
function presetFor(value, presets) {
|
|
155
|
-
|
|
153
|
+
const comparable = (stack) => stack.split(",").map((name) => name.trim().replace(/^["']|["']$/g, "")).join(",");
|
|
154
|
+
return presets.find((preset) => comparable(preset.value) === comparable(value));
|
|
156
155
|
}
|
|
157
156
|
/** Build the committed stack for a family picked from the system font list.
|
|
158
157
|
* The family name comes from the OS (Local Font Access API); quotes and
|
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/** localStorage key. Fonts are a property of the rendering environment (each\n * browser/machine has its own installed fonts), so they persist browser-local,\n * exactly like the host's own GUI preferences (dsh-client-store, ui-theme\n * notes). The conversation font size is NOT stored here: the official\n * `ctx.theme.setFontSize()` entry persists it host-side already. */\nexport const STORAGE_KEY = 'klarkxy.dsh-font.v1'\n\n/** DSH 0.2.0-rc.2 ui-theme/theme-settings.ts bounds, shared by its native 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 return presets.find(preset => preset.value === 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;;;;;;AAO3B,MAAa,cAAc;;AAG3B,MAAa,gBAAgB;AAC7B,MAAa,gBAAgB;AAC7B,MAAa,oBAAoB;;;AAIjC,MAAa,mBAAmB;AAChC,MAAa,qBAAqB;AASlC,S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KAAG;IAAS;IACtC,MAAM,UAAU,MAAM,iBAAiB,IAAI;IAC3C,MAAM,WAAW,MAAM,oBAAoB,IAAI;IAC/C,MAAM,QAAQ,MAAM,IAAI,IAAI;IAC5B,IAAI,CAAC,SAAS,YAAY,MAAM,SAAS,aAAa,IACpD,MAAM,IAAI,MAAM;KAAE,UAAU;KAAS;KAAU,OAAO;IAAG,CAAC;IAE5D,MAAM,YAAY,MAAM,KAAK;IAC7B,MAAM,IAAI,IAAI,CAAC,CAAE,QAAQ,MAAM,iBAAiB,IAAI;GACtD;EACF;EACA,UAAU;GAAE,KAAK,MAAM,QAAQ,CAAC,GAAG,MAAM,KAAK,CAAC,GAAG,QAAQ,IAAI;EAAE;CAClE;AACF;;;;AAgBA,SAAgB,wBAAwB,SAA6D;CACnG,IAAI,UAAU,oBAAoB;CAGlC,IAAI,qBAAqB;CACzB,OAAO;EACL,OAAO;GACL,IAAI,WAAW,CAAC,oBACd,IAAI;IACF,UAAU,kBAAkB,QAAQ,QAAQ,WAAW,CAAC;GAC1D,QAAQ,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\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;;;;AAKjC,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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;CAClE;AACF;;;;AAgBA,SAAgB,wBAAwB,SAA6D;CACnG,IAAI,UAAU,oBAAoB;CAGlC,IAAI,qBAAqB;CACzB,OAAO;EACL,OAAO;GACL,IAAI,WAAW,CAAC,oBACd,IAAI;IACF,UAAU,kBAAkB,QAAQ,QAAQ,WAAW,CAAC;GAC1D,QAAQ,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"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@klarkxy/dsh-font",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Font: customize the interface font, code font, and conversation font size of the DeepSeek Harness Web GUI.",
|
|
5
5
|
"icon": "./icon.svg",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"engines": {
|
|
9
9
|
"node": ">=22",
|
|
10
|
-
"dsh": ">=0.2.
|
|
10
|
+
"dsh": ">=0.2.1-rc.1"
|
|
11
11
|
},
|
|
12
12
|
"main": "./lib/index.js",
|
|
13
13
|
"types": "./lib/index.d.ts",
|
|
@@ -56,20 +56,18 @@
|
|
|
56
56
|
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
57
57
|
"test": "vitest run --root ../.. --config vitest.editor-plugins.config.ts --maxWorkers=2 plugins/dsh-font/src"
|
|
58
58
|
},
|
|
59
|
-
"dependencies": {
|
|
60
|
-
"@klarkxy/dsh-plugin-kit": "0.3.4"
|
|
61
|
-
},
|
|
62
59
|
"peerDependencies": {
|
|
63
60
|
"@deepseek-ai/cordis": ">=4.0.4"
|
|
64
61
|
},
|
|
65
62
|
"devDependencies": {
|
|
66
|
-
"@
|
|
63
|
+
"@klarkxy/dsh-plugin-kit": "0.5.0",
|
|
64
|
+
"@deepseek-ai/cordis": "4.0.5",
|
|
67
65
|
"react": "^18.2.0",
|
|
68
66
|
"@types/react": "18.3.31",
|
|
69
67
|
"react-dom": "^18.2.0",
|
|
70
68
|
"@types/react-dom": "^18.3.1",
|
|
71
|
-
"@deepseek-ai/dsh-client-ui-primitives": "0.2.
|
|
72
|
-
"@deepseek-ai/dsh-client-ui-theme": "0.2.
|
|
69
|
+
"@deepseek-ai/dsh-client-ui-primitives": "0.2.1-rc.1",
|
|
70
|
+
"@deepseek-ai/dsh-client-ui-theme": "0.2.1-rc.1",
|
|
73
71
|
"jsdom": "29.1.1",
|
|
74
72
|
"@types/jsdom": "28.0.3"
|
|
75
73
|
},
|
|
@@ -97,6 +95,11 @@
|
|
|
97
95
|
},
|
|
98
96
|
"homepage": "https://klarkxy.github.io/dsh-plugins/plugins/font/",
|
|
99
97
|
"dshRelease": {
|
|
100
|
-
"contentHash": "
|
|
98
|
+
"contentHash": "b13c66bd161d995299a9826c4aee408b2c6d8c4dbea1e33ca74a0ff933321912"
|
|
99
|
+
},
|
|
100
|
+
"peerDependenciesMeta": {
|
|
101
|
+
"@deepseek-ai/cordis": {
|
|
102
|
+
"optional": true
|
|
103
|
+
}
|
|
101
104
|
}
|
|
102
105
|
}
|