perf: 将 layouts 配置文件迁移到 layouts 目录

This commit is contained in:
pany 2024-11-26 18:13:39 +08:00
parent 8a224cc946
commit cc65b1d6c7
4 changed files with 24 additions and 24 deletions

View File

@ -1,7 +1,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import { useLayoutMode } from "@/composables/useLayoutMode" import { useLayoutMode } from "@/composables/useLayoutMode"
import { useSettingsStore } from "@/pinia/stores/settings" import { useSettingsStore } from "@/pinia/stores/settings"
import { removeConfigLayout } from "@/utils/cache/local-storage" import { removeLayoutsConfig } from "@/utils/cache/local-storage"
import { Refresh } from "@element-plus/icons-vue" import { Refresh } from "@element-plus/icons-vue"
import { storeToRefs } from "pinia" import { storeToRefs } from "pinia"
import { watchEffect } from "vue" import { watchEffect } from "vue"
@ -49,8 +49,8 @@ watchEffect(() => {
}) })
/** 重置项目配置 */ /** 重置项目配置 */
function resetConfigLayout() { function resetLayoutsConfig() {
removeConfigLayout() removeLayoutsConfig()
location.reload() location.reload()
} }
</script> </script>
@ -65,7 +65,7 @@ function resetConfigLayout() {
<span class="setting-name">{{ settingName }}</span> <span class="setting-name">{{ settingName }}</span>
<el-switch v-model="settingValue.value" :disabled="!isLeft && settingName === '固定 Header'" /> <el-switch v-model="settingValue.value" :disabled="!isLeft && settingName === '固定 Header'" />
</div> </div>
<el-button type="danger" :icon="Refresh" @click="resetConfigLayout"> <el-button type="danger" :icon="Refresh" @click="resetLayoutsConfig">
</el-button> </el-button>
</div> </div>

View File

@ -1,9 +1,9 @@
import { LayoutModeEnum } from "@/constants/app-key" import { LayoutModeEnum } from "@/constants/app-key"
import { getConfigLayout } from "@/utils/cache/local-storage" import { getLayoutsConfig } from "@/utils/cache/local-storage"
/** 项目配置类型 */ /** 项目配置类型 */
export interface LayoutSettings { export interface LayoutsConfig {
/** 是否显示 Settings Panel */ /** 是否显示设置按钮和面板 */
showSettings: boolean showSettings: boolean
/** 布局模式 */ /** 布局模式 */
layoutMode: LayoutModeEnum layoutMode: LayoutModeEnum
@ -13,7 +13,7 @@ export interface LayoutSettings {
showLogo: boolean showLogo: boolean
/** 是否固定 Header */ /** 是否固定 Header */
fixedHeader: boolean fixedHeader: boolean
/** 是否显示页脚 Footer */ /** 是否显示页脚 */
showFooter: boolean showFooter: boolean
/** 是否显示消息通知 */ /** 是否显示消息通知 */
showNotify: boolean showNotify: boolean
@ -34,7 +34,7 @@ export interface LayoutSettings {
} }
/** 默认配置 */ /** 默认配置 */
const defaultSettings: LayoutSettings = { const DEFAULT_CONFIG: LayoutsConfig = {
layoutMode: LayoutModeEnum.Left, layoutMode: LayoutModeEnum.Left,
showSettings: true, showSettings: true,
showTagsView: true, showTagsView: true,
@ -52,4 +52,4 @@ const defaultSettings: LayoutSettings = {
} }
/** 项目配置 */ /** 项目配置 */
export const layoutSettings: LayoutSettings = { ...defaultSettings, ...getConfigLayout() } export const layoutsConfig: LayoutsConfig = { ...DEFAULT_CONFIG, ...getLayoutsConfig() }

View File

@ -1,14 +1,14 @@
import type { LayoutSettings } from "@/config/layouts" import type { LayoutsConfig } from "@/layouts/config"
import type { Ref } from "vue" import type { Ref } from "vue"
import { layoutSettings } from "@/config/layouts" import { layoutsConfig } from "@/layouts/config"
import { pinia } from "@/pinia" import { pinia } from "@/pinia"
import { setConfigLayout } from "@/utils/cache/local-storage" import { setLayoutsConfig } from "@/utils/cache/local-storage"
import { defineStore } from "pinia" import { defineStore } from "pinia"
import { ref, watch } from "vue" import { ref, watch } from "vue"
type SettingsStore = { type SettingsStore = {
// 使用映射类型来遍历 layoutSettings 对象的键 // 使用映射类型来遍历 LayoutsConfig 对象的键
[Key in keyof LayoutSettings]: Ref<LayoutSettings[Key]> [Key in keyof LayoutsConfig]: Ref<LayoutsConfig[Key]>
} }
type SettingsStoreKey = keyof SettingsStore type SettingsStoreKey = keyof SettingsStore
@ -16,8 +16,8 @@ type SettingsStoreKey = keyof SettingsStore
export const useSettingsStore = defineStore("settings", () => { export const useSettingsStore = defineStore("settings", () => {
// 状态对象 // 状态对象
const state = {} as SettingsStore const state = {} as SettingsStore
// 遍历 layoutSettings 对象的键值对 // 遍历 LayoutsConfig 对象的键值对
for (const [key, value] of Object.entries(layoutSettings)) { for (const [key, value] of Object.entries(layoutsConfig)) {
// 使用类型断言来指定 key 的类型,将 value 包装在 ref 函数中,创建一个响应式变量 // 使用类型断言来指定 key 的类型,将 value 包装在 ref 函数中,创建一个响应式变量
const refValue = ref(value) const refValue = ref(value)
// @ts-expect-error ignore // @ts-expect-error ignore
@ -26,12 +26,12 @@ export const useSettingsStore = defineStore("settings", () => {
watch(refValue, () => { watch(refValue, () => {
// 缓存 // 缓存
const settings = getCacheData() const settings = getCacheData()
setConfigLayout(settings) setLayoutsConfig(settings)
}) })
} }
// 获取要缓存的数据:将 state 对象转化为 settings 对象 // 获取要缓存的数据:将 state 对象转化为 settings 对象
const getCacheData = () => { const getCacheData = () => {
const settings = {} as LayoutSettings const settings = {} as LayoutsConfig
for (const [key, value] of Object.entries(state)) { for (const [key, value] of Object.entries(state)) {
// @ts-expect-error ignore // @ts-expect-error ignore
settings[key as SettingsStoreKey] = value.value settings[key as SettingsStoreKey] = value.value

View File

@ -1,20 +1,20 @@
// 统一处理 localStorage // 统一处理 localStorage
import type { ThemeName } from "@/composables/useTheme" import type { ThemeName } from "@/composables/useTheme"
import type { LayoutSettings } from "@/config/layouts"
import type { SidebarClosed, SidebarOpened } from "@/constants/app-key" import type { SidebarClosed, SidebarOpened } from "@/constants/app-key"
import type { LayoutsConfig } from "@/layouts/config"
import type { TagView } from "@/pinia/stores/tags-view" import type { TagView } from "@/pinia/stores/tags-view"
import { CacheKey } from "@/constants/cache-key" import { CacheKey } from "@/constants/cache-key"
// #region 系统布局配置 // #region 系统布局配置
export function getConfigLayout() { export function getLayoutsConfig() {
const json = localStorage.getItem(CacheKey.CONFIG_LAYOUT) const json = localStorage.getItem(CacheKey.CONFIG_LAYOUT)
return json ? (JSON.parse(json) as LayoutSettings) : null return json ? (JSON.parse(json) as LayoutsConfig) : null
} }
export function setConfigLayout(settings: LayoutSettings) { export function setLayoutsConfig(settings: LayoutsConfig) {
localStorage.setItem(CacheKey.CONFIG_LAYOUT, JSON.stringify(settings)) localStorage.setItem(CacheKey.CONFIG_LAYOUT, JSON.stringify(settings))
} }
export function removeConfigLayout() { export function removeLayoutsConfig() {
localStorage.removeItem(CacheKey.CONFIG_LAYOUT) localStorage.removeItem(CacheKey.CONFIG_LAYOUT)
} }
// #endregion // #endregion