@bridgebrew/plugin-manager 0.0.0-stage → 0.0.2
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 +21 -0
- package/browser/browser.tsx +2173 -0
- package/browser/screen/harness-profiles.tsx +1348 -0
- package/browser/screen/marketplace-model.ts +356 -0
- package/browser/screen/marketplace.tsx +1173 -0
- package/browser/screen/plugin-dependencies.tsx +139 -0
- package/browser/screen/plugin-docs.tsx +142 -0
- package/browser/screen/plugin-logs.tsx +549 -0
- package/browser/screen/plugin-parts.tsx +257 -0
- package/browser/screen/plugin-readme.tsx +152 -0
- package/browser/screen/plugin-settings.tsx +169 -0
- package/browser/screen/plugins-model.ts +427 -0
- package/browser/screen/plugins.tsx +2593 -0
- package/browser/screen/settings-action.ts +42 -0
- package/browser/screen/settings-fields.ts +414 -0
- package/browser/screen/settings-form.tsx +1669 -0
- package/browser/screen/settings-kinds.tsx +918 -0
- package/browser/screen/settings-page.tsx +72 -0
- package/browser/screen/settings-row.tsx +558 -0
- package/browser/screen/update-all.tsx +175 -0
- package/browser/tsconfig.json +15 -0
- package/package.json +61 -4
- package/tsconfig.json +5 -0
- package/README.md +0 -3
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// An `x-bridge-action` run (./settings-form.tsx): the saved entry is posted to the plugin's route, in the workspace
|
|
2
|
+
// that holds the settings, and the answer becomes what the button shows.
|
|
3
|
+
import { isRecord, refusalOf } from '@bridge/ui';
|
|
4
|
+
import type { ActionResult } from './settings-form.tsx';
|
|
5
|
+
|
|
6
|
+
export async function runSettingsAction(
|
|
7
|
+
fetcher: (url: string, init: RequestInit) => Promise<Response>,
|
|
8
|
+
{ root, plugin, level }: { root: string; plugin: string; level: string },
|
|
9
|
+
{ path }: { path: string },
|
|
10
|
+
entry: unknown,
|
|
11
|
+
): Promise<ActionResult> {
|
|
12
|
+
try {
|
|
13
|
+
const res = await fetcher(
|
|
14
|
+
`/${encodeURIComponent(root)}/api/${encodeURIComponent(plugin)}${path}`,
|
|
15
|
+
{
|
|
16
|
+
method: 'POST',
|
|
17
|
+
headers: { 'content-type': 'application/json', 'x-bridge-node': level },
|
|
18
|
+
body: JSON.stringify(entry),
|
|
19
|
+
},
|
|
20
|
+
);
|
|
21
|
+
const body: unknown = await res.json().catch(() => undefined);
|
|
22
|
+
if (!res.ok) {
|
|
23
|
+
const { message, hint } = refusalOf(body, res.status);
|
|
24
|
+
return { ok: false, error: message, hint };
|
|
25
|
+
}
|
|
26
|
+
if (isRecord(body) && body.ok === true)
|
|
27
|
+
return { ok: true, message: typeof body.message === 'string' ? body.message : undefined };
|
|
28
|
+
if (isRecord(body) && body.ok === false && typeof body.error === 'string')
|
|
29
|
+
return {
|
|
30
|
+
ok: false,
|
|
31
|
+
error: body.error,
|
|
32
|
+
hint: typeof body.hint === 'string' ? body.hint : undefined,
|
|
33
|
+
};
|
|
34
|
+
return { ok: false, error: 'The answer was not understood.' };
|
|
35
|
+
} catch {
|
|
36
|
+
return {
|
|
37
|
+
ok: false,
|
|
38
|
+
error: 'Bridge could not be reached',
|
|
39
|
+
hint: 'Check that it is running, then try again.',
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// The Settings screen's pure part: which control a settings property gets from its JSON Schema, how a typed value is
|
|
2
|
+
// read back, and how a value's origin (AD-26) is labelled.
|
|
3
|
+
|
|
4
|
+
import { cutText } from '@bridge/kernel/text';
|
|
5
|
+
|
|
6
|
+
export type Control =
|
|
7
|
+
| 'text'
|
|
8
|
+
| 'number'
|
|
9
|
+
| 'slider'
|
|
10
|
+
| 'boolean'
|
|
11
|
+
| 'enum'
|
|
12
|
+
| 'swatches'
|
|
13
|
+
| 'options'
|
|
14
|
+
| 'colour'
|
|
15
|
+
| 'colour-pair'
|
|
16
|
+
| 'image'
|
|
17
|
+
| 'image-pair'
|
|
18
|
+
| 'object'
|
|
19
|
+
| 'map'
|
|
20
|
+
| 'tags'
|
|
21
|
+
| 'list'
|
|
22
|
+
| 'json';
|
|
23
|
+
export type Origin = 'default' | 'workspace' | { node: string };
|
|
24
|
+
/** What swatches draw (`x-bridge-swatches`): a kind, and the section's fields it draws with. */
|
|
25
|
+
export interface Swatches {
|
|
26
|
+
kind: string;
|
|
27
|
+
colour?: string;
|
|
28
|
+
colours?: string[];
|
|
29
|
+
image?: string;
|
|
30
|
+
paint?: string;
|
|
31
|
+
mode?: 'light' | 'dark';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The JSON Schema keywords the form reads. */
|
|
35
|
+
export interface Schema {
|
|
36
|
+
type?: unknown;
|
|
37
|
+
title?: string;
|
|
38
|
+
description?: string;
|
|
39
|
+
applies?: string;
|
|
40
|
+
enum?: unknown[];
|
|
41
|
+
properties?: Record<string, Schema>;
|
|
42
|
+
required?: string[];
|
|
43
|
+
additionalProperties?: boolean | Schema;
|
|
44
|
+
items?: Schema;
|
|
45
|
+
minimum?: number;
|
|
46
|
+
maximum?: number;
|
|
47
|
+
minLength?: number;
|
|
48
|
+
maxLength?: number;
|
|
49
|
+
pattern?: string;
|
|
50
|
+
'x-bridge-pattern-message'?: string;
|
|
51
|
+
/** On a setting that is one object, or on the object schema of the items of a setting's own list (not a nested one):
|
|
52
|
+
* a button (or a list of them) that posts the saved value, or the saved entry, to the plugin's route `path` (under
|
|
53
|
+
* `/<workspace>/api/<plugin>`) and shows its answer `{ ok: true }` or `{ ok: false, error, hint? }`. `save: true`
|
|
54
|
+
* tests the draft of an explicit setting, then saves it once ok; `draft: true` tests the draft and never saves. `id`: the name an attention's fix
|
|
55
|
+
* gives it (`{ action: <id>, label }`, research-047), so a toast runs it. */
|
|
56
|
+
'x-bridge-action'?:
|
|
57
|
+
| { id?: string; label: string; path: string; save?: boolean; draft?: boolean }
|
|
58
|
+
| { id?: string; label: string; path: string; save?: boolean; draft?: boolean }[];
|
|
59
|
+
/** `explicit`: an edit stays a draft until a `save: true` action saves it (or Reset); Cancel drops the draft. */
|
|
60
|
+
'x-bridge-commit'?: 'explicit';
|
|
61
|
+
/** The choices a select or tags field offers: a page kind (the plugins, the drivers, `modes`, a rendering, an icon set,
|
|
62
|
+
* a window manager, a navigator, a theme, a prompt), or `route:<path>`: the choices the plugin's route answers to
|
|
63
|
+
* `GET` (`{ options: [{ value, label? }] }`). */
|
|
64
|
+
'x-bridge-options'?: string;
|
|
65
|
+
/** On an array of strings: its items can be reordered (drag, Alt+arrows). */
|
|
66
|
+
'x-bridge-ordered'?: boolean;
|
|
67
|
+
/** With `x-bridge-options`: when the page offers no choice, a disabled select saying this ("None for this model")
|
|
68
|
+
* rather than a free text field. */
|
|
69
|
+
'x-bridge-empty'?: string;
|
|
70
|
+
default?: unknown;
|
|
71
|
+
/** An `enum`'s labels, in its order (else the values themselves). */
|
|
72
|
+
'x-bridge-labels'?: string[];
|
|
73
|
+
/** Shown in Settings only with "Show advanced settings" (or once set at the level shown). */
|
|
74
|
+
'x-bridge-advanced'?: boolean;
|
|
75
|
+
/** On a string property: a secret when its object's `sensitive` is true (a password field; the host stores it);
|
|
76
|
+
* `"always"`: a secret whatever `sensitive` says (no Sensitive checkbox; the host stores it as one). */
|
|
77
|
+
'x-bridge-secret'?: boolean | 'always';
|
|
78
|
+
/** On a string property: a CSS colour, picked or typed (its rule the schema's `pattern`). */
|
|
79
|
+
'x-bridge-colour'?: boolean;
|
|
80
|
+
/** On a string property: a multi-line field, the kit's auto-growing `Textarea` instead of an `Input`. */
|
|
81
|
+
'x-bridge-multiline'?: boolean;
|
|
82
|
+
/** Sibling fields set with this one, to these values, when it is given a value (the wallpaper chosen: the paint
|
|
83
|
+
* becomes `wallpaper`), at the same level. */
|
|
84
|
+
'x-bridge-also'?: Record<string, unknown>;
|
|
85
|
+
/** On a number with a `minimum` and a `maximum`: a slider, its value read out beside it, saved once it rests. */
|
|
86
|
+
'x-bridge-slider'?: boolean;
|
|
87
|
+
/** With `x-bridge-slider`: how far one move goes (default: 1 for an integer, a hundredth of the range otherwise). */
|
|
88
|
+
'x-bridge-step'?: number;
|
|
89
|
+
/** With `x-bridge-colour` or `x-bridge-image`: a value by mode (research-028), one string for both modes or
|
|
90
|
+
* `{ light?, dark? }` (the schema's `type` `["string", "object"]`, its `properties` `light` and `dark`): a colour's
|
|
91
|
+
* half left unset follows the theme, an image's the other half. */
|
|
92
|
+
'x-bridge-modes'?: boolean;
|
|
93
|
+
/** With `x-bridge-colour`: the theme's custom property an unset colour takes (`--accent`), drawn as such. */
|
|
94
|
+
'x-bridge-theme-colour'?: string;
|
|
95
|
+
/** On a string property: an image the host keeps, the value its id. The route below `/_bridge/workspaces/<slug>/` that
|
|
96
|
+
* takes its bytes (`PUT`, answering `{ value }` once stored) and serves it (`GET ?<value>`). */
|
|
97
|
+
'x-bridge-image'?: string;
|
|
98
|
+
/** On an `enum` (or a string with `x-bridge-options`, its choices the page's): its choices drawn as swatches
|
|
99
|
+
* (./settings-kinds.tsx `swatchKinds`), with the other fields of the section they draw with named here:
|
|
100
|
+
* `{ kind: "paint", colours: [a, b], image? }` (a canvas painted from two colours; its `wallpaper` choice the
|
|
101
|
+
* image), `{ kind: "shape", colour }` (the rail's
|
|
102
|
+
* tile in a colour), `{ kind: "fit", image, paint?, colours? }` (an image laid on a canvas), `{ kind: "theme", mode }`
|
|
103
|
+
* (a theme's window in miniature; unset: the Bridge default's theme in `mode`), `{ kind: "choice" }` (labels only). */
|
|
104
|
+
'x-bridge-swatches'?: Swatches;
|
|
105
|
+
/** On a property of an object: shown only while each sibling named here has one of the values listed (its default
|
|
106
|
+
* when unset); hidden, its value is kept. */
|
|
107
|
+
'x-bridge-shown-if'?: Record<string, unknown[]>;
|
|
108
|
+
/** With an enum: choices that need another field of the section set (`field`), unavailable until it is, `message`
|
|
109
|
+
* saying why. */
|
|
110
|
+
'x-bridge-requires'?: Record<string, { field: string; message: string }>;
|
|
111
|
+
/** `workspace`: a value for the whole workspace, set on its root only; Settings reads and writes it there whatever the
|
|
112
|
+
* level shown. On a Bridge-wide (`_host`) plugin's field: a value a workspace may also set for itself, through a
|
|
113
|
+
* field `x-bridge-settings-of` naming that plugin (its own value is then Bridge's default). */
|
|
114
|
+
'x-bridge-level'?: 'workspace';
|
|
115
|
+
/** On an object property: the workspace's own values of the `_host` plugin chosen in the sibling field named here
|
|
116
|
+
* (`{ [plugin key]: { [field]: value } }`): Settings shows, in its place, a row for each field of the chosen plugin
|
|
117
|
+
* marked `x-bridge-level: "workspace"`, under the choice (research-028 CAP-6: a window manager's settings). */
|
|
118
|
+
'x-bridge-settings-of'?: string;
|
|
119
|
+
/** The group a row is shown in, under its name as a heading (consecutive rows of one group together). */
|
|
120
|
+
'x-bridge-group'?: string;
|
|
121
|
+
/** More about the setting than its `description` (at most 255 characters): shown on demand, by its help button. */
|
|
122
|
+
'x-bridge-details'?: string;
|
|
123
|
+
/** `true`: the user may change this field for themselves in a workspace, over the workspace's value ("Mine", kept by
|
|
124
|
+
* their client: Settings' `mineLevel`, research-028 CAP-1, CAP-2); `"client"`: the user's alone, the same in every
|
|
125
|
+
* workspace (a client-wide preference: the mode), shown on Mine only. */
|
|
126
|
+
'x-bridge-mine'?: true | 'client';
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const isSchema = (value: unknown): value is Schema =>
|
|
130
|
+
typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
131
|
+
const asSchema = (value: unknown): Schema => (isSchema(value) ? value : {});
|
|
132
|
+
|
|
133
|
+
/** A schema's control: an `enum` a select (swatches with `x-bridge-swatches`); a string with `x-bridge-options` a select
|
|
134
|
+
* of those options (swatches with `x-bridge-swatches`), with `x-bridge-colour` a colour, with `x-bridge-image` an image,
|
|
135
|
+
* either by mode with `x-bridge-modes` (a light and a dark half); `string`,
|
|
136
|
+
* `number`/`integer`, `boolean` their own; an object with `properties` a group of sub-fields, one whose
|
|
137
|
+
* `additionalProperties` is a schema a map (cards by name); an array of strings tags, of objects a list of cards;
|
|
138
|
+
* anything else (several types, none) the JSON editor. */
|
|
139
|
+
export function controlOf(property: unknown): Control {
|
|
140
|
+
const {
|
|
141
|
+
type,
|
|
142
|
+
enum: allowed,
|
|
143
|
+
properties,
|
|
144
|
+
additionalProperties,
|
|
145
|
+
items,
|
|
146
|
+
'x-bridge-options': options,
|
|
147
|
+
} = asSchema(property);
|
|
148
|
+
const schema = asSchema(property);
|
|
149
|
+
if (Array.isArray(allowed) && allowed.length > 0)
|
|
150
|
+
return typeof schema['x-bridge-swatches']?.kind === 'string' ? 'swatches' : 'enum';
|
|
151
|
+
if (schema['x-bridge-modes'] === true && schema['x-bridge-colour'] === true) return 'colour-pair';
|
|
152
|
+
if (schema['x-bridge-modes'] === true && typeof schema['x-bridge-image'] === 'string')
|
|
153
|
+
return 'image-pair';
|
|
154
|
+
if (
|
|
155
|
+
type === 'string' &&
|
|
156
|
+
typeof options === 'string' &&
|
|
157
|
+
typeof schema['x-bridge-swatches']?.kind === 'string'
|
|
158
|
+
)
|
|
159
|
+
return 'swatches';
|
|
160
|
+
if (type === 'string' && schema['x-bridge-colour'] === true) return 'colour';
|
|
161
|
+
if (type === 'string' && typeof schema['x-bridge-image'] === 'string') return 'image';
|
|
162
|
+
if (type === 'string') return typeof options === 'string' ? 'options' : 'text';
|
|
163
|
+
if (
|
|
164
|
+
(type === 'number' || type === 'integer') &&
|
|
165
|
+
schema['x-bridge-slider'] === true &&
|
|
166
|
+
typeof schema.minimum === 'number' &&
|
|
167
|
+
typeof schema.maximum === 'number'
|
|
168
|
+
)
|
|
169
|
+
return 'slider';
|
|
170
|
+
if (type === 'number' || type === 'integer') return 'number';
|
|
171
|
+
if (type === 'boolean') return 'boolean';
|
|
172
|
+
if (type === 'object' && isSchema(properties) && Object.keys(properties).length > 0)
|
|
173
|
+
return 'object';
|
|
174
|
+
if (type === 'object' && isSchema(additionalProperties)) return 'map';
|
|
175
|
+
if (type === 'array' && asSchema(items).type === 'string') return 'tags';
|
|
176
|
+
if (type === 'array' && asSchema(items).type === 'object') return 'list';
|
|
177
|
+
return 'json';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** The deepest place along `path` (an error's) that the form draws a field for: a property of an object, an entry of a
|
|
181
|
+
* map, an item of a list; the rest is said in the message. */
|
|
182
|
+
export function anchorOf(schema: unknown, path: (string | number)[]): (string | number)[] {
|
|
183
|
+
const anchor: (string | number)[] = [];
|
|
184
|
+
let at = asSchema(schema);
|
|
185
|
+
for (const step of path) {
|
|
186
|
+
const control = controlOf(at);
|
|
187
|
+
if (control === 'object' && typeof step === 'string' && Object.hasOwn(at.properties!, step))
|
|
188
|
+
at = asSchema(at.properties![step]);
|
|
189
|
+
else if (control === 'map' && typeof step === 'string') at = asSchema(at.additionalProperties);
|
|
190
|
+
else if (control === 'list' && typeof step === 'number') at = asSchema(at.items);
|
|
191
|
+
else if (
|
|
192
|
+
(control === 'colour-pair' || control === 'image-pair') &&
|
|
193
|
+
(step === 'light' || step === 'dark')
|
|
194
|
+
)
|
|
195
|
+
at = asSchema(at.properties?.[step]);
|
|
196
|
+
else break;
|
|
197
|
+
anchor.push(step);
|
|
198
|
+
}
|
|
199
|
+
return anchor;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Every title, key, and description below a schema, for the search. */
|
|
203
|
+
export function schemaWords(schema: unknown): string[] {
|
|
204
|
+
const { properties, additionalProperties, items } = asSchema(schema);
|
|
205
|
+
const below = [
|
|
206
|
+
...Object.entries(isSchema(properties) ? properties : {}).flatMap(([key, each]) => [
|
|
207
|
+
key,
|
|
208
|
+
humanize(key),
|
|
209
|
+
...[asSchema(each).title, asSchema(each).description].filter(
|
|
210
|
+
(word): word is string => !!word,
|
|
211
|
+
),
|
|
212
|
+
...schemaWords(each),
|
|
213
|
+
]),
|
|
214
|
+
...(isSchema(additionalProperties)
|
|
215
|
+
? [additionalProperties.title ?? '', ...schemaWords(additionalProperties)]
|
|
216
|
+
: []),
|
|
217
|
+
...(isSchema(items) ? [items.title ?? '', ...schemaWords(items)] : []),
|
|
218
|
+
];
|
|
219
|
+
return below.filter(Boolean);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** A map entry's new name: trimmed, not empty, not another entry's. */
|
|
223
|
+
export function checkName(name: string, taken: string[]): string | undefined {
|
|
224
|
+
const trimmed = name.trim();
|
|
225
|
+
if (!trimmed) return 'Enter a name.';
|
|
226
|
+
if (taken.includes(trimmed)) return `An entry named ${trimmed} already exists.`;
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** `record` with key `from` renamed `to`, in place (the order kept). */
|
|
231
|
+
export const renameKey = (record: Record<string, unknown>, from: string, to: string) =>
|
|
232
|
+
Object.fromEntries(Object.entries(record).map(([key, each]) => [key === from ? to : key, each]));
|
|
233
|
+
|
|
234
|
+
/** What the user typed, as the value to save: a number for `number` (an empty or non-numeric text refused, a fraction
|
|
235
|
+
* too when `integer`), parsed JSON
|
|
236
|
+
* for `json`; the host checks the type. */
|
|
237
|
+
export function readValue(
|
|
238
|
+
control: Control,
|
|
239
|
+
text: string,
|
|
240
|
+
integer = false,
|
|
241
|
+
): { ok: true; value: unknown } | { ok: false; message: string } {
|
|
242
|
+
if (control === 'number') {
|
|
243
|
+
const value = Number(text);
|
|
244
|
+
if (text.trim() === '' || !Number.isFinite(value))
|
|
245
|
+
return { ok: false, message: 'Enter a number.' };
|
|
246
|
+
return integer && !Number.isInteger(value)
|
|
247
|
+
? { ok: false, message: 'Enter a whole number.' }
|
|
248
|
+
: { ok: true, value };
|
|
249
|
+
}
|
|
250
|
+
if (control === 'json') {
|
|
251
|
+
try {
|
|
252
|
+
return { ok: true, value: JSON.parse(text) };
|
|
253
|
+
} catch (error) {
|
|
254
|
+
return { ok: false, message: `Not valid JSON: ${(error as Error).message}` };
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return { ok: true, value: text };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** A value as the editor shows it: JSON pretty-printed, a string as it is; nothing when unset. */
|
|
261
|
+
export const textOf = (control: Control, value: unknown) =>
|
|
262
|
+
value === undefined
|
|
263
|
+
? ''
|
|
264
|
+
: control === 'json'
|
|
265
|
+
? JSON.stringify(value, null, 2)
|
|
266
|
+
: value === null
|
|
267
|
+
? ''
|
|
268
|
+
: String(value);
|
|
269
|
+
|
|
270
|
+
/** An origin's line on node `here`: "Default", "Set in: Workspace" (`installations.json`, or an override on `root`, the
|
|
271
|
+
* workspace level), "Set in: <title>" (here), or "Inherited from <title>". */
|
|
272
|
+
/** A row's origin when its value is Bridge's default: not shown (the row says nothing then). */
|
|
273
|
+
export const DEFAULT_ORIGIN = 'Default';
|
|
274
|
+
|
|
275
|
+
export function originLabel(origin: Origin, here: string, titleOf: (id: string) => string): string {
|
|
276
|
+
if (origin === 'default') return DEFAULT_ORIGIN;
|
|
277
|
+
if (origin === 'workspace' || origin.node === 'root') return 'Set in: Workspace';
|
|
278
|
+
if (origin.node === here) return `Set in: ${titleOf(here)}`;
|
|
279
|
+
return `Inherited from ${titleOf(origin.node)}`;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** The level `property`'s value is read and written at when node `here` is shown: `root` for a workspace's
|
|
283
|
+
* (`x-bridge-level`), else `here`. */
|
|
284
|
+
export const fieldLevel = (property: Schema, here: string) =>
|
|
285
|
+
property['x-bridge-level'] === 'workspace' ? 'root' : here;
|
|
286
|
+
|
|
287
|
+
/** The level of the user's own values (`x-bridge-mine`), kept by their client, beside a workspace's nodes. */
|
|
288
|
+
export const mineLevel = 'mine';
|
|
289
|
+
/** How the user may change `property` for themselves: per workspace, client-wide, or not at all. */
|
|
290
|
+
export const mineKind = (property: Schema): 'workspace' | 'client' | undefined =>
|
|
291
|
+
property['x-bridge-mine'] === true
|
|
292
|
+
? 'workspace'
|
|
293
|
+
: property['x-bridge-mine'] === 'client'
|
|
294
|
+
? 'client'
|
|
295
|
+
: undefined;
|
|
296
|
+
/** A row's origin line on Mine: what applies and why. `set`: the user changed it; else, for a workspace's field, whether
|
|
297
|
+
* the workspace sets it (`proposed`) or leaves it to its default, Bridge's or the theme's (`inherited`: the line). */
|
|
298
|
+
export function mineOrigin(kind: 'workspace' | 'client', set: boolean, inherited?: string): string {
|
|
299
|
+
if (kind === 'client')
|
|
300
|
+
return set ? 'Changed by you, in every workspace' : 'Default, in every workspace';
|
|
301
|
+
return set ? 'Changed by you' : (inherited ?? 'Proposed by the workspace');
|
|
302
|
+
}
|
|
303
|
+
/** A workspace's row (`label` its origin line) whose value the user's Mine changes for them: the line says theirs applies. */
|
|
304
|
+
const OVERRIDDEN = ' · Changed by you in Custom, which applies for you';
|
|
305
|
+
export const overriddenOrigin = (label: string) => `${label}${OVERRIDDEN}`;
|
|
306
|
+
|
|
307
|
+
/** The level the value applied comes from, in a word, for a row's badge (`label` its origin line in full): Custom,
|
|
308
|
+
* Workspace, Bridge, or a node's title; '' for Bridge's default (no badge). */
|
|
309
|
+
export function originSource(label: string): string {
|
|
310
|
+
if (label.endsWith(OVERRIDDEN) || label.startsWith('Changed by you')) return 'Custom';
|
|
311
|
+
if (label === 'Proposed by the workspace' || label.startsWith('Set in: Workspace'))
|
|
312
|
+
return 'Workspace';
|
|
313
|
+
if (label === 'Set for all of Bridge') return 'Bridge';
|
|
314
|
+
for (const prefix of ['Set in: ', 'Inherited from '])
|
|
315
|
+
if (label.startsWith(prefix)) return label.slice(prefix.length);
|
|
316
|
+
return '';
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** Whether the value is an override on node `here` (`root` included), which Reset removes. */
|
|
320
|
+
export const setOn = (origin: Origin | undefined, here: string) =>
|
|
321
|
+
typeof origin === 'object' && origin.node === here;
|
|
322
|
+
|
|
323
|
+
/** A section's title: its plugin's screen title, else its key capitalised. */
|
|
324
|
+
export const sectionTitle = (key: string, screenTitle: string | undefined) =>
|
|
325
|
+
screenTitle ?? `${key.charAt(0).toUpperCase()}${key.slice(1)}`;
|
|
326
|
+
|
|
327
|
+
/** A field id as a title: words split at case changes, `-`, `_`, and `.`, each capitalised (`decisionTimeoutMs` →
|
|
328
|
+
* "Decision Timeout Ms"). */
|
|
329
|
+
export const humanize = (field: string) =>
|
|
330
|
+
field
|
|
331
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
|
|
332
|
+
.split(/[\s_.-]+/)
|
|
333
|
+
.filter(Boolean)
|
|
334
|
+
.map((word) => `${word.charAt(0).toUpperCase()}${word.slice(1)}`)
|
|
335
|
+
.join(' ');
|
|
336
|
+
|
|
337
|
+
/** A search: `@modified` (only values set at the level shown) and the rest, trimmed and lowercased. */
|
|
338
|
+
export function parseQuery(query: string) {
|
|
339
|
+
const words = query.split(/\s+/).filter(Boolean);
|
|
340
|
+
return {
|
|
341
|
+
modified: words.includes('@modified'),
|
|
342
|
+
text: words
|
|
343
|
+
.filter((word) => word !== '@modified')
|
|
344
|
+
.join(' ')
|
|
345
|
+
.toLowerCase(),
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Whether a setting matches `text`: in any of `parts` (label, id, description, section), case-insensitive. */
|
|
350
|
+
export const matches = (text: string, parts: (string | undefined)[]) =>
|
|
351
|
+
!text || parts.some((part) => part?.toLowerCase().includes(text));
|
|
352
|
+
|
|
353
|
+
/** `text` cut around each occurrence of `needle` (case-insensitive), for highlighting. */
|
|
354
|
+
export function highlight(text: string, needle: string): { text: string; match: boolean }[] {
|
|
355
|
+
if (!needle) return [{ text, match: false }];
|
|
356
|
+
const parts: { text: string; match: boolean }[] = [];
|
|
357
|
+
const lower = text.toLowerCase();
|
|
358
|
+
let at = 0;
|
|
359
|
+
for (let found = lower.indexOf(needle, at); found >= 0; found = lower.indexOf(needle, at)) {
|
|
360
|
+
if (found > at) parts.push({ text: text.slice(at, found), match: false });
|
|
361
|
+
parts.push({ text: text.slice(found, found + needle.length), match: true });
|
|
362
|
+
at = found + needle.length;
|
|
363
|
+
}
|
|
364
|
+
if (at < text.length) parts.push({ text: text.slice(at), match: false });
|
|
365
|
+
return parts;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** An object or array value in one line: its JSON when short, else its keys or its length. */
|
|
369
|
+
export function summaryOf(value: unknown): string {
|
|
370
|
+
if (value === undefined) return 'Not set';
|
|
371
|
+
const json = JSON.stringify(value);
|
|
372
|
+
if (json.length <= 60) return json;
|
|
373
|
+
if (Array.isArray(value)) return `${value.length} items`;
|
|
374
|
+
if (typeof value === 'object' && value !== null) {
|
|
375
|
+
const keys = Object.keys(value);
|
|
376
|
+
return `${keys.length} ${keys.length === 1 ? 'entry' : 'entries'}: ${keys.join(', ')}`;
|
|
377
|
+
}
|
|
378
|
+
return `${cutText(json, 59)}…`;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** A stored secret's marker, as the host leaves it in a setting in place of the value (packages/host/src/secrets.ts). */
|
|
382
|
+
export const isSecretMarker = (value: unknown) =>
|
|
383
|
+
typeof value === 'string' && /^\[secret:[^\]/]+\/[^\]]+\]$/.test(value);
|
|
384
|
+
|
|
385
|
+
/** Saves values one at a time through `save` (which answers a refusal, or nothing once written): one given while another
|
|
386
|
+
* saves waits, only the last one kept, and is saved next, whatever the first one's answer (a later choice stands on
|
|
387
|
+
* its own). `settled` once nothing saves or waits, with the last value saved and its refusal: until then the host's
|
|
388
|
+
* values are intermediate and the draft keeps the user's. */
|
|
389
|
+
export function serialSaver(
|
|
390
|
+
save: (value: unknown) => Promise<string | undefined>,
|
|
391
|
+
settled: (value: unknown, refusal: string | undefined) => void,
|
|
392
|
+
) {
|
|
393
|
+
let saving = false;
|
|
394
|
+
let waiting: { value: unknown } | undefined;
|
|
395
|
+
const run = async (value: unknown): Promise<void> => {
|
|
396
|
+
saving = true;
|
|
397
|
+
const refusal = await save(value).catch((error: unknown) =>
|
|
398
|
+
error instanceof Error ? error.message : String(error),
|
|
399
|
+
);
|
|
400
|
+
const next = waiting;
|
|
401
|
+
waiting = undefined;
|
|
402
|
+
if (next) return run(next.value);
|
|
403
|
+
saving = false;
|
|
404
|
+
settled(value, refusal);
|
|
405
|
+
};
|
|
406
|
+
return {
|
|
407
|
+
/** A value saving, or waiting. */
|
|
408
|
+
busy: () => saving,
|
|
409
|
+
push: (value: unknown) => {
|
|
410
|
+
if (saving) waiting = { value };
|
|
411
|
+
else void run(value);
|
|
412
|
+
},
|
|
413
|
+
};
|
|
414
|
+
}
|