返回 DeepSeek-Reasonix
fontFamily.ts
根目录 / desktop / frontend / src / lib / fontFamily.ts
1 export const FONT_FAMILIES = ["system", "yahei", "pingfang", "noto", "custom"] as const;
2 export const MONO_FONT_FAMILIES = ["system", "cascadia", "jetbrains", "sfmono", "custom"] as const;
3
4 export type FontFamily = (typeof FONT_FAMILIES)[number];
5 export type MonoFontFamily = (typeof MONO_FONT_FAMILIES)[number];
6
7 export const DEFAULT_FONT_FAMILY: FontFamily = "system";
8 export const DEFAULT_MONO_FONT_FAMILY: MonoFontFamily = "system";
9
10 const FONT_FAMILY_KEY = "reasonix-font-family";
11 const CUSTOM_FONT_KEY = "reasonix-font-family-custom";
12 const MONO_FONT_FAMILY_KEY = "reasonix-mono-font-family";
13 const CUSTOM_MONO_FONT_KEY = "reasonix-mono-font-family-custom";
14
15 export function isFontFamily(value: unknown): value is FontFamily {
16 return typeof value === "string" && (FONT_FAMILIES as readonly string[]).includes(value);
17 }
18
19 export function isMonoFontFamily(value: unknown): value is MonoFontFamily {
20 return typeof value === "string" && (MONO_FONT_FAMILIES as readonly string[]).includes(value);
21 }
22
23 export function getFontFamily(): FontFamily {
24 const stored = typeof localStorage !== "undefined" ? localStorage.getItem(FONT_FAMILY_KEY) : null;
25 return isFontFamily(stored) ? stored : DEFAULT_FONT_FAMILY;
26 }
27
28 export function getMonoFontFamily(): MonoFontFamily {
29 const stored = typeof localStorage !== "undefined" ? localStorage.getItem(MONO_FONT_FAMILY_KEY) : null;
30 return isMonoFontFamily(stored) ? stored : DEFAULT_MONO_FONT_FAMILY;
31 }
32
33 export function getCustomFontName(): string {
34 if (typeof localStorage === "undefined") return "";
35 return localStorage.getItem(CUSTOM_FONT_KEY) ?? "";
36 }
37
38 export function getCustomMonoFontName(): string {
39 if (typeof localStorage === "undefined") return "";
40 return localStorage.getItem(CUSTOM_MONO_FONT_KEY) ?? "";
41 }
42
43 export function setCustomFontName(name: string): void {
44 try {
45 localStorage.setItem(CUSTOM_FONT_KEY, name);
46 } catch {
47 /* private mode / no storage */
48 }
49 }
50
51 export function setCustomMonoFontName(name: string): void {
52 try {
53 localStorage.setItem(CUSTOM_MONO_FONT_KEY, name);
54 } catch {
55 /* private mode / no storage */
56 }
57 }
58
59 export function applyFontFamily(font: FontFamily): void {
60 if (typeof document === "undefined") return;
61 const root = document.documentElement;
62 if (font === DEFAULT_FONT_FAMILY) {
63 root.removeAttribute("data-font-family");
64 root.style.removeProperty("--font-family-custom");
65 } else {
66 root.setAttribute("data-font-family", font);
67 if (font === "custom") {
68 const name = getCustomFontName().trim();
69 if (name) root.style.setProperty("--font-family-custom", name);
70 else root.style.removeProperty("--font-family-custom");
71 } else {
72 root.style.removeProperty("--font-family-custom");
73 }
74 }
75 try {
76 localStorage.setItem(FONT_FAMILY_KEY, font);
77 } catch {
78 /* private mode / no storage */
79 }
80 }
81
82 export function applyMonoFontFamily(font: MonoFontFamily): void {
83 if (typeof document === "undefined") return;
84 const root = document.documentElement;
85 if (font === DEFAULT_MONO_FONT_FAMILY) {
86 root.removeAttribute("data-mono-font-family");
87 root.style.removeProperty("--font-family-mono-custom");
88 } else {
89 root.setAttribute("data-mono-font-family", font);
90 if (font === "custom") {
91 const name = getCustomMonoFontName().trim();
92 if (name) root.style.setProperty("--font-family-mono-custom", name);
93 else root.style.removeProperty("--font-family-mono-custom");
94 } else {
95 root.style.removeProperty("--font-family-mono-custom");
96 }
97 }
98 try {
99 localStorage.setItem(MONO_FONT_FAMILY_KEY, font);
100 } catch {
101 /* private mode / no storage */
102 }
103 }
104
105 export function initFontFamily(): void {
106 applyFontFamily(getFontFamily());
107 applyMonoFontFamily(getMonoFontFamily());
108 }
109
109 lines TYPESCRIPT