From 68229a4d2f58dd784c4416b88b9b98e7ba2a5ac0 Mon Sep 17 00:00:00 2001 From: vben Date: Sun, 9 Jun 2024 12:53:38 +0800 Subject: [PATCH] chore: remove flatPreferences --- .../@core/forward/preferences/src/index.ts | 8 +- .../forward/preferences/src/preferences.ts | 65 ++--- .../@core/forward/preferences/src/types.ts | 5 +- .../preferences/src/use-preferences.ts | 27 +- .../authentication/widgets/layout-toggle.vue | 18 +- .../preferences/blocks/general/animation.vue | 16 +- .../preferences/blocks/general/general.vue | 12 +- .../common-ui/src/preferences/blocks/index.ts | 2 +- .../preferences/blocks/layout/breadcrumb.vue | 37 ++- .../src/preferences/blocks/layout/footer.vue | 6 +- .../src/preferences/blocks/layout/header.vue | 6 +- .../preferences/blocks/layout/navigation.vue | 4 +- .../src/preferences/blocks/layout/sidebar.vue | 16 +- .../blocks/layout/{tabs.vue => tabbar.vue} | 8 +- .../preferences/blocks/theme/color-mode.vue | 8 +- .../src/preferences/blocks/theme/theme.vue | 4 +- .../src/preferences/preferences-widget.vue | 256 +++++++++++++++--- .../common-ui/src/preferences/preferences.vue | 141 ++++++---- .../src/theme-toggle/theme-toggle.vue | 9 +- .../business/layouts/src/basic/layout.vue | 38 ++- 20 files changed, 462 insertions(+), 224 deletions(-) rename packages/business/common-ui/src/preferences/blocks/layout/{tabs.vue => tabbar.vue} (66%) diff --git a/packages/@core/forward/preferences/src/index.ts b/packages/@core/forward/preferences/src/index.ts index 011b814e..be82caac 100644 --- a/packages/@core/forward/preferences/src/index.ts +++ b/packages/@core/forward/preferences/src/index.ts @@ -1,5 +1,3 @@ -import type { Flatten } from '@vben-core/typings'; - import type { Preferences } from './types'; import { preferencesManager } from './preferences'; @@ -8,8 +6,8 @@ import { preferencesManager } from './preferences'; const preferences: Preferences = preferencesManager.getPreferences(); // 扁平化后的偏好设置 -const flatPreferences: Flatten = - preferencesManager.getFlatPreferences(); +// const flatPreferences: Flatten = +// preferencesManager.getFlatPreferences(); // 更新偏好设置 const updatePreferences = @@ -20,7 +18,7 @@ const resetPreferences = preferencesManager.resetPreferences.bind(preferencesManager); export { - flatPreferences, + // flatPreferences, preferences, preferencesManager, resetPreferences, diff --git a/packages/@core/forward/preferences/src/preferences.ts b/packages/@core/forward/preferences/src/preferences.ts index 19b27bda..5baf1532 100644 --- a/packages/@core/forward/preferences/src/preferences.ts +++ b/packages/@core/forward/preferences/src/preferences.ts @@ -1,15 +1,10 @@ -import type { - DeepPartial, - Flatten, - FlattenObjectKeys, -} from '@vben-core/typings'; +import type { DeepPartial } from '@vben-core/typings'; import type { Preferences } from './types'; -import { markRaw, reactive, watch } from 'vue'; +import { markRaw, reactive, readonly, watch } from 'vue'; import { StorageManager } from '@vben-core/cache'; -import { flattenObject, nestedObject } from '@vben-core/helpers'; import { convertToHslCssVar, merge } from '@vben-core/toolkit'; import { @@ -40,7 +35,7 @@ function isDarkTheme(theme: string) { class PreferenceManager { private cache: StorageManager | null = null; - private flattenedState: Flatten; + // private flattenedState: Flatten; private initialPreferences: Preferences = defaultPreferences; private isInitialized: boolean = false; private savePreferences: (preference: Preferences) => void; @@ -49,7 +44,7 @@ class PreferenceManager { }); constructor() { this.cache = new StorageManager(); - this.flattenedState = reactive(flattenObject(this.state)); + // this.flattenedState = reactive(flattenObject(this.state)); this.savePreferences = useDebounceFn( (preference: Preferences) => this._savePreferences(preference), @@ -113,28 +108,28 @@ class PreferenceManager { return; } - const debounceWaterState = useDebounceFn(() => { - const newFlattenedState = flattenObject(this.state); - for (const k in newFlattenedState) { - const key = k as FlattenObjectKeys; - this.flattenedState[key] = newFlattenedState[key]; - } - this.savePreferences(this.state); - }, 16); + // const debounceWaterState = useDebounceFn(() => { + // const newFlattenedState = flattenObject(this.state); + // for (const k in newFlattenedState) { + // const key = k as FlattenObjectKeys; + // this.flattenedState[key] = newFlattenedState[key]; + // } + // this.savePreferences(this.state); + // }, 16); - const debounceWaterFlattenedState = useDebounceFn( - (val: Flatten) => { - this.updateState(val); - this.savePreferences(this.state); - }, - 16, - ); + // const debounceWaterFlattenedState = useDebounceFn( + // (val: Flatten) => { + // this.updateState(val); + // this.savePreferences(this.state); + // }, + // 16, + // ); // 监听 state 的变化 - watch(this.state, debounceWaterState, { deep: true }); + // watch(this.state, debounceWaterState, { deep: true }); // 监听 flattenedState 的变化并触发 set 方法 - watch(this.flattenedState, debounceWaterFlattenedState, { deep: true }); + // watch(this.flattenedState, debounceWaterFlattenedState, { deep: true }); // 监听断点,判断是否移动端 const breakpoints = useBreakpoints(breakpointsTailwind); @@ -200,10 +195,10 @@ class PreferenceManager { * 将新的扁平对象转换为嵌套对象,并与当前状态合并。 * @param {FlattenObject} newValue - 新的扁平对象 */ - private updateState(newValue: Flatten) { - const nestObj = nestedObject(newValue, 2); - Object.assign(this.state, merge(nestObj, this.state)); - } + // private updateState(newValue: Flatten) { + // const nestObj = nestedObject(newValue, 2); + // Object.assign(this.state, merge(nestObj, this.state)); + // } /** * 更新主题 @@ -222,16 +217,16 @@ class PreferenceManager { } } - public getFlatPreferences() { - return this.flattenedState; - } + // public getFlatPreferences() { + // return this.flattenedState; + // } public getInitialPreferences() { return this.initialPreferences; } public getPreferences() { - return this.state; + return readonly(this.state); } /** @@ -291,7 +286,7 @@ class PreferenceManager { Object.assign(this.state, mergedState); - Object.assign(this.flattenedState, flattenObject(this.state)); + // Object.assign(this.flattenedState, flattenObject(this.state)); // 根据更新的键值执行相应的操作 this.handleUpdates(updates); diff --git a/packages/@core/forward/preferences/src/types.ts b/packages/@core/forward/preferences/src/types.ts index 17e016a1..fffb1f5d 100644 --- a/packages/@core/forward/preferences/src/types.ts +++ b/packages/@core/forward/preferences/src/types.ts @@ -10,7 +10,7 @@ type BreadcrumbStyleType = 'background' | 'normal'; type NavigationStyleType = 'plain' | 'rounded'; -type PageTransitionType = 'fade-slide'; +type PageTransitionType = 'fade' | 'fade-down' | 'fade-slide' | 'fade-up'; type AuthPageLayoutType = 'panel-center' | 'panel-left' | 'panel-right'; @@ -132,7 +132,7 @@ interface TransitionPreferences { /** 页面切换动画是否启用 */ enable: boolean; /** 页面切换动画 */ - name: PageTransitionType; + name: PageTransitionType | string; /** 是否开启页面加载进度动画 */ progress: boolean; } @@ -176,6 +176,7 @@ export type { LayoutType, LogoPreferences, NavigationPreferences, + NavigationStyleType, PageTransitionType, Preferences, PreferencesKeys, diff --git a/packages/@core/forward/preferences/src/use-preferences.ts b/packages/@core/forward/preferences/src/use-preferences.ts index 21fad0ad..27c923d2 100644 --- a/packages/@core/forward/preferences/src/use-preferences.ts +++ b/packages/@core/forward/preferences/src/use-preferences.ts @@ -6,7 +6,6 @@ import { isDarkTheme, preferencesManager } from './preferences'; function usePreferences() { const preferences = preferencesManager.getPreferences(); - const flatPreferences = preferencesManager.getFlatPreferences(); const initialPreferences = preferencesManager.getInitialPreferences(); /** * @zh_CN 计算偏好设置的变化 @@ -15,13 +14,15 @@ function usePreferences() { return diff(initialPreferences, preferences); }); + const appPreferences = computed(() => preferences.app); + /** * @zh_CN 判断是否为暗黑模式 * @param preferences - 当前偏好设置对象,它的主题值将被用来判断是否为暗黑模式。 * @returns 如果主题为暗黑模式,返回 true,否则返回 false。 */ const isDark = computed(() => { - return isDarkTheme(flatPreferences.appThemeMode); + return isDarkTheme(appPreferences.value.themeMode); }); const theme = computed(() => { @@ -32,39 +33,41 @@ function usePreferences() { * @zh_CN 布局方式 */ const layout = computed(() => - flatPreferences.appIsMobile ? 'side-nav' : flatPreferences.appLayout, + appPreferences.value.isMobile ? 'side-nav' : appPreferences.value.layout, ); /** * @zh_CN 是否全屏显示content,不需要侧边、底部、顶部、tab区域 */ const isFullContent = computed( - () => flatPreferences.appLayout === 'full-content', + () => appPreferences.value.layout === 'full-content', ); /** * @zh_CN 是否侧边导航模式 */ - const isSideNav = computed(() => flatPreferences.appLayout === 'side-nav'); + const isSideNav = computed(() => appPreferences.value.layout === 'side-nav'); /** * @zh_CN 是否侧边混合模式 */ const isSideMixedNav = computed( - () => flatPreferences.appLayout === 'side-mixed-nav', + () => appPreferences.value.layout === 'side-mixed-nav', ); /** * @zh_CN 是否为头部导航模式 */ const isHeaderNav = computed( - () => flatPreferences.appLayout === 'header-nav', + () => appPreferences.value.layout === 'header-nav', ); /** * @zh_CN 是否为混合导航模式 */ - const isMixedNav = computed(() => flatPreferences.appLayout === 'mixed-nav'); + const isMixedNav = computed( + () => appPreferences.value.layout === 'mixed-nav', + ); /** * @zh_CN 是否包含侧边导航模式 @@ -78,28 +81,28 @@ function usePreferences() { * 在tabs可见以及开启keep-alive的情况下才开启 */ const keepAlive = computed( - () => flatPreferences.tabbarKeepAlive && flatPreferences.tabbarEnable, + () => preferences.tabbar.enable && preferences.tabbar.keepAlive, ); /** * @zh_CN 登录注册页面布局是否为左侧 */ const authPanelLeft = computed(() => { - return flatPreferences.appAuthPageLayout === 'panel-left'; + return appPreferences.value.authPageLayout === 'panel-left'; }); /** * @zh_CN 登录注册页面布局是否为左侧 */ const authPanelRight = computed(() => { - return flatPreferences.appAuthPageLayout === 'panel-right'; + return appPreferences.value.authPageLayout === 'panel-right'; }); /** * @zh_CN 登录注册页面布局是否为中间 */ const authPanelCenter = computed(() => { - return flatPreferences.appAuthPageLayout === 'panel-center'; + return appPreferences.value.authPageLayout === 'panel-center'; }); return { diff --git a/packages/business/common-ui/src/authentication/widgets/layout-toggle.vue b/packages/business/common-ui/src/authentication/widgets/layout-toggle.vue index 17591f8b..1f450029 100644 --- a/packages/business/common-ui/src/authentication/widgets/layout-toggle.vue +++ b/packages/business/common-ui/src/authentication/widgets/layout-toggle.vue @@ -5,7 +5,12 @@ import { computed } from 'vue'; import { $t } from '@vben/locales'; import { MdiDockBottom, MdiDockLeft, MdiDockRight } from '@vben-core/iconify'; -import { preferences, usePreferences } from '@vben-core/preferences'; +import { + type AuthPageLayoutType, + preferences, + updatePreferences, + usePreferences, +} from '@vben-core/preferences'; import { VbenDropdownRadioMenu, VbenIconButton } from '@vben-core/shadcn-ui'; defineOptions({ @@ -31,12 +36,21 @@ const menus = computed((): VbenDropdownMenuItem[] => [ ]); const { authPanelCenter, authPanelLeft, authPanelRight } = usePreferences(); + +function handleUpdate(value: string) { + updatePreferences({ + app: { + authPageLayout: value as AuthPageLayoutType, + }, + }); +}