33 lines
1007 B
TypeScript
33 lines
1007 B
TypeScript
export const GUANGHU_THEME_STORAGE_KEY = 'guanghu-theme'
|
|
|
|
export const GUANGHU_THEMES = ['native', 'lake-light', 'lake-dark'] as const
|
|
export type GuanghuTheme = typeof GUANGHU_THEMES[number]
|
|
|
|
export const GUANGHU_THEME_LABELS: Record<GuanghuTheme, string> = {
|
|
native: '原生',
|
|
'lake-light': '湖光浅色',
|
|
'lake-dark': '湖心深色',
|
|
}
|
|
|
|
export function normalizeGuanghuTheme(value: unknown): GuanghuTheme {
|
|
return typeof value === 'string' && (GUANGHU_THEMES as readonly string[]).includes(value)
|
|
? value as GuanghuTheme
|
|
: 'native'
|
|
}
|
|
|
|
export function readGuanghuTheme(storage: Pick<Storage, 'getItem'>): GuanghuTheme {
|
|
try {
|
|
return normalizeGuanghuTheme(storage.getItem(GUANGHU_THEME_STORAGE_KEY))
|
|
} catch {
|
|
return 'native'
|
|
}
|
|
}
|
|
|
|
export function writeGuanghuTheme(storage: Pick<Storage, 'setItem'>, theme: GuanghuTheme): void {
|
|
try {
|
|
storage.setItem(GUANGHU_THEME_STORAGE_KEY, theme)
|
|
} catch {
|
|
// Storage may be unavailable in restricted browser contexts.
|
|
}
|
|
}
|