Files
chat/client/shared/utils/color-scheme-helper.ts
2026-04-25 16:36:34 +08:00

42 lines
923 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { isValidStr } from './string-helper';
/**
* 解析配色方案优先dark模式
*/
export function parseColorScheme(colorScheme: string): {
isDarkMode: boolean;
extraSchemeName: string | null;
} {
if (colorScheme === 'dark') {
return {
isDarkMode: true,
extraSchemeName: null,
};
} else if (colorScheme === 'light') {
return {
isDarkMode: false,
extraSchemeName: null,
};
} else if (colorScheme === 'auto') {
return {
isDarkMode: window.matchMedia
? window.matchMedia('(prefers-color-scheme: dark)').matches
: true,
extraSchemeName: null,
};
} else {
// 可能是插件 for example: dark+miku
let [base, name] = colorScheme.split('+');
if (!isValidStr(name)) {
name = base;
base = 'dark';
}
return {
isDarkMode: base === 'dark',
extraSchemeName: `theme-${name}`,
};
}
}