@sdeck/plugin 0.0.0-stage → 0.2.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.
@@ -0,0 +1,293 @@
1
+ /**
2
+ * Action settings are described by a plain-data schema. The host turns it into a form in the
3
+ * configuration UI, and `resolveSettings` turns whatever is stored into well-typed values.
4
+ */
5
+
6
+ type Primitive = string | number | boolean;
7
+
8
+ interface FieldBase {
9
+ /** Label shown in the configuration UI. */
10
+ label: string;
11
+ /** Help text under the field. */
12
+ description?: string;
13
+ /**
14
+ * It only changes how the button looks. It is asked for on a button, but not in a step of a
15
+ * workflow or gesture, where nothing is drawn.
16
+ */
17
+ appearance?: boolean;
18
+ /**
19
+ * Shown only while other fields have these values: `{ to: "page" }` or
20
+ * `{ mode: ["a", "b"] }`. Hidden fields keep their value.
21
+ */
22
+ visibleIf?: Readonly<Record<string, Primitive | readonly Primitive[]>>;
23
+ }
24
+
25
+ export interface TextField extends FieldBase {
26
+ type: "text";
27
+ default?: string;
28
+ placeholder?: string;
29
+ multiline?: boolean;
30
+ /** Suggestions from one of the plugin's `optionSources` (any text is still allowed). */
31
+ source?: string;
32
+ /** Other fields the suggestions depend on, passed to the source. */
33
+ dependsOn?: readonly string[];
34
+ }
35
+
36
+ export interface NumberField extends FieldBase {
37
+ type: "number";
38
+ default?: number;
39
+ min?: number;
40
+ max?: number;
41
+ step?: number;
42
+ }
43
+
44
+ export interface BooleanField extends FieldBase {
45
+ type: "boolean";
46
+ default?: boolean;
47
+ }
48
+
49
+ /**
50
+ * A password or token, only for a plugin's own settings. It is kept in the host's secrets file
51
+ * (never in the config, so it is safe to share or commit) and the UI never reads it back.
52
+ */
53
+ export interface SecretField extends FieldBase {
54
+ type: "secret";
55
+ default?: string;
56
+ placeholder?: string;
57
+ }
58
+
59
+ /** A `#rrggbb` colour. */
60
+ export interface ColorField extends FieldBase {
61
+ type: "color";
62
+ default?: string;
63
+ }
64
+
65
+ export interface SelectOption {
66
+ value: string;
67
+ label: string;
68
+ }
69
+
70
+ export interface SelectField extends FieldBase {
71
+ type: "select";
72
+ /** Fixed options. With `source`, shown before the fetched ones. */
73
+ options: readonly SelectOption[];
74
+ /**
75
+ * Options fetched from one of the plugin's `optionSources` when the form opens (OBS
76
+ * scenes, audio devices). Any stored string is then kept, since the list can change.
77
+ */
78
+ source?: string;
79
+ /**
80
+ * Other fields the fetched options depend on, passed to the source. The form asks for them
81
+ * first, and clears this field when one of them changes.
82
+ */
83
+ dependsOn?: readonly string[];
84
+ default?: string;
85
+ }
86
+
87
+ /** The name of one of the user's pages (the UI shows a page picker). */
88
+ export interface PageField extends FieldBase {
89
+ type: "page";
90
+ default?: string;
91
+ }
92
+
93
+ /** An image: an `http(s)` URL or a `data:` URI (the UI accepts a URL or a file). */
94
+ export interface ImageField extends FieldBase {
95
+ type: "image";
96
+ default?: string;
97
+ }
98
+
99
+ /** A named set the user made of options, from a `groups` field. */
100
+ export interface OptionGroup {
101
+ /** Stays the same when the group is renamed; refer to the group by it. */
102
+ id: string;
103
+ name: string;
104
+ /** The chosen options' values. */
105
+ members: readonly string[];
106
+ }
107
+
108
+ /** Named sets of options the user puts together (rooms of lights), only for a plugin's own settings. */
109
+ export interface GroupsField extends FieldBase {
110
+ type: "groups";
111
+ /** The plugin's `optionSources` entry that lists what a group can contain. */
112
+ source: string;
113
+ /** What a group contains, for the form, e.g. `lights` (default: `items`). */
114
+ noun?: string;
115
+ default?: readonly OptionGroup[];
116
+ }
117
+
118
+ export type SettingField =
119
+ | TextField
120
+ | NumberField
121
+ | BooleanField
122
+ | ColorField
123
+ | SelectField
124
+ | PageField
125
+ | ImageField
126
+ | GroupsField
127
+ | SecretField;
128
+
129
+ export type SettingsSchema = Readonly<Record<string, SettingField>>;
130
+
131
+ /** The value type of one field. */
132
+ export type FieldValue<F extends SettingField> = F extends NumberField
133
+ ? number
134
+ : F extends BooleanField
135
+ ? boolean
136
+ : F extends GroupsField
137
+ ? readonly OptionGroup[]
138
+ : F extends SelectField
139
+ ? F extends { source: string }
140
+ ? string
141
+ : F["options"][number]["value"]
142
+ : string;
143
+
144
+ /** A value of any field. */
145
+ export type SettingValue = Primitive | readonly OptionGroup[];
146
+
147
+ /** The settings object an action receives for a schema. */
148
+ export type InferSettings<S extends SettingsSchema> = {
149
+ -readonly [K in keyof S]: FieldValue<S[K]>;
150
+ };
151
+
152
+ const HEX = /^#[0-9a-f]{6}$/i;
153
+
154
+ /** The value a field takes when nothing (valid) is stored. */
155
+ export function defaultValue(field: SettingField): SettingValue {
156
+ switch (field.type) {
157
+ case "number":
158
+ return field.default ?? field.min ?? 0;
159
+ case "boolean":
160
+ return field.default ?? false;
161
+ case "color":
162
+ return field.default ?? "#000000";
163
+ case "groups":
164
+ return field.default ?? [];
165
+ case "select":
166
+ return field.default ?? field.options[0]?.value ?? "";
167
+ default:
168
+ return field.default ?? "";
169
+ }
170
+ }
171
+
172
+ /** The valid groups in a stored value, or `undefined` if it isn't a list. */
173
+ export function optionGroups(value: unknown): OptionGroup[] | undefined {
174
+ if (!Array.isArray(value)) return undefined;
175
+ const ids = new Set<string>();
176
+ return value.flatMap((group: Partial<OptionGroup> | null) => {
177
+ if (typeof group?.id !== "string" || !group.id || ids.has(group.id)) return [];
178
+ ids.add(group.id);
179
+ const members = Array.isArray(group.members) ? group.members : [];
180
+ return [
181
+ {
182
+ id: group.id,
183
+ name: typeof group.name === "string" ? group.name : "",
184
+ members: [...new Set(members.filter((m) => typeof m === "string"))],
185
+ },
186
+ ];
187
+ });
188
+ }
189
+
190
+ function coerce(field: SettingField, value: unknown): SettingValue {
191
+ switch (field.type) {
192
+ case "number": {
193
+ const n = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
194
+ if (typeof n !== "number" || !Number.isFinite(n)) return defaultValue(field);
195
+ return Math.min(field.max ?? Infinity, Math.max(field.min ?? -Infinity, n));
196
+ }
197
+ case "boolean":
198
+ return typeof value === "boolean" ? value : defaultValue(field);
199
+ case "color":
200
+ return typeof value === "string" && HEX.test(value)
201
+ ? value.toLowerCase()
202
+ : defaultValue(field);
203
+ case "groups":
204
+ return optionGroups(value) ?? defaultValue(field);
205
+ case "select":
206
+ if (typeof value !== "string") return defaultValue(field);
207
+ if (field.source !== undefined) return value;
208
+ return field.options.some((o) => o.value === value) ? value : defaultValue(field);
209
+ default:
210
+ return typeof value === "string" ? value : defaultValue(field);
211
+ }
212
+ }
213
+
214
+ /**
215
+ * Stored settings, made safe: every field of `schema` is present and of the right type (invalid
216
+ * or missing values get the default, numbers are clamped), and unknown keys are dropped.
217
+ */
218
+ export function resolveSettings<S extends SettingsSchema>(
219
+ schema: S,
220
+ stored: Readonly<Record<string, unknown>> | undefined,
221
+ ): InferSettings<S> {
222
+ const out: Record<string, unknown> = {};
223
+ for (const [key, field] of Object.entries(schema)) out[key] = coerce(field, stored?.[key]);
224
+ return out as InferSettings<S>;
225
+ }
226
+
227
+ /** The fields that change what an action does, without those for how its button looks. */
228
+ export function behaviorFields(schema: SettingsSchema): SettingsSchema {
229
+ return Object.fromEntries(Object.entries(schema).filter(([, field]) => !field.appearance));
230
+ }
231
+
232
+ /** Whether `field` is shown for these (resolved) settings. */
233
+ export function isFieldVisible(
234
+ field: SettingField,
235
+ settings: Readonly<Record<string, unknown>>,
236
+ ): boolean {
237
+ if (!field.visibleIf) return true;
238
+ return Object.entries(field.visibleIf).every(([key, want]) => {
239
+ const value = settings[key];
240
+ return Array.isArray(want) ? want.includes(value as Primitive) : value === want;
241
+ });
242
+ }
243
+
244
+ /**
245
+ * Problems with a schema (empty when it is fine). Used when a plugin is loaded. `sources` are
246
+ * the plugin's option sources, checked against fields that name one.
247
+ */
248
+ export function checkSchema(
249
+ schema: SettingsSchema,
250
+ sources: Readonly<Record<string, unknown>> = {},
251
+ ): string[] {
252
+ const problems: string[] = [];
253
+ const types = new Set([
254
+ "text",
255
+ "number",
256
+ "boolean",
257
+ "color",
258
+ "select",
259
+ "page",
260
+ "image",
261
+ "groups",
262
+ "secret",
263
+ ]);
264
+ for (const [key, field] of Object.entries(schema)) {
265
+ if (!field || typeof field !== "object" || !types.has(field.type)) {
266
+ problems.push(`setting "${key}" has an unknown type`);
267
+ continue;
268
+ }
269
+ if (typeof field.label !== "string") problems.push(`setting "${key}" needs a label`);
270
+ const source = "source" in field ? field.source : undefined;
271
+ if (field.type === "groups" && source === undefined) {
272
+ problems.push(`setting "${key}" needs a source`);
273
+ }
274
+ if (source !== undefined && !(source in sources)) {
275
+ problems.push(`setting "${key}" uses unknown option source "${source}"`);
276
+ }
277
+ if (
278
+ field.type === "select" &&
279
+ (!Array.isArray(field.options) || (field.options.length === 0 && source === undefined))
280
+ ) {
281
+ problems.push(`setting "${key}" needs at least one option or a source`);
282
+ }
283
+ for (const other of ("dependsOn" in field && field.dependsOn) || []) {
284
+ if (!(other in schema)) problems.push(`setting "${key}" depends on unknown "${other}"`);
285
+ }
286
+ if (field.visibleIf !== undefined) {
287
+ for (const other of Object.keys(field.visibleIf)) {
288
+ if (!(other in schema)) problems.push(`setting "${key}" depends on unknown "${other}"`);
289
+ }
290
+ }
291
+ }
292
+ return problems;
293
+ }
package/src/shared.ts ADDED
@@ -0,0 +1,34 @@
1
+ /** A source started on the first listener and stopped after the last one leaves. */
2
+ export class Shared<T> {
3
+ readonly #listeners = new Set<(value: T) => void>();
4
+ readonly #start: (emit: (value: T) => void) => () => void;
5
+ #stop: (() => void) | undefined;
6
+ #last: { value: T } | undefined;
7
+
8
+ constructor(start: (emit: (value: T) => void) => () => void) {
9
+ this.#start = start;
10
+ }
11
+
12
+ /** The latest value, if any arrived since it started. */
13
+ get last(): T | undefined {
14
+ return this.#last?.value;
15
+ }
16
+
17
+ listen(listener: (value: T) => void): () => void {
18
+ this.#listeners.add(listener);
19
+ if (!this.#stop) {
20
+ this.#stop = this.#start((value) => {
21
+ this.#last = { value };
22
+ for (const l of [...this.#listeners]) l(value);
23
+ });
24
+ }
25
+ return () => {
26
+ this.#listeners.delete(listener);
27
+ if (this.#listeners.size === 0) {
28
+ this.#stop?.();
29
+ this.#stop = undefined;
30
+ this.#last = undefined;
31
+ }
32
+ };
33
+ }
34
+ }
package/src/ui.ts ADDED
@@ -0,0 +1,3 @@
1
+ /** The parts that are plain data and functions, for UIs: no React, engine or runtime behind them. */
2
+ export * from "./permissions.ts";
3
+ export * from "./settings.ts";
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.