@bagelink/vue 1.9.59 → 1.9.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/composables/index.d.ts +2 -2
- package/dist/composables/index.d.ts.map +1 -1
- package/dist/composables/useLocalStore.d.ts +12 -0
- package/dist/composables/useLocalStore.d.ts.map +1 -0
- package/dist/index.cjs +13 -13
- package/dist/index.mjs +910 -904
- package/package.json +1 -1
- package/src/composables/index.ts +3 -28
- package/src/composables/useLocalStore.ts +43 -0
package/package.json
CHANGED
package/src/composables/index.ts
CHANGED
|
@@ -1,40 +1,15 @@
|
|
|
1
1
|
import type { BglFormSchemaT } from '@bagelink/vue'
|
|
2
2
|
|
|
3
|
-
import type { MaybeRefOrGetter
|
|
3
|
+
import type { MaybeRefOrGetter } from 'vue'
|
|
4
4
|
import { getFallbackSchema } from '@bagelink/vue'
|
|
5
|
-
import { computed,
|
|
5
|
+
import { computed, toValue } from 'vue'
|
|
6
6
|
|
|
7
7
|
export { useAddToCalendar } from './useAddToCalendar'
|
|
8
8
|
export { useDevice } from './useDevice'
|
|
9
9
|
export { useExcel } from './useExcel'
|
|
10
|
+
export { useLocalStore } from './useLocalStore'
|
|
10
11
|
export { usePolling } from './usePolling'
|
|
11
12
|
export { useTheme } from './useTheme'
|
|
12
|
-
|
|
13
|
-
const _localStoreCache: Record<string, Ref> = {}
|
|
14
|
-
let _storageListenerRegistered = false
|
|
15
|
-
|
|
16
|
-
export function useLocalStore() {
|
|
17
|
-
if (!_storageListenerRegistered) {
|
|
18
|
-
_storageListenerRegistered = true
|
|
19
|
-
window.addEventListener('storage', (e) => {
|
|
20
|
-
if (e.key && _localStoreCache[e.key]) {
|
|
21
|
-
_localStoreCache[e.key].value = e.newValue !== null ? JSON.parse(e.newValue) : null
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
return new Proxy({} as Record<string, Ref>, {
|
|
26
|
-
get(_, key: string) {
|
|
27
|
-
if (!_localStoreCache[key]) {
|
|
28
|
-
const stored = localStorage.getItem(key)
|
|
29
|
-
_localStoreCache[key] = ref(stored !== null ? JSON.parse(stored) : null)
|
|
30
|
-
watch(_localStoreCache[key], (val) => {
|
|
31
|
-
localStorage.setItem(key, JSON.stringify(val))
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
return _localStoreCache[key]
|
|
35
|
-
},
|
|
36
|
-
})
|
|
37
|
-
}
|
|
38
13
|
interface UseBglSchemaParamsT<T> {
|
|
39
14
|
schema?: MaybeRefOrGetter<BglFormSchemaT<T>>
|
|
40
15
|
columns?: MaybeRefOrGetter<string[]>
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Ref } from 'vue'
|
|
2
|
+
import { ref, watch } from 'vue'
|
|
3
|
+
|
|
4
|
+
type LocalStoreDefaults = Record<string, unknown>
|
|
5
|
+
type LocalStoreResult<T extends LocalStoreDefaults> = { [K in keyof T]: Ref<T[K]> }
|
|
6
|
+
type LocalStoreTyped<T extends LocalStoreDefaults> = { [K in keyof T]: Ref<T[K] | null> }
|
|
7
|
+
|
|
8
|
+
const _localStoreCache: Record<string, Ref> = {}
|
|
9
|
+
let _storageListenerRegistered = false
|
|
10
|
+
|
|
11
|
+
function _ensureListener() {
|
|
12
|
+
if (_storageListenerRegistered) return
|
|
13
|
+
_storageListenerRegistered = true
|
|
14
|
+
window.addEventListener('storage', (e) => {
|
|
15
|
+
if (e.key !== null && e.key in _localStoreCache) {
|
|
16
|
+
_localStoreCache[e.key].value = e.newValue !== null ? JSON.parse(e.newValue) : null
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function _getRef<T>(key: string, defaultValue: T | null = null): Ref<T | null> {
|
|
22
|
+
if (!(key in _localStoreCache)) {
|
|
23
|
+
const stored = localStorage.getItem(key)
|
|
24
|
+
_localStoreCache[key] = ref(stored !== null ? JSON.parse(stored) : defaultValue)
|
|
25
|
+
watch(_localStoreCache[key], (val) => {
|
|
26
|
+
localStorage.setItem(key, JSON.stringify(val))
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
return _localStoreCache[key] as Ref<T | null>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function useLocalStore<T extends LocalStoreDefaults>(defaults: T): LocalStoreResult<T>
|
|
33
|
+
// eslint-disable-next-line no-redeclare
|
|
34
|
+
export function useLocalStore<T extends LocalStoreDefaults>(): LocalStoreTyped<T>
|
|
35
|
+
// eslint-disable-next-line no-redeclare
|
|
36
|
+
export function useLocalStore<T extends LocalStoreDefaults>(defaults?: T) {
|
|
37
|
+
_ensureListener()
|
|
38
|
+
return new Proxy({} as Record<string, Ref>, {
|
|
39
|
+
get(_, key: string) {
|
|
40
|
+
return _getRef(key, defaults?.[key] ?? null)
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
}
|