@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
|
@@ -5,7 +5,6 @@ import { evaluate } from '../../expression';
|
|
|
5
5
|
import type { ASTNode } from '../../markup/ast';
|
|
6
6
|
import { splitArgs } from './arg-utils';
|
|
7
7
|
import { createNamespace } from '../../utils/namespace';
|
|
8
|
-
import { useRenderOptions } from '../../hooks/use-render-options';
|
|
9
8
|
import { LocalsScope } from './locals-scope';
|
|
10
9
|
import { useInterpolate } from '../../hooks/use-interpolate';
|
|
11
10
|
import { wrapContent } from './display';
|
|
@@ -40,8 +39,6 @@ function WidgetContent({
|
|
|
40
39
|
rawArgs,
|
|
41
40
|
invocationChildren,
|
|
42
41
|
}: WidgetInvocationProps) {
|
|
43
|
-
const renderOptions = useRenderOptions();
|
|
44
|
-
const parentValues = renderOptions.locals;
|
|
45
42
|
const [mergedVars, mergedTemps, mergedLocals, mergedTrans] =
|
|
46
43
|
useMergedLocals();
|
|
47
44
|
|
|
@@ -76,6 +73,9 @@ function WidgetContent({
|
|
|
76
73
|
values.push(value);
|
|
77
74
|
}
|
|
78
75
|
|
|
76
|
+
// The names too: the widget may be redefined under a mounted invocation,
|
|
77
|
+
// with other parameters for the same argument values (#511)
|
|
78
|
+
const paramsKey = params.join(' ');
|
|
79
79
|
const ownKeys = useMemo(() => {
|
|
80
80
|
const keys = createNamespace();
|
|
81
81
|
for (let i = 0; i < params.length; i++) {
|
|
@@ -83,15 +83,14 @@ function WidgetContent({
|
|
|
83
83
|
values[i];
|
|
84
84
|
}
|
|
85
85
|
return keys;
|
|
86
|
-
//
|
|
86
|
+
// paramsKey stands for params; values tracks evaluated args
|
|
87
87
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
88
|
-
}, values);
|
|
88
|
+
}, [paramsKey, ...values]);
|
|
89
89
|
|
|
90
90
|
return (
|
|
91
91
|
<WidgetChildrenContext.Provider value={childrenValue}>
|
|
92
92
|
<LocalsScope
|
|
93
93
|
body={body}
|
|
94
|
-
parentValues={parentValues}
|
|
95
94
|
ownKeys={ownKeys}
|
|
96
95
|
/>
|
|
97
96
|
</WidgetChildrenContext.Provider>
|
|
@@ -133,23 +133,38 @@ export function topLevelIndices(
|
|
|
133
133
|
return indices;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
/** The start and end of a run of text in a larger one. */
|
|
137
|
+
export type Span = [start: number, end: number];
|
|
138
|
+
|
|
136
139
|
/**
|
|
137
|
-
*
|
|
138
|
-
*
|
|
140
|
+
* The segments of `src` between its depth-0 separators (see
|
|
141
|
+
* `topLevelIndices`), as spans: untrimmed, and empty segments are kept, so
|
|
139
142
|
* `n` separators always give `n + 1` segments.
|
|
140
143
|
*/
|
|
141
|
-
export function
|
|
144
|
+
export function topLevelSpans(
|
|
142
145
|
src: string,
|
|
143
146
|
isSeparator: (ch: string) => boolean,
|
|
144
|
-
):
|
|
145
|
-
const
|
|
147
|
+
): Span[] {
|
|
148
|
+
const spans: Span[] = [];
|
|
146
149
|
let from = 0;
|
|
147
150
|
for (const i of topLevelIndices(src, isSeparator)) {
|
|
148
|
-
|
|
151
|
+
spans.push([from, i]);
|
|
149
152
|
from = i + 1;
|
|
150
153
|
}
|
|
151
|
-
|
|
152
|
-
return
|
|
154
|
+
spans.push([from, src.length]);
|
|
155
|
+
return spans;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Split `src` at every depth-0 separator (see `topLevelIndices`). Segments
|
|
160
|
+
* are returned as written, untrimmed, and empty segments are kept, so
|
|
161
|
+
* `n` separators always give `n + 1` segments.
|
|
162
|
+
*/
|
|
163
|
+
export function splitTopLevel(
|
|
164
|
+
src: string,
|
|
165
|
+
isSeparator: (ch: string) => boolean,
|
|
166
|
+
): string[] {
|
|
167
|
+
return topLevelSpans(src, isSeparator).map(([a, b]) => src.slice(a, b));
|
|
153
168
|
}
|
|
154
169
|
|
|
155
170
|
/**
|
|
@@ -189,38 +204,60 @@ function startsLikeValue(token: string): boolean {
|
|
|
189
204
|
* Try to split a raw string on whitespace at depth 0 (respecting strings,
|
|
190
205
|
* template literals, parentheses, brackets, and braces). Each resulting token
|
|
191
206
|
* must pass `isStandaloneValue()` or the split is rejected and `null` is
|
|
192
|
-
* returned.
|
|
207
|
+
* returned. The spans are those of the tokens in `raw`.
|
|
193
208
|
*/
|
|
194
|
-
function trySplitOnWhitespace(raw: string):
|
|
195
|
-
const args =
|
|
209
|
+
function trySplitOnWhitespace(raw: string): Span[] | null {
|
|
210
|
+
const args = topLevelSpans(raw, isWhitespace).filter(([a, b]) => b > a);
|
|
196
211
|
|
|
197
212
|
// Need 2+ tokens
|
|
198
213
|
if (args.length < 2) return null;
|
|
199
214
|
|
|
200
215
|
// Every token must be a standalone value (not an operator)
|
|
201
|
-
for (const
|
|
202
|
-
if (!isStandaloneValue(
|
|
216
|
+
for (const [a, b] of args) {
|
|
217
|
+
if (!isStandaloneValue(raw.slice(a, b))) return null;
|
|
203
218
|
}
|
|
204
219
|
|
|
205
220
|
return args;
|
|
206
221
|
}
|
|
207
222
|
|
|
223
|
+
/** `span` less the whitespace at its ends. */
|
|
224
|
+
function trimmed(src: string, [start, end]: Span): Span {
|
|
225
|
+
const text = src.slice(start, end);
|
|
226
|
+
const lead = text.length - text.trimStart().length;
|
|
227
|
+
return [
|
|
228
|
+
start + lead,
|
|
229
|
+
Math.max(start + lead, end - (text.length - text.trimEnd().length)),
|
|
230
|
+
];
|
|
231
|
+
}
|
|
232
|
+
|
|
208
233
|
/**
|
|
209
|
-
*
|
|
210
|
-
*
|
|
211
|
-
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
234
|
+
* Where the arguments in `raw` are written (see `splitArgs`), as spans of
|
|
235
|
+
* `raw`.
|
|
212
236
|
*/
|
|
213
|
-
export function
|
|
214
|
-
const args =
|
|
237
|
+
export function splitArgSpans(raw: string): Span[] {
|
|
238
|
+
const args = topLevelSpans(raw, (ch) => ch === ',').map((span) =>
|
|
239
|
+
trimmed(raw, span),
|
|
240
|
+
);
|
|
215
241
|
const hasComma = args.length > 1;
|
|
216
|
-
|
|
242
|
+
const last = args[args.length - 1]!;
|
|
243
|
+
if (last[0] === last[1]) args.pop();
|
|
217
244
|
|
|
218
245
|
// If no commas were found and we got a single expression, try splitting
|
|
219
246
|
// on whitespace at depth 0 (e.g. "Label" "target", $var "text", $x $y).
|
|
220
247
|
if (!hasComma && args.length === 1) {
|
|
221
|
-
const
|
|
222
|
-
|
|
248
|
+
const [start, end] = args[0]!;
|
|
249
|
+
const split = trySplitOnWhitespace(raw.slice(start, end));
|
|
250
|
+
if (split) return split.map(([a, b]) => [start + a, start + b]);
|
|
223
251
|
}
|
|
224
252
|
|
|
225
253
|
return args;
|
|
226
254
|
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Split rawArgs by commas, respecting parentheses, brackets, braces, and
|
|
258
|
+
* strings. When no top-level commas are present, also supports adjacent quoted
|
|
259
|
+
* string literals separated by whitespace (e.g. `"Label" "target"`).
|
|
260
|
+
*/
|
|
261
|
+
export function splitArgs(raw: string): string[] {
|
|
262
|
+
return splitArgSpans(raw).map(([a, b]) => raw.slice(a, b));
|
|
263
|
+
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
useState,
|
|
3
|
+
useCallback,
|
|
4
|
+
useContext,
|
|
5
|
+
useRef,
|
|
6
|
+
useMemo,
|
|
7
|
+
} from 'preact/hooks';
|
|
2
8
|
import {
|
|
3
9
|
LocalsValuesContext,
|
|
4
10
|
LocalsUpdateContext,
|
|
@@ -13,17 +19,15 @@ import {
|
|
|
13
19
|
} from '../../utils/namespace';
|
|
14
20
|
|
|
15
21
|
/**
|
|
16
|
-
* Render `body` in a new locals scope: the locals of
|
|
17
|
-
* `ownKeys` (an iteration's loop variables, a widget's parameters) on
|
|
18
|
-
* Locals the body assigns are kept in this scope.
|
|
22
|
+
* Render `body` in a new locals scope: the locals of the enclosing scope,
|
|
23
|
+
* with `ownKeys` (an iteration's loop variables, a widget's parameters) on
|
|
24
|
+
* top. Locals the body assigns are kept in this scope.
|
|
19
25
|
*/
|
|
20
26
|
export function LocalsScope({
|
|
21
27
|
body,
|
|
22
|
-
parentValues,
|
|
23
28
|
ownKeys,
|
|
24
29
|
}: {
|
|
25
30
|
body: ASTNode[];
|
|
26
|
-
parentValues: Record<string, unknown>;
|
|
27
31
|
ownKeys: Record<string, unknown>;
|
|
28
32
|
}) {
|
|
29
33
|
const options = useRenderOptions();
|
|
@@ -31,9 +35,16 @@ export function LocalsScope({
|
|
|
31
35
|
{},
|
|
32
36
|
);
|
|
33
37
|
|
|
38
|
+
// The enclosing scope as it is now: a local assigned before this scope
|
|
39
|
+
// renders is in it, while the context value only gains it in the re-render
|
|
40
|
+
// that follows (and the body may have acted on its absence by then).
|
|
41
|
+
const enclosing = useContext(LocalsUpdateContext).getValues();
|
|
42
|
+
const parentValues = useContext(LocalsValuesContext);
|
|
34
43
|
const localState = useMemo(
|
|
35
|
-
() => createNamespace(
|
|
36
|
-
|
|
44
|
+
() => createNamespace(enclosing, ownKeys, localMutations),
|
|
45
|
+
// `parentValues` renews the scope when the enclosing one re-renders
|
|
46
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
47
|
+
[enclosing, parentValues, ownKeys, localMutations],
|
|
37
48
|
);
|
|
38
49
|
|
|
39
50
|
const valuesRef = useRef(localState);
|
|
@@ -182,10 +182,16 @@ const LEADING_OPERATOR_RE = /^(?:[-+*/&|^=<>?:,.]|%(?![A-Za-z_]))/;
|
|
|
182
182
|
* Where the rest of `src` is without the flag `word` as its last or first
|
|
183
183
|
* term, or `null` if it has no such flag. Next to a binary operator the
|
|
184
184
|
* word is an operand (`"a" + inline`), not a flag; the closing `/` of a
|
|
185
|
-
* regex literal is not one.
|
|
185
|
+
* regex literal is not one. A word with nothing else beside it is the flag
|
|
186
|
+
* only when `alone` says no argument is waiting for it (#499).
|
|
186
187
|
*/
|
|
187
|
-
function takeFlag(src: string, word: string): Span | null {
|
|
188
|
+
function takeFlag(src: string, word: string, alone: boolean): Span | null {
|
|
188
189
|
const spans = terms(src);
|
|
190
|
+
if (spans.length === 1) {
|
|
191
|
+
return alone && slice(src, spans[0]!) === word
|
|
192
|
+
? [src.length, src.length]
|
|
193
|
+
: null;
|
|
194
|
+
}
|
|
189
195
|
if (spans.length < 2) return null;
|
|
190
196
|
if (slice(src, spans[spans.length - 1]!) === word) {
|
|
191
197
|
const rest: Span = [0, spans[spans.length - 2]![1]];
|
|
@@ -249,8 +255,10 @@ function readGroup(
|
|
|
249
255
|
{ at: srcAt, spans }: Place,
|
|
250
256
|
): void {
|
|
251
257
|
let [text, at] = trimmed(src ?? '', srcAt);
|
|
258
|
+
// A lone word is the passage of {include inline}, not its flag
|
|
259
|
+
const alone = !params.some((p) => p.type !== 'flag' && p.required);
|
|
252
260
|
for (const flag of params.filter((p) => p.type === 'flag')) {
|
|
253
|
-
const rest = src === null ? null : takeFlag(text, flag.name);
|
|
261
|
+
const rest = src === null ? null : takeFlag(text, flag.name, alone);
|
|
254
262
|
args[flag.name] = rest !== null;
|
|
255
263
|
if (rest) [text, at] = trimmed(slice(text, rest), at + rest[0]);
|
|
256
264
|
}
|
package/src/expression.ts
CHANGED
|
@@ -2,7 +2,13 @@ import type { StoryState } from './store';
|
|
|
2
2
|
import { useStoryStore } from './store';
|
|
3
3
|
import type { Passage } from './parser';
|
|
4
4
|
import { random, randomInt } from './prng';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
BINDINGS,
|
|
7
|
+
FNS_BINDING,
|
|
8
|
+
clearTransformCache,
|
|
9
|
+
transform,
|
|
10
|
+
} from './transform';
|
|
11
|
+
import type { JsGoal } from './js-lexer';
|
|
6
12
|
import {
|
|
7
13
|
EMPTY_NAMESPACE,
|
|
8
14
|
asNamespace,
|
|
@@ -40,10 +46,31 @@ export { transform };
|
|
|
40
46
|
const FN_CACHE_MAX = 500;
|
|
41
47
|
const fnCache = new Map<string, CompiledExpression>();
|
|
42
48
|
|
|
43
|
-
const preamble =
|
|
44
|
-
'const {currentPassage,previousPassage,visited,hasVisited,hasVisitedAny,hasVisitedAll,rendered,hasRendered,hasRenderedAny,hasRenderedAll,random,randomInt}=__fns;';
|
|
49
|
+
const preamble = `const {currentPassage,previousPassage,visited,hasVisited,hasVisitedAny,hasVisitedAll,rendered,hasRendered,hasRenderedAny,hasRenderedAll,random,randomInt}=${FNS_BINDING};`;
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Compile `source`: the function takes the namespaces and the function
|
|
53
|
+
* table under the names the transform uses for the references (see
|
|
54
|
+
* BINDINGS). The statements are a block of their own, so that they may
|
|
55
|
+
* declare any name, those of the history functions the preamble declares
|
|
56
|
+
* included.
|
|
57
|
+
*/
|
|
58
|
+
function compile(source: string, goal: JsGoal): CompiledExpression {
|
|
59
|
+
const code = transform(source, goal);
|
|
60
|
+
// The line break keeps a trailing `// comment` from swallowing the closer.
|
|
61
|
+
const body = goal === 'statements' ? `{${code}\n}` : `return (${code}\n);`;
|
|
62
|
+
return new Function(
|
|
63
|
+
BINDINGS.$,
|
|
64
|
+
BINDINGS._,
|
|
65
|
+
BINDINGS['@'],
|
|
66
|
+
FNS_BINDING,
|
|
67
|
+
BINDINGS['%'],
|
|
68
|
+
preamble + body,
|
|
69
|
+
) as CompiledExpression;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function getOrCompile(source: string, goal: JsGoal): CompiledExpression {
|
|
73
|
+
const key = (goal === 'statements' ? 's' : 'e') + source;
|
|
47
74
|
const cached = fnCache.get(key);
|
|
48
75
|
if (cached) {
|
|
49
76
|
// Move to end for LRU ordering (Map preserves insertion order)
|
|
@@ -51,14 +78,7 @@ function getOrCompile(key: string, body: string): CompiledExpression {
|
|
|
51
78
|
fnCache.set(key, cached);
|
|
52
79
|
return cached;
|
|
53
80
|
}
|
|
54
|
-
const fn =
|
|
55
|
-
'variables',
|
|
56
|
-
'temporary',
|
|
57
|
-
'locals',
|
|
58
|
-
'__fns',
|
|
59
|
-
'transient',
|
|
60
|
-
preamble + body,
|
|
61
|
-
) as CompiledExpression;
|
|
81
|
+
const fn = compile(source, goal);
|
|
62
82
|
fnCache.set(key, fn);
|
|
63
83
|
if (fnCache.size > FN_CACHE_MAX) {
|
|
64
84
|
// Evict oldest entry
|
|
@@ -188,10 +208,7 @@ function run(
|
|
|
188
208
|
* e.g. evaluate("$health + 10", variables, temporary) → number
|
|
189
209
|
*/
|
|
190
210
|
export function evaluate(expr: string, ...namespaces: Namespaces): unknown {
|
|
191
|
-
|
|
192
|
-
// The line break keeps a trailing `// comment` from swallowing the `)`.
|
|
193
|
-
const body = `return (${transformed}\n);`;
|
|
194
|
-
return run(getOrCompile(body, body), ...namespaces);
|
|
211
|
+
return run(getOrCompile(expr, 'expression'), ...namespaces);
|
|
195
212
|
}
|
|
196
213
|
|
|
197
214
|
/**
|
|
@@ -199,8 +216,7 @@ export function evaluate(expr: string, ...namespaces: Namespaces): unknown {
|
|
|
199
216
|
* e.g. execute("$health = 100; $name = 'Hero'", variables, temporary)
|
|
200
217
|
*/
|
|
201
218
|
export function execute(code: string, ...namespaces: Namespaces): void {
|
|
202
|
-
|
|
203
|
-
run(getOrCompile('exec:' + transformed, transformed), ...namespaces);
|
|
219
|
+
run(getOrCompile(code, 'statements'), ...namespaces);
|
|
204
220
|
}
|
|
205
221
|
|
|
206
222
|
/**
|
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { useContext, useMemo } from 'preact/hooks';
|
|
2
2
|
import { useStoryFields } from './use-story-fields';
|
|
3
|
-
import { LocalsValuesContext } from '../markup/render';
|
|
3
|
+
import { LocalsUpdateContext, LocalsValuesContext } from '../markup/render';
|
|
4
|
+
import { liveLocalsView } from '../utils/live-locals';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The locals in scope, read as they are when code looks (a local the
|
|
8
|
+
* preceding {set} assigned in this pass included, which the context value
|
|
9
|
+
* only gains in the re-render that follows). The returned view is renewed
|
|
10
|
+
* whenever the context value changes, so what depends on it re-renders and
|
|
11
|
+
* recomputes as before.
|
|
12
|
+
*/
|
|
13
|
+
export function useLiveLocals(): Record<string, unknown> {
|
|
14
|
+
const values = useContext(LocalsValuesContext);
|
|
15
|
+
const { getValues } = useContext(LocalsUpdateContext);
|
|
16
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `values` renews the view
|
|
17
|
+
return useMemo(() => liveLocalsView(getValues), [getValues, values]);
|
|
18
|
+
}
|
|
4
19
|
|
|
5
20
|
/**
|
|
6
21
|
* Return store variables, temporary, locals from context, and transient.
|
|
@@ -20,7 +35,7 @@ export function useMergedLocals(): readonly [
|
|
|
20
35
|
'transient',
|
|
21
36
|
'renderCounts',
|
|
22
37
|
);
|
|
23
|
-
const localsValues =
|
|
38
|
+
const localsValues = useLiveLocals();
|
|
24
39
|
|
|
25
40
|
return useMemo(() => {
|
|
26
41
|
return [variables, temporary, localsValues, transient] as const;
|