refactor: ♻️ 优化主题和主题色集中监听,避免多处初始化
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import defaultSettings from "@/settings";
|
||||
import { genMixColor } from "@/utils/color";
|
||||
import { setStyleProperty } from "@/utils";
|
||||
import { ThemeEnum } from "@/enums/ThemeEnum";
|
||||
|
||||
type SettingsValue = boolean | string;
|
||||
|
||||
@@ -32,6 +35,33 @@ export const useSettingsStore = defineStore("setting", () => {
|
||||
defaultSettings.watermarkEnabled
|
||||
);
|
||||
|
||||
watch(
|
||||
[theme, themeColor],
|
||||
([newTheme, newThemeColor], [oldTheme, oldThemeColor]) => {
|
||||
if (newTheme !== oldTheme) {
|
||||
if (newTheme === ThemeEnum.DARK) {
|
||||
document.documentElement.classList.add("dark");
|
||||
} else {
|
||||
document.documentElement.classList.remove("dark");
|
||||
}
|
||||
}
|
||||
|
||||
if (newThemeColor !== oldThemeColor) {
|
||||
const { DEFAULT, dark, light } = genMixColor(newThemeColor);
|
||||
setStyleProperty(`--el-color-primary`, DEFAULT);
|
||||
setStyleProperty(`--el-color-primary-dark-2`, dark[2]);
|
||||
setStyleProperty(`--el-color-primary-light-3`, light[3]);
|
||||
setStyleProperty(`--el-color-primary-light-5`, light[5]);
|
||||
setStyleProperty(`--el-color-primary-light-7`, light[7]);
|
||||
setStyleProperty(`--el-color-primary-light-8`, light[8]);
|
||||
setStyleProperty(`--el-color-primary-light-9`, light[9]);
|
||||
}
|
||||
},
|
||||
{
|
||||
immediate: true, // 立即执行,确保在侦听器创建时执行一次
|
||||
}
|
||||
);
|
||||
|
||||
const settingsMap: Record<string, Ref<SettingsValue>> = {
|
||||
fixedHeader,
|
||||
tagsView,
|
||||
@@ -62,9 +92,12 @@ export const useSettingsStore = defineStore("setting", () => {
|
||||
|
||||
/**
|
||||
* 切换主题颜色
|
||||
*
|
||||
* @param color 主题颜色
|
||||
*
|
||||
*/
|
||||
function changeThemeColor(val: string) {
|
||||
themeColor.value = val;
|
||||
function changeThemeColor(color: string) {
|
||||
themeColor.value = color;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user