|
@@ -1,19 +1,21 @@
|
|
|
import { useStorage } from '@vueuse/core'
|
|
|
-import { mergeWith } from 'lodash-es'
|
|
|
+import { mergeWith, flatMapDeep, keys, sortBy, isEqual, isPlainObject } from 'lodash-es'
|
|
|
import { acceptHMRUpdate, defineStore } from 'pinia'
|
|
|
-import { watch } from 'vue'
|
|
|
+import { ref, watch } from 'vue'
|
|
|
+
|
|
|
+import { useTabsStore } from './tabs'
|
|
|
+import { useUserStore } from './user'
|
|
|
|
|
|
import type { WatermarkProps } from 'naive-ui'
|
|
|
|
|
|
+export type LayoutSlideDirection = 'left' | 'right' | null
|
|
|
+
|
|
|
export interface PreferencesOptions {
|
|
|
menu: Partial<{
|
|
|
collapsed: boolean
|
|
|
width: number
|
|
|
maxWidth: number
|
|
|
}>
|
|
|
- mobile: {
|
|
|
- mainLayoutSlider: 'left' | 'right' | null
|
|
|
- }
|
|
|
shouldRefreshTab: boolean
|
|
|
showFooter: boolean
|
|
|
showLogo: boolean
|
|
@@ -36,9 +38,6 @@ const DEFAULT_PREFERENCES_OPTIONS: PreferencesOptions = {
|
|
|
width: 64,
|
|
|
maxWidth: 272,
|
|
|
},
|
|
|
- mobile: {
|
|
|
- mainLayoutSlider: null,
|
|
|
- },
|
|
|
shouldRefreshTab: false,
|
|
|
showFooter: true,
|
|
|
showTabs: true,
|
|
@@ -76,9 +75,26 @@ const DEFAULT_PREFERENCES_OPTIONS: PreferencesOptions = {
|
|
|
noiseOpacity: 0.02,
|
|
|
}
|
|
|
|
|
|
+function getAllKeys(obj: Record<string, any>, prefix = ''): string[] {
|
|
|
+ return flatMapDeep(keys(obj), (key) => {
|
|
|
+ const fullKey = prefix ? `${prefix}.${key}` : key
|
|
|
+ return isPlainObject(obj[key])
|
|
|
+ ? [fullKey, ...getAllKeys(obj[key] as Record<string, any>, fullKey)]
|
|
|
+ : fullKey
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function haveSameKeys<T extends object, U extends object>(a: T, b: U): boolean {
|
|
|
+ const keysA = sortBy(getAllKeys(a as Record<string, any>))
|
|
|
+ const keysB = sortBy(getAllKeys(b as Record<string, any>))
|
|
|
+ return isEqual(keysA, keysB)
|
|
|
+}
|
|
|
+
|
|
|
export const usePreferencesStore = defineStore('preferencesStore', () => {
|
|
|
const preferences = useStorage<PreferencesOptions>('preferences', DEFAULT_PREFERENCES_OPTIONS)
|
|
|
|
|
|
+ const layoutSlideDirection = ref<LayoutSlideDirection>(null)
|
|
|
+
|
|
|
const modify = (options: Partial<PreferencesOptions>) => {
|
|
|
preferences.value = mergeWith({}, preferences.value, options, (objValue, srcValue) => {
|
|
|
if (Array.isArray(objValue) && Array.isArray(srcValue)) {
|
|
@@ -87,10 +103,22 @@ export const usePreferencesStore = defineStore('preferencesStore', () => {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ const setLayoutSlideDirection = (direction: LayoutSlideDirection) => {
|
|
|
+ layoutSlideDirection.value = direction
|
|
|
+ }
|
|
|
+
|
|
|
const reset = () => {
|
|
|
preferences.value = structuredClone(DEFAULT_PREFERENCES_OPTIONS)
|
|
|
}
|
|
|
|
|
|
+ const oldLocalStorage = localStorage.getItem('configure')
|
|
|
+ if (oldLocalStorage || haveSameKeys(preferences.value, DEFAULT_PREFERENCES_OPTIONS)) {
|
|
|
+ useTabsStore().clearTabs()
|
|
|
+ reset()
|
|
|
+ useUserStore().cleanup()
|
|
|
+ localStorage.clear()
|
|
|
+ }
|
|
|
+
|
|
|
watch(
|
|
|
() => preferences.value.enableTextSelect,
|
|
|
(newValue) => {
|
|
@@ -103,6 +131,8 @@ export const usePreferencesStore = defineStore('preferencesStore', () => {
|
|
|
|
|
|
return {
|
|
|
preferences,
|
|
|
+ layoutSlideDirection,
|
|
|
+ setLayoutSlideDirection,
|
|
|
reset,
|
|
|
modify,
|
|
|
}
|