@rohal12/spindle 0.62.4 → 0.62.6
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 +127 -45
- package/dist/pkg/story-variables.js +207 -86
- package/package.json +1 -1
- package/src/code-check.ts +43 -5
- package/src/completion-context.ts +58 -13
- package/src/components/macros/PassageDisplay.tsx +3 -16
- package/src/components/macros/detached-body.tsx +7 -0
- package/src/format/format-passage.ts +3 -3
- package/src/hooks/use-modal-focus.ts +1 -11
- package/src/interpolation.ts +3 -3
- package/src/js-bindings.ts +7 -4
- package/src/js-lexer.ts +5 -1
- package/src/markup/code-attributes.ts +3 -4
- package/src/markup/code-end.ts +68 -1
- package/src/markup/pair.ts +14 -2
- package/src/markup/parse.ts +22 -12
- package/src/markup/render.tsx +3 -3
- package/src/registry.ts +3 -0
- package/src/saves/save-manager.ts +14 -3
- package/src/store.ts +30 -16
- package/src/story-variables.ts +2 -4
- package/src/tooling.ts +57 -9
- package/src/utils/element-visibility.ts +22 -9
- package/src/widgets/ast-scanner.ts +2 -1
- package/types/tooling.d.ts +18 -1
package/src/registry.ts
CHANGED
|
@@ -276,6 +276,9 @@ export function getMacroParameters(
|
|
|
276
276
|
return metadataRegistry.get(name)?.parameters;
|
|
277
277
|
}
|
|
278
278
|
|
|
279
|
+
getMacroParameters.source = (name: string) =>
|
|
280
|
+
metadataRegistry.get(name)?.source;
|
|
281
|
+
|
|
279
282
|
export function getMacroRegistry(): MacroMetadata[] {
|
|
280
283
|
return Array.from(metadataRegistry.values());
|
|
281
284
|
}
|
|
@@ -318,6 +318,12 @@ export function establishPlaythrough(
|
|
|
318
318
|
|
|
319
319
|
// --- Save Hooks ---
|
|
320
320
|
|
|
321
|
+
/** A synchronous hook capture; dispose also runs when a hook throws. */
|
|
322
|
+
export interface SaveCapture {
|
|
323
|
+
(): SavePayload;
|
|
324
|
+
dispose(): void;
|
|
325
|
+
}
|
|
326
|
+
|
|
321
327
|
/**
|
|
322
328
|
* Run a save through the `beforesave` / `aftersave` hooks. `beforesave` fires
|
|
323
329
|
* before the payload is captured, so data a hook sets is part of the save;
|
|
@@ -339,13 +345,18 @@ export function establishPlaythrough(
|
|
|
339
345
|
export function saveWithHooks<T>(
|
|
340
346
|
slot: string | undefined,
|
|
341
347
|
custom: Record<string, unknown> | undefined,
|
|
342
|
-
beginCapture: () =>
|
|
348
|
+
beginCapture: () => SaveCapture,
|
|
343
349
|
write: (payload: SavePayload) => Promise<T>,
|
|
344
350
|
): Promise<T> {
|
|
345
351
|
try {
|
|
346
352
|
const capture = beginCapture();
|
|
347
|
-
|
|
348
|
-
|
|
353
|
+
let payload: SavePayload;
|
|
354
|
+
try {
|
|
355
|
+
withoutDraws(() => emit('beforesave', slot, custom));
|
|
356
|
+
payload = capture();
|
|
357
|
+
} finally {
|
|
358
|
+
capture.dispose();
|
|
359
|
+
}
|
|
349
360
|
return write(payload).then((result) => {
|
|
350
361
|
withoutDraws(() => emit('aftersave', slot));
|
|
351
362
|
return result;
|
package/src/store.ts
CHANGED
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
startNewPlaythrough,
|
|
36
36
|
quickSave,
|
|
37
37
|
saveWithHooks,
|
|
38
|
+
type SaveCapture,
|
|
38
39
|
loadSlotSave,
|
|
39
40
|
adoptPlaythrough,
|
|
40
41
|
populateKnownSaves,
|
|
@@ -493,6 +494,7 @@ function enterStart(
|
|
|
493
494
|
): void {
|
|
494
495
|
state.currentPassage = passage;
|
|
495
496
|
state.navigationId++;
|
|
497
|
+
state.replacementNavigationId = state.navigationId;
|
|
496
498
|
state.variables = variables;
|
|
497
499
|
state.transient = freshTransients(transientDefaults);
|
|
498
500
|
state.temporary = createNamespace();
|
|
@@ -841,11 +843,11 @@ function loadedEntryMoment(
|
|
|
841
843
|
}
|
|
842
844
|
|
|
843
845
|
/** The variable paths `Story.set` writes while a save's hooks run. */
|
|
844
|
-
|
|
846
|
+
const hookWrites = new Set<Map<string, string[]>>();
|
|
845
847
|
|
|
846
848
|
/** Note that `path` of the variables was written (see keepHookWrites). */
|
|
847
849
|
export function recordHookWrite(path: string[]): void {
|
|
848
|
-
hookWrites
|
|
850
|
+
for (const writes of hookWrites) writes.set(pathKey(path), path);
|
|
849
851
|
}
|
|
850
852
|
|
|
851
853
|
/**
|
|
@@ -997,6 +999,8 @@ export interface StoryState {
|
|
|
997
999
|
* so the passage display can remount even when the passage name is unchanged.
|
|
998
1000
|
*/
|
|
999
1001
|
navigationId: number;
|
|
1002
|
+
/** Navigation that most recently initialized, restarted or loaded the game. */
|
|
1003
|
+
replacementNavigationId: number;
|
|
1000
1004
|
variables: Record<string, unknown>;
|
|
1001
1005
|
variableDefaults: Record<string, unknown>;
|
|
1002
1006
|
transient: Record<string, unknown>;
|
|
@@ -1080,7 +1084,7 @@ export interface StoryState {
|
|
|
1080
1084
|
* function builds the payload after them, and keeps the variables the
|
|
1081
1085
|
* hooks changed in the saved moment's snapshot, so a load restores them.
|
|
1082
1086
|
*/
|
|
1083
|
-
beginSave: () =>
|
|
1087
|
+
beginSave: () => SaveCapture;
|
|
1084
1088
|
/**
|
|
1085
1089
|
* Replace the game state with a live (deserialized) payload. `slot` is
|
|
1086
1090
|
* passed to the `beforeload`/`afterload` events. Pass the save's
|
|
@@ -1303,6 +1307,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1303
1307
|
storyData: null,
|
|
1304
1308
|
currentPassage: '',
|
|
1305
1309
|
navigationId: 0,
|
|
1310
|
+
replacementNavigationId: 0,
|
|
1306
1311
|
variables: createNamespace(),
|
|
1307
1312
|
variableDefaults: {},
|
|
1308
1313
|
transient: createNamespace(),
|
|
@@ -1834,20 +1839,28 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1834
1839
|
const before = get().variables;
|
|
1835
1840
|
const replaced = latestStateApplied;
|
|
1836
1841
|
const writes = new Map<string, string[]>();
|
|
1837
|
-
hookWrites
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
// A restart or load in a hook left the game the save was issued
|
|
1841
|
-
// for (its playthrough) for another: no save describes both
|
|
1842
|
-
if (latestStateApplied !== replaced) {
|
|
1843
|
-
throw new Error(
|
|
1844
|
-
'spindle: a beforesave handler restarted or loaded the game, so the save was not made',
|
|
1845
|
-
);
|
|
1846
|
-
}
|
|
1847
|
-
const payload = get().getSavePayload();
|
|
1848
|
-
keepHookWrites(payload, before, get().variables, [...writes.values()]);
|
|
1849
|
-
return payload;
|
|
1842
|
+
hookWrites.add(writes);
|
|
1843
|
+
const dispose = () => {
|
|
1844
|
+
hookWrites.delete(writes);
|
|
1850
1845
|
};
|
|
1846
|
+
return Object.assign(
|
|
1847
|
+
() => {
|
|
1848
|
+
dispose();
|
|
1849
|
+
// A restart or load in a hook left the game the save was issued
|
|
1850
|
+
// for (its playthrough) for another: no save describes both
|
|
1851
|
+
if (latestStateApplied !== replaced) {
|
|
1852
|
+
throw new Error(
|
|
1853
|
+
'spindle: a beforesave handler restarted or loaded the game, so the save was not made',
|
|
1854
|
+
);
|
|
1855
|
+
}
|
|
1856
|
+
const payload = get().getSavePayload();
|
|
1857
|
+
keepHookWrites(payload, before, get().variables, [
|
|
1858
|
+
...writes.values(),
|
|
1859
|
+
]);
|
|
1860
|
+
return payload;
|
|
1861
|
+
},
|
|
1862
|
+
{ dispose },
|
|
1863
|
+
);
|
|
1851
1864
|
},
|
|
1852
1865
|
|
|
1853
1866
|
loadFromPayload: (
|
|
@@ -1937,6 +1950,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1937
1950
|
set((state) => {
|
|
1938
1951
|
state.currentPassage = payload.passage;
|
|
1939
1952
|
state.navigationId++;
|
|
1953
|
+
state.replacementNavigationId = state.navigationId;
|
|
1940
1954
|
state.variables = createNamespace(
|
|
1941
1955
|
deepClone(entry?.variables ?? payload.variables),
|
|
1942
1956
|
);
|
package/src/story-variables.ts
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
pieceOffset,
|
|
23
23
|
widgetPieces,
|
|
24
24
|
withParseCache,
|
|
25
|
+
executesRawBody,
|
|
25
26
|
type ParametersOf,
|
|
26
27
|
type Piece,
|
|
27
28
|
} from './code-check';
|
|
@@ -832,10 +833,7 @@ function collectTokenRefs(
|
|
|
832
833
|
}
|
|
833
834
|
}
|
|
834
835
|
|
|
835
|
-
if (
|
|
836
|
-
token.name.toLowerCase() === 'do' &&
|
|
837
|
-
!parametersOf.widget?.(token.name)
|
|
838
|
-
) {
|
|
836
|
+
if (executesRawBody(token.name, parametersOf)) {
|
|
839
837
|
// A {do} body is JavaScript: scan its source text as code, however
|
|
840
838
|
// the markup tokens split it up.
|
|
841
839
|
let close = t + 1;
|
package/src/tooling.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import { NameMap } from './utils/macro-names';
|
|
1
2
|
export {
|
|
2
3
|
completionContext,
|
|
3
4
|
type CompletionContext,
|
|
5
|
+
type CompletionContainer,
|
|
4
6
|
} from './completion-context';
|
|
5
7
|
import { branchParents } from './registry';
|
|
6
8
|
/**
|
|
@@ -15,7 +17,12 @@ import {
|
|
|
15
17
|
type MarkupPassage,
|
|
16
18
|
} from './markup/validate';
|
|
17
19
|
import { isBlockMacro, isRawMacro } from './markup/ast';
|
|
18
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
tokenizeMarkup as tokenizeRawMarkup,
|
|
22
|
+
tokenizeMarkupTolerant as tokenizeRawMarkupTolerant,
|
|
23
|
+
type ParseMarkupOptions as RuntimeParseMarkupOptions,
|
|
24
|
+
type TolerantTokens,
|
|
25
|
+
} from './markup/parse';
|
|
19
26
|
import {
|
|
20
27
|
parameterLookup,
|
|
21
28
|
passagePiecesOf,
|
|
@@ -82,13 +89,8 @@ export type {
|
|
|
82
89
|
JsLexHandlers,
|
|
83
90
|
Sigil,
|
|
84
91
|
} from './js-lexer';
|
|
85
|
-
export {
|
|
86
|
-
|
|
87
|
-
parseSelectors,
|
|
88
|
-
tokenizeMarkup,
|
|
89
|
-
tokenizeMarkupTolerant,
|
|
90
|
-
} from './markup/parse';
|
|
91
|
-
export type { ParseMarkupOptions, TolerantTokens } from './markup/parse';
|
|
92
|
+
export { MarkupError, parseSelectors } from './markup/parse';
|
|
93
|
+
export type { TolerantTokens } from './markup/parse';
|
|
92
94
|
export { SIGIL_SCOPES, isSigil } from './markup/tokens';
|
|
93
95
|
export type {
|
|
94
96
|
AttributeSpan,
|
|
@@ -177,6 +179,8 @@ export interface ValidateMarkupOptions {
|
|
|
177
179
|
|
|
178
180
|
export interface ToolingMacro {
|
|
179
181
|
name: string;
|
|
182
|
+
/** Preserve builtin provenance; an explicit macro without it is a replacement. */
|
|
183
|
+
source?: 'builtin' | 'user';
|
|
180
184
|
block: boolean;
|
|
181
185
|
subMacros: string[];
|
|
182
186
|
/**
|
|
@@ -196,6 +200,46 @@ export interface ToolingMacro {
|
|
|
196
200
|
storeVar?: boolean;
|
|
197
201
|
}
|
|
198
202
|
|
|
203
|
+
/** Public tokenization can use the same effective metadata as validation. */
|
|
204
|
+
export interface ParseMarkupOptions extends RuntimeParseMarkupOptions {
|
|
205
|
+
/** Macro and widget metadata; widgets override builtin raw-body behavior. */
|
|
206
|
+
macros?: Iterable<ToolingMacro>;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function tokenizationOptions(
|
|
210
|
+
options: ParseMarkupOptions,
|
|
211
|
+
): RuntimeParseMarkupOptions {
|
|
212
|
+
if (!options.macros) return options;
|
|
213
|
+
const macros = [...options.macros];
|
|
214
|
+
const parametersOf = parameterLookup(macros);
|
|
215
|
+
return {
|
|
216
|
+
...options,
|
|
217
|
+
hooks: {
|
|
218
|
+
isRaw: (name) =>
|
|
219
|
+
isRawMacro(name, parametersOf.widget?.(name) !== undefined),
|
|
220
|
+
isBlock: blockLookup(macros),
|
|
221
|
+
branchParents: (name) => branchParents(name, macros),
|
|
222
|
+
...options.hooks,
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** Flat tokens with optional effective macro/widget metadata. */
|
|
228
|
+
export function tokenizeMarkup(
|
|
229
|
+
source: string,
|
|
230
|
+
options: ParseMarkupOptions = {},
|
|
231
|
+
): Token[] {
|
|
232
|
+
return tokenizeRawMarkup(source, tokenizationOptions(options));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Metadata-aware flat tokens and recovery errors, in original source offsets. */
|
|
236
|
+
export function tokenizeMarkupTolerant(
|
|
237
|
+
source: string,
|
|
238
|
+
options: ParseMarkupOptions = {},
|
|
239
|
+
): TolerantTokens {
|
|
240
|
+
return tokenizeRawMarkupTolerant(source, tokenizationOptions(options));
|
|
241
|
+
}
|
|
242
|
+
|
|
199
243
|
/**
|
|
200
244
|
* Validate the markup of a story's passages as Spindle does when the story
|
|
201
245
|
* starts, against the given macros (built-in and user-defined): malformed
|
|
@@ -341,13 +385,17 @@ function referenceOptions(
|
|
|
341
385
|
passages: readonly MarkupPassage[] = [],
|
|
342
386
|
) {
|
|
343
387
|
const list = [...macros];
|
|
388
|
+
const effective = new NameMap<ToolingMacro>();
|
|
389
|
+
for (const macro of list) effective.set(macro.name, macro);
|
|
344
390
|
const widgets = effectiveWidgetDefinitions(
|
|
345
391
|
readWidgetDefinitions(passages, parameterLookup(list)),
|
|
346
392
|
);
|
|
347
393
|
return {
|
|
348
394
|
all,
|
|
349
395
|
storeVarMacros: new Set(
|
|
350
|
-
|
|
396
|
+
[...effective.values()]
|
|
397
|
+
.filter((m) => m.storeVar)
|
|
398
|
+
.map((m) => m.name.toLowerCase()),
|
|
351
399
|
),
|
|
352
400
|
parametersOf: parameterLookup([...list, ...definedWidgetMacros(widgets)]),
|
|
353
401
|
tolerant: true,
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
/** The parent in the rendered tree, including slots and shadow hosts. */
|
|
2
|
+
export function composedParent(el: Element): Element | null {
|
|
3
|
+
return (
|
|
4
|
+
el.assignedSlot ??
|
|
5
|
+
el.parentElement ??
|
|
6
|
+
(el.getRootNode() as ShadowRoot).host ??
|
|
7
|
+
null
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
/** Elements whose content is never text the reader reads. */
|
|
2
12
|
const NOT_TEXT = new Set(['SCRIPT', 'STYLE', 'TEMPLATE', 'NOSCRIPT']);
|
|
3
13
|
|
|
@@ -26,13 +36,16 @@ export function isHidden(el: Element): boolean {
|
|
|
26
36
|
* toggles it: the browser renders none of that content (#389).
|
|
27
37
|
*/
|
|
28
38
|
function inClosedDetails(el: Element, root?: Element): boolean {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
for (let child = el; child !== root;) {
|
|
40
|
+
const parent = composedParent(child);
|
|
41
|
+
if (!parent) break;
|
|
42
|
+
if (parent.localName === 'details' && !parent.hasAttribute('open')) {
|
|
43
|
+
const summary = Array.from(parent.children).find(
|
|
44
|
+
(c) => c.localName === 'summary',
|
|
45
|
+
);
|
|
46
|
+
if (child !== summary) return true;
|
|
47
|
+
}
|
|
48
|
+
child = parent;
|
|
36
49
|
}
|
|
37
50
|
return false;
|
|
38
51
|
}
|
|
@@ -49,7 +62,7 @@ export function isRendered(el: Element, root?: Element): boolean {
|
|
|
49
62
|
e.ownerDocument.defaultView?.getComputedStyle(e);
|
|
50
63
|
const visibility = style(el)?.visibility;
|
|
51
64
|
if (visibility === 'hidden' || visibility === 'collapse') return false;
|
|
52
|
-
for (let e: Element | null = el; e; e = e
|
|
65
|
+
for (let e: Element | null = el; e; e = composedParent(e)) {
|
|
53
66
|
if (style(e)?.display === 'none') return false;
|
|
54
67
|
if (e === root) break;
|
|
55
68
|
}
|
|
@@ -59,7 +72,7 @@ export function isRendered(el: Element, root?: Element): boolean {
|
|
|
59
72
|
/** Visibility shared by reader observations and native control handling. */
|
|
60
73
|
export function isReaderVisible(el: Element): boolean {
|
|
61
74
|
if (!el.isConnected || !isRendered(el)) return false;
|
|
62
|
-
for (let at: Element | null = el; at; at = at
|
|
75
|
+
for (let at: Element | null = el; at; at = composedParent(at)) {
|
|
63
76
|
if (isHidden(at) || at.hasAttribute('inert')) return false;
|
|
64
77
|
}
|
|
65
78
|
return true;
|
|
@@ -2,11 +2,12 @@ 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
4
|
import { parseMarkup, type MarkupHooks } from '../markup/parse';
|
|
5
|
-
import { getMacroRegistry } from '../registry';
|
|
5
|
+
import { getMacroRegistry, getMacroParameters } from '../registry';
|
|
6
6
|
|
|
7
7
|
/** The declared parameters of the macros registered so far. */
|
|
8
8
|
export const registeredParameters: ParametersOf = (name) =>
|
|
9
9
|
parameterLookup(getMacroRegistry())(name);
|
|
10
|
+
registeredParameters.source = getMacroParameters.source;
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Whether the text of an attribute value or macro label renders a
|
package/types/tooling.d.ts
CHANGED
|
@@ -496,6 +496,8 @@ export declare class MarkupError extends Error {
|
|
|
496
496
|
}
|
|
497
497
|
|
|
498
498
|
export interface ParseMarkupOptions {
|
|
499
|
+
/** Effective macro/widget metadata; a do widget has a markup body. */
|
|
500
|
+
macros?: Iterable<ToolingMacro>;
|
|
499
501
|
/**
|
|
500
502
|
* Text mode, for markup that becomes a string (HTML attribute values,
|
|
501
503
|
* macro labels): only `{…}` markup and brace escapes are recognized.
|
|
@@ -653,6 +655,8 @@ export declare function collectPassageReferences(
|
|
|
653
655
|
/** What tooling knows about a macro (a `MacroMetadata` has all of it). */
|
|
654
656
|
export interface ToolingMacro {
|
|
655
657
|
name: string;
|
|
658
|
+
/** Preserve builtin provenance; an explicit macro without it is a replacement. */
|
|
659
|
+
source?: 'builtin' | 'user';
|
|
656
660
|
block: boolean;
|
|
657
661
|
subMacros: string[];
|
|
658
662
|
/** A widget's parameters and their argument contracts. */
|
|
@@ -1243,9 +1247,22 @@ export declare function macroArgumentSpans(
|
|
|
1243
1247
|
): MacroArgumentSpan[];
|
|
1244
1248
|
|
|
1245
1249
|
/** Executable code at a cursor, or prose/literal text that suppresses completion. */
|
|
1250
|
+
/** Enclosing text containers, in order from the passage to the cursor. */
|
|
1251
|
+
export type CompletionContainer =
|
|
1252
|
+
| { kind: 'string'; quote: '"' | "'"; escapes: 'quotes' | 'javascript' }
|
|
1253
|
+
| { kind: 'attribute'; name: string; quote: '"' | "'" | null; code: boolean }
|
|
1254
|
+
| { kind: 'link-label' };
|
|
1255
|
+
|
|
1246
1256
|
export type CompletionContext =
|
|
1247
1257
|
| { kind: 'literal' }
|
|
1248
|
-
| (PieceBase & {
|
|
1258
|
+
| (PieceBase & {
|
|
1259
|
+
kind: 'code';
|
|
1260
|
+
code: string;
|
|
1261
|
+
goal: JsGoal;
|
|
1262
|
+
cursor: number;
|
|
1263
|
+
/** Encode replacement text through these containers, innermost first. */
|
|
1264
|
+
containers?: readonly CompletionContainer[];
|
|
1265
|
+
});
|
|
1249
1266
|
|
|
1250
1267
|
/** Inspect only the prefix before cursor (UTF-16), accepting unfinished markup.
|
|
1251
1268
|
* Strings, comments, regexes and template text are literal; template
|