@sdeck/plugin 0.0.0-stage → 0.1.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/dist/bridge.d.ts +42 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/connection.d.ts +44 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3352 -0
- package/dist/index.js.map +37 -0
- package/dist/manifest.d.ts +45 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/plugin.d.ts +304 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/settings.d.ts +107 -0
- package/dist/settings.d.ts.map +1 -0
- package/dist/shared.d.ts +9 -0
- package/dist/shared.d.ts.map +1 -0
- package/package.json +41 -4
- package/src/bridge.ts +128 -0
- package/src/connection.ts +206 -0
- package/src/index.ts +83 -0
- package/src/manifest.ts +85 -0
- package/src/plugin.ts +541 -0
- package/src/settings.ts +232 -0
- package/src/shared.ts +34 -0
- package/README.md +0 -3
package/src/settings.ts
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
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
|
+
* Shown only while other fields have these values: `{ to: "page" }` or
|
|
15
|
+
* `{ mode: ["a", "b"] }`. Hidden fields keep their value.
|
|
16
|
+
*/
|
|
17
|
+
visibleIf?: Readonly<Record<string, Primitive | readonly Primitive[]>>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface TextField extends FieldBase {
|
|
21
|
+
type: "text";
|
|
22
|
+
default?: string;
|
|
23
|
+
placeholder?: string;
|
|
24
|
+
multiline?: boolean;
|
|
25
|
+
/** Suggestions from one of the plugin's `optionSources` (any text is still allowed). */
|
|
26
|
+
source?: string;
|
|
27
|
+
/** Other fields the suggestions depend on, passed to the source. */
|
|
28
|
+
dependsOn?: readonly string[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface NumberField extends FieldBase {
|
|
32
|
+
type: "number";
|
|
33
|
+
default?: number;
|
|
34
|
+
min?: number;
|
|
35
|
+
max?: number;
|
|
36
|
+
step?: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface BooleanField extends FieldBase {
|
|
40
|
+
type: "boolean";
|
|
41
|
+
default?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A password or token, only for a plugin's own settings. It is kept in the host's secrets file
|
|
46
|
+
* (never in the config, so it is safe to share or commit) and the UI never reads it back.
|
|
47
|
+
*/
|
|
48
|
+
export interface SecretField extends FieldBase {
|
|
49
|
+
type: "secret";
|
|
50
|
+
default?: string;
|
|
51
|
+
placeholder?: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A `#rrggbb` colour. */
|
|
55
|
+
export interface ColorField extends FieldBase {
|
|
56
|
+
type: "color";
|
|
57
|
+
default?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface SelectOption {
|
|
61
|
+
value: string;
|
|
62
|
+
label: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface SelectField extends FieldBase {
|
|
66
|
+
type: "select";
|
|
67
|
+
/** Fixed options. With `source`, shown before the fetched ones. */
|
|
68
|
+
options: readonly SelectOption[];
|
|
69
|
+
/**
|
|
70
|
+
* Options fetched from one of the plugin's `optionSources` when the form opens (OBS
|
|
71
|
+
* scenes, audio devices). Any stored string is then kept, since the list can change.
|
|
72
|
+
*/
|
|
73
|
+
source?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Other fields the fetched options depend on, passed to the source. The form asks for them
|
|
76
|
+
* first, and clears this field when one of them changes.
|
|
77
|
+
*/
|
|
78
|
+
dependsOn?: readonly string[];
|
|
79
|
+
default?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** The name of one of the user's pages (the UI shows a page picker). */
|
|
83
|
+
export interface PageField extends FieldBase {
|
|
84
|
+
type: "page";
|
|
85
|
+
default?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** An image: an `http(s)` URL or a `data:` URI (the UI accepts a URL or a file). */
|
|
89
|
+
export interface ImageField extends FieldBase {
|
|
90
|
+
type: "image";
|
|
91
|
+
default?: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type SettingField =
|
|
95
|
+
| TextField
|
|
96
|
+
| NumberField
|
|
97
|
+
| BooleanField
|
|
98
|
+
| ColorField
|
|
99
|
+
| SelectField
|
|
100
|
+
| PageField
|
|
101
|
+
| ImageField
|
|
102
|
+
| SecretField;
|
|
103
|
+
|
|
104
|
+
export type SettingsSchema = Readonly<Record<string, SettingField>>;
|
|
105
|
+
|
|
106
|
+
/** The value type of one field. */
|
|
107
|
+
export type FieldValue<F extends SettingField> = F extends NumberField
|
|
108
|
+
? number
|
|
109
|
+
: F extends BooleanField
|
|
110
|
+
? boolean
|
|
111
|
+
: F extends SelectField
|
|
112
|
+
? F extends { source: string }
|
|
113
|
+
? string
|
|
114
|
+
: F["options"][number]["value"]
|
|
115
|
+
: string;
|
|
116
|
+
|
|
117
|
+
/** The settings object an action receives for a schema. */
|
|
118
|
+
export type InferSettings<S extends SettingsSchema> = {
|
|
119
|
+
-readonly [K in keyof S]: FieldValue<S[K]>;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const HEX = /^#[0-9a-f]{6}$/i;
|
|
123
|
+
|
|
124
|
+
/** The value a field takes when nothing (valid) is stored. */
|
|
125
|
+
export function defaultValue(field: SettingField): string | number | boolean {
|
|
126
|
+
switch (field.type) {
|
|
127
|
+
case "number":
|
|
128
|
+
return field.default ?? field.min ?? 0;
|
|
129
|
+
case "boolean":
|
|
130
|
+
return field.default ?? false;
|
|
131
|
+
case "color":
|
|
132
|
+
return field.default ?? "#000000";
|
|
133
|
+
case "select":
|
|
134
|
+
return field.default ?? field.options[0]?.value ?? "";
|
|
135
|
+
default:
|
|
136
|
+
return field.default ?? "";
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function coerce(field: SettingField, value: unknown): string | number | boolean {
|
|
141
|
+
switch (field.type) {
|
|
142
|
+
case "number": {
|
|
143
|
+
const n = typeof value === "string" && value.trim() !== "" ? Number(value) : value;
|
|
144
|
+
if (typeof n !== "number" || !Number.isFinite(n)) return defaultValue(field);
|
|
145
|
+
return Math.min(field.max ?? Infinity, Math.max(field.min ?? -Infinity, n));
|
|
146
|
+
}
|
|
147
|
+
case "boolean":
|
|
148
|
+
return typeof value === "boolean" ? value : defaultValue(field);
|
|
149
|
+
case "color":
|
|
150
|
+
return typeof value === "string" && HEX.test(value)
|
|
151
|
+
? value.toLowerCase()
|
|
152
|
+
: defaultValue(field);
|
|
153
|
+
case "select":
|
|
154
|
+
if (typeof value !== "string") return defaultValue(field);
|
|
155
|
+
if (field.source !== undefined) return value;
|
|
156
|
+
return field.options.some((o) => o.value === value) ? value : defaultValue(field);
|
|
157
|
+
default:
|
|
158
|
+
return typeof value === "string" ? value : defaultValue(field);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Stored settings, made safe: every field of `schema` is present and of the right type (invalid
|
|
164
|
+
* or missing values get the default, numbers are clamped), and unknown keys are dropped.
|
|
165
|
+
*/
|
|
166
|
+
export function resolveSettings<S extends SettingsSchema>(
|
|
167
|
+
schema: S,
|
|
168
|
+
stored: Readonly<Record<string, unknown>> | undefined,
|
|
169
|
+
): InferSettings<S> {
|
|
170
|
+
const out: Record<string, unknown> = {};
|
|
171
|
+
for (const [key, field] of Object.entries(schema)) out[key] = coerce(field, stored?.[key]);
|
|
172
|
+
return out as InferSettings<S>;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Whether `field` is shown for these (resolved) settings. */
|
|
176
|
+
export function isFieldVisible(
|
|
177
|
+
field: SettingField,
|
|
178
|
+
settings: Readonly<Record<string, unknown>>,
|
|
179
|
+
): boolean {
|
|
180
|
+
if (!field.visibleIf) return true;
|
|
181
|
+
return Object.entries(field.visibleIf).every(([key, want]) => {
|
|
182
|
+
const value = settings[key];
|
|
183
|
+
return Array.isArray(want) ? want.includes(value as Primitive) : value === want;
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Problems with a schema (empty when it is fine). Used when a plugin is loaded. `sources` are
|
|
189
|
+
* the plugin's option sources, checked against fields that name one.
|
|
190
|
+
*/
|
|
191
|
+
export function checkSchema(
|
|
192
|
+
schema: SettingsSchema,
|
|
193
|
+
sources: Readonly<Record<string, unknown>> = {},
|
|
194
|
+
): string[] {
|
|
195
|
+
const problems: string[] = [];
|
|
196
|
+
const types = new Set([
|
|
197
|
+
"text",
|
|
198
|
+
"number",
|
|
199
|
+
"boolean",
|
|
200
|
+
"color",
|
|
201
|
+
"select",
|
|
202
|
+
"page",
|
|
203
|
+
"image",
|
|
204
|
+
"secret",
|
|
205
|
+
]);
|
|
206
|
+
for (const [key, field] of Object.entries(schema)) {
|
|
207
|
+
if (!field || typeof field !== "object" || !types.has(field.type)) {
|
|
208
|
+
problems.push(`setting "${key}" has an unknown type`);
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
if (typeof field.label !== "string") problems.push(`setting "${key}" needs a label`);
|
|
212
|
+
const source = "source" in field ? field.source : undefined;
|
|
213
|
+
if (source !== undefined && !(source in sources)) {
|
|
214
|
+
problems.push(`setting "${key}" uses unknown option source "${source}"`);
|
|
215
|
+
}
|
|
216
|
+
if (
|
|
217
|
+
field.type === "select" &&
|
|
218
|
+
(!Array.isArray(field.options) || (field.options.length === 0 && source === undefined))
|
|
219
|
+
) {
|
|
220
|
+
problems.push(`setting "${key}" needs at least one option or a source`);
|
|
221
|
+
}
|
|
222
|
+
for (const other of ("dependsOn" in field && field.dependsOn) || []) {
|
|
223
|
+
if (!(other in schema)) problems.push(`setting "${key}" depends on unknown "${other}"`);
|
|
224
|
+
}
|
|
225
|
+
if (field.visibleIf !== undefined) {
|
|
226
|
+
for (const other of Object.keys(field.visibleIf)) {
|
|
227
|
+
if (!(other in schema)) problems.push(`setting "${key}" depends on unknown "${other}"`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return problems;
|
|
232
|
+
}
|
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/README.md
DELETED