@rohal12/spindle 0.59.28 → 0.59.30
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 +1 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +718 -300
- package/dist/pkg/macro-registry.js +19 -0
- package/dist/pkg/macro-registry.json +19 -0
- package/dist/pkg/story-variables.js +731 -234
- package/package.json +1 -1
- package/src/action-registry.ts +59 -5
- package/src/automation/load-yaml.ts +111 -20
- package/src/automation/runner.ts +15 -13
- package/src/automation/types.ts +7 -5
- package/src/class-registry.ts +8 -5
- package/src/code-check.ts +187 -24
- package/src/components/macros/Checkbox.tsx +5 -2
- package/src/components/macros/Cycle.tsx +1 -0
- package/src/components/macros/For.tsx +1 -7
- package/src/components/macros/Listbox.tsx +1 -0
- package/src/components/macros/MenubarButtons.tsx +6 -1
- package/src/components/macros/Nobr.tsx +1 -0
- package/src/components/macros/PassageDisplay.tsx +42 -0
- package/src/components/macros/Radiobutton.tsx +1 -0
- package/src/components/macros/SaveManager.tsx +19 -4
- package/src/components/macros/SettingsControls.tsx +19 -4
- package/src/components/macros/Span.tsx +1 -0
- package/src/components/macros/StoryTitle.tsx +1 -0
- package/src/components/macros/VarDisplay.tsx +2 -3
- package/src/components/macros/Widget.tsx +2 -0
- package/src/components/macros/WidgetInvocation.tsx +5 -6
- package/src/components/macros/arg-utils.ts +58 -21
- package/src/components/macros/input-macro.ts +1 -0
- package/src/components/macros/locals-scope.tsx +19 -8
- package/src/components/macros/macro-args.ts +11 -3
- package/src/expression.ts +34 -18
- package/src/hooks/use-merged-locals.ts +17 -2
- package/src/hooks/use-modal-focus.ts +190 -129
- package/src/index.tsx +23 -13
- package/src/js-bindings.ts +332 -0
- package/src/js-lexer.ts +12 -3
- package/src/macro-discovery.ts +25 -22
- package/src/static-literal.ts +87 -16
- package/src/store.ts +31 -2
- package/src/story-api.ts +15 -5
- package/src/story-variables.ts +91 -28
- package/src/structural.ts +19 -15
- package/src/tooling.ts +36 -10
- package/src/transform.ts +22 -7
- package/src/utils/namespace.ts +4 -0
- package/src/utils/object-path.ts +2 -2
- package/src/utils/stable-key.ts +17 -6
- package/src/utils/value-kinds.ts +38 -1
- package/src/widgets/register-widget-def.ts +7 -8
- package/src/widgets/widget-def.ts +41 -4
- package/src/widgets/widget-registry.ts +14 -0
- package/types/index.d.ts +12 -5
- package/types/tooling.d.ts +12 -2
package/package.json
CHANGED
package/src/action-registry.ts
CHANGED
|
@@ -26,7 +26,13 @@ export interface StoryAction {
|
|
|
26
26
|
options?: string[];
|
|
27
27
|
value?: unknown;
|
|
28
28
|
disabled?: boolean;
|
|
29
|
-
|
|
29
|
+
/**
|
|
30
|
+
* What the control does. An action that finishes later (QuickSave and
|
|
31
|
+
* QuickLoad wait for storage) returns a promise that settles when it has
|
|
32
|
+
* finished; `Story.performAction()` passes it on and
|
|
33
|
+
* `Story.waitForActions()` waits for it (#504).
|
|
34
|
+
*/
|
|
35
|
+
perform: (value?: unknown) => void | Promise<void>;
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
/** The fields of a StoryAction that are left out when unset. */
|
|
@@ -60,19 +66,30 @@ interface Entry {
|
|
|
60
66
|
*/
|
|
61
67
|
const actions = new Map<string, Entry[]>();
|
|
62
68
|
const idCounters = new Map<string, number>();
|
|
69
|
+
/**
|
|
70
|
+
* IDs handed out and not yet registered, or registered: a control registers
|
|
71
|
+
* in a layout effect, after every control of the same render has allocated its
|
|
72
|
+
* ID, so the registry alone can't tell that "link:Room:2" is taken (#486).
|
|
73
|
+
* Released when the last registration of an ID goes.
|
|
74
|
+
*/
|
|
75
|
+
const reserved = new Set<string>();
|
|
63
76
|
|
|
64
77
|
/**
|
|
65
78
|
* The counters reset on every navigation, so a passage's controls get the
|
|
66
79
|
* same IDs each time it is shown. Controls that stay mounted across
|
|
67
80
|
* navigations (e.g. in StoryInterface) keep theirs, so allocation skips every
|
|
68
|
-
* ID still registered (#233)
|
|
81
|
+
* ID still registered (#233) and every one allocated but not registered yet
|
|
82
|
+
* (#486).
|
|
69
83
|
*/
|
|
70
84
|
export function generateActionId(
|
|
71
85
|
type: ActionType,
|
|
72
86
|
key: string,
|
|
73
87
|
authorId?: string,
|
|
74
88
|
): string {
|
|
75
|
-
if (authorId)
|
|
89
|
+
if (authorId) {
|
|
90
|
+
reserved.add(authorId);
|
|
91
|
+
return authorId;
|
|
92
|
+
}
|
|
76
93
|
|
|
77
94
|
const base = `${type}:${key}`;
|
|
78
95
|
let count = idCounters.get(base) ?? 0;
|
|
@@ -80,8 +97,9 @@ export function generateActionId(
|
|
|
80
97
|
do {
|
|
81
98
|
count++;
|
|
82
99
|
id = count === 1 ? base : `${base}:${count}`;
|
|
83
|
-
} while (actions.has(id));
|
|
100
|
+
} while (actions.has(id) || reserved.has(id));
|
|
84
101
|
idCounters.set(base, count);
|
|
102
|
+
reserved.add(id);
|
|
85
103
|
return id;
|
|
86
104
|
}
|
|
87
105
|
|
|
@@ -113,12 +131,45 @@ export function registerAction(action: StoryAction): ActionRegistration {
|
|
|
113
131
|
const index = current ? current.indexOf(entry) : -1;
|
|
114
132
|
if (!current || index === -1) return;
|
|
115
133
|
current.splice(index, 1);
|
|
116
|
-
if (current.length === 0)
|
|
134
|
+
if (current.length === 0) {
|
|
135
|
+
actions.delete(id);
|
|
136
|
+
reserved.delete(id);
|
|
137
|
+
}
|
|
117
138
|
if (index === current.length) notify(); // it was the listed one
|
|
118
139
|
},
|
|
119
140
|
};
|
|
120
141
|
}
|
|
121
142
|
|
|
143
|
+
/** The promises of the performed actions that have not settled yet. */
|
|
144
|
+
const running = new Set<Promise<void>>();
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Perform an action, as `Story.performAction()` does. One that returns a
|
|
148
|
+
* promise is tracked until it settles, for `settleRunningActions()`, and the
|
|
149
|
+
* promise is returned for the caller to await (its rejection stays the
|
|
150
|
+
* caller's to handle).
|
|
151
|
+
*/
|
|
152
|
+
export function performRegisteredAction(
|
|
153
|
+
action: StoryAction,
|
|
154
|
+
value?: unknown,
|
|
155
|
+
): void | Promise<void> {
|
|
156
|
+
const result = action.perform(value);
|
|
157
|
+
if (!result || typeof result.then !== 'function') return result;
|
|
158
|
+
|
|
159
|
+
const done = () => void running.delete(result);
|
|
160
|
+
running.add(result);
|
|
161
|
+
result.then(done, done);
|
|
162
|
+
return result.then((v) => v);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Wait until every performed action has finished, whether or not it failed. */
|
|
166
|
+
export async function settleRunningActions(): Promise<void> {
|
|
167
|
+
// An action can perform another while it finishes
|
|
168
|
+
while (running.size > 0) {
|
|
169
|
+
await Promise.allSettled(running);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
122
173
|
export function getActions(): StoryAction[] {
|
|
123
174
|
return Array.from(actions.values(), listed);
|
|
124
175
|
}
|
|
@@ -134,11 +185,14 @@ function listed(entries: Entry[]): StoryAction {
|
|
|
134
185
|
|
|
135
186
|
export function clearActions(): void {
|
|
136
187
|
actions.clear();
|
|
188
|
+
reserved.clear();
|
|
137
189
|
notify();
|
|
138
190
|
}
|
|
139
191
|
|
|
140
192
|
export function resetIdCounters(): void {
|
|
141
193
|
idCounters.clear();
|
|
194
|
+
// Only what is registered stays taken; the rest was never mounted
|
|
195
|
+
for (const id of reserved) if (!actions.has(id)) reserved.delete(id);
|
|
142
196
|
}
|
|
143
197
|
|
|
144
198
|
function notify(): void {
|
|
@@ -1,33 +1,118 @@
|
|
|
1
1
|
import * as yaml from 'js-yaml';
|
|
2
2
|
import type { AutomationScript, AutomationStep } from './types';
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
type Fail = (message: string) => never;
|
|
5
|
+
|
|
6
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
7
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** `value` as an object whose keys are all in `allowed`. */
|
|
11
|
+
function checkObject(
|
|
12
|
+
value: unknown,
|
|
13
|
+
what: string,
|
|
14
|
+
allowed: readonly string[],
|
|
15
|
+
fail: Fail,
|
|
16
|
+
): Record<string, unknown> {
|
|
17
|
+
if (!isRecord(value)) fail(`${what} must be an object`);
|
|
18
|
+
for (const key of Object.keys(value)) {
|
|
19
|
+
if (!allowed.includes(key)) {
|
|
20
|
+
fail(`${what} has an unknown field "${key}" (${allowed.join(', ')})`);
|
|
21
|
+
}
|
|
7
22
|
}
|
|
8
|
-
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
9
25
|
|
|
10
|
-
|
|
11
|
-
const hasAssert = 'assert' in s;
|
|
12
|
-
const hasWait = 'wait' in s;
|
|
13
|
-
const hasSet = 'set' in s;
|
|
26
|
+
type FieldCheck = (value: unknown) => boolean;
|
|
14
27
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
const isString: FieldCheck = (v) => typeof v === 'string';
|
|
29
|
+
const isBoolean: FieldCheck = (v) => typeof v === 'boolean';
|
|
30
|
+
const isCount: FieldCheck = (v) => Number.isInteger(v) && (v as number) >= 0;
|
|
31
|
+
|
|
32
|
+
function checkFields(
|
|
33
|
+
obj: Record<string, unknown>,
|
|
34
|
+
what: string,
|
|
35
|
+
checks: Record<string, [FieldCheck, string]>,
|
|
36
|
+
fail: Fail,
|
|
37
|
+
): void {
|
|
38
|
+
for (const [key, [valid, expected]] of Object.entries(checks)) {
|
|
39
|
+
if (key in obj && !valid(obj[key])) {
|
|
40
|
+
fail(`"${key}" of ${what} must be ${expected}`);
|
|
41
|
+
}
|
|
19
42
|
}
|
|
43
|
+
}
|
|
20
44
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
45
|
+
const MATCHER_FIELDS: Record<string, [FieldCheck, string]> = {
|
|
46
|
+
type: [isString, 'a string'],
|
|
47
|
+
id: [isString, 'a string'],
|
|
48
|
+
target: [isString, 'a string'],
|
|
49
|
+
variable: [isString, 'a string'],
|
|
50
|
+
label: [isString, 'a string'],
|
|
51
|
+
disabled: [isBoolean, 'true or false'],
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const ASSERT_FIELDS: Record<string, [FieldCheck, string]> = {
|
|
55
|
+
passage: [isString, 'a string'],
|
|
56
|
+
variables: [isRecord, 'an object of variable names and values'],
|
|
57
|
+
actions: [Array.isArray, 'a list of action matchers'],
|
|
58
|
+
actionCount: [isCount, 'a non-negative integer'],
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function checkAssert(value: unknown, fail: Fail): void {
|
|
62
|
+
const assert = checkObject(
|
|
63
|
+
value,
|
|
64
|
+
'"assert"',
|
|
65
|
+
Object.keys(ASSERT_FIELDS),
|
|
66
|
+
fail,
|
|
67
|
+
);
|
|
68
|
+
checkFields(assert, '"assert"', ASSERT_FIELDS, fail);
|
|
69
|
+
for (const matcher of (assert.actions as unknown[] | undefined) ?? []) {
|
|
70
|
+
const m = checkObject(
|
|
71
|
+
matcher,
|
|
72
|
+
'an action matcher in "assert.actions"',
|
|
73
|
+
Object.keys(MATCHER_FIELDS),
|
|
74
|
+
fail,
|
|
24
75
|
);
|
|
76
|
+
checkFields(m, 'an action matcher', MATCHER_FIELDS, fail);
|
|
25
77
|
}
|
|
78
|
+
}
|
|
26
79
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
80
|
+
function checkAction(value: unknown, fail: Fail): void {
|
|
81
|
+
if (typeof value === 'string') return;
|
|
82
|
+
const action = checkObject(value, '"action"', ['id', 'value'], fail);
|
|
83
|
+
if (typeof action.id !== 'string') {
|
|
84
|
+
fail('"action" needs an "id" string (or be an action ID string)');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const STEP_CHECKS: Record<string, (value: unknown, fail: Fail) => void> = {
|
|
89
|
+
action: checkAction,
|
|
90
|
+
assert: checkAssert,
|
|
91
|
+
wait(value, fail) {
|
|
92
|
+
if (typeof value !== 'number' || !(value >= 0) || !isFinite(value)) {
|
|
93
|
+
fail('"wait" must be a number of milliseconds');
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
set(value, fail) {
|
|
97
|
+
if (!isRecord(value)) fail('"set" must be an object');
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
function validateStep(step: unknown, index: number): AutomationStep {
|
|
102
|
+
const fail: Fail = (message) => {
|
|
103
|
+
throw new Error(`Invalid automation step ${index + 1}: ${message}`);
|
|
104
|
+
};
|
|
105
|
+
const fields = Object.keys(STEP_CHECKS);
|
|
106
|
+
if (typeof step !== 'object' || step === null) {
|
|
107
|
+
fail('expected an object');
|
|
108
|
+
}
|
|
109
|
+
const s = checkObject(step, 'a step', fields, fail);
|
|
110
|
+
|
|
111
|
+
if (!fields.some((field) => field in s)) {
|
|
112
|
+
fail(`must have at least one of ${fields.join(', ')}`);
|
|
113
|
+
}
|
|
114
|
+
for (const [field, check] of Object.entries(STEP_CHECKS)) {
|
|
115
|
+
if (field in s) check(s[field], fail);
|
|
31
116
|
}
|
|
32
117
|
|
|
33
118
|
return s as AutomationStep;
|
|
@@ -50,9 +135,15 @@ export function parseAutomationYaml(yamlContent: string): AutomationScript {
|
|
|
50
135
|
|
|
51
136
|
const steps = doc.steps.map((step, i) => validateStep(step, i));
|
|
52
137
|
|
|
138
|
+
if ('start' in doc && typeof doc.start !== 'string') {
|
|
139
|
+
throw new Error(
|
|
140
|
+
'Invalid automation script: "start" must be a passage name',
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
53
144
|
return {
|
|
54
145
|
name: doc.name,
|
|
55
|
-
start:
|
|
146
|
+
start: doc.start as string | undefined,
|
|
56
147
|
steps,
|
|
57
148
|
};
|
|
58
149
|
}
|
package/src/automation/runner.ts
CHANGED
|
@@ -66,7 +66,8 @@ async function executeStep(
|
|
|
66
66
|
const id = typeof step.action === 'string' ? step.action : step.action.id;
|
|
67
67
|
const value =
|
|
68
68
|
typeof step.action === 'string' ? undefined : step.action.value;
|
|
69
|
-
|
|
69
|
+
// An action that finishes later (QuickLoad) is awaited, then the UI
|
|
70
|
+
await story.performAction(id, value);
|
|
70
71
|
await story.waitForActions();
|
|
71
72
|
}
|
|
72
73
|
|
|
@@ -122,20 +123,21 @@ async function executeStep(
|
|
|
122
123
|
}
|
|
123
124
|
}
|
|
124
125
|
|
|
126
|
+
/** The matcher fields compared with the action field of the same name. */
|
|
127
|
+
const MATCHED_FIELDS = ['id', 'type', 'target', 'variable', 'label'] as const;
|
|
128
|
+
|
|
125
129
|
function findMatchingAction(
|
|
126
130
|
actions: StoryAction[],
|
|
127
131
|
matcher: ActionMatcher,
|
|
128
132
|
): StoryAction | undefined {
|
|
129
|
-
return actions.find(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return true;
|
|
140
|
-
});
|
|
133
|
+
return actions.find(
|
|
134
|
+
(action) =>
|
|
135
|
+
MATCHED_FIELDS.every(
|
|
136
|
+
(field) =>
|
|
137
|
+
matcher[field] === undefined || action[field] === matcher[field],
|
|
138
|
+
) &&
|
|
139
|
+
// An action that does not set `disabled` is enabled
|
|
140
|
+
(matcher.disabled === undefined ||
|
|
141
|
+
!!action.disabled === matcher.disabled),
|
|
142
|
+
);
|
|
141
143
|
}
|
package/src/automation/types.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import type { StoryAction } from '../action-registry';
|
|
2
|
+
|
|
3
|
+
export interface ActionMatcher extends Partial<
|
|
4
|
+
Pick<StoryAction, 'id' | 'target' | 'variable' | 'label'>
|
|
5
|
+
> {
|
|
2
6
|
type?: string;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
variable?: string;
|
|
6
|
-
label?: string;
|
|
7
|
+
/** `true` matches only disabled actions, `false` only enabled ones. */
|
|
8
|
+
disabled?: boolean;
|
|
7
9
|
}
|
|
8
10
|
|
|
9
11
|
export interface AssertStep {
|
package/src/class-registry.ts
CHANGED
|
@@ -2,7 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
import { parse, stringify } from 'devalue';
|
|
4
4
|
import { hasOwn } from './utils/namespace';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
extraKeys,
|
|
7
|
+
mapEntries,
|
|
8
|
+
regExpParts,
|
|
9
|
+
setMembers,
|
|
10
|
+
} from './utils/value-kinds';
|
|
6
11
|
|
|
7
12
|
type Constructor = new (...args: any[]) => any;
|
|
8
13
|
|
|
@@ -229,9 +234,7 @@ function classData(value: object): unknown {
|
|
|
229
234
|
return ['Date', Date.prototype.getTime.call(value), extra()];
|
|
230
235
|
}
|
|
231
236
|
if (value instanceof RegExp) {
|
|
232
|
-
|
|
233
|
-
const flags = Reflect.get(RegExp.prototype, 'flags', value) as string;
|
|
234
|
-
return ['RegExp', [source, flags, value.lastIndex], extra()];
|
|
237
|
+
return ['RegExp', [...regExpParts(value), value.lastIndex], extra()];
|
|
235
238
|
}
|
|
236
239
|
return savedData(ownData(value));
|
|
237
240
|
}
|
|
@@ -297,7 +300,7 @@ function reducers(): Record<string, (value: unknown) => unknown> {
|
|
|
297
300
|
};
|
|
298
301
|
out[REGEXP_TAG] = (v) =>
|
|
299
302
|
v instanceof RegExp &&
|
|
300
|
-
v.lastIndex !== 0 && [v
|
|
303
|
+
v.lastIndex !== 0 && [...regExpParts(v), v.lastIndex];
|
|
301
304
|
if (GUARD_V8_KEYS) {
|
|
302
305
|
out[KEYS_TAG] = (v) => {
|
|
303
306
|
return isPlainData(v) && keyEntries(v);
|