@stacksjs/components 0.2.928 → 0.2.930

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.
@@ -51,6 +51,7 @@ export const wash = SETTINGS_NATIVE.rowWash
51
51
  */
52
52
  import { SettingsWindowTree } from './native'
53
53
  import { SettingsWindowView } from './view'
54
+ import { settingsStorageName } from './core'
54
55
  import { registerComponentApi } from '../../utils/component-api'
55
56
 
56
57
  const emit = defineEmits(['pane', 'search', 'navigate'])
@@ -76,10 +77,19 @@ const searchFocus = state(false)
76
77
  let stopCommands = null
77
78
  let unregister = null
78
79
 
79
- /** The app's own storage on a native host, which stx serves as localStorage. */
80
- const nativeStorage = {
81
- get: name => (typeof localStorage === 'undefined' ? null : localStorage.getItem(name)),
82
- set: (name, value) => { if (typeof localStorage !== 'undefined') localStorage.setItem(name, value) },
80
+ /*
81
+ * The last open pane, kept under `stx:settings:<key>`: local storage in a
82
+ * browser, the app's own storage on a native host. One signal for both, and
83
+ * no storage at all without a key. Made on mount, where the window has always
84
+ * read it, and stored as the bare pane id it has always been (no JSON quotes),
85
+ * so a pane remembered by an earlier version still opens.
86
+ */
87
+ const storageKey = {{ storageKey }}
88
+ const paneAsIs = { read: value => value, write: value => value }
89
+ let rememberedPane = null
90
+ const paneStorage = {
91
+ get: () => (rememberedPane ? rememberedPane() || null : null),
92
+ set: (_name, value) => { if (rememberedPane) rememberedPane.set(value) },
83
93
  }
84
94
 
85
95
  function onSearchInput(event) {
@@ -106,7 +116,7 @@ function focusSearch() {
106
116
  if (!settings) return
107
117
  if (!native) { settings.focusSearch(); return }
108
118
  searchFocus.set(false)
109
- setTimeout(() => searchFocus.set(true), 0)
119
+ nextTick(() => searchFocus.set(true))
110
120
  }
111
121
 
112
122
  const callbacks = {
@@ -121,13 +131,14 @@ const callbacks = {
121
131
  onMount(() => {
122
132
  const root = rootRef()
123
133
  if (!root) return
134
+ if (storageKey) rememberedPane = useLocalStorage(settingsStorageName(storageKey), '', { serializer: paneAsIs })
124
135
 
125
136
  if (native) {
126
137
  const craftHost = typeof craft === 'undefined' ? null : craft
127
138
  settings = new SettingsWindowTree(window.stx._host, root, {
128
139
  pane: asText(openPane()),
129
- storageKey: {{ storageKey }},
130
- storage: nativeStorage,
140
+ storageKey,
141
+ storage: paneStorage,
131
142
  wash: () => (craftHost && craftHost.appearance && craftHost.appearance.colorScheme() === 'dark' ? wash.dark : wash.light),
132
143
  }, {
133
144
  ...callbacks,
@@ -155,7 +166,8 @@ onMount(() => {
155
166
  else {
156
167
  settings = new SettingsWindowView(root, {
157
168
  pane: asText(openPane()),
158
- storageKey: {{ storageKey }},
169
+ storageKey,
170
+ storage: paneStorage,
159
171
  globalKeys: {{ globalKeys }},
160
172
  }, callbacks)
161
173
  }
@@ -8,13 +8,20 @@
8
8
  * back: which pane is shown, which item is selected, what a search lists.
9
9
  */
10
10
  import type { SettingsHistory, SettingsItemInfo, SettingsPaneInfo, SettingsRowInfo, SettingsSearchHit, SettingsTypeAhead } from './core'
11
+ import type { SettingsTreeStorage } from './native'
11
12
  import { emptySettingsHistory, findSettingsPane, resolveSettingsPane, searchSettings, SETTINGS_HIGHLIGHT_MS, settingsCanGoBack, settingsCanGoForward, settingsHistoryBack, settingsHistoryCurrent, settingsHistoryForward, settingsHistoryVisit, settingsParentPane, settingsResultsAnnouncement, settingsRovingIndex, settingsSidebarPane, settingsStorageName, settingsTypeAheadAdd, settingsTypeAheadMatch } from './core'
12
13
 
13
14
  export interface SettingsWindowOptions {
14
15
  /** The pane to open on. A stored pane wins over it; the first pane is the fallback. */
15
16
  pane?: string
16
- /** Remembers the open pane in local storage under `stx:settings:<key>`. */
17
+ /** Remembers the open pane under `stx:settings:<key>`. */
17
18
  storageKey?: string
19
+ /**
20
+ * Where the open pane is remembered. `<SettingsWindow>` passes its
21
+ * `useLocalStorage` signal; constructed directly, the view uses
22
+ * `localStorage`.
23
+ */
24
+ storage?: SettingsTreeStorage
18
25
  /**
19
26
  * Whether the shortcuts (find, back, forward) are heard anywhere in the
20
27
  * document, as in a window that is all Settings, or only inside the shell.
@@ -73,6 +80,7 @@ export class SettingsWindowView {
73
80
  private current = ''
74
81
  private query = ''
75
82
  private storageKey: string
83
+ private readonly storage: SettingsTreeStorage | null
76
84
  private typed: SettingsTypeAhead | null = null
77
85
  private hits: Array<{ hit: SettingsSearchHit, row: HTMLElement }> = []
78
86
  private highlighted: HTMLElement | null = null
@@ -93,6 +101,7 @@ export class SettingsWindowView {
93
101
 
94
102
  constructor(private readonly root: HTMLElement, options: SettingsWindowOptions = {}, private readonly callbacks: SettingsWindowCallbacks = {}) {
95
103
  this.storageKey = options.storageKey || ''
104
+ this.storage = options.storage ?? null
96
105
  if (!root.id)
97
106
  root.id = `settings-${Math.random().toString(36).slice(2, 8)}`
98
107
 
@@ -256,7 +265,8 @@ export class SettingsWindowView {
256
265
  if (!this.storageKey)
257
266
  return ''
258
267
  try {
259
- return localStorage.getItem(settingsStorageName(this.storageKey)) || ''
268
+ const name = settingsStorageName(this.storageKey)
269
+ return (this.storage ? this.storage.get(name) : localStorage.getItem(name)) || ''
260
270
  }
261
271
  catch {
262
272
  // Storage can be switched off, and a window that forgets still opens.
@@ -268,7 +278,11 @@ export class SettingsWindowView {
268
278
  if (!this.storageKey || !this.current)
269
279
  return
270
280
  try {
271
- localStorage.setItem(settingsStorageName(this.storageKey), this.current)
281
+ const name = settingsStorageName(this.storageKey)
282
+ if (this.storage)
283
+ this.storage.set(name, this.current)
284
+ else
285
+ localStorage.setItem(name, this.current)
272
286
  }
273
287
  catch {}
274
288
  }