@dickpy/dsh-imagegen 1.0.0
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/LICENSE +201 -0
- package/README.md +126 -0
- package/cordis.patch.yml +8 -0
- package/lib/client.js +2413 -0
- package/lib/client.js.map +1 -0
- package/lib/index.js +896 -0
- package/package.json +66 -0
- package/src/client/ImageGenPanel.tsx +687 -0
- package/src/client/SettingsCard.tsx +373 -0
- package/src/client/api.ts +89 -0
- package/src/client/controller.ts +46 -0
- package/src/client/css-modules.d.ts +5 -0
- package/src/client/helpers.ts +33 -0
- package/src/client/index.ts +127 -0
- package/src/client/locales.ts +204 -0
- package/src/client/mount.tsx +119 -0
- package/src/client/panel.module.css +970 -0
- package/src/client/settings-card.module.css +288 -0
- package/src/client/settings-form.ts +324 -0
- package/src/client/settings-scope.ts +227 -0
- package/src/client/sidebar-entry.ts +144 -0
- package/src/engine.ts +284 -0
- package/src/history-store.ts +217 -0
- package/src/index.ts +139 -0
- package/src/protocol.ts +118 -0
- package/src/routes.ts +373 -0
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-imagegen settings card styles. Mirrors the official plugin-config card
|
|
3
|
+
* chrome (@deepseek-ai/dsh-client-ui-settings-plugins PluginCard + fields) so
|
|
4
|
+
* the card reads as a sibling of the bash / agent-loop / web-search cards;
|
|
5
|
+
* colors ride the dsh --dsw-* tokens.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
.card {
|
|
9
|
+
list-style: none;
|
|
10
|
+
border: 1px solid var(--dsw-alias-border-l2);
|
|
11
|
+
border-radius: 12px;
|
|
12
|
+
background: var(--dsw-alias-bg-layer-3);
|
|
13
|
+
transition: border-color 0.16s, background 0.16s;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
.card:hover {
|
|
17
|
+
border-color: var(--dsw-alias-label-dimmed);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* An open card reads as the one being worked on, not merely taller. */
|
|
21
|
+
.card:has(.body) {
|
|
22
|
+
background: var(--dsw-alias-bg-layer-2);
|
|
23
|
+
border-color: var(--dsw-alias-label-dimmed);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.header {
|
|
27
|
+
width: 100%;
|
|
28
|
+
appearance: none;
|
|
29
|
+
border: 0;
|
|
30
|
+
background: none;
|
|
31
|
+
font: inherit;
|
|
32
|
+
color: inherit;
|
|
33
|
+
text-align: left;
|
|
34
|
+
cursor: pointer;
|
|
35
|
+
display: flex;
|
|
36
|
+
align-items: center;
|
|
37
|
+
gap: 12px;
|
|
38
|
+
padding: 14px 16px;
|
|
39
|
+
border-radius: 12px;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
.header:focus-visible {
|
|
43
|
+
outline: 2px solid var(--dsw-alias-brand-primary);
|
|
44
|
+
outline-offset: -2px;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
.headText {
|
|
48
|
+
flex: 1;
|
|
49
|
+
min-width: 0;
|
|
50
|
+
display: flex;
|
|
51
|
+
flex-direction: column;
|
|
52
|
+
gap: 4px;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
.name {
|
|
56
|
+
font-size: 15px;
|
|
57
|
+
font-weight: 600;
|
|
58
|
+
line-height: 1.4;
|
|
59
|
+
color: var(--dsw-alias-label-primary);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.description {
|
|
63
|
+
font-size: 13px;
|
|
64
|
+
line-height: 1.5;
|
|
65
|
+
color: var(--dsw-alias-label-tertiary);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.chevron,
|
|
69
|
+
.chevronOpen {
|
|
70
|
+
flex: none;
|
|
71
|
+
color: var(--dsw-alias-label-tertiary);
|
|
72
|
+
transition: transform 0.16s;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
.chevronOpen {
|
|
76
|
+
transform: rotate(180deg);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* Carried on the header so a collapsed card still says it holds edits. */
|
|
80
|
+
.pending {
|
|
81
|
+
flex: none;
|
|
82
|
+
border-radius: 999px;
|
|
83
|
+
padding: 1px 8px;
|
|
84
|
+
font-size: 11px;
|
|
85
|
+
line-height: 17px;
|
|
86
|
+
font-weight: 500;
|
|
87
|
+
white-space: nowrap;
|
|
88
|
+
background: var(--dsw-alias-bg-module-platform);
|
|
89
|
+
color: var(--dsw-alias-label-secondary);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.body {
|
|
93
|
+
border-top: 1px solid var(--dsw-alias-border-l2);
|
|
94
|
+
margin: 0 16px;
|
|
95
|
+
padding-bottom: 8px;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/* --- fields (mirror of the official plugin-config fields) --------------------- */
|
|
99
|
+
|
|
100
|
+
.field {
|
|
101
|
+
display: flex;
|
|
102
|
+
flex-direction: column;
|
|
103
|
+
gap: 6px;
|
|
104
|
+
padding: 12px 0;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
.field + .field {
|
|
108
|
+
border-top: 1px solid var(--dsw-alias-border-l2);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
.head {
|
|
112
|
+
display: flex;
|
|
113
|
+
align-items: center;
|
|
114
|
+
gap: 8px;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
.label {
|
|
118
|
+
flex: 1;
|
|
119
|
+
min-width: 0;
|
|
120
|
+
font-size: 13px;
|
|
121
|
+
font-weight: 500;
|
|
122
|
+
line-height: 1.5;
|
|
123
|
+
color: var(--dsw-alias-label-primary);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.badges {
|
|
127
|
+
display: inline-flex;
|
|
128
|
+
align-items: center;
|
|
129
|
+
gap: 8px;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
.badge {
|
|
133
|
+
border-radius: 999px;
|
|
134
|
+
padding: 1px 8px;
|
|
135
|
+
font-size: 11px;
|
|
136
|
+
line-height: 17px;
|
|
137
|
+
white-space: nowrap;
|
|
138
|
+
font-weight: 500;
|
|
139
|
+
background: var(--dsw-alias-bg-module-platform);
|
|
140
|
+
color: var(--dsw-alias-label-secondary);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
.reset {
|
|
144
|
+
border: none;
|
|
145
|
+
background: none;
|
|
146
|
+
padding: 0;
|
|
147
|
+
font: inherit;
|
|
148
|
+
font-size: 12px;
|
|
149
|
+
line-height: 1.5;
|
|
150
|
+
color: var(--dsw-alias-label-secondary);
|
|
151
|
+
cursor: pointer;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.reset:hover:not(:disabled) {
|
|
155
|
+
color: var(--dsw-alias-label-primary);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
.reset:disabled {
|
|
159
|
+
cursor: default;
|
|
160
|
+
opacity: 0.5;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.input,
|
|
164
|
+
.select {
|
|
165
|
+
height: 34px;
|
|
166
|
+
padding: 0 12px;
|
|
167
|
+
border: 1px solid var(--dsw-alias-border-l2);
|
|
168
|
+
border-radius: 8px;
|
|
169
|
+
background: var(--dsw-alias-bg-layer-3);
|
|
170
|
+
font: inherit;
|
|
171
|
+
font-size: 13px;
|
|
172
|
+
line-height: 1.5;
|
|
173
|
+
color: var(--dsw-alias-label-primary);
|
|
174
|
+
outline: none;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
.input:focus-visible,
|
|
178
|
+
.select:focus-visible {
|
|
179
|
+
border-color: var(--dsw-alias-brand-primary);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.input:disabled,
|
|
183
|
+
.select:disabled {
|
|
184
|
+
color: var(--dsw-alias-label-tertiary);
|
|
185
|
+
cursor: default;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
.inputInvalid {
|
|
189
|
+
height: 34px;
|
|
190
|
+
padding: 0 12px;
|
|
191
|
+
border: 1px solid var(--dsw-alias-label-error);
|
|
192
|
+
border-radius: 8px;
|
|
193
|
+
background: var(--dsw-alias-bg-layer-3);
|
|
194
|
+
font: inherit;
|
|
195
|
+
font-size: 13px;
|
|
196
|
+
line-height: 1.5;
|
|
197
|
+
color: var(--dsw-alias-label-primary);
|
|
198
|
+
outline: none;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
.hint,
|
|
202
|
+
.invalid {
|
|
203
|
+
margin: 0;
|
|
204
|
+
font-size: 12px;
|
|
205
|
+
line-height: 1.5;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
.hint {
|
|
209
|
+
color: var(--dsw-alias-label-tertiary);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
.invalid {
|
|
213
|
+
color: var(--dsw-alias-label-error);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
.readOnly,
|
|
217
|
+
.notExposed {
|
|
218
|
+
margin: 12px 0 0;
|
|
219
|
+
font-size: 12px;
|
|
220
|
+
line-height: 1.5;
|
|
221
|
+
color: var(--dsw-alias-label-tertiary);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/* --- footer (mirror of the official card footer) ------------------------------ */
|
|
225
|
+
|
|
226
|
+
.footer {
|
|
227
|
+
display: flex;
|
|
228
|
+
align-items: center;
|
|
229
|
+
justify-content: flex-end;
|
|
230
|
+
gap: 8px;
|
|
231
|
+
padding: 12px 0 4px;
|
|
232
|
+
border-top: 1px solid var(--dsw-alias-border-l2);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
.failed {
|
|
236
|
+
flex: 1;
|
|
237
|
+
min-width: 0;
|
|
238
|
+
margin: 0;
|
|
239
|
+
font-size: 12px;
|
|
240
|
+
line-height: 1.5;
|
|
241
|
+
color: var(--dsw-alias-label-error);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
.discard,
|
|
245
|
+
.save {
|
|
246
|
+
appearance: none;
|
|
247
|
+
border: 1px solid transparent;
|
|
248
|
+
border-radius: 8px;
|
|
249
|
+
padding: 5px 14px;
|
|
250
|
+
font: inherit;
|
|
251
|
+
font-size: 13px;
|
|
252
|
+
line-height: 1.5;
|
|
253
|
+
cursor: pointer;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
.discard {
|
|
257
|
+
border-color: var(--dsw-alias-border-l2);
|
|
258
|
+
background: none;
|
|
259
|
+
color: var(--dsw-alias-label-secondary);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
.discard:hover:not(:disabled) {
|
|
263
|
+
color: var(--dsw-alias-label-primary);
|
|
264
|
+
border-color: var(--dsw-alias-label-dimmed);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
.save {
|
|
268
|
+
background: var(--dsw-alias-label-primary);
|
|
269
|
+
color: var(--dsw-alias-bg-layer-3);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
.discard:disabled,
|
|
273
|
+
.save:disabled {
|
|
274
|
+
opacity: 0.4;
|
|
275
|
+
cursor: default;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
.discard:focus-visible,
|
|
279
|
+
.save:focus-visible {
|
|
280
|
+
outline: 2px solid var(--dsw-alias-brand-primary);
|
|
281
|
+
outline-offset: 1px;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
@media (prefers-reduced-motion: reduce) {
|
|
285
|
+
.card, .chevron, .chevronOpen {
|
|
286
|
+
transition: none;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Staged form model behind the plugin settings card. A card stages what the
|
|
3
|
+
* user types and writes it only when they save — the settings write is a
|
|
4
|
+
* durable, revision-fenced document mutation, so staging keeps what is on
|
|
5
|
+
* screen exactly what a save would store. Self-contained slice of the same
|
|
6
|
+
* pattern the dsh-web-ui family cards use (this package must not depend on a
|
|
7
|
+
* sibling UI package).
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createSnapshotStore, type SettingsScope, type SettingsScopeSnapshot, type SnapshotStore } from '@deepseek-ai/dsh-client-runtime/client'
|
|
11
|
+
|
|
12
|
+
/** The write one field's staged text performs when the card is saved. */
|
|
13
|
+
export type FieldWrite =
|
|
14
|
+
| { kind: 'set'; value: unknown }
|
|
15
|
+
| { kind: 'clear' }
|
|
16
|
+
|
|
17
|
+
/** How one field converts between its stored value and its draft text. */
|
|
18
|
+
export interface FieldSpec {
|
|
19
|
+
/** Field name inside the namespace section. */
|
|
20
|
+
field: string
|
|
21
|
+
/** Render a stored value as draft text; the empty string when the section carries none. */
|
|
22
|
+
format: (value: unknown) => string
|
|
23
|
+
/**
|
|
24
|
+
* The write this draft text stages, or undefined when the text is not a
|
|
25
|
+
* value this field accepts — which blocks the save rather than discarding it.
|
|
26
|
+
*/
|
|
27
|
+
parse: (text: string) => FieldWrite | undefined
|
|
28
|
+
/**
|
|
29
|
+
* True for secret fields (role('secret') in the namespace schema): the
|
|
30
|
+
* redacted wire view never returns the stored value, so the form treats an
|
|
31
|
+
* empty draft as "no change" and judges writes by the namespace's secrets
|
|
32
|
+
* sidecar instead of the user layer.
|
|
33
|
+
*/
|
|
34
|
+
secret?: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** One field as the card renders it. */
|
|
38
|
+
export interface FieldState {
|
|
39
|
+
/** Draft text the control renders. */
|
|
40
|
+
text: string
|
|
41
|
+
/** Whether saving would leave a user-layer entry for this field. */
|
|
42
|
+
overridden: boolean
|
|
43
|
+
/** Whether the draft is not a value this field accepts, which blocks saving. */
|
|
44
|
+
invalid: boolean
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Form state every plugin settings card shares. */
|
|
48
|
+
export interface CardShell {
|
|
49
|
+
/** False while the namespace is still loading; the card renders nothing. */
|
|
50
|
+
available: boolean
|
|
51
|
+
/** Whether the namespace is actually served (the bridge answered). */
|
|
52
|
+
exposed: boolean
|
|
53
|
+
/** Whether the Host document accepts writes. */
|
|
54
|
+
writable: boolean
|
|
55
|
+
/** Whether the form holds edits that a save would write. */
|
|
56
|
+
dirty: boolean
|
|
57
|
+
/** Whether any staged draft is invalid, which blocks the save. */
|
|
58
|
+
invalid: boolean
|
|
59
|
+
/** Whether a save is crossing the wire. */
|
|
60
|
+
saving: boolean
|
|
61
|
+
/** Whether the last save did not land as staged; cleared by the next edit or save. */
|
|
62
|
+
failed: boolean
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The write actions the card's slot entry injects. */
|
|
66
|
+
export interface CardActions {
|
|
67
|
+
/** Stage draft text for one field. */
|
|
68
|
+
edit: (field: string, text: string) => void
|
|
69
|
+
/** Stage a clear, so saving lets the field re-inherit the composition layer. */
|
|
70
|
+
resetField: (field: string) => void
|
|
71
|
+
/** Write every staged edit, then re-seed from what the Host accepted. */
|
|
72
|
+
save: () => void
|
|
73
|
+
/** Drop every staged edit. */
|
|
74
|
+
discard: () => void
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** One field's staged edit. */
|
|
78
|
+
interface StagedEdit {
|
|
79
|
+
/** Draft text the control renders. */
|
|
80
|
+
text: string
|
|
81
|
+
/** True when this edit clears the field whatever text it shows. */
|
|
82
|
+
clear: boolean
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** One staged edit resolved into the write a save performs. */
|
|
86
|
+
interface PlannedWrite {
|
|
87
|
+
/** Field this entry writes. */
|
|
88
|
+
field: string
|
|
89
|
+
/** Perform the write and report whether the Host holds the staged value afterwards. */
|
|
90
|
+
run: (() => Promise<boolean>) | undefined
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** A free-text field. An empty draft clears the field. */
|
|
94
|
+
export function textField(field: string): FieldSpec {
|
|
95
|
+
return {
|
|
96
|
+
field,
|
|
97
|
+
format: value => typeof value === 'string' ? value : '',
|
|
98
|
+
parse: (text) => {
|
|
99
|
+
const trimmed = text.trim()
|
|
100
|
+
return trimmed === '' ? { kind: 'clear' } : { kind: 'set', value: trimmed }
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A boolean field, edited through true/false draft text. */
|
|
106
|
+
export function booleanField(field: string): FieldSpec {
|
|
107
|
+
return {
|
|
108
|
+
field,
|
|
109
|
+
format: value => typeof value === 'boolean' ? String(value) : '',
|
|
110
|
+
parse: (text) => {
|
|
111
|
+
if (text === 'true') return { kind: 'set', value: true }
|
|
112
|
+
if (text === 'false') return { kind: 'set', value: false }
|
|
113
|
+
return undefined
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A secret field (role('secret') in the namespace schema). The stored value is
|
|
120
|
+
* never rendered or returned by the redacted wire view, so:
|
|
121
|
+
* - an empty draft means "no change" (typing nothing must never clear an
|
|
122
|
+
* invisible stored key); the dedicated clear action stages an explicit clear;
|
|
123
|
+
* - a write's outcome is judged by the namespace's secrets sidecar through
|
|
124
|
+
* the {@link CardForm} `secretSettled` hook, never by the user layer.
|
|
125
|
+
*/
|
|
126
|
+
export function secretField(field: string): FieldSpec {
|
|
127
|
+
return {
|
|
128
|
+
field,
|
|
129
|
+
secret: true,
|
|
130
|
+
format: () => '',
|
|
131
|
+
parse: (text) => {
|
|
132
|
+
const trimmed = text.trim()
|
|
133
|
+
if (trimmed === '') return undefined
|
|
134
|
+
return { kind: 'set', value: trimmed }
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Stages one card's edits over one settings scope and writes them on save.
|
|
141
|
+
*
|
|
142
|
+
* The Host is the only authority on whether a value was accepted — its
|
|
143
|
+
* validators own the constraints no schema can express — so the outcome is
|
|
144
|
+
* read back from the section rather than predicted here. A save that did not
|
|
145
|
+
* land keeps its drafts, so the user can correct them instead of retyping.
|
|
146
|
+
*/
|
|
147
|
+
export class CardForm<T> {
|
|
148
|
+
private readonly specs: Map<string, FieldSpec>
|
|
149
|
+
private readonly staged = new Map<string, StagedEdit>()
|
|
150
|
+
private readonly listeners = new Set<() => void>()
|
|
151
|
+
private saving = false
|
|
152
|
+
private failed = false
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param scope - the bound settings scope for this card's namespace.
|
|
156
|
+
* @param specs - the fields this card edits.
|
|
157
|
+
* @param options.secretSettled - for secret fields, whether the namespace
|
|
158
|
+
* currently holds a stored secret (the redacted view never round-trips the
|
|
159
|
+
* value, so a write's outcome is read from the secrets sidecar instead).
|
|
160
|
+
*/
|
|
161
|
+
constructor(
|
|
162
|
+
private readonly scope: SettingsScope<T>,
|
|
163
|
+
specs: FieldSpec[],
|
|
164
|
+
private readonly options: { secretSettled?: (field: string) => boolean } = {},
|
|
165
|
+
) {
|
|
166
|
+
this.specs = new Map(specs.map(spec => [spec.field, spec]))
|
|
167
|
+
scope.subscribe(() => { this.publish() })
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Publish a projection of this form, rebuilt whenever the scope or a draft changes. */
|
|
171
|
+
bind<S>(project: () => S): SnapshotStore<S> {
|
|
172
|
+
const store = createSnapshotStore(project())
|
|
173
|
+
this.listeners.add(() => { store.set(project()) })
|
|
174
|
+
return store
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Read the card-level state: what the Host serves, and what a save would do. */
|
|
178
|
+
shell(): CardShell {
|
|
179
|
+
const snapshot = this.scope.getSnapshot()
|
|
180
|
+
const plan = this.plan()
|
|
181
|
+
return {
|
|
182
|
+
available: snapshot.status !== 'loading',
|
|
183
|
+
exposed: snapshot.status === 'ready',
|
|
184
|
+
writable: snapshot.writable,
|
|
185
|
+
dirty: plan.length > 0,
|
|
186
|
+
invalid: plan.some(item => item.run === undefined),
|
|
187
|
+
saving: this.saving,
|
|
188
|
+
failed: this.failed,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Read one field's state from the effective section and its staged draft. */
|
|
193
|
+
field(field: string): FieldState {
|
|
194
|
+
const spec = this.specOf(field)
|
|
195
|
+
const staged = this.staged.get(field)
|
|
196
|
+
if (staged === undefined) {
|
|
197
|
+
return { text: spec.format(this.sectionValue(field)), overridden: this.stored(field), invalid: false }
|
|
198
|
+
}
|
|
199
|
+
const write = staged.clear ? { kind: 'clear' as const } : spec.parse(staged.text)
|
|
200
|
+
return {
|
|
201
|
+
text: staged.text,
|
|
202
|
+
overridden: write?.kind === 'set',
|
|
203
|
+
// A secret field's empty draft is "no change" (see secretField), never
|
|
204
|
+
// an invalid state — the stored value is invisible, so the user cannot
|
|
205
|
+
// be expected to type over it.
|
|
206
|
+
invalid: write === undefined && !(spec.secret === true && staged.text.trim() === ''),
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** The actions the card's slot registration injects. */
|
|
211
|
+
actions(): CardActions {
|
|
212
|
+
return {
|
|
213
|
+
edit: (field, text) => { this.stage(field, { text, clear: false }) },
|
|
214
|
+
resetField: (field) => {
|
|
215
|
+
this.stage(field, { text: this.specOf(field).format(this.baseValue(field)), clear: true })
|
|
216
|
+
},
|
|
217
|
+
save: () => { void this.save() },
|
|
218
|
+
discard: () => {
|
|
219
|
+
if (this.staged.size === 0 && !this.failed) return
|
|
220
|
+
this.staged.clear()
|
|
221
|
+
this.failed = false
|
|
222
|
+
this.publish()
|
|
223
|
+
},
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Write every staged edit, then re-seed from what the Host accepted.
|
|
229
|
+
* @returns settlement after every write and the read-back.
|
|
230
|
+
*/
|
|
231
|
+
async save(): Promise<void> {
|
|
232
|
+
const plan = this.plan()
|
|
233
|
+
const writes = plan.flatMap(item => item.run === undefined ? [] : [item.run])
|
|
234
|
+
if (plan.length === 0 || this.saving || writes.length !== plan.length) return
|
|
235
|
+
this.saving = true
|
|
236
|
+
this.failed = false
|
|
237
|
+
this.publish()
|
|
238
|
+
let landed = true
|
|
239
|
+
for (const write of writes) {
|
|
240
|
+
landed = await write() && landed
|
|
241
|
+
}
|
|
242
|
+
if (landed) this.staged.clear()
|
|
243
|
+
this.saving = false
|
|
244
|
+
this.failed = !landed
|
|
245
|
+
this.publish()
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Every staged edit a save would write. An entry whose draft is not a value
|
|
250
|
+
* its field accepts carries no write: the form is still dirty, and the save
|
|
251
|
+
* refuses rather than dropping the edit. A staged edit that matches the
|
|
252
|
+
* effective section is not a write at all.
|
|
253
|
+
*/
|
|
254
|
+
private plan(): PlannedWrite[] {
|
|
255
|
+
const plan: PlannedWrite[] = []
|
|
256
|
+
for (const [field, staged] of this.staged) {
|
|
257
|
+
const spec = this.specOf(field)
|
|
258
|
+
if (staged.clear) {
|
|
259
|
+
const present = spec.secret === true
|
|
260
|
+
? (this.options.secretSettled?.(field) ?? false)
|
|
261
|
+
: this.stored(field)
|
|
262
|
+
if (present) plan.push({ field, run: () => this.clear(field) })
|
|
263
|
+
continue
|
|
264
|
+
}
|
|
265
|
+
if (staged.text === spec.format(this.sectionValue(field))) continue
|
|
266
|
+
const write = spec.parse(staged.text)
|
|
267
|
+
if (write === undefined) plan.push({ field, run: undefined })
|
|
268
|
+
else if (write.kind === 'clear') plan.push({ field, run: () => this.clear(field) })
|
|
269
|
+
else plan.push({ field, run: () => this.store(field, write.value) })
|
|
270
|
+
}
|
|
271
|
+
return plan
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private async clear(field: string): Promise<boolean> {
|
|
275
|
+
await this.scope.unset(field)
|
|
276
|
+
const spec = this.specOf(field)
|
|
277
|
+
if (spec.secret === true) return !(this.options.secretSettled?.(field) ?? false)
|
|
278
|
+
return !this.stored(field)
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
private async store(field: string, value: unknown): Promise<boolean> {
|
|
282
|
+
await this.scope.set(field, value)
|
|
283
|
+
const spec = this.specOf(field)
|
|
284
|
+
if (spec.secret === true) return this.options.secretSettled?.(field) ?? true
|
|
285
|
+
return this.userLayer()?.[field] === value
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
private stage(field: string, edit: StagedEdit): void {
|
|
289
|
+
this.staged.set(field, edit)
|
|
290
|
+
this.failed = false
|
|
291
|
+
this.publish()
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
private specOf(field: string): FieldSpec {
|
|
295
|
+
const spec = this.specs.get(field)
|
|
296
|
+
if (spec === undefined) throw new Error(`settings card has no field ${field}`)
|
|
297
|
+
return spec
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
private snapshotOf(): SettingsScopeSnapshot<T> {
|
|
301
|
+
return this.scope.getSnapshot()
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
private sectionValue(field: string): unknown {
|
|
305
|
+
return (this.snapshotOf().value as Record<string, unknown> | undefined)?.[field]
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private baseValue(field: string): unknown {
|
|
309
|
+
return (this.snapshotOf().base as Record<string, unknown> | undefined)?.[field]
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private userLayer(): Record<string, unknown> | undefined {
|
|
313
|
+
return this.snapshotOf().user as Record<string, unknown> | undefined
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private stored(field: string): boolean {
|
|
317
|
+
const user = this.userLayer()
|
|
318
|
+
return user !== undefined && Object.hasOwn(user, field)
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private publish(): void {
|
|
322
|
+
for (const listener of [...this.listeners]) listener()
|
|
323
|
+
}
|
|
324
|
+
}
|