|
@@ -1,10 +1,8 @@
|
|
|
import { useStorage } from '@vueuse/core'
|
|
|
-import { mergeWith, flatMapDeep, keys, sortBy, isEqual, isPlainObject } from 'lodash-es'
|
|
|
import { acceptHMRUpdate, defineStore } from 'pinia'
|
|
|
import { ref, watch } from 'vue'
|
|
|
|
|
|
-import { useTabsStore } from './tabs'
|
|
|
-import { useUserStore } from './user'
|
|
|
+import { mergeWithArrayReplace } from '@/utils/lodash-helpers'
|
|
|
|
|
|
import type { WatermarkProps } from 'naive-ui'
|
|
|
|
|
@@ -32,7 +30,7 @@ export interface PreferencesOptions {
|
|
|
noiseOpacity: number
|
|
|
}
|
|
|
|
|
|
-const DEFAULT_PREFERENCES_OPTIONS: PreferencesOptions = {
|
|
|
+export const DEFAULT_PREFERENCES_OPTIONS: PreferencesOptions = {
|
|
|
menu: {
|
|
|
collapsed: false,
|
|
|
width: 64,
|
|
@@ -75,32 +73,13 @@ 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)) {
|
|
|
- return srcValue
|
|
|
- }
|
|
|
- })
|
|
|
+ preferences.value = mergeWithArrayReplace(preferences.value, options)
|
|
|
}
|
|
|
|
|
|
const setLayoutSlideDirection = (direction: LayoutSlideDirection) => {
|
|
@@ -111,14 +90,6 @@ export const usePreferencesStore = defineStore('preferencesStore', () => {
|
|
|
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) => {
|