2022-01-07 13:22:13 +08:00
|
|
|
import type { GlobalThemeOverrides } from 'naive-ui';
|
2022-01-22 00:48:17 +08:00
|
|
|
import { cloneDeep, kebabCase } from 'lodash-es';
|
|
|
|
import { themeSetting } from '@/settings';
|
|
|
|
import { getThemeColor, getColorPalette, addColorAlpha } from '@/utils';
|
|
|
|
|
|
|
|
/** 获取主题配置 */
|
|
|
|
export function getThemeSettings() {
|
|
|
|
const themeColor = getThemeColor() || themeSetting.themeColor;
|
|
|
|
const info = themeSetting.isCustomizeInfoColor ? themeSetting.otherColor.info : getColorPalette(themeColor, 7);
|
|
|
|
const otherColor = { ...themeSetting.otherColor, info };
|
|
|
|
const setting = cloneDeep({ ...themeSetting, themeColor, otherColor });
|
|
|
|
return setting;
|
|
|
|
}
|
2022-01-04 02:20:32 +08:00
|
|
|
|
|
|
|
type ColorType = 'primary' | 'info' | 'success' | 'warning' | 'error';
|
|
|
|
type ColorScene = '' | 'Suppl' | 'Hover' | 'Pressed' | 'Active';
|
|
|
|
type ColorKey = `${ColorType}Color${ColorScene}`;
|
|
|
|
type ThemeColor = {
|
|
|
|
[key in ColorKey]?: string;
|
|
|
|
};
|
|
|
|
interface ColorAction {
|
|
|
|
scene: ColorScene;
|
|
|
|
handler: (color: string) => string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 获取主题颜色的各种场景对应的颜色 */
|
2022-01-07 13:22:13 +08:00
|
|
|
function getThemeColors(colors: [ColorType, string][]) {
|
2022-01-04 02:20:32 +08:00
|
|
|
const colorActions: ColorAction[] = [
|
2022-03-12 16:21:40 +08:00
|
|
|
{ scene: '', handler: (color) => color },
|
|
|
|
{ scene: 'Suppl', handler: (color) => color },
|
|
|
|
{ scene: 'Hover', handler: (color) => getColorPalette(color, 5) },
|
|
|
|
{ scene: 'Pressed', handler: (color) => getColorPalette(color, 7) },
|
|
|
|
{ scene: 'Active', handler: (color) => addColorAlpha(color, 0.1) },
|
2022-01-04 02:20:32 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
const themeColor: ThemeColor = {};
|
|
|
|
|
2022-03-12 16:21:40 +08:00
|
|
|
colors.forEach((color) => {
|
|
|
|
colorActions.forEach((action) => {
|
2022-01-04 02:20:32 +08:00
|
|
|
const [colorType, colorValue] = color;
|
|
|
|
const colorKey: ColorKey = `${colorType}Color${action.scene}`;
|
|
|
|
themeColor[colorKey] = action.handler(colorValue);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return themeColor;
|
|
|
|
}
|
2022-01-05 01:35:32 +08:00
|
|
|
|
2022-01-07 13:22:13 +08:00
|
|
|
/** 获取naive的主题颜色 */
|
2022-01-21 23:59:14 +08:00
|
|
|
export function getNaiveThemeOverrides(colors: Record<ColorType, string>): GlobalThemeOverrides {
|
2022-01-22 00:48:17 +08:00
|
|
|
const { primary, success, warning, error } = colors;
|
|
|
|
|
|
|
|
const info = themeSetting.isCustomizeInfoColor ? colors.info : getColorPalette(primary, 7);
|
|
|
|
|
2022-01-07 13:22:13 +08:00
|
|
|
const themeColors = getThemeColors([
|
|
|
|
['primary', primary],
|
|
|
|
['info', info],
|
|
|
|
['success', success],
|
|
|
|
['warning', warning],
|
2022-03-12 16:21:40 +08:00
|
|
|
['error', error],
|
2022-01-07 13:22:13 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
const colorLoading = primary;
|
|
|
|
|
|
|
|
return {
|
|
|
|
common: {
|
2022-03-12 16:21:40 +08:00
|
|
|
...themeColors,
|
2022-01-07 13:22:13 +08:00
|
|
|
},
|
|
|
|
LoadingBar: {
|
2022-03-12 16:21:40 +08:00
|
|
|
colorLoading,
|
|
|
|
},
|
2022-01-07 13:22:13 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
type ThemeVars = Exclude<GlobalThemeOverrides['common'], undefined>;
|
|
|
|
type ThemeVarsKeys = keyof ThemeVars;
|
|
|
|
|
|
|
|
/** 添加css vars至html */
|
2022-01-16 20:13:11 +08:00
|
|
|
export function addThemeCssVarsToHtml(themeVars: ThemeVars) {
|
2022-01-07 13:22:13 +08:00
|
|
|
const keys = Object.keys(themeVars) as ThemeVarsKeys[];
|
|
|
|
const style: string[] = [];
|
2022-03-12 16:21:40 +08:00
|
|
|
keys.forEach((key) => {
|
2022-01-07 13:22:13 +08:00
|
|
|
style.push(`--${kebabCase(key)}: ${themeVars[key]}`);
|
|
|
|
});
|
|
|
|
const styleStr = style.join(';');
|
2022-01-22 00:48:17 +08:00
|
|
|
document.documentElement.style.cssText += styleStr;
|
2022-01-07 13:22:13 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:35:32 +08:00
|
|
|
/** windicss 暗黑模式 */
|
|
|
|
export function handleWindicssDarkMode() {
|
|
|
|
const DARK_CLASS = 'dark';
|
|
|
|
function addDarkClass() {
|
2022-01-07 13:22:13 +08:00
|
|
|
document.documentElement.classList.add(DARK_CLASS);
|
2022-01-05 01:35:32 +08:00
|
|
|
}
|
|
|
|
function removeDarkClass() {
|
2022-01-07 13:22:13 +08:00
|
|
|
document.documentElement.classList.remove(DARK_CLASS);
|
2022-01-05 01:35:32 +08:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
addDarkClass,
|
2022-03-12 16:21:40 +08:00
|
|
|
removeDarkClass,
|
2022-01-05 01:35:32 +08:00
|
|
|
};
|
|
|
|
}
|