dsh-sidebar-fold 0.1.6 → 0.1.12
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.zh.md +141 -120
- package/dsh.plugin.json +2 -2
- package/lib/client.js +879 -100
- package/package.json +3 -3
- 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 +300 -210
- package/src/client/menu-entry.ts +157 -0
- package/src/client/settings-card.ts +451 -334
- package/src/index.js +99 -88
package/src/client/index.ts
CHANGED
|
@@ -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 {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
const
|
|
52
|
-
const
|
|
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
|
-
|
|
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
|
+
}
|