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.
- package/README.md +164 -135
- package/README.pt.md +171 -0
- package/README.zh.md +141 -120
- package/dsh.plugin.json +3 -3
- package/lib/client.js +910 -102
- package/locale/pt.json +6 -0
- package/package.json +6 -5
- package/src/client/constants.ts +319 -237
- package/src/client/fold.ts +1031 -657
- package/src/client/index.ts +223 -187
- package/src/client/locales.ts +330 -210
- package/src/client/menu-entry.ts +157 -0
- package/src/client/settings-card.ts +451 -334
- package/src/index.js +99 -88
|
@@ -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 {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
)
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
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']
|