dsh-sidebar-fold 0.1.6 → 0.1.13

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.
@@ -1,187 +1,223 @@
1
- /**
2
- * Browser half of `dsh-sidebar-fold`.
3
- *
4
- * Activation shape (each rule is an incident lesson, not a preference):
5
- * - the fold is mounted at the **top level** of `apply` and never waits on a
6
- * service, so a host without the settings surface still folds;
7
- * - the settings seat is registered at the top level too, inside its own guard:
8
- * a missing seat must not take the fold down with it;
9
- * - the two settings waists (`configForms` for 0.1.7+, `settingsScope` for
10
- * ≤0.1.6) are declared through separate `ctx.inject` calls — naming one that
11
- * a generation does not register would otherwise leave the whole fiber
12
- * pending and the plugin silently inert.
13
- */
14
- import { PKG_NAME } from './constants.ts'
15
- import { applyConfig, BUILTIN_CONFIG, installFold, normalizeAlign, rescan } from './fold.ts'
16
- import { createConfigCard, onFallback, onScope, type SettingsScopeLike } from './settings-card.ts'
17
-
18
- /** Cordis services the client half reads. Only `slots` is unconditional. */
19
- export const inject = ['slots']
20
-
21
- /** A tolerant view of the client context: every face is probed, never assumed. */
22
- interface ClientCtx {
23
- slots: {
24
- inject: (name: string, fn: () => unknown) => () => void
25
- register: (options: Record<string, unknown>, component: unknown) => () => void
26
- }
27
- inject?: (names: readonly string[], fn: (sctx: Record<string, any>) => void) => unknown
28
- effect?: (factory: () => () => void, label: string) => unknown
29
- get?: (name: string) => unknown
30
- }
31
-
32
- /** Fold + card wiring, run once the fold is installed. */
33
- let disposer: (() => void) | null = null
34
- /** Whether the fold found its anchors on the last scan (diagnostics only). */
35
- let mountedNow = false
36
-
37
- /** One guard per surface: a missing host face degrades that surface only. */
38
- function safe(tag: string, fn: () => void): void {
39
- try {
40
- fn()
41
- } catch (error) {
42
- console.warn(`${PKG_NAME}: surface '${tag}' unavailable:`, error)
43
- }
44
- }
45
-
46
- /** Adopt a served snapshot, falling back to the built-ins when it says nothing. */
47
- function adopt(scope: SettingsScopeLike): void {
48
- onScope(scope)
49
- const push = (): void => {
50
- const snap = scope.getSnapshot()
51
- const value = snap.value
52
- const next = {
53
- defaultFolded: typeof value?.defaultFolded === 'boolean' ? value.defaultFolded : BUILTIN_CONFIG.defaultFolded,
54
- keepLabels: Array.isArray(value?.keepLabels) ? value.keepLabels : BUILTIN_CONFIG.keepLabels,
55
- // Alignment is validated by `normalizeAlign` here so everything downstream
56
- // (fold marks, card radios) only ever sees one of the three known modes.
57
- align: normalizeAlign(typeof value?.align === 'string' ? value.align : BUILTIN_CONFIG.align),
58
- }
59
- onFallback(next)
60
- applyConfig(next)
61
- }
62
- push()
63
- scope.subscribe(push)
64
- }
65
-
66
- /**
67
- * Candidate namespaces for this bundle's served settings form.
68
- *
69
- * `ConfigForms.get(entryId)` keys forms by the *host plugin entry id*, and the
70
- * Plugin Manager only opens a form whose namespace appears in the served
71
- * `settings.describe` mirror. The entry id is the bundle row's id, which
72
- * `cordis.patch.yml` inserts as the package name; the scope-stripped variant is
73
- * tried as well because sibling plugins namespace their forms that way. Asking
74
- * for an unserved key is cheap (the form reports `unavailable`), so we probe
75
- * rather than guess, and adopt whichever key the host actually serves first.
76
- */
77
- const NAMESPACE_CANDIDATES = [PKG_NAME, PKG_NAME.replace(/^dsh-/, '')]
78
-
79
- /** Ask the host which of our candidate namespaces it serves, if any. */
80
- function servedNamespace(forms: any): string | undefined {
81
- const mirror = forms.describe?.()
82
- const served = new Set((mirror?.getSnapshot()?.view?.namespaces ?? []).map((entry: { ns: string }) => entry.ns))
83
- return NAMESPACE_CANDIDATES.find((candidate) => served.has(candidate))
84
- }
85
-
86
- /**
87
- * Bind the two generations' settings waists, each behind its own inject.
88
- *
89
- * The `configForms` probe is **not** one-shot: the settings mirror is still
90
- * loading when a client half activates, so `view` can be `undefined` at that
91
- * instant. First smoke run proved the cost of probing once — the card rendered
92
- * read-only forever while the host was serving a writable, live-reloadable
93
- * namespace. So: subscribe to the mirror, kick it with `ensure()`, and keep a
94
- * bounded timer as a belt-and-braces retry.
95
- */
96
- function bindSettingsWaists(ctx: ClientCtx): void {
97
- if (typeof ctx.inject !== 'function') return
98
-
99
- safe('configForms', () => {
100
- ctx.inject?.(['configForms'], (sctx) => {
101
- const forms = sctx.configForms
102
- if (forms === undefined) return
103
- let adopted = false
104
- let unsubscribe: (() => void) | undefined
105
- let tries = 0
106
- const attempt = (): void => {
107
- if (adopted) return
108
- try {
109
- const served = servedNamespace(forms)
110
- if (served === undefined) {
111
- forms.describe?.().ensure?.()
112
- return
113
- }
114
- adopted = true
115
- unsubscribe?.()
116
- adopt(forms.get(served))
117
- } catch (error) {
118
- console.warn(`${PKG_NAME}: configForms unreadable:`, error)
119
- }
120
- }
121
- try {
122
- unsubscribe = forms.describe?.().subscribe?.(attempt)
123
- } catch {
124
- // A mirror without a subscribe face still gets the timer below.
125
- }
126
- attempt()
127
- const timer = setInterval(() => {
128
- tries += 1
129
- attempt()
130
- if (adopted || tries > 40) clearInterval(timer)
131
- }, 250)
132
- if (adopted) clearInterval(timer)
133
- })
134
- })
135
-
136
- // ≤0.1.6: the imperative settings scope, same namespace family.
137
- safe('settingsScope', () => {
138
- ctx.inject?.(['settingsScope'], (sctx) => {
139
- try {
140
- const scope = sctx.settingsScope?.bind({ namespace: NAMESPACE_CANDIDATES[0] })
141
- if (scope !== undefined) adopt(scope)
142
- } catch (error) {
143
- console.warn(`${PKG_NAME}: settingsScope unreadable:`, error)
144
- }
145
- })
146
- })
147
- }
148
-
149
- /** Register the configuration card on this bundle's detail page. */
150
- function mountConfigCard(ctx: ClientCtx): void {
151
- safe('plugins.bundle.config', () => {
152
- ctx.slots.inject('plugins.bundle.config', () =>
153
- ctx.slots.register(
154
- {
155
- name: 'plugins.bundle.config',
156
- key: PKG_NAME,
157
- inject: () => ({}),
158
- },
159
- createConfigCard(),
160
- ),
161
- )
162
- })
163
- }
164
-
165
- /**
166
- * Activate the plugin.
167
- * @param ctx - the client context, narrowed to the faces this plugin probes.
168
- */
169
- export function apply(ctx: ClientCtx): void {
170
- onFallback(BUILTIN_CONFIG)
171
- // `installFold` scans immediately and keeps an observer alive, so a sidebar
172
- // that mounts after boot is picked up without a second install call.
173
- disposer = installFold(BUILTIN_CONFIG)
174
- mountedNow = rescan()
175
- mountConfigCard(ctx)
176
- bindSettingsWaists(ctx)
177
- // One line so a silent failure is never silent: without this the sidebar simply
178
- // keeps its shipped shape and nothing says why. `mounted:false` means the
179
- // selectors missed (host DOM drifted) — see findings §7 for the fallback policy.
180
- console.info(`${PKG_NAME}: fold installed`, { mounted: mountedNow })
181
-
182
- ctx.effect?.(() => () => {
183
- disposer?.()
184
- disposer = null
185
- console.info(`${PKG_NAME}: fold removed`)
186
- }, `${PKG_NAME}: sidebar fold`)
187
- }
1
+ /**
2
+ * Browser half of `dsh-sidebar-fold`.
3
+ *
4
+ * Activation shape (each rule is an incident lesson, not a preference):
5
+ * - the fold is mounted at the **top level** of `apply` and never waits on a
6
+ * service, so a host without the settings surface still folds;
7
+ * - the settings seat is registered at the top level too, inside its own guard:
8
+ * a missing seat must not take the fold down with it;
9
+ * - the two settings waists (`configForms` for 0.1.7+, `settingsScope` for
10
+ * ≤0.1.6) are declared through separate `ctx.inject` calls — naming one that
11
+ * a generation does not register would otherwise leave the whole fiber
12
+ * pending and the plugin silently inert.
13
+ */
14
+ import { PKG_NAME } from './constants.ts'
15
+ import { applyConfig, BUILTIN_CONFIG, installFold, normalizeAlign, rescan } from './fold.ts'
16
+ import { createSettingsRow, adoptPluginNavigation } from './menu-entry.ts'
17
+ import { createConfigCard, onFallback, onScope, type SettingsScopeLike } from './settings-card.ts'
18
+
19
+ /** Cordis services the client half reads. Only `slots` is unconditional. */
20
+ export const inject = ['slots']
21
+
22
+ /** A tolerant view of the client context: every face is probed, never assumed. */
23
+ interface ClientCtx {
24
+ slots: {
25
+ inject: (name: string, fn: () => unknown) => () => void
26
+ register: (options: Record<string, unknown>, component: unknown) => () => void
27
+ }
28
+ inject?: (names: readonly string[], fn: (sctx: Record<string, any>) => void) => unknown
29
+ effect?: (factory: () => () => void, label: string) => unknown
30
+ get?: (name: string) => unknown
31
+ }
32
+
33
+ /** Fold + card wiring, run once the fold is installed. */
34
+ let disposer: (() => void) | null = null
35
+ /** Whether the fold found its anchors on the last scan (diagnostics only). */
36
+ let mountedNow = false
37
+
38
+ /** One guard per surface: a missing host face degrades that surface only. */
39
+ function safe(tag: string, fn: () => void): void {
40
+ try {
41
+ fn()
42
+ } catch (error) {
43
+ console.warn(`${PKG_NAME}: surface '${tag}' unavailable:`, error)
44
+ }
45
+ }
46
+
47
+ /** Adopt a served snapshot, falling back to the built-ins when it says nothing. */
48
+ function adopt(scope: SettingsScopeLike): void {
49
+ onScope(scope)
50
+ const push = (): void => {
51
+ const snap = scope.getSnapshot()
52
+ const value = snap.value
53
+ const next = {
54
+ defaultFolded: typeof value?.defaultFolded === 'boolean' ? value.defaultFolded : BUILTIN_CONFIG.defaultFolded,
55
+ keepLabels: Array.isArray(value?.keepLabels) ? value.keepLabels : BUILTIN_CONFIG.keepLabels,
56
+ // The third state: rows the host owns in its "More" list. Absent on a host
57
+ // that serves an older document, so it falls back to none.
58
+ hideLabels: Array.isArray(value?.hideLabels) ? value.hideLabels : BUILTIN_CONFIG.hideLabels,
59
+ // Alignment is validated by `normalizeAlign` here so everything downstream
60
+ // (fold marks, card radios) only ever sees one of the three known modes.
61
+ align: normalizeAlign(typeof value?.align === 'string' ? value.align : BUILTIN_CONFIG.align),
62
+ }
63
+ onFallback(next)
64
+ applyConfig(next)
65
+ }
66
+ push()
67
+ scope.subscribe(push)
68
+ }
69
+
70
+ /**
71
+ * Candidate namespaces for this bundle's served settings form.
72
+ *
73
+ * `ConfigForms.get(entryId)` keys forms by the *host plugin entry id*, and the
74
+ * Plugin Manager only opens a form whose namespace appears in the served
75
+ * `settings.describe` mirror. The entry id is the bundle row's id, which
76
+ * `cordis.patch.yml` inserts as the package name; the scope-stripped variant is
77
+ * tried as well because sibling plugins namespace their forms that way. Asking
78
+ * for an unserved key is cheap (the form reports `unavailable`), so we probe
79
+ * rather than guess, and adopt whichever key the host actually serves first.
80
+ */
81
+ const NAMESPACE_CANDIDATES = [PKG_NAME, PKG_NAME.replace(/^dsh-/, '')]
82
+
83
+ /** Ask the host which of our candidate namespaces it serves, if any. */
84
+ function servedNamespace(forms: any): string | undefined {
85
+ const mirror = forms.describe?.()
86
+ const served = new Set((mirror?.getSnapshot()?.view?.namespaces ?? []).map((entry: { ns: string }) => entry.ns))
87
+ return NAMESPACE_CANDIDATES.find((candidate) => served.has(candidate))
88
+ }
89
+
90
+ /**
91
+ * Bind the two generations' settings waists, each behind its own inject.
92
+ *
93
+ * The `configForms` probe is **not** one-shot: the settings mirror is still
94
+ * loading when a client half activates, so `view` can be `undefined` at that
95
+ * instant. First smoke run proved the cost of probing once — the card rendered
96
+ * read-only forever while the host was serving a writable, live-reloadable
97
+ * namespace. So: subscribe to the mirror, kick it with `ensure()`, and keep a
98
+ * bounded timer as a belt-and-braces retry.
99
+ */
100
+ function bindSettingsWaists(ctx: ClientCtx): void {
101
+ if (typeof ctx.inject !== 'function') return
102
+
103
+ safe('configForms', () => {
104
+ ctx.inject?.(['configForms'], (sctx) => {
105
+ const forms = sctx.configForms
106
+ if (forms === undefined) return
107
+ let adopted = false
108
+ let unsubscribe: (() => void) | undefined
109
+ let tries = 0
110
+ const attempt = (): void => {
111
+ if (adopted) return
112
+ try {
113
+ const served = servedNamespace(forms)
114
+ if (served === undefined) {
115
+ forms.describe?.().ensure?.()
116
+ return
117
+ }
118
+ adopted = true
119
+ unsubscribe?.()
120
+ adopt(forms.get(served))
121
+ } catch (error) {
122
+ console.warn(`${PKG_NAME}: configForms unreadable:`, error)
123
+ }
124
+ }
125
+ try {
126
+ unsubscribe = forms.describe?.().subscribe?.(attempt)
127
+ } catch {
128
+ // A mirror without a subscribe face still gets the timer below.
129
+ }
130
+ attempt()
131
+ const timer = setInterval(() => {
132
+ tries += 1
133
+ attempt()
134
+ if (adopted || tries > 40) clearInterval(timer)
135
+ }, 250)
136
+ if (adopted) clearInterval(timer)
137
+ })
138
+ })
139
+
140
+ // ≤0.1.6: the imperative settings scope, same namespace family.
141
+ safe('settingsScope', () => {
142
+ ctx.inject?.(['settingsScope'], (sctx) => {
143
+ try {
144
+ const scope = sctx.settingsScope?.bind({ namespace: NAMESPACE_CANDIDATES[0] })
145
+ if (scope !== undefined) adopt(scope)
146
+ } catch (error) {
147
+ console.warn(`${PKG_NAME}: settingsScope unreadable:`, error)
148
+ }
149
+ })
150
+ })
151
+
152
+ // The More-menu row navigates with this service; without it the row falls back
153
+ // to selecting the plugins panel, so the seat is optional all the way down.
154
+ safe('pluginNavigation', () => {
155
+ ctx.inject?.(['pluginNavigation'], (sctx) => {
156
+ adoptPluginNavigation(sctx.pluginNavigation)
157
+ })
158
+ })
159
+ }
160
+
161
+ /** Register the configuration card on this bundle's detail page. */
162
+ function mountConfigCard(ctx: ClientCtx): void {
163
+ safe('plugins.bundle.config', () => {
164
+ ctx.slots.inject('plugins.bundle.config', () =>
165
+ ctx.slots.register(
166
+ {
167
+ name: 'plugins.bundle.config',
168
+ key: PKG_NAME,
169
+ inject: () => ({}),
170
+ },
171
+ createConfigCard(),
172
+ ),
173
+ )
174
+ })
175
+ }
176
+
177
+ /**
178
+ * Add the "More" menu row that jumps straight to this bundle's settings page.
179
+ *
180
+ * Seated in `sidebar.panellist.actions` — the list the shipped More menu renders,
181
+ * so the row inherits the menu's keyboard walk and dismiss path. `order: 100`
182
+ * keeps it after the host's own rows (a list seat can only append).
183
+ */
184
+ function mountMenuEntry(ctx: ClientCtx): void {
185
+ safe('panellist.actions', () => {
186
+ ctx.slots.inject('sidebar.panellist.actions', () =>
187
+ ctx.slots.register(
188
+ {
189
+ name: 'sidebar.panellist.actions',
190
+ id: `${PKG_NAME}-settings`,
191
+ order: 100,
192
+ inject: () => ({}),
193
+ },
194
+ createSettingsRow(ctx) as unknown as () => unknown,
195
+ ),
196
+ )
197
+ })
198
+ }
199
+
200
+ /**
201
+ * Activate the plugin.
202
+ * @param ctx - the client context, narrowed to the faces this plugin probes.
203
+ */
204
+ export function apply(ctx: ClientCtx): void {
205
+ onFallback(BUILTIN_CONFIG)
206
+ // `installFold` scans immediately and keeps an observer alive, so a sidebar
207
+ // that mounts after boot is picked up without a second install call.
208
+ disposer = installFold(BUILTIN_CONFIG)
209
+ mountedNow = rescan()
210
+ mountConfigCard(ctx)
211
+ mountMenuEntry(ctx)
212
+ bindSettingsWaists(ctx)
213
+ // One line so a silent failure is never silent: without this the sidebar simply
214
+ // keeps its shipped shape and nothing says why. `mounted:false` means the
215
+ // selectors missed (host DOM drifted) — see findings §7 for the fallback policy.
216
+ console.info(`${PKG_NAME}: fold installed`, { mounted: mountedNow })
217
+
218
+ ctx.effect?.(() => () => {
219
+ disposer?.()
220
+ disposer = null
221
+ console.info(`${PKG_NAME}: fold removed`)
222
+ }, `${PKG_NAME}: sidebar fold`)
223
+ }