refactor: 优化 useTheme 代码结构
This commit is contained in:
parent
ebfa35d92f
commit
5d641ace1f
@ -1,19 +1,22 @@
|
|||||||
import { ref } from "vue"
|
import { ref } from "vue"
|
||||||
import { getActiveThemeName, setActiveThemeName } from "@/utils/cache/localStorage"
|
import { getActiveThemeName, setActiveThemeName } from "@/utils/cache/localStorage"
|
||||||
|
|
||||||
|
const DEFAULT_THEME_NAME = "normal"
|
||||||
|
type DefaultThemeNameType = typeof DEFAULT_THEME_NAME
|
||||||
|
|
||||||
|
/** 注册的主题名称, 其中 DefaultThemeNameType 是必填的 */
|
||||||
|
export type ThemeName = DefaultThemeNameType | "dark" | "dark-blue"
|
||||||
|
|
||||||
interface IThemeList {
|
interface IThemeList {
|
||||||
title: string
|
title: string
|
||||||
name: ThemeName
|
name: ThemeName
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 注册的主题名称, 其中 normal 是必填的 */
|
|
||||||
export type ThemeName = "normal" | "dark" | "dark-blue"
|
|
||||||
|
|
||||||
/** 主题列表 */
|
/** 主题列表 */
|
||||||
const themeList: IThemeList[] = [
|
const themeList: IThemeList[] = [
|
||||||
{
|
{
|
||||||
title: "默认",
|
title: "默认",
|
||||||
name: "normal"
|
name: DEFAULT_THEME_NAME
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "黑暗",
|
title: "黑暗",
|
||||||
@ -26,7 +29,7 @@ const themeList: IThemeList[] = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
/** 正在应用的主题名称 */
|
/** 正在应用的主题名称 */
|
||||||
const activeThemeName = ref<ThemeName>(getActiveThemeName() || "normal")
|
const activeThemeName = ref<ThemeName>(getActiveThemeName() || DEFAULT_THEME_NAME)
|
||||||
|
|
||||||
const initTheme = () => {
|
const initTheme = () => {
|
||||||
setHtmlClassName(activeThemeName.value)
|
setHtmlClassName(activeThemeName.value)
|
||||||
@ -34,8 +37,8 @@ const initTheme = () => {
|
|||||||
|
|
||||||
const setTheme = (value: ThemeName) => {
|
const setTheme = (value: ThemeName) => {
|
||||||
activeThemeName.value = value
|
activeThemeName.value = value
|
||||||
setHtmlClassName(activeThemeName.value)
|
setHtmlClassName(value)
|
||||||
setActiveThemeName(activeThemeName.value)
|
setActiveThemeName(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 在 html 根元素上挂载 class */
|
/** 在 html 根元素上挂载 class */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user