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,334 +1,451 @@
1
- /**
2
- * The configuration card on this bundle's Plugin-Manager page.
3
- *
4
- * Seat: `plugins.bundle.config` (kind `keyed`, key = the bundle's package name) —
5
- * verified present in rc.3 (`dsh-cordis-client-runner` slot-catalog, rendered at
6
- * `dsh-client-ui-plugin-manager/lib/client.js:2964`).
7
- *
8
- * Two design points earned from the first smoke runs:
9
- * - the slot renderer is React, so the component must return a **React element**
10
- * (a bare DOM node made React throw and the host dropped the entry into
11
- * `data-slot-error`); the element is a thin wrapper whose ref mounts the
12
- * plain-DOM card body;
13
- * - the fold scope is **read from the live sidebar**, not typed by the user: the
14
- * card lists the panel rows that exist right now and gives each one a switch.
15
- * Free-text labels were the wrong shape — they let a stale list silently
16
- * exempt every row, which reads to the user as "the button does nothing".
17
- *
18
- * `react` is served by the host's module table (not a `@deepseek-ai/dsh-*`
19
- * package, which stays off-limits), so the factory's own `require` is the way in.
20
- */
21
- import { ATTR, CLS, type AlignMode } from './constants.ts'
22
- import { pickDictionary } from './locales.ts'
23
- import { deriveKeep, normalizeAlign, roster, subscribeRoster } from './fold.ts'
24
-
25
- /** The module-loader factory's `require`, injected by the host's module table. */
26
- declare const require: (id: string) => any
27
- const react: any = require('react')
28
-
29
- /** One settings snapshot as the host's scoped form reports it. */
30
- export interface ConfigSnapshot {
31
- status: 'loading' | 'ready' | 'unavailable'
32
- value: { defaultFolded?: boolean; keepLabels?: string[]; align?: string } | undefined
33
- revision: number | undefined
34
- writable: boolean
35
- }
36
-
37
- /** The scoped-form surface this card consumes (mirrors `dsh-session-guard` §scope-face). */
38
- export interface SettingsScopeLike {
39
- getSnapshot(): ConfigSnapshot
40
- subscribe(listener: () => void): () => void
41
- set(field: string, value: unknown): Promise<boolean>
42
- }
43
-
44
- /** The card's live data source, filled by `index.ts` whichever settings waist lands. */
45
- const scopeRef: { scope: SettingsScopeLike | undefined } = { scope: undefined }
46
-
47
- /** Fallback shown when no host form is served (≤0.1.6, or an unserved namespace). */
48
- let fallback: { defaultFolded: boolean; keepLabels: string[]; align: AlignMode } = {
49
- defaultFolded: false,
50
- keepLabels: ['插件', 'Plugins'],
51
- align: 'start',
52
- }
53
-
54
- /** The card body node, created once and re-used by every mount. */
55
- let cardRoot: HTMLElement | null = null
56
-
57
- /** Create the card body once, and hand the same node back on every mount. */
58
- function ensureCardRoot(): HTMLElement {
59
- if (cardRoot === null) {
60
- cardRoot = document.createElement('div')
61
- cardRoot.className = CLS.card
62
- cardRoot.setAttribute(ATTR.own, '')
63
- }
64
- return cardRoot
65
- }
66
-
67
- /** Read the effective values: live form first, built-in fallback second. */
68
- function currentValues(): {
69
- defaultFolded: boolean
70
- keepLabels: string[]
71
- align: AlignMode
72
- writable: boolean
73
- status: ConfigSnapshot['status']
74
- } {
75
- const scope = scopeRef.scope
76
- if (scope !== undefined) {
77
- const snap = scope.getSnapshot()
78
- return {
79
- defaultFolded: snap.value?.defaultFolded ?? fallback.defaultFolded,
80
- keepLabels: snap.value?.keepLabels ?? fallback.keepLabels,
81
- align: normalizeAlign(snap.value?.align ?? fallback.align),
82
- writable: snap.writable && snap.status === 'ready',
83
- status: snap.status,
84
- }
85
- }
86
- return {
87
- defaultFolded: fallback.defaultFolded,
88
- keepLabels: fallback.keepLabels,
89
- align: normalizeAlign(fallback.align),
90
- writable: false,
91
- status: 'unavailable',
92
- }
93
- }
94
-
95
- /** One labelled row: text block on the left, control on the right. */
96
- function buildRow(label: string, hint: string, control: HTMLElement): HTMLElement {
97
- const row = document.createElement('div')
98
- row.className = CLS.row
99
- row.setAttribute(ATTR.own, '')
100
- const main = document.createElement('div')
101
- main.className = CLS.rowMain
102
- const title = document.createElement('span')
103
- title.className = CLS.label
104
- title.textContent = label
105
- const note = document.createElement('span')
106
- note.className = CLS.hint
107
- note.textContent = hint
108
- main.append(title, note)
109
- row.append(main, control)
110
- return row
111
- }
112
-
113
- /**
114
- * A pill switch bound to one field.
115
- *
116
- * A real `<button role="switch">` (not a checkbox): the card is a settings
117
- * surface, and this plugin family renders on/off rows as capsule switches. The
118
- * `aria-checked` attribute is also the paint hook (track colour + knob travel).
119
- */
120
- function switchControl(
121
- field: string,
122
- checked: boolean,
123
- disabled: boolean,
124
- onChange: (next: boolean) => void,
125
- ): HTMLElement {
126
- const button = document.createElement('button')
127
- button.type = 'button'
128
- button.className = CLS.switch
129
- button.setAttribute('role', 'switch')
130
- button.setAttribute('aria-checked', String(checked))
131
- button.dataset.field = field
132
- button.disabled = disabled
133
- const knob = document.createElement('span')
134
- knob.className = CLS.switchKnob
135
- button.appendChild(knob)
136
- button.addEventListener('click', () => {
137
- if (button.disabled) return
138
- const next = button.getAttribute('aria-checked') !== 'true'
139
- button.setAttribute('aria-checked', String(next))
140
- onChange(next)
141
- })
142
- return button
143
- }
144
-
145
- /** Write one field through the host's scoped form (the host owns conflicts). */
146
- function write(field: string, value: unknown): void {
147
- const scope = scopeRef.scope
148
- if (scope === undefined) return
149
- void scope.set(field, value)
150
- }
151
-
152
- /**
153
- * A segmented control for a small closed set of options.
154
- *
155
- * Row alignment is one of three mutually exclusive choices, which reads better as
156
- * a single track than as three independent switches. The host's own UI primitives
157
- * package is off-limits to plugins, so the track is drawn with this plugin's own
158
- * classes over host theme tokens.
159
- */
160
- function segmentedControl(
161
- field: string,
162
- value: string,
163
- options: readonly { value: string; label: string }[],
164
- disabled: boolean,
165
- onChange: (next: string) => void,
166
- ): HTMLElement {
167
- const group = document.createElement('div')
168
- group.className = CLS.segmentGroup
169
- group.setAttribute('role', 'radiogroup')
170
- group.dataset.field = field
171
- for (const option of options) {
172
- const button = document.createElement('button')
173
- button.type = 'button'
174
- button.className = CLS.segment
175
- button.setAttribute('role', 'radio')
176
- button.setAttribute('aria-checked', String(option.value === value))
177
- button.dataset.option = option.value
178
- button.textContent = option.label
179
- button.disabled = disabled
180
- button.addEventListener('click', () => {
181
- if (button.disabled) return
182
- // Move the mark locally so the click feels immediate; the host echoes a
183
- // snapshot and the whole card repaints anyway.
184
- for (const sibling of Array.from(group.children)) {
185
- sibling.setAttribute('aria-checked', String(sibling === button))
186
- }
187
- onChange(option.value)
188
- })
189
- group.appendChild(button)
190
- }
191
- return group
192
- }
193
-
194
- /**
195
- * Persist the keep list produced by the per-row switches.
196
- *
197
- * The stored list is exactly "labels currently switched on" — the switch state is
198
- * the source of truth, so a row that disappears from the sidebar drops out and a
199
- * row that appears starts folded.
200
- */
201
- function writeKeep(nextLabels: readonly string[]): void {
202
- write('keepLabels', [...nextLabels])
203
- }
204
-
205
- /** Paint (or repaint) the card body from the current values and the live roster. */
206
- function paint(): void {
207
- if (cardRoot === null) return
208
- const dict = pickDictionary(document.documentElement.lang ?? '')
209
- const values = currentValues()
210
- const rows = roster()
211
- cardRoot.replaceChildren()
212
-
213
- // ① default state
214
- cardRoot.appendChild(
215
- buildRow(
216
- dict['settings.defaultFolded'],
217
- dict['settings.defaultFoldedHint'],
218
- switchControl('defaultFolded', values.defaultFolded, !values.writable, (next) => {
219
- write('defaultFolded', next)
220
- }),
221
- ),
222
- )
223
-
224
- // ② row-content alignment: left / centred / right for the icon + label pair
225
- cardRoot.appendChild(
226
- buildRow(
227
- dict['settings.align'],
228
- dict['settings.alignHint'],
229
- segmentedControl(
230
- 'align',
231
- values.align,
232
- [
233
- { value: 'start', label: dict['settings.alignStart'] },
234
- { value: 'center', label: dict['settings.alignCenter'] },
235
- { value: 'end', label: dict['settings.alignEnd'] },
236
- ],
237
- !values.writable,
238
- (next) => {
239
- write('align', normalizeAlign(next))
240
- },
241
- ),
242
- ),
243
- )
244
-
245
- // ③ fold scope: one switch per row that exists in this profile right now
246
- const scopeTitle = document.createElement('div')
247
- scopeTitle.className = CLS.label
248
- scopeTitle.setAttribute(ATTR.own, '')
249
- scopeTitle.textContent = dict['settings.keep']
250
- cardRoot.appendChild(scopeTitle)
251
-
252
- const hint = document.createElement('span')
253
- hint.className = CLS.hint
254
- hint.setAttribute(ATTR.own, '')
255
- hint.textContent = dict['settings.keepHint']
256
- cardRoot.appendChild(hint)
257
-
258
- if (rows.length === 0) {
259
- const empty = document.createElement('span')
260
- empty.className = CLS.hint
261
- empty.setAttribute(ATTR.own, '')
262
- empty.textContent = dict['settings.keepEmpty']
263
- cardRoot.appendChild(empty)
264
- }
265
-
266
- for (const entry of rows) {
267
- cardRoot.appendChild(
268
- buildRow(
269
- entry.label,
270
- dict['settings.keepRowHint'],
271
- switchControl(`keep:${entry.label}`, entry.keep, !values.writable, (next) => {
272
- // The switch state IS the stored list: switched-on rows are the kept ones.
273
- writeKeep(deriveKeep(rows, entry.label, next))
274
- }),
275
- ),
276
- )
277
- }
278
-
279
- if (!values.writable) {
280
- const note = document.createElement('span')
281
- note.className = CLS.hint
282
- note.setAttribute(ATTR.own, '')
283
- note.textContent = dict['settings.readonly']
284
- cardRoot.appendChild(note)
285
- }
286
- }
287
-
288
- /**
289
- * Point the card at a settings scope (called by `index.ts` when a waist resolves).
290
- * @param scope - the scoped form for this bundle's namespace.
291
- */
292
- export function onScope(scope: SettingsScopeLike): void {
293
- scopeRef.scope = scope
294
- scope.subscribe(() => {
295
- paint()
296
- })
297
- paint()
298
- }
299
-
300
- /** Adopt the built-in fallback (used when no writable surface exists). */
301
- export function onFallback(next: typeof fallback): void {
302
- fallback = { defaultFolded: next.defaultFolded, keepLabels: [...next.keepLabels], align: normalizeAlign(next.align) }
303
- paint()
304
- }
305
-
306
- /**
307
- * Build the card component for `ctx.slots.register`.
308
- *
309
- * The slot's owner props (`{ view, form }`) are ignored on purpose: the host's own
310
- * form object is not a stable write path for a plugin, while our scoped form is —
311
- * the same reasoning `dsh-session-guard` records for its family card.
312
- * @returns a React component the slot can render.
313
- */
314
- export function createConfigCard(): () => unknown {
315
- // Repaint whenever the sidebar's row inventory changes, so the switch list
316
- // follows installs/removals without a reload. Subscribed once, at factory time.
317
- subscribeRoster(() => {
318
- paint()
319
- })
320
- return function SidebarFoldCard() {
321
- return react.createElement('div', {
322
- className: CLS.cardHost,
323
- ref: (node: HTMLElement | null) => {
324
- if (node === null) return
325
- const card = ensureCardRoot()
326
- if (node.firstChild !== card) node.append(card)
327
- paint()
328
- },
329
- })
330
- }
331
- }
332
-
333
- /** Exported for the guard test: the card writes no free-text label list. */
334
- export const CARD_WRITES: readonly string[] = ['defaultFolded', 'align', 'keepLabels']
1
+ /**
2
+ * The configuration card on this bundle's Plugin-Manager page.
3
+ *
4
+ * Seat: `plugins.bundle.config` (kind `keyed`, key = the bundle's package name) —
5
+ * verified present in rc.3 (`dsh-cordis-client-runner` slot-catalog, rendered at
6
+ * `dsh-client-ui-plugin-manager/lib/client.js:2964`).
7
+ *
8
+ * Two design points earned from the first smoke runs:
9
+ * - the slot renderer is React, so the component must return a **React element**
10
+ * (a bare DOM node made React throw and the host dropped the entry into
11
+ * `data-slot-error`); the element is a thin wrapper whose ref mounts the
12
+ * plain-DOM card body;
13
+ * - the fold scope is **read from the live sidebar**, not typed by the user: the
14
+ * card lists the panel rows that exist right now and gives each one a switch.
15
+ * Free-text labels were the wrong shape — they let a stale list silently
16
+ * exempt every row, which reads to the user as "the button does nothing".
17
+ *
18
+ * `react` is served by the host's module table (not a `@deepseek-ai/dsh-*`
19
+ * package, which stays off-limits), so the factory's own `require` is the way in.
20
+ */
21
+ import { ATTR, CLS, type AlignMode } from './constants.ts'
22
+ import { pickDictionary } from './locales.ts'
23
+ import {
24
+ applyConfig,
25
+ canMoveToMore,
26
+ deriveLists,
27
+ effectiveConfig,
28
+ hostHasPin,
29
+ isFixedLabel,
30
+ normalizeAlign,
31
+ roster,
32
+ subscribeRoster,
33
+ type RowState,
34
+ } from './fold.ts'
35
+
36
+ /** The module-loader factory's `require`, injected by the host's module table. */
37
+ declare const require: (id: string) => any
38
+ const react: any = require('react')
39
+
40
+ /** One settings snapshot as the host's scoped form reports it. */
41
+ export interface ConfigSnapshot {
42
+ status: 'loading' | 'ready' | 'unavailable'
43
+ value: { defaultFolded?: boolean; keepLabels?: string[]; hideLabels?: string[]; align?: string } | undefined
44
+ revision: number | undefined
45
+ writable: boolean
46
+ }
47
+
48
+ /** The scoped-form surface this card consumes (mirrors `dsh-session-guard` §scope-face). */
49
+ export interface SettingsScopeLike {
50
+ getSnapshot(): ConfigSnapshot
51
+ subscribe(listener: () => void): () => void
52
+ set(field: string, value: unknown): Promise<boolean>
53
+ }
54
+
55
+ /** The card's live data source, filled by `index.ts` whichever settings waist lands. */
56
+ const scopeRef: { scope: SettingsScopeLike | undefined } = { scope: undefined }
57
+
58
+ /** Fallback shown when no host form is served (≤0.1.6, or an unserved namespace). */
59
+ let fallback: { defaultFolded: boolean; keepLabels: string[]; hideLabels: string[]; align: AlignMode } = {
60
+ defaultFolded: false,
61
+ keepLabels: ['插件', 'Plugins'],
62
+ hideLabels: [],
63
+ align: 'start',
64
+ }
65
+
66
+ /** The card body node, created once and re-used by every mount. */
67
+ let cardRoot: HTMLElement | null = null
68
+
69
+ /** Create the card body once, and hand the same node back on every mount. */
70
+ function ensureCardRoot(): HTMLElement {
71
+ if (cardRoot === null) {
72
+ cardRoot = document.createElement('div')
73
+ cardRoot.className = CLS.card
74
+ cardRoot.setAttribute(ATTR.own, '')
75
+ }
76
+ return cardRoot
77
+ }
78
+
79
+ /** Read the effective values: live form first, built-in fallback second. */
80
+ function currentValues(): {
81
+ defaultFolded: boolean
82
+ keepLabels: string[]
83
+ hideLabels: string[]
84
+ align: AlignMode
85
+ writable: boolean
86
+ status: ConfigSnapshot['status']
87
+ } {
88
+ const scope = scopeRef.scope
89
+ if (scope !== undefined) {
90
+ const snap = scope.getSnapshot()
91
+ return {
92
+ defaultFolded: snap.value?.defaultFolded ?? fallback.defaultFolded,
93
+ keepLabels: snap.value?.keepLabels ?? fallback.keepLabels,
94
+ hideLabels: snap.value?.hideLabels ?? fallback.hideLabels,
95
+ align: normalizeAlign(snap.value?.align ?? fallback.align),
96
+ writable: snap.writable && snap.status === 'ready',
97
+ status: snap.status,
98
+ }
99
+ }
100
+ return {
101
+ defaultFolded: fallback.defaultFolded,
102
+ keepLabels: fallback.keepLabels,
103
+ hideLabels: fallback.hideLabels,
104
+ align: normalizeAlign(fallback.align),
105
+ writable: false,
106
+ status: 'unavailable',
107
+ }
108
+ }
109
+
110
+ /** One labelled row: text block on the left, control on the right. */
111
+ function buildRow(label: string, hint: string, control: HTMLElement): HTMLElement {
112
+ const row = document.createElement('div')
113
+ row.className = CLS.row
114
+ row.setAttribute(ATTR.own, '')
115
+ const main = document.createElement('div')
116
+ main.className = CLS.rowMain
117
+ const title = document.createElement('span')
118
+ title.className = CLS.label
119
+ title.textContent = label
120
+ const note = document.createElement('span')
121
+ note.className = CLS.hint
122
+ note.textContent = hint
123
+ main.append(title, note)
124
+ row.append(main, control)
125
+ return row
126
+ }
127
+
128
+ /**
129
+ * A pill switch bound to one field.
130
+ *
131
+ * A real `<button role="switch">` (not a checkbox): the card is a settings
132
+ * surface, and this plugin family renders on/off rows as capsule switches. The
133
+ * `aria-checked` attribute is also the paint hook (track colour + knob travel).
134
+ */
135
+ function switchControl(
136
+ field: string,
137
+ checked: boolean,
138
+ disabled: boolean,
139
+ onChange: (next: boolean) => void,
140
+ ): HTMLElement {
141
+ const button = document.createElement('button')
142
+ button.type = 'button'
143
+ button.className = CLS.switch
144
+ button.setAttribute('role', 'switch')
145
+ button.setAttribute('aria-checked', String(checked))
146
+ button.dataset.field = field
147
+ button.disabled = disabled
148
+ const knob = document.createElement('span')
149
+ knob.className = CLS.switchKnob
150
+ button.appendChild(knob)
151
+ button.addEventListener('click', () => {
152
+ if (button.disabled) return
153
+ const next = button.getAttribute('aria-checked') !== 'true'
154
+ button.setAttribute('aria-checked', String(next))
155
+ onChange(next)
156
+ })
157
+ return button
158
+ }
159
+
160
+ /** What the last write did, so the card can say it instead of failing silently. */
161
+ let saveState: { status: 'idle' | 'saving' | 'saved' | 'error'; fields: Record<string, unknown> } = {
162
+ status: 'idle',
163
+ fields: {},
164
+ }
165
+ /** How long a host write may stay unconfirmed before the card reads the store. */
166
+ const SAVE_CONFIRM_MS = 1200
167
+
168
+ /** Deep-equality over JSON values — enough for the strings and string arrays here. */
169
+ function sameValue(left: unknown, right: unknown): boolean {
170
+ return JSON.stringify(left ?? null) === JSON.stringify(right ?? null)
171
+ }
172
+
173
+ /**
174
+ * Write fields through the host's scoped form and report the outcome.
175
+ *
176
+ * Two lies this replaces: the first build dropped `set()`'s promise entirely, and
177
+ * the second believed it. On the 0.2.2 line the write is persisted but the
178
+ * promise **never settles** (measured: still pending after 6s), so the confirm is
179
+ * bounded by {@link SAVE_CONFIRM_MS} and then settled against the served
180
+ * snapshot — the host's own store is the only thing that can say “stored”.
181
+ */
182
+ function commit(fields: Record<string, unknown>): void {
183
+ const scope = scopeRef.scope
184
+ if (scope === undefined) return
185
+ saveState = { status: 'saving', fields }
186
+ paint()
187
+ const writes = Object.entries(fields).map(([field, value]) =>
188
+ Promise.resolve(scope.set(field, value)).then((ok) => ok === true).catch(() => false),
189
+ )
190
+ const confirmed = new Promise<boolean>((resolve) => {
191
+ setTimeout(() => {
192
+ let value: Record<string, unknown> | undefined
193
+ try {
194
+ value = scope.getSnapshot().value as Record<string, unknown> | undefined
195
+ } catch {
196
+ value = undefined
197
+ }
198
+ resolve(Object.entries(fields).every(([field, expected]) => sameValue(value?.[field], expected)))
199
+ }, SAVE_CONFIRM_MS)
200
+ })
201
+ void Promise.race([Promise.all(writes).then((results) => results.every(Boolean)), confirmed]).then((ok) => {
202
+ saveState = { status: ok ? 'saved' : 'error', fields }
203
+ paint()
204
+ })
205
+ }
206
+
207
+ /** Write one field (the single-value controls go through here). */
208
+ function write(field: string, value: unknown): void {
209
+ commit({ [field]: value })
210
+ }
211
+
212
+ /**
213
+ * A segmented control for a small closed set of options.
214
+ *
215
+ * Row alignment is one of three mutually exclusive choices, which reads better as
216
+ * a single track than as three independent switches. The host's own UI primitives
217
+ * package is off-limits to plugins, so the track is drawn with this plugin's own
218
+ * classes over host theme tokens.
219
+ */
220
+ function segmentedControl(
221
+ field: string,
222
+ value: string,
223
+ options: readonly { value: string; label: string; unavailable?: boolean }[],
224
+ disabled: boolean,
225
+ onChange: (next: string) => void,
226
+ ): HTMLElement {
227
+ const group = document.createElement('div')
228
+ group.className = CLS.segmentGroup
229
+ group.setAttribute('role', 'radiogroup')
230
+ group.dataset.field = field
231
+ for (const option of options) {
232
+ const button = document.createElement('button')
233
+ button.type = 'button'
234
+ button.className = CLS.segment
235
+ button.setAttribute('role', 'radio')
236
+ button.setAttribute('aria-checked', String(option.value === value))
237
+ button.dataset.option = option.value
238
+ button.textContent = option.label
239
+ // One option can be absent from this host (a 0.2.1 sidebar has no pin
240
+ // control); disabling just that segment keeps the other two usable.
241
+ button.disabled = disabled || option.unavailable === true
242
+ button.addEventListener('click', () => {
243
+ if (button.disabled) return
244
+ // Move the mark locally so the click feels immediate; the host echoes a
245
+ // snapshot and the whole card repaints anyway.
246
+ for (const sibling of Array.from(group.children)) {
247
+ sibling.setAttribute('aria-checked', String(sibling === button))
248
+ }
249
+ onChange(option.value)
250
+ })
251
+ group.appendChild(button)
252
+ }
253
+ return group
254
+ }
255
+
256
+ /**
257
+ * Persist one row's moved control.
258
+ *
259
+ * Two lists, one source of truth: `always` rows are written to `keepLabels` and
260
+ * `more` rows to `hideLabels`, so the control state never has to be inferred
261
+ * from a stale list.
262
+ *
263
+ * The write is also adopted **locally**: the host persists the value but does not
264
+ * echo a new snapshot within seconds (measured on 0.2.2-rc.3 while moving a row
265
+ * into “More”), and a control whose click visibly does nothing reads as broken.
266
+ */
267
+ function writeRowState(rows: ReturnType<typeof roster>, label: string, next: RowState): void {
268
+ const lists = deriveLists(rows, label, next)
269
+ applyConfig({ ...effectiveConfig(), keepLabels: lists.keepLabels, hideLabels: lists.hideLabels })
270
+ paint()
271
+ commit({ keepLabels: lists.keepLabels, hideLabels: lists.hideLabels })
272
+ }
273
+
274
+ /** Paint (or repaint) the card body from the current values and the live roster. */
275
+ function paint(): void {
276
+ if (cardRoot === null) return
277
+ const dict = pickDictionary(document.documentElement.lang ?? '')
278
+ const values = currentValues()
279
+ const rows = roster()
280
+ cardRoot.replaceChildren()
281
+
282
+ // ① default state
283
+ cardRoot.appendChild(
284
+ buildRow(
285
+ dict['settings.defaultFolded'],
286
+ dict['settings.defaultFoldedHint'],
287
+ switchControl('defaultFolded', values.defaultFolded, !values.writable, (next) => {
288
+ write('defaultFolded', next)
289
+ }),
290
+ ),
291
+ )
292
+
293
+ // ② row-content alignment: left / centred / right for the icon + label pair
294
+ cardRoot.appendChild(
295
+ buildRow(
296
+ dict['settings.align'],
297
+ dict['settings.alignHint'],
298
+ segmentedControl(
299
+ 'align',
300
+ values.align,
301
+ [
302
+ { value: 'start', label: dict['settings.alignStart'] },
303
+ { value: 'center', label: dict['settings.alignCenter'] },
304
+ { value: 'end', label: dict['settings.alignEnd'] },
305
+ ],
306
+ !values.writable,
307
+ (next) => {
308
+ write('align', normalizeAlign(next))
309
+ },
310
+ ),
311
+ ),
312
+ )
313
+
314
+ // ③ fold scope: one three-state control per row that exists in this profile
315
+ const scopeTitle = document.createElement('div')
316
+ scopeTitle.className = CLS.label
317
+ scopeTitle.setAttribute(ATTR.own, '')
318
+ scopeTitle.textContent = dict['settings.rows']
319
+ cardRoot.appendChild(scopeTitle)
320
+
321
+ const hint = document.createElement('span')
322
+ hint.className = CLS.hint
323
+ hint.setAttribute(ATTR.own, '')
324
+ hint.textContent = dict['settings.rowsHint']
325
+ cardRoot.appendChild(hint)
326
+
327
+ if (rows.length === 0) {
328
+ const empty = document.createElement('span')
329
+ empty.className = CLS.hint
330
+ empty.setAttribute(ATTR.own, '')
331
+ empty.textContent = dict['settings.rowsEmpty']
332
+ cardRoot.appendChild(empty)
333
+ }
334
+
335
+ // Two gates on the third state, per row: this host must ship the pin mechanism
336
+ // at all (0.2.2 on), and the row must not be the host's fixed panel — the host
337
+ // never unpins that one, so offering it would be a promise it silently refuses.
338
+ const canHide = hostHasPin()
339
+ for (const entry of rows) {
340
+ const movable = canMoveToMore(entry.label, canHide)
341
+ const hintText = movable
342
+ ? dict['settings.rowHint']
343
+ : isFixedLabel(entry.label) ? dict['settings.moreFixed'] : dict['settings.moreUnavailable']
344
+ cardRoot.appendChild(
345
+ buildRow(
346
+ entry.label,
347
+ hintText,
348
+ segmentedControl(
349
+ `row:${entry.label}`,
350
+ entry.state,
351
+ [
352
+ { value: 'always', label: dict['settings.rowAlways'] },
353
+ { value: 'foldable', label: dict['settings.rowFold'] },
354
+ { value: 'more', label: dict['settings.rowMore'], unavailable: !movable },
355
+ ],
356
+ !values.writable,
357
+ (next) => {
358
+ writeRowState(rows, entry.label, next as RowState)
359
+ },
360
+ ),
361
+ ),
362
+ )
363
+ }
364
+
365
+ // ④ write outcome. The host owns persistence; a refused write leaves the card
366
+ // and the sidebar agreeing with each other and disagreeing with the store, so
367
+ // the state has to be on screen, with the retry next to it.
368
+ if (saveState.status !== 'idle') {
369
+ const status = document.createElement('span')
370
+ status.className = CLS.hint
371
+ status.setAttribute(ATTR.own, '')
372
+ status.dataset.saveState = saveState.status
373
+ status.textContent = saveState.status === 'saving'
374
+ ? dict['settings.saving']
375
+ : saveState.status === 'saved' ? dict['settings.saved'] : dict['settings.saveFailed']
376
+ cardRoot.appendChild(status)
377
+ }
378
+ if (saveState.status === 'error') {
379
+ const retry = document.createElement('button')
380
+ retry.type = 'button'
381
+ retry.className = CLS.control
382
+ retry.dataset.field = 'retry'
383
+ retry.setAttribute(ATTR.own, '')
384
+ retry.textContent = dict['settings.retrySave']
385
+ retry.addEventListener('click', () => {
386
+ commit(saveState.fields)
387
+ })
388
+ cardRoot.appendChild(retry)
389
+ }
390
+
391
+ if (!values.writable) {
392
+ const note = document.createElement('span')
393
+ note.className = CLS.hint
394
+ note.setAttribute(ATTR.own, '')
395
+ note.textContent = dict['settings.readonly']
396
+ cardRoot.appendChild(note)
397
+ }
398
+ }
399
+
400
+ /**
401
+ * Point the card at a settings scope (called by `index.ts` when a waist resolves).
402
+ * @param scope - the scoped form for this bundle's namespace.
403
+ */
404
+ export function onScope(scope: SettingsScopeLike): void {
405
+ scopeRef.scope = scope
406
+ scope.subscribe(() => {
407
+ paint()
408
+ })
409
+ paint()
410
+ }
411
+
412
+ /** Adopt the built-in fallback (used when no writable surface exists). */
413
+ export function onFallback(next: typeof fallback): void {
414
+ fallback = {
415
+ defaultFolded: next.defaultFolded,
416
+ keepLabels: [...next.keepLabels],
417
+ hideLabels: [...next.hideLabels],
418
+ align: normalizeAlign(next.align),
419
+ }
420
+ paint()
421
+ }
422
+
423
+ /**
424
+ * Build the card component for `ctx.slots.register`.
425
+ *
426
+ * The slot's owner props (`{ view, form }`) are ignored on purpose: the host's own
427
+ * form object is not a stable write path for a plugin, while our scoped form is —
428
+ * the same reasoning `dsh-session-guard` records for its family card.
429
+ * @returns a React component the slot can render.
430
+ */
431
+ export function createConfigCard(): () => unknown {
432
+ // Repaint whenever the sidebar's row inventory changes, so the switch list
433
+ // follows installs/removals without a reload. Subscribed once, at factory time.
434
+ subscribeRoster(() => {
435
+ paint()
436
+ })
437
+ return function SidebarFoldCard() {
438
+ return react.createElement('div', {
439
+ className: CLS.cardHost,
440
+ ref: (node: HTMLElement | null) => {
441
+ if (node === null) return
442
+ const card = ensureCardRoot()
443
+ if (node.firstChild !== card) node.append(card)
444
+ paint()
445
+ },
446
+ })
447
+ }
448
+ }
449
+
450
+ /** Exported for the guard test: the card writes no free-text label list. */
451
+ export const CARD_WRITES: readonly string[] = ['defaultFolded', 'align', 'keepLabels', 'hideLabels']