@bagelink/vue 1.9.56 → 1.9.61

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bagelink/vue",
3
3
  "type": "module",
4
- "version": "1.9.56",
4
+ "version": "1.9.61",
5
5
  "description": "Bagel core sdk packages",
6
6
  "author": {
7
7
  "name": "Bagel Studio",
@@ -1,5 +1,5 @@
1
1
  <script lang="ts" setup>
2
- import { Btn, CheckInput, TextInput, ToggleInput, localRef, ListItem, Dropdown, useI18n } from '@bagelink/vue'
2
+ import { Btn, CheckInput, TextInput, ToggleInput, useLocalStore, ListItem, Dropdown, useI18n } from '@bagelink/vue'
3
3
  import { computed, ref, watch, nextTick, onUnmounted } from 'vue'
4
4
  import SpreadsheetTable from './SpreadsheetTable.vue'
5
5
 
@@ -229,7 +229,7 @@ interface CellPosition {
229
229
  const isSelecting = ref(false)
230
230
  const selectionStart = ref<CellPosition | null>(null)
231
231
  const selectionEnd = ref<CellPosition | null>(null)
232
- const wrapText = localRef('wrapText', false)
232
+ const { wrapText } = useLocalStore()
233
233
 
234
234
  // Reactive variable to track the currently editing cell
235
235
  const editingCell = ref<CellPosition | null>(null)
@@ -1,12 +1,13 @@
1
- import type { BglFormSchemaT, IfAny } from '@bagelink/vue'
1
+ import type { BglFormSchemaT } from '@bagelink/vue'
2
2
 
3
- import type { MaybeRefOrGetter, Ref, UnwrapRef } from 'vue'
3
+ import type { MaybeRefOrGetter } from 'vue'
4
4
  import { getFallbackSchema } from '@bagelink/vue'
5
- import { computed, ref, toValue, watch } from 'vue'
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
  interface UseBglSchemaParamsT<T> {
@@ -31,22 +32,4 @@ export function useBglSchema<T = { [key: string]: unknown }>(
31
32
  return getFallbackSchema(data, _columns.value)
32
33
  }
33
34
 
34
- export function localRef<T>(
35
- key: string,
36
- initialValue: T
37
- ): [T] extends [Ref<any, any>] ?
38
- IfAny<T, Ref<T, T>, T> :
39
- Ref<UnwrapRef<T>, T | UnwrapRef<T>> {
40
- const storedValue = localStorage.getItem(key)
41
- const initial = storedValue !== null ? JSON.parse(storedValue) : initialValue
42
- const value = ref<T>(initial)
43
-
44
- watch(() => value.value, (val) => {
45
- localStorage.setItem(key, JSON.stringify(val))
46
- }, { immediate: true, deep: true })
47
-
48
- return value as any
49
- }
50
-
51
- export const useLocalStorage = localRef
52
35
  export { useValidateFieldValue } from './useValidateFieldValue'
@@ -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
+ }