@rohal12/spindle 0.62.2 → 0.62.4
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/pkg/format.js +1 -1
- package/dist/pkg/headless.js +464 -272
- package/dist/pkg/play-worker.js +95 -7
- package/dist/pkg/play.js +3 -3
- package/dist/pkg/story-variables.js +590 -386
- package/dist/pkg/tooling.js +1 -0
- package/package.json +1 -1
- package/src/code-check.ts +1 -1
- package/src/completion-context.ts +226 -0
- package/src/components/Passage.tsx +28 -3
- package/src/define-macro.ts +6 -1
- package/src/hooks/use-render-options.ts +4 -0
- package/src/macro-discovery.ts +27 -15
- package/src/markup/code-attributes.ts +10 -3
- package/src/markup/parse.ts +19 -4
- package/src/markup/render.tsx +6 -4
- package/src/markup/spindle.d.peggy.ts +2 -0
- package/src/markup/spindle.peggy +22 -13
- package/src/passage-render-state.ts +7 -0
- package/src/play/session.ts +3 -3
- package/src/play/window-events.ts +63 -0
- package/src/play/worker.ts +62 -9
- package/src/saves/format.ts +4 -0
- package/src/saves/types.ts +6 -6
- package/src/static-literal.ts +24 -22
- package/src/store.ts +42 -16
- package/src/tooling.ts +4 -0
- package/src/triggers.ts +107 -0
- package/src/twine-editor/references.ts +9 -11
- package/src/widgets/ast-scanner.ts +40 -44
- package/src/widgets/widget-def.ts +74 -45
- package/types/play.d.ts +6 -5
- package/types/tooling.d.ts +17 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ASTNode } from '../markup/ast';
|
|
2
2
|
import type { Token } from '../markup/tokens';
|
|
3
3
|
import { codeAndText, parameterLookup, type ParametersOf } from '../code-check';
|
|
4
|
-
import { parseMarkup } from '../markup/parse';
|
|
4
|
+
import { parseMarkup, type MarkupHooks } from '../markup/parse';
|
|
5
5
|
import { getMacroRegistry } from '../registry';
|
|
6
6
|
|
|
7
7
|
/** The declared parameters of the macros registered so far. */
|
|
@@ -15,16 +15,17 @@ export const registeredParameters: ParametersOf = (name) =>
|
|
|
15
15
|
function textContainsChildren(
|
|
16
16
|
text: string,
|
|
17
17
|
parametersOf: ParametersOf,
|
|
18
|
+
hooks?: Partial<MarkupHooks>,
|
|
18
19
|
): boolean {
|
|
19
20
|
if (!text.includes('{')) return false;
|
|
20
21
|
let nodes: ASTNode[];
|
|
21
22
|
try {
|
|
22
|
-
nodes = parseMarkup(text, { text: true });
|
|
23
|
+
nodes = parseMarkup(text, { text: true, hooks });
|
|
23
24
|
} catch {
|
|
24
25
|
// The renderer reports the markup that does not parse
|
|
25
26
|
return false;
|
|
26
27
|
}
|
|
27
|
-
return astContainsChildren(nodes, parametersOf);
|
|
28
|
+
return astContainsChildren(nodes, parametersOf, hooks);
|
|
28
29
|
}
|
|
29
30
|
|
|
30
31
|
/**
|
|
@@ -32,17 +33,19 @@ function textContainsChildren(
|
|
|
32
33
|
* argument that holds markup (`<span title="{@children}">`,
|
|
33
34
|
* `{button "{@children}"}`). Literal arguments and code are not markup.
|
|
34
35
|
* `src` is the text a flat token's offsets index into (its attribute values
|
|
35
|
-
* are read from it); a token made from an AST has none.
|
|
36
|
+
* are read from it); a token made from an AST has none. Discovery hooks
|
|
37
|
+
* also apply inside these texts, before widgets have been registered.
|
|
36
38
|
*/
|
|
37
39
|
export function tokenTextContainsChildren(
|
|
38
40
|
token: Token,
|
|
39
41
|
parametersOf: ParametersOf = registeredParameters,
|
|
40
42
|
src = '',
|
|
43
|
+
hooks?: Partial<MarkupHooks>,
|
|
41
44
|
): boolean {
|
|
42
45
|
for (const piece of codeAndText(src, [token], parametersOf)) {
|
|
43
46
|
if (
|
|
44
47
|
piece.kind === 'text' &&
|
|
45
|
-
textContainsChildren(piece.text, parametersOf)
|
|
48
|
+
textContainsChildren(piece.text, parametersOf, hooks)
|
|
46
49
|
) {
|
|
47
50
|
return true;
|
|
48
51
|
}
|
|
@@ -60,6 +63,7 @@ export function tokenTextContainsChildren(
|
|
|
60
63
|
export function astContainsChildren(
|
|
61
64
|
nodes: ASTNode[],
|
|
62
65
|
parametersOf: ParametersOf = registeredParameters,
|
|
66
|
+
hooks?: Partial<MarkupHooks>,
|
|
63
67
|
): boolean {
|
|
64
68
|
for (const node of nodes) {
|
|
65
69
|
if (
|
|
@@ -69,46 +73,38 @@ export function astContainsChildren(
|
|
|
69
73
|
) {
|
|
70
74
|
return true;
|
|
71
75
|
}
|
|
72
|
-
if (node.type
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
76
|
+
if (node.type !== 'html' && node.type !== 'macro') continue;
|
|
77
|
+
if (node.type === 'macro' && node.name.toLowerCase() === 'widget') continue;
|
|
78
|
+
// A token in name only: the scan reads its label or attribute values.
|
|
79
|
+
const span = { isClose: false, start: 0, end: 0 };
|
|
80
|
+
const tag: Token =
|
|
81
|
+
node.type === 'html'
|
|
82
|
+
? {
|
|
83
|
+
...node,
|
|
84
|
+
...span,
|
|
85
|
+
isSelfClose: false,
|
|
86
|
+
tagNameStart: 0,
|
|
87
|
+
tagNameEnd: 0,
|
|
88
|
+
attributeSpans: [],
|
|
89
|
+
}
|
|
90
|
+
: {
|
|
91
|
+
...node,
|
|
92
|
+
...span,
|
|
93
|
+
nameStart: 0,
|
|
94
|
+
nameEnd: 0,
|
|
95
|
+
argsStart: 0,
|
|
96
|
+
argsEnd: 0,
|
|
97
|
+
};
|
|
98
|
+
if (
|
|
99
|
+
tokenTextContainsChildren(tag, parametersOf, '', hooks) ||
|
|
100
|
+
astContainsChildren(node.children, parametersOf, hooks)
|
|
101
|
+
) {
|
|
102
|
+
return true;
|
|
90
103
|
}
|
|
91
|
-
if (node.type === 'macro') {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
nameStart: 0,
|
|
96
|
-
nameEnd: 0,
|
|
97
|
-
argsStart: 0,
|
|
98
|
-
argsEnd: 0,
|
|
99
|
-
...node,
|
|
100
|
-
start: 0,
|
|
101
|
-
end: 0,
|
|
102
|
-
};
|
|
103
|
-
if (
|
|
104
|
-
tokenTextContainsChildren(tag, parametersOf) ||
|
|
105
|
-
astContainsChildren(node.children, parametersOf)
|
|
106
|
-
) {
|
|
107
|
-
return true;
|
|
108
|
-
}
|
|
109
|
-
if (node.branches) {
|
|
110
|
-
for (const branch of node.branches) {
|
|
111
|
-
if (astContainsChildren(branch.children, parametersOf)) return true;
|
|
104
|
+
if (node.type === 'macro' && node.branches) {
|
|
105
|
+
for (const branch of node.branches) {
|
|
106
|
+
if (astContainsChildren(branch.children, parametersOf, hooks)) {
|
|
107
|
+
return true;
|
|
112
108
|
}
|
|
113
109
|
}
|
|
114
110
|
}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
import { parseMacroArgs, type ArgSpans } from '../components/macros/macro-args';
|
|
6
6
|
import { STRING_HOLDS, type MacroArgs, type StringHolds } from '../registry';
|
|
7
7
|
import { tokenizeMarkupTolerant } from '../markup/parse';
|
|
8
|
+
import { isRawMacro } from '../markup/ast';
|
|
8
9
|
import type { MacroToken } from '../markup/tokens';
|
|
9
10
|
import type { ParametersOf } from '../code-check';
|
|
10
11
|
import { NameMap } from '../utils/macro-names';
|
|
@@ -131,55 +132,83 @@ export function widgetDefinitions(
|
|
|
131
132
|
passages: Iterable<WidgetPassage>,
|
|
132
133
|
parametersOf: ParametersOf = registeredParameters,
|
|
133
134
|
): WidgetDefinition[] {
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
135
|
+
const list = [...passages];
|
|
136
|
+
const scan = (replacesDo: boolean): WidgetDefinition[] => {
|
|
137
|
+
const found: WidgetDefinition[] = [];
|
|
138
|
+
const hooks = {
|
|
139
|
+
isRaw: (name: string) =>
|
|
140
|
+
isRawMacro(
|
|
141
|
+
name,
|
|
142
|
+
replacesDo || parametersOf.widget?.(name) !== undefined,
|
|
143
|
+
),
|
|
144
|
+
};
|
|
145
|
+
for (const passage of list) {
|
|
146
|
+
if (passage.name !== 'StoryInit' && !passage.tags?.includes('widget')) {
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
// The open definitions: their tags, and whether a slot was seen
|
|
150
|
+
const open: {
|
|
151
|
+
token: MacroToken;
|
|
152
|
+
isBlock: boolean;
|
|
153
|
+
closeStart?: number;
|
|
154
|
+
}[] = [];
|
|
155
|
+
const done: typeof open = [];
|
|
156
|
+
for (const token of tokenizeMarkupTolerant(passage.content, { hooks })
|
|
157
|
+
.tokens) {
|
|
158
|
+
if (token.type === 'macro' && token.name.toLowerCase() === 'widget') {
|
|
159
|
+
if (!token.isClose) {
|
|
160
|
+
open.push({ token, isBlock: false });
|
|
161
|
+
} else {
|
|
162
|
+
const def = open.pop();
|
|
163
|
+
if (def) done.push({ ...def, closeStart: token.start });
|
|
164
|
+
}
|
|
165
|
+
} else if (
|
|
166
|
+
open.length > 0 &&
|
|
167
|
+
((token.type === 'variable' &&
|
|
168
|
+
token.scope === 'local' &&
|
|
169
|
+
token.name === 'children') ||
|
|
170
|
+
tokenTextContainsChildren(
|
|
171
|
+
token,
|
|
172
|
+
parametersOf,
|
|
173
|
+
passage.content,
|
|
174
|
+
hooks,
|
|
175
|
+
))
|
|
176
|
+
) {
|
|
177
|
+
open[open.length - 1]!.isBlock = true;
|
|
150
178
|
}
|
|
151
|
-
} else if (
|
|
152
|
-
open.length > 0 &&
|
|
153
|
-
((token.type === 'variable' &&
|
|
154
|
-
token.scope === 'local' &&
|
|
155
|
-
token.name === 'children') ||
|
|
156
|
-
tokenTextContainsChildren(token, parametersOf, passage.content))
|
|
157
|
-
) {
|
|
158
|
-
open[open.length - 1]!.isBlock = true;
|
|
159
179
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
180
|
+
done.push(...open);
|
|
181
|
+
const defs: WidgetDefinition[] = [];
|
|
182
|
+
for (const { token, isBlock, closeStart } of done) {
|
|
183
|
+
try {
|
|
184
|
+
const [from, to] = nameSpan(token.rawArgs);
|
|
185
|
+
defs.push({
|
|
186
|
+
...parseWidgetDef(token.rawArgs),
|
|
187
|
+
block: isBlock,
|
|
188
|
+
passage: passage.name,
|
|
189
|
+
start: token.start,
|
|
190
|
+
end: token.end,
|
|
191
|
+
nameStart: token.argsStart + from,
|
|
192
|
+
nameEnd: token.argsStart + to,
|
|
193
|
+
...(closeStart === undefined ? {} : { closeStart }),
|
|
194
|
+
});
|
|
195
|
+
} catch {
|
|
196
|
+
// The {widget} macro reports a definition it cannot read
|
|
197
|
+
}
|
|
178
198
|
}
|
|
199
|
+
found.push(...defs.sort((a, b) => a.start - b.start));
|
|
179
200
|
}
|
|
180
|
-
found
|
|
181
|
-
}
|
|
182
|
-
|
|
201
|
+
return found;
|
|
202
|
+
};
|
|
203
|
+
const initial = scan(false);
|
|
204
|
+
// Discover definitions with real raw bodies first, so JavaScript strings
|
|
205
|
+
// that merely mention a widget cannot replace the built-in do. Once a
|
|
206
|
+
// closed do widget is known, its body and its callers are markup.
|
|
207
|
+
return effectiveWidgetDefinitions(initial).some(
|
|
208
|
+
(def) => def.name.toLowerCase() === 'do',
|
|
209
|
+
)
|
|
210
|
+
? scan(true)
|
|
211
|
+
: initial;
|
|
183
212
|
}
|
|
184
213
|
|
|
185
214
|
/**
|
package/types/play.d.ts
CHANGED
|
@@ -73,11 +73,12 @@ export interface PlaySession {
|
|
|
73
73
|
* Node APIs, files and environment. Worker threads and happy-dom are not a
|
|
74
74
|
* security sandbox. Only run code you trust.
|
|
75
75
|
*
|
|
76
|
-
* In the worker, `window` is happy-dom's
|
|
77
|
-
*
|
|
78
|
-
* `
|
|
79
|
-
* not share a `BroadcastChannel`, `localStorage` or `navigator`,
|
|
80
|
-
* hear each other. The story's `console.log` goes to stderr,
|
|
76
|
+
* In the worker, `window` is `globalThis`, with happy-dom's DOM globals.
|
|
77
|
+
* Properties set on `window` are also bare globals, as in a browser.
|
|
78
|
+
* `requestIdleCallback` and `cancelIdleCallback` use a timer-backed scheduler.
|
|
79
|
+
* Sessions do not share a `BroadcastChannel`, `localStorage` or `navigator`,
|
|
80
|
+
* so they never hear each other. The story's `console.log` goes to stderr,
|
|
81
|
+
* not stdout.
|
|
81
82
|
*
|
|
82
83
|
* A session left open does not keep Node running (a call in progress does).
|
|
83
84
|
*/
|
package/types/tooling.d.ts
CHANGED
|
@@ -1241,3 +1241,20 @@ export declare function macroArgumentSpans(
|
|
|
1241
1241
|
rawArgs: string,
|
|
1242
1242
|
parameters: readonly ParameterDef[],
|
|
1243
1243
|
): MacroArgumentSpan[];
|
|
1244
|
+
|
|
1245
|
+
/** Executable code at a cursor, or prose/literal text that suppresses completion. */
|
|
1246
|
+
export type CompletionContext =
|
|
1247
|
+
| { kind: 'literal' }
|
|
1248
|
+
| (PieceBase & { kind: 'code'; code: string; goal: JsGoal; cursor: number });
|
|
1249
|
+
|
|
1250
|
+
/** Inspect only the prefix before cursor (UTF-16), accepting unfinished markup.
|
|
1251
|
+
* Strings, comments, regexes and template text are literal; template
|
|
1252
|
+
* interpolations are code. Code offsets map to source with pieceOffset.
|
|
1253
|
+
* Uses macro parameter metadata for nested labels, attributes and held code.
|
|
1254
|
+
* Throws RangeError for an invalid cursor; never executes story code.
|
|
1255
|
+
*/
|
|
1256
|
+
export declare function completionContext(
|
|
1257
|
+
source: string,
|
|
1258
|
+
cursor: number,
|
|
1259
|
+
macros?: Iterable<ToolingMacro>,
|
|
1260
|
+
): CompletionContext;
|