2022-01-07 13:22:13 +08:00
|
|
|
|
import type { GlobalThemeOverrides } from 'naive-ui';
|
2022-04-21 00:43:17 +08:00
|
|
|
|
import { cloneDeep } from 'lodash-es';
|
2022-01-22 00:48:17 +08:00
|
|
|
|
import { themeSetting } from '@/settings';
|
2022-06-20 23:23:55 +08:00
|
|
|
|
import { EnumStorageKey } from '@/enum';
|
2022-08-10 21:31:59 +08:00
|
|
|
|
import { addColorAlpha, getColorPalette, getLocal, getThemeColor, removeLocal, setLocal } from '@/utils';
|
2022-06-20 23:23:55 +08:00
|
|
|
|
|
|
|
|
|
/** 初始化主题配置 */
|
|
|
|
|
export function initThemeSettings() {
|
|
|
|
|
const isProd = import.meta.env.PROD;
|
|
|
|
|
// 生产环境才缓存主题配置,本地开发实时调整配置更改配置的json
|
|
|
|
|
const storageSettings = getThemeSettings();
|
|
|
|
|
if (isProd && storageSettings) {
|
|
|
|
|
return storageSettings;
|
|
|
|
|
}
|
2022-01-22 00:48:17 +08:00
|
|
|
|
|
|
|
|
|
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}`;
|
2022-05-19 00:15:37 +08:00
|
|
|
|
type ThemeColor = Partial<Record<ColorKey, string>>;
|
|
|
|
|
|
2022-01-04 02:20:32 +08:00
|
|
|
|
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-04-01 14:47:57 +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-04-01 14:47:57 +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-04-01 14:47:57 +08:00
|
|
|
|
['error', error]
|
2022-01-07 13:22:13 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const colorLoading = primary;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
common: {
|
2022-04-01 14:47:57 +08:00
|
|
|
|
...themeColors
|
2022-01-07 13:22:13 +08:00
|
|
|
|
},
|
|
|
|
|
LoadingBar: {
|
2022-04-01 14:47:57 +08:00
|
|
|
|
colorLoading
|
|
|
|
|
}
|
2022-01-07 13:22:13 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
2022-06-20 23:23:55 +08:00
|
|
|
|
|
|
|
|
|
/** 获取缓存中的主题配置 */
|
|
|
|
|
function getThemeSettings() {
|
|
|
|
|
return getLocal<Theme.Setting>(EnumStorageKey['theme-settings']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 获取缓存中的主题配置 */
|
|
|
|
|
export function setThemeSettings(settings: Theme.Setting) {
|
|
|
|
|
return setLocal(EnumStorageKey['theme-settings'], settings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 清除缓存配置 */
|
|
|
|
|
export function clearThemeSettings() {
|
|
|
|
|
removeLocal(EnumStorageKey['theme-settings']);
|
|
|
|
|
}
|