@sdeck/plugin 0.3.1 → 0.4.1
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/create.d.ts +105 -0
- package/dist/create.d.ts.map +1 -0
- package/dist/fields.d.ts +62 -0
- package/dist/fields.d.ts.map +1 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +200 -5
- package/dist/index.js.map +13 -10
- package/dist/manifest.d.ts +10 -3
- package/dist/manifest.d.ts.map +1 -1
- package/dist/permissions.d.ts +16 -0
- package/dist/permissions.d.ts.map +1 -1
- package/dist/plugin.d.ts +28 -20
- package/dist/plugin.d.ts.map +1 -1
- package/dist/settings.d.ts +20 -11
- package/dist/settings.d.ts.map +1 -1
- package/dist/shared.d.ts +11 -0
- package/dist/shared.d.ts.map +1 -1
- package/dist/validate.d.ts +33 -0
- package/dist/validate.d.ts.map +1 -0
- package/package.json +4 -4
- package/src/create.ts +223 -0
- package/src/fields.ts +112 -0
- package/src/index.ts +15 -1
- package/src/manifest.ts +23 -5
- package/src/permissions.ts +17 -0
- package/src/plugin.ts +45 -20
- package/src/settings.ts +24 -11
- package/src/shared.ts +33 -0
- package/src/validate.ts +61 -0
package/src/settings.ts
CHANGED
|
@@ -2,10 +2,11 @@
|
|
|
2
2
|
* Action settings are described by a plain-data schema. The host turns it into a form in the
|
|
3
3
|
* configuration UI, and `resolveSettings` turns whatever is stored into well-typed values.
|
|
4
4
|
*/
|
|
5
|
+
import type { Validator } from "./validate.ts";
|
|
5
6
|
|
|
6
7
|
type Primitive = string | number | boolean;
|
|
7
8
|
|
|
8
|
-
interface FieldBase {
|
|
9
|
+
interface FieldBase<T = unknown> {
|
|
9
10
|
/** Label shown in the configuration UI. */
|
|
10
11
|
label: string;
|
|
11
12
|
/** Help text under the field. */
|
|
@@ -15,6 +16,14 @@ interface FieldBase {
|
|
|
15
16
|
* workflow or gesture, where nothing is drawn.
|
|
16
17
|
*/
|
|
17
18
|
appearance?: boolean;
|
|
19
|
+
/** It cannot be left empty: the action does not run until it is filled in. */
|
|
20
|
+
required?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Checks what was entered: a function that returns what is wrong (`"must be a URL"`), or a
|
|
23
|
+
* schema from zod, valibot or another Standard Schema library. The form shows it, and the action
|
|
24
|
+
* does not run until it is fixed. Values with `{{templates}}` are checked when it runs.
|
|
25
|
+
*/
|
|
26
|
+
validate?: Validator<T>;
|
|
18
27
|
/**
|
|
19
28
|
* Shown only while other fields have these values: `{ to: "page" }` or
|
|
20
29
|
* `{ mode: ["a", "b"] }`. Hidden fields keep their value.
|
|
@@ -22,7 +31,7 @@ interface FieldBase {
|
|
|
22
31
|
visibleIf?: Readonly<Record<string, Primitive | readonly Primitive[]>>;
|
|
23
32
|
}
|
|
24
33
|
|
|
25
|
-
export interface TextField extends FieldBase {
|
|
34
|
+
export interface TextField extends FieldBase<string> {
|
|
26
35
|
type: "text";
|
|
27
36
|
default?: string;
|
|
28
37
|
placeholder?: string;
|
|
@@ -38,7 +47,7 @@ export interface TextField extends FieldBase {
|
|
|
38
47
|
live?: boolean;
|
|
39
48
|
}
|
|
40
49
|
|
|
41
|
-
export interface NumberField extends FieldBase {
|
|
50
|
+
export interface NumberField extends FieldBase<number> {
|
|
42
51
|
type: "number";
|
|
43
52
|
default?: number;
|
|
44
53
|
min?: number;
|
|
@@ -46,7 +55,7 @@ export interface NumberField extends FieldBase {
|
|
|
46
55
|
step?: number;
|
|
47
56
|
}
|
|
48
57
|
|
|
49
|
-
export interface BooleanField extends FieldBase {
|
|
58
|
+
export interface BooleanField extends FieldBase<boolean> {
|
|
50
59
|
type: "boolean";
|
|
51
60
|
default?: boolean;
|
|
52
61
|
}
|
|
@@ -55,14 +64,14 @@ export interface BooleanField extends FieldBase {
|
|
|
55
64
|
* A password or token, only for a plugin's own settings. It is kept in the host's secrets file
|
|
56
65
|
* (never in the config, so it is safe to share or commit) and the UI never reads it back.
|
|
57
66
|
*/
|
|
58
|
-
export interface SecretField extends FieldBase {
|
|
67
|
+
export interface SecretField extends FieldBase<string> {
|
|
59
68
|
type: "secret";
|
|
60
69
|
default?: string;
|
|
61
70
|
placeholder?: string;
|
|
62
71
|
}
|
|
63
72
|
|
|
64
73
|
/** A `#rrggbb` colour. */
|
|
65
|
-
export interface ColorField extends FieldBase {
|
|
74
|
+
export interface ColorField extends FieldBase<string> {
|
|
66
75
|
type: "color";
|
|
67
76
|
default?: string;
|
|
68
77
|
}
|
|
@@ -72,7 +81,7 @@ export interface SelectOption {
|
|
|
72
81
|
label: string;
|
|
73
82
|
}
|
|
74
83
|
|
|
75
|
-
export interface SelectField extends FieldBase {
|
|
84
|
+
export interface SelectField extends FieldBase<string> {
|
|
76
85
|
type: "select";
|
|
77
86
|
/** Fixed options. With `source`, shown before the fetched ones. */
|
|
78
87
|
options: readonly SelectOption[];
|
|
@@ -90,7 +99,7 @@ export interface SelectField extends FieldBase {
|
|
|
90
99
|
}
|
|
91
100
|
|
|
92
101
|
/** The name of one of the user's pages (the UI shows a page picker). */
|
|
93
|
-
export interface PageField extends FieldBase {
|
|
102
|
+
export interface PageField extends FieldBase<string> {
|
|
94
103
|
type: "page";
|
|
95
104
|
default?: string;
|
|
96
105
|
/** Offers choosing no page (an empty value), under this name: `Every page`. */
|
|
@@ -101,13 +110,13 @@ export interface PageField extends FieldBase {
|
|
|
101
110
|
* Keys, touch sensors and the window, picked on a picture of the deck: `key:0`, `sensor:1`,
|
|
102
111
|
* `window`. An empty list stands for the entire deck.
|
|
103
112
|
*/
|
|
104
|
-
export interface SlotsField extends FieldBase {
|
|
113
|
+
export interface SlotsField extends FieldBase<readonly string[]> {
|
|
105
114
|
type: "slots";
|
|
106
115
|
default?: readonly string[];
|
|
107
116
|
}
|
|
108
117
|
|
|
109
118
|
/** An image: an `http(s)` URL or a `data:` URI (the UI accepts a URL or a file). */
|
|
110
|
-
export interface ImageField extends FieldBase {
|
|
119
|
+
export interface ImageField extends FieldBase<string> {
|
|
111
120
|
type: "image";
|
|
112
121
|
default?: string;
|
|
113
122
|
/** As on a text field: `{{spotify.art}}` reaches `run` as written. */
|
|
@@ -124,7 +133,7 @@ export interface OptionGroup {
|
|
|
124
133
|
}
|
|
125
134
|
|
|
126
135
|
/** Named sets of options the user puts together (rooms of lights), only for a plugin's own settings. */
|
|
127
|
-
export interface GroupsField extends FieldBase {
|
|
136
|
+
export interface GroupsField extends FieldBase<readonly OptionGroup[]> {
|
|
128
137
|
type: "groups";
|
|
129
138
|
/** The plugin's `optionSources` entry that lists what a group can contain. */
|
|
130
139
|
source: string;
|
|
@@ -323,6 +332,10 @@ export function checkSchema(
|
|
|
323
332
|
for (const other of ("dependsOn" in field && field.dependsOn) || []) {
|
|
324
333
|
if (!(other in schema)) problems.push(`setting "${key}" depends on unknown "${other}"`);
|
|
325
334
|
}
|
|
335
|
+
const { validate } = field;
|
|
336
|
+
if (validate !== undefined && typeof validate !== "function" && !("~standard" in validate)) {
|
|
337
|
+
problems.push(`setting "${key}": validate must be a function or a Standard Schema`);
|
|
338
|
+
}
|
|
326
339
|
if (field.visibleIf !== undefined) {
|
|
327
340
|
for (const other of Object.keys(field.visibleIf)) {
|
|
328
341
|
if (!(other in schema)) problems.push(`setting "${key}" depends on unknown "${other}"`);
|
package/src/shared.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { useCallback, useSyncExternalStore } from "react";
|
|
2
|
+
|
|
1
3
|
/** A source started on the first listener and stopped after the last one leaves. */
|
|
2
4
|
export class Shared<T> {
|
|
3
5
|
readonly #listeners = new Set<(value: T) => void>();
|
|
@@ -32,3 +34,34 @@ export class Shared<T> {
|
|
|
32
34
|
};
|
|
33
35
|
}
|
|
34
36
|
}
|
|
37
|
+
|
|
38
|
+
/** The latest value of a {@link Shared} in a component, which keeps it started meanwhile. */
|
|
39
|
+
export function useShared<T>(shared: Shared<T>): T | undefined {
|
|
40
|
+
return useSyncExternalStore(
|
|
41
|
+
useCallback((changed) => shared.listen(changed), [shared]),
|
|
42
|
+
() => shared.last,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* A live value read from a {@link Shared}, for the `start` of a state. `undefined` while the source
|
|
48
|
+
* has nothing.
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* plugin.state.string("track", "Track", { start: following(player, (p) => p.track) });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export const following =
|
|
55
|
+
<T, V>(shared: Shared<T>, pick: (value: NonNullable<T>) => V | undefined) =>
|
|
56
|
+
(set: (value: V | undefined) => void): (() => void) => {
|
|
57
|
+
const update = (value: T | undefined) =>
|
|
58
|
+
set(value === undefined || value === null ? undefined : pick(value as NonNullable<T>));
|
|
59
|
+
let heard = false;
|
|
60
|
+
const stop = shared.listen((value) => {
|
|
61
|
+
heard = true;
|
|
62
|
+
update(value);
|
|
63
|
+
});
|
|
64
|
+
// Something else may have started it already.
|
|
65
|
+
if (!heard && shared.last !== undefined) update(shared.last);
|
|
66
|
+
return stop;
|
|
67
|
+
};
|
package/src/validate.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checking what the user entered. Fields say what is wrong in a sentence, or leave it to a schema
|
|
3
|
+
* from any library that follows Standard Schema (zod, valibot, arktype), without depending on one.
|
|
4
|
+
*/
|
|
5
|
+
import { isFieldVisible, type SettingField, type SettingsSchema } from "./settings.ts";
|
|
6
|
+
|
|
7
|
+
/** The part of https://standardschema.dev that checking a value needs. */
|
|
8
|
+
export interface StandardSchema {
|
|
9
|
+
readonly "~standard": {
|
|
10
|
+
readonly version: 1;
|
|
11
|
+
readonly vendor: string;
|
|
12
|
+
validate(value: unknown): StandardResult | Promise<StandardResult>;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type StandardResult =
|
|
17
|
+
| { readonly value: unknown; readonly issues?: undefined }
|
|
18
|
+
| { readonly issues: readonly { readonly message: string }[] };
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Says what is wrong with a value, or nothing when it is fine. A schema only checks: what it would
|
|
22
|
+
* turn the value into is not used.
|
|
23
|
+
*/
|
|
24
|
+
export type Validator<T> =
|
|
25
|
+
// biome-ignore lint/suspicious/noConfusingVoidType: lets a validator simply not return anything.
|
|
26
|
+
((value: T) => string | undefined | void | Promise<string | undefined | void>) | StandardSchema;
|
|
27
|
+
|
|
28
|
+
const isSchema = (v: object): v is StandardSchema => "~standard" in v;
|
|
29
|
+
|
|
30
|
+
async function problem(field: SettingField, value: unknown): Promise<string | undefined> {
|
|
31
|
+
const empty = Array.isArray(value) ? value.length === 0 : String(value ?? "").trim() === "";
|
|
32
|
+
if (empty) return field.required ? "this is required" : undefined;
|
|
33
|
+
const validate = field.validate as Validator<unknown> | undefined;
|
|
34
|
+
// What a template gives is only known when it runs, and is checked then.
|
|
35
|
+
if (!validate || (typeof value === "string" && value.includes("{{"))) return undefined;
|
|
36
|
+
if (!isSchema(validate)) return (await validate(value)) || undefined;
|
|
37
|
+
const { issues } = await validate["~standard"].validate(value);
|
|
38
|
+
return issues?.[0]?.message;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* What is wrong with `settings` (as `resolveSettings` gives them), by field. Fields that are not
|
|
43
|
+
* shown, and empty ones that are not `required`, are fine.
|
|
44
|
+
*/
|
|
45
|
+
export async function checkSettings(
|
|
46
|
+
schema: SettingsSchema,
|
|
47
|
+
settings: Readonly<Record<string, unknown>>,
|
|
48
|
+
): Promise<Record<string, string>> {
|
|
49
|
+
const found: Record<string, string> = {};
|
|
50
|
+
for (const [key, field] of Object.entries(schema)) {
|
|
51
|
+
if (!field.required && !field.validate) continue;
|
|
52
|
+
if (!isFieldVisible(field, settings)) continue;
|
|
53
|
+
try {
|
|
54
|
+
const text = await problem(field, settings[key]);
|
|
55
|
+
if (text) found[key] = text;
|
|
56
|
+
} catch (error) {
|
|
57
|
+
found[key] = error instanceof Error ? error.message : String(error);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return found;
|
|
61
|
+
}
|