Skip to content

Commit

Permalink
refactor: 重写灰色模式和色弱模式挂载方式
Browse files Browse the repository at this point in the history
  • Loading branch information
pany-ang committed Nov 14, 2024
1 parent 047909f commit ade5d80
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
7 changes: 5 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<script lang="ts" setup>
import { useTheme } from "@/hooks/useTheme"
import { useGreyAndColorWeakness } from "@/hooks/useGreyAndColorWeakness"
import { ElNotification } from "element-plus"
// 将 Element Plus 的语言设置为中文
import zhCn from "element-plus/es/locale/lang/zh-cn"
import zhCn from "element-plus/es/locale/lang/zh-cn" // Element Plus 中文包
const { initTheme } = useTheme()
const { initGreyAndColorWeakness } = useGreyAndColorWeakness()
/** 初始化主题 */
initTheme()
/** 初始化灰色模式和色弱模式 */
initGreyAndColorWeakness()
/** 作者小心思 */
ElNotification({
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useGreyAndColorWeakness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { watchEffect } from "vue"
import { useSettingsStore } from "@/store/modules/settings"

const GREY_MODE = "grey-mode"
const COLOR_WEAKNESS = "color-weakness"
const classList = document.documentElement.classList

/** 初始化 */
const initGreyAndColorWeakness = () => {
const settingsStore = useSettingsStore()
watchEffect(() => {
settingsStore.showGreyMode ? classList.add(GREY_MODE) : classList.remove(GREY_MODE)
settingsStore.showColorWeakness ? classList.add(COLOR_WEAKNESS) : classList.remove(COLOR_WEAKNESS)
})
}

/** 灰色模式和色弱模式 hook */
export function useGreyAndColorWeakness() {
return { initGreyAndColorWeakness }
}
13 changes: 10 additions & 3 deletions src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,23 @@ const setTheme = (value: ThemeName) => {
}

/** 在 html 根元素上挂载 class */
const setHtmlRootClassName = (value: ThemeName) => {
document.documentElement.className = value
const addHtmlClass = (value: ThemeName) => {
document.documentElement.classList.add(value)
}

/** 在 html 根元素上移除其他主题 class */
const removeHtmlClass = (value: ThemeName) => {
const otherThemeNameList = themeList.map((item) => item.name).filter((name) => name !== value)
document.documentElement.classList.remove(...otherThemeNameList)
}

/** 初始化 */
const initTheme = () => {
// watchEffect 来收集副作用
watchEffect(() => {
const value = activeThemeName.value
setHtmlRootClassName(value)
removeHtmlClass(value)
addHtmlClass(value)
setActiveThemeName(value)
})
}
Expand Down
23 changes: 3 additions & 20 deletions src/layouts/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts" setup>
import { computed, watchEffect } from "vue"
import { watchEffect } from "vue"
import { storeToRefs } from "pinia"
import { useSettingsStore } from "@/store/modules/settings"
import useResize from "./hooks/useResize"
Expand All @@ -19,14 +19,7 @@ const { setWatermark, clearWatermark } = useWatermark()
const { isMobile } = useDevice()
const { isLeft, isTop, isLeftTop } = useLayoutMode()
const settingsStore = useSettingsStore()
const { showSettings, showTagsView, showWatermark, showGreyMode, showColorWeakness } = storeToRefs(settingsStore)
const classes = computed(() => {
return {
showGreyMode: showGreyMode.value,
showColorWeakness: showColorWeakness.value
}
})
const { showSettings, showTagsView, showWatermark } = storeToRefs(settingsStore)
//#region 隐藏标签栏时删除其高度,是为了让 Logo 组件高度和 Header 区域高度始终一致
const cssVarName = "--v3-tagsview-height"
Expand All @@ -43,7 +36,7 @@ watchEffect(() => {
</script>

<template>
<div :class="classes">
<div>
<!-- 左侧模式 -->
<LeftMode v-if="isLeft || isMobile" />
<!-- 顶部模式 -->
Expand All @@ -56,13 +49,3 @@ watchEffect(() => {
</RightPanel>
</div>
</template>

<style lang="scss" scoped>
.showGreyMode {
filter: grayscale(1);
}
.showColorWeakness {
filter: invert(0.8);
}
</style>
8 changes: 8 additions & 0 deletions src/styles/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@

html {
height: 100%;
// 灰色模式
&.grey-mode {
filter: grayscale(1);
}
// 色弱模式
&.color-weakness {
filter: invert(0.8);
}
}

body {
Expand Down

0 comments on commit ade5d80

Please sign in to comment.