From a6f2409e026a2e4f22011a882e6d14aa221d6377 Mon Sep 17 00:00:00 2001 From: cshaptx4869 <994774638@qq.com> Date: Mon, 4 Mar 2024 09:33:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20:recycle:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=B8=BB=E9=A2=98=E5=92=8C=E4=B8=BB=E9=A2=98=E8=89=B2=E9=9B=86?= =?UTF-8?q?=E4=B8=AD=E7=9B=91=E5=90=AC=EF=BC=8C=E9=81=BF=E5=85=8D=E5=A4=9A?= =?UTF-8?q?=E5=A4=84=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layout/components/Settings/index.vue | 28 +------ src/store/modules/settings.ts | 37 ++++++++- src/utils/index.ts | 12 +++ src/views/login/index.vue | 99 ++++++++++-------------- 4 files changed, 91 insertions(+), 85 deletions(-) diff --git a/src/layout/components/Settings/index.vue b/src/layout/components/Settings/index.vue index fd4b3255..fe0293e1 100644 --- a/src/layout/components/Settings/index.vue +++ b/src/layout/components/Settings/index.vue @@ -55,7 +55,6 @@ import { useSettingsStore, usePermissionStore, useAppStore } from "@/store"; import { Sunny, Moon } from "@element-plus/icons-vue"; import { LayoutEnum } from "@/enums/LayoutEnum"; import { ThemeEnum } from "@/enums/ThemeEnum"; -import { genMixColor } from "@/utils/color"; const route = useRoute(); const appStore = useAppStore(); @@ -76,30 +75,15 @@ const settingsVisible = computed({ */ function changeThemeColor(color: string) { settingsStore.changeThemeColor(color); - - const { DEFAULT, dark, light } = genMixColor(color); - 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]); -} - -function setStyleProperty(propName: string, value: string) { - document.documentElement.style.setProperty(propName, value); } /** * 切换主题 */ const isDark = ref(settingsStore.theme === ThemeEnum.DARK); -const changeTheme = (isDark: any) => { - useToggle(isDark); - const theme = isDark ? ThemeEnum.DARK : ThemeEnum.LIGHT; - settingsStore.changeTheme(theme); - document.documentElement.classList.toggle("dark", theme === ThemeEnum.DARK); +const changeTheme = (val: any) => { + isDark.value = val; + settingsStore.changeTheme(isDark.value ? ThemeEnum.DARK : ThemeEnum.LIGHT); }; /** @@ -148,11 +132,6 @@ function findOutermostParent(tree: any[], findName: string) { return null; } - -onMounted(() => { - changeTheme(settingsStore.theme == ThemeEnum.DARK); // 初始化主题 - changeThemeColor(settingsStore.themeColor); // 初始化主题颜色 -}); -@/utils/color diff --git a/src/store/modules/settings.ts b/src/store/modules/settings.ts index aad3de41..1a54daae 100644 --- a/src/store/modules/settings.ts +++ b/src/store/modules/settings.ts @@ -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> = { 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; } /** diff --git a/src/utils/index.ts b/src/utils/index.ts index 0d626608..fb42864a 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -30,6 +30,8 @@ export function removeClass(ele: HTMLElement, cls: string) { } /** + * 判断是否是外部链接 + * * @param {string} path * @returns {Boolean} */ @@ -37,3 +39,13 @@ export function isExternal(path: string) { const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path); return isExternal; } + +/** + * 设置Style属性 + * + * @param propName + * @param value + */ +export function setStyleProperty(propName: string, value: string) { + document.documentElement.style.setProperty(propName, value); +} diff --git a/src/views/login/index.vue b/src/views/login/index.vue index 0a139b21..0335e567 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -7,9 +7,7 @@ inline-prompt :active-icon="Moon" :inactive-icon="Sunny" - active-color="var(--el-fill-color-dark)" - inactive-color="var(--el-color-primary)" - @change="handleThemeChange" + @change="toggleTheme" /> @@ -117,61 +115,36 @@