@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/src/utils/stable-key.ts
CHANGED
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
isBoxed,
|
|
7
7
|
isTemporal,
|
|
8
8
|
mapEntries,
|
|
9
|
+
regExpText,
|
|
9
10
|
setMembers,
|
|
10
11
|
toStringTag,
|
|
11
12
|
} from './value-kinds';
|
|
@@ -24,7 +25,9 @@ import {
|
|
|
24
25
|
* built-ins (see isAtomic) have their contents in the key: the bytes of a
|
|
25
26
|
* buffer, the place of a view in its buffer, the text of a URL or
|
|
26
27
|
* URLSearchParams, the class, message, cause and errors of an Error, the
|
|
27
|
-
* value of a boxed primitive (#418).
|
|
28
|
+
* value of a boxed primitive (#418). As for deepEqual and a save, -0 is not
|
|
29
|
+
* 0 (#498) and a hole in an array is not an undefined element (#497), so
|
|
30
|
+
* only the keys of -0 and of arrays with holes differ from JSON.stringify.
|
|
28
31
|
*
|
|
29
32
|
* An object met again, through a cycle or a second reference to it, is a
|
|
30
33
|
* marker naming where it was first met rather than another copy of its
|
|
@@ -39,6 +42,9 @@ export function stableKey(value: unknown): string {
|
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
|
|
45
|
+
/** The key of an array slot that holds no element. */
|
|
46
|
+
const HOLE = '<hole>';
|
|
47
|
+
|
|
42
48
|
/**
|
|
43
49
|
* The key of `val`. `seen` numbers the objects keyed so far, in the order
|
|
44
50
|
* they were first met.
|
|
@@ -48,6 +54,8 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
48
54
|
case 'string':
|
|
49
55
|
return JSON.stringify(val);
|
|
50
56
|
case 'number':
|
|
57
|
+
// JSON.stringify and String() read -0 as 0: the key does not (#498)
|
|
58
|
+
if (Object.is(val, -0)) return '-0';
|
|
51
59
|
return Number.isFinite(val) ? JSON.stringify(val) : String(val);
|
|
52
60
|
case 'boolean':
|
|
53
61
|
return String(val);
|
|
@@ -89,16 +97,19 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
89
97
|
return withFields(
|
|
90
98
|
val instanceof Date
|
|
91
99
|
? `Date(${Date.prototype.getTime.call(val)})`
|
|
92
|
-
: `RegExp(${
|
|
100
|
+
: `RegExp(${regExpText(val)}@${val.lastIndex})`,
|
|
93
101
|
);
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
seen.set(obj, seen.size);
|
|
97
105
|
if (Array.isArray(val)) {
|
|
98
|
-
//
|
|
99
|
-
// `[,]` the key of `[]`
|
|
100
|
-
|
|
101
|
-
|
|
106
|
+
// A hole is no element, not an undefined one (#497), as for deepEqual
|
|
107
|
+
// and a save; a plain `map` would skip it, giving `[,]` the key of `[]`
|
|
108
|
+
const items: string[] = [];
|
|
109
|
+
for (let i = 0; i < val.length; i++) {
|
|
110
|
+
items.push(i in val ? keyOf(val[i], seen) : HOLE);
|
|
111
|
+
}
|
|
112
|
+
return withFields(`[${items.join(',')}]`);
|
|
102
113
|
}
|
|
103
114
|
const atomic = atomicKey(obj, seen);
|
|
104
115
|
if (atomic !== undefined) return named(atomic);
|
package/src/utils/value-kinds.ts
CHANGED
|
@@ -50,7 +50,7 @@ export function atomicName(value: object): string | undefined {
|
|
|
50
50
|
export const ERROR_HIDDEN_KEYS = ['message', 'cause', 'errors'] as const;
|
|
51
51
|
|
|
52
52
|
/** Whether `key` is an index of an array element. */
|
|
53
|
-
const isIndexKey = (key: string): boolean =>
|
|
53
|
+
export const isIndexKey = (key: string): boolean =>
|
|
54
54
|
key !== '4294967295' && String(Number(key) >>> 0) === key;
|
|
55
55
|
|
|
56
56
|
/**
|
|
@@ -63,6 +63,43 @@ export function extraKeys(collection: object): string[] {
|
|
|
63
63
|
return Array.isArray(collection) ? keys.filter((k) => !isIndexKey(k)) : keys;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
// A subclass of RegExp may override `source`, `flags` and the getter of each
|
|
67
|
+
// flag (`global`, `sticky`, ...), and the built-in `flags` getter reads those
|
|
68
|
+
// getters, so a pattern is read with the built-in getters one by one (#391,
|
|
69
|
+
// #491). What a RegExp holds is its source and the flags in its slots.
|
|
70
|
+
|
|
71
|
+
/** The built-in getter of each flag, with the letter `flags` lists it by. */
|
|
72
|
+
const FLAG_GETTERS: [string, (this: RegExp) => boolean][] = (
|
|
73
|
+
[
|
|
74
|
+
['hasIndices', 'd'],
|
|
75
|
+
['global', 'g'],
|
|
76
|
+
['ignoreCase', 'i'],
|
|
77
|
+
['multiline', 'm'],
|
|
78
|
+
['dotAll', 's'],
|
|
79
|
+
['unicode', 'u'],
|
|
80
|
+
['unicodeSets', 'v'],
|
|
81
|
+
['sticky', 'y'],
|
|
82
|
+
] as const
|
|
83
|
+
).flatMap(([name, letter]) => {
|
|
84
|
+
const get = Object.getOwnPropertyDescriptor(RegExp.prototype, name)?.get;
|
|
85
|
+
return get ? [[letter, get] as [string, (this: RegExp) => boolean]] : [];
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
/** The pattern of a RegExp and its flags, as its slots hold them. */
|
|
89
|
+
export function regExpParts(re: RegExp): [source: string, flags: string] {
|
|
90
|
+
const source = Reflect.get(RegExp.prototype, 'source', re) as string;
|
|
91
|
+
const flags = FLAG_GETTERS.map(([letter, get]) =>
|
|
92
|
+
get.call(re) ? letter : '',
|
|
93
|
+
).join('');
|
|
94
|
+
return [source, flags];
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** `/source/flags` of a RegExp, as its slots hold them (see regExpParts). */
|
|
98
|
+
export function regExpText(re: RegExp): string {
|
|
99
|
+
const [source, flags] = regExpParts(re);
|
|
100
|
+
return `/${source}/${flags}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
66
103
|
// A subclass of Map or Set may override how it is iterated, read or written
|
|
67
104
|
// (an inventory that iterates only the items on hand). What it holds is in
|
|
68
105
|
// its built-in slots, so structural operations read and write those with the
|
|
@@ -2,21 +2,20 @@ import { registerWidget } from './widget-registry';
|
|
|
2
2
|
import { astContainsChildren } from './ast-scanner';
|
|
3
3
|
import { registerBlockMacro, type ASTNode } from '../markup/ast';
|
|
4
4
|
import { errorMessage } from '../utils/error-message';
|
|
5
|
-
import { parseWidgetDef, type WidgetDef } from './widget-def';
|
|
5
|
+
import { checkWidgetHolds, parseWidgetDef, type WidgetDef } from './widget-def';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Register the widget a definition declares, with its body. Widgets whose
|
|
9
9
|
* body renders {@children} take a closing tag, so they are registered as
|
|
10
10
|
* block macros too: passages parsed later nest their content. A parameter
|
|
11
|
-
* that no namespace can hold throws (see
|
|
12
|
-
* nothing.
|
|
11
|
+
* that no namespace can hold or declares what it holds wrongly throws (see
|
|
12
|
+
* registerWidget and checkWidgetHolds), registering nothing.
|
|
13
13
|
*/
|
|
14
|
-
export function registerWidgetDef(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
): void {
|
|
14
|
+
export function registerWidgetDef(def: WidgetDef, body: ASTNode[]): void {
|
|
15
|
+
checkWidgetHolds(def);
|
|
16
|
+
const { name, params, holds } = def;
|
|
18
17
|
const isBlock = astContainsChildren(body);
|
|
19
|
-
registerWidget(name, body, params, isBlock);
|
|
18
|
+
registerWidget(name, body, params, isBlock, holds);
|
|
20
19
|
if (isBlock) registerBlockMacro(name);
|
|
21
20
|
}
|
|
22
21
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* macro, startup and markup validation (also in tooling) share these.
|
|
4
4
|
*/
|
|
5
5
|
import { parseMacroArgs, type ArgSpans } from '../components/macros/macro-args';
|
|
6
|
-
import type
|
|
6
|
+
import { STRING_HOLDS, type MacroArgs, type StringHolds } from '../registry';
|
|
7
7
|
import { tokenizeMarkupTolerant } from '../markup/parse';
|
|
8
8
|
import type { MacroToken } from '../markup/tokens';
|
|
9
9
|
import type { ParametersOf } from '../code-check';
|
|
@@ -17,25 +17,62 @@ export const WIDGET_PARAMETERS = [
|
|
|
17
17
|
|
|
18
18
|
export interface WidgetDef {
|
|
19
19
|
name: string;
|
|
20
|
+
/** The `@` parameters, without what they declare to hold. */
|
|
20
21
|
params: string[];
|
|
22
|
+
/**
|
|
23
|
+
* What the parameters that declare it hold (`@target:passage`), by name
|
|
24
|
+
* with the `@`: the check of a call reads such an argument as it reads
|
|
25
|
+
* the same `holds` of a macro's `string` parameter. Absent when none does.
|
|
26
|
+
*/
|
|
27
|
+
holds?: Record<string, StringHolds>;
|
|
28
|
+
/** The parameters that declare something no `holds` is (see checkWidgetHolds). */
|
|
29
|
+
badHolds?: { param: string; written: string }[];
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
/**
|
|
24
33
|
* The widget a {widget} definition's arguments declare: its name and its
|
|
25
34
|
* parameters, the words after it that start with `@` (docs/widgets.md),
|
|
26
|
-
* separated by spaces or commas (`@a, @b`).
|
|
27
|
-
*
|
|
35
|
+
* separated by spaces or commas (`@a, @b`). A parameter may say what its
|
|
36
|
+
* argument holds after a colon (`@target:passage`, one of the `holds` of a
|
|
37
|
+
* macro parameter; another word is a `badHolds`). Other words, such as
|
|
38
|
+
* `$name`, are not parameters.
|
|
28
39
|
*/
|
|
29
40
|
export function widgetDef({
|
|
30
41
|
name = '',
|
|
31
42
|
parameters = '',
|
|
32
43
|
}: MacroArgs<typeof WIDGET_PARAMETERS>): WidgetDef {
|
|
44
|
+
const params: string[] = [];
|
|
45
|
+
const holds: Record<string, StringHolds> = {};
|
|
46
|
+
const badHolds: WidgetDef['badHolds'] = [];
|
|
47
|
+
for (const word of parameters.split(/[\s,]+/)) {
|
|
48
|
+
if (!word.startsWith('@')) continue;
|
|
49
|
+
const [param = word, written] = word.split(/:(.*)/s);
|
|
50
|
+
params.push(param);
|
|
51
|
+
if (written === undefined) continue;
|
|
52
|
+
if (STRING_HOLDS.includes(written as StringHolds)) {
|
|
53
|
+
holds[param] = written as StringHolds;
|
|
54
|
+
} else {
|
|
55
|
+
badHolds.push({ param, written });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
33
58
|
return {
|
|
34
59
|
name,
|
|
35
|
-
params
|
|
60
|
+
params,
|
|
61
|
+
...(Object.keys(holds).length > 0 ? { holds } : {}),
|
|
62
|
+
...(badHolds.length > 0 ? { badHolds } : {}),
|
|
36
63
|
};
|
|
37
64
|
}
|
|
38
65
|
|
|
66
|
+
/** Throw if a parameter of `def` declares what it holds wrongly. */
|
|
67
|
+
export function checkWidgetHolds({ name, badHolds = [] }: WidgetDef): void {
|
|
68
|
+
const [bad] = badHolds;
|
|
69
|
+
if (!bad) return;
|
|
70
|
+
throw new Error(
|
|
71
|
+
`spindle: The widget parameter ${bad.param} of {widget "${name}"} holds the unknown ${JSON.stringify(bad.written)}. ` +
|
|
72
|
+
`Write one of ${STRING_HOLDS.join(', ')} after the colon (see docs/widgets.md#what-an-argument-holds).`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
39
76
|
/** Read the arguments of a {widget} definition (see widgetDef). */
|
|
40
77
|
export function parseWidgetDef(rawArgs: string): WidgetDef {
|
|
41
78
|
return widgetDef(parseMacroArgs(rawArgs, WIDGET_PARAMETERS));
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import type { ASTNode } from '../markup/ast';
|
|
2
2
|
import { checkVariableName } from '../utils/namespace';
|
|
3
3
|
import { NameMap } from '../utils/macro-names';
|
|
4
|
+
import type { StringHolds } from '../registry';
|
|
5
|
+
import type { MacroParameters } from '../code-check';
|
|
4
6
|
|
|
5
7
|
interface WidgetEntry {
|
|
6
8
|
body: ASTNode[];
|
|
7
9
|
params: string[];
|
|
10
|
+
/** What the parameters declare to hold, by name with the `@`. */
|
|
11
|
+
holds: Record<string, StringHolds>;
|
|
8
12
|
isBlock: boolean;
|
|
9
13
|
}
|
|
10
14
|
|
|
@@ -20,6 +24,7 @@ export function registerWidget(
|
|
|
20
24
|
bodyAST: ASTNode[],
|
|
21
25
|
params: string[],
|
|
22
26
|
isBlock = false,
|
|
27
|
+
holds: Record<string, StringHolds> = {},
|
|
23
28
|
): void {
|
|
24
29
|
const filteredParams = params.filter((p) => p !== '@children');
|
|
25
30
|
for (const param of filteredParams) {
|
|
@@ -28,6 +33,7 @@ export function registerWidget(
|
|
|
28
33
|
widgets.set(name, {
|
|
29
34
|
body: bodyAST,
|
|
30
35
|
params: filteredParams,
|
|
36
|
+
holds,
|
|
31
37
|
isBlock,
|
|
32
38
|
});
|
|
33
39
|
}
|
|
@@ -36,6 +42,14 @@ export function getWidget(name: string): WidgetEntry | undefined {
|
|
|
36
42
|
return widgets.get(name);
|
|
37
43
|
}
|
|
38
44
|
|
|
45
|
+
/** The widgets as `parameterLookup` reads them (see MacroParameters). */
|
|
46
|
+
export function widgetMacros(): MacroParameters[] {
|
|
47
|
+
return [...widgets].map(([name, { params, holds }]) => ({
|
|
48
|
+
name,
|
|
49
|
+
widget: { params, holds },
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
|
|
39
53
|
export function isBlockWidget(name: string): boolean {
|
|
40
54
|
return widgets.get(name)?.isBlock ?? false;
|
|
41
55
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -215,7 +215,7 @@ export interface StoryAction {
|
|
|
215
215
|
options?: string[];
|
|
216
216
|
value?: unknown;
|
|
217
217
|
disabled?: boolean;
|
|
218
|
-
perform: (value?: unknown) => void
|
|
218
|
+
perform: (value?: unknown) => void | Promise<void>;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
221
|
/**
|
|
@@ -425,7 +425,7 @@ export interface UseActionOptions {
|
|
|
425
425
|
options?: string[];
|
|
426
426
|
value?: unknown;
|
|
427
427
|
disabled?: boolean;
|
|
428
|
-
perform: (value?: unknown) => void
|
|
428
|
+
perform: (value?: unknown) => void | Promise<void>;
|
|
429
429
|
}
|
|
430
430
|
|
|
431
431
|
/**
|
|
@@ -825,8 +825,11 @@ export interface StoryAPI {
|
|
|
825
825
|
/** Return all registered interactive actions. */
|
|
826
826
|
getActions(): StoryAction[];
|
|
827
827
|
|
|
828
|
-
/**
|
|
829
|
-
|
|
828
|
+
/**
|
|
829
|
+
* Perform a registered action by ID. An action that finishes later
|
|
830
|
+
* (`{quicksave}`, `{quickload}`) returns a promise that settles when it has.
|
|
831
|
+
*/
|
|
832
|
+
performAction(id: string, value?: unknown): void | Promise<void>;
|
|
830
833
|
|
|
831
834
|
/** Subscribe to a story event. Returns an unsubscribe function. */
|
|
832
835
|
on<E extends StoryEvent>(
|
|
@@ -834,7 +837,11 @@ export interface StoryAPI {
|
|
|
834
837
|
callback: StoryEventCallback<E>,
|
|
835
838
|
): () => void;
|
|
836
839
|
|
|
837
|
-
/**
|
|
840
|
+
/**
|
|
841
|
+
* Wait for the actions performed with `performAction()` that are still
|
|
842
|
+
* running, then for the next frame's actions to be registered, and return
|
|
843
|
+
* them.
|
|
844
|
+
*/
|
|
838
845
|
waitForActions(): Promise<StoryAction[]>;
|
|
839
846
|
|
|
840
847
|
/** Register a trigger that fires when a condition expression becomes truthy. Returns an unsubscribe function. */
|
package/types/tooling.d.ts
CHANGED
|
@@ -257,8 +257,18 @@ export interface JsLexHandlers {
|
|
|
257
257
|
* comment, or a piece of a template literal.
|
|
258
258
|
*/
|
|
259
259
|
literal?(text: string, index: number, nesting: number): void;
|
|
260
|
-
/**
|
|
261
|
-
|
|
260
|
+
/**
|
|
261
|
+
* A sigil variable reference (`$name`), covering the sigil and name, which
|
|
262
|
+
* is the name JavaScript reads (an escaped spelling is decoded). It is
|
|
263
|
+
* written from `index` up to `end`.
|
|
264
|
+
*/
|
|
265
|
+
variable?(
|
|
266
|
+
sigil: Sigil,
|
|
267
|
+
name: string,
|
|
268
|
+
index: number,
|
|
269
|
+
nesting: number,
|
|
270
|
+
end: number,
|
|
271
|
+
): void;
|
|
262
272
|
}
|
|
263
273
|
|
|
264
274
|
/**
|