@rohal12/spindle 0.64.8 → 0.64.10
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 +337 -228
- package/dist/pkg/story-variables.js +458 -208
- package/package.json +1 -1
- package/src/format/markdown-lines.ts +2 -2
- package/src/headless.ts +6 -1
- package/src/hooks/use-modal-focus.ts +5 -3
- package/src/interpolation.ts +5 -4
- package/src/markup/grammar-shared.ts +2 -1
- package/src/markup/html-elements.ts +16 -0
- package/src/markup/markdown.ts +10 -2
- package/src/markup/render.tsx +5 -4
- package/src/markup/spindle.peggy +20 -9
- package/src/parser.ts +2 -1
- package/src/store.ts +6 -0
- package/src/story-api.ts +9 -3
- package/src/twine-editor/highlight.ts +32 -11
- package/src/twine-editor/mode.ts +14 -4
- package/src/utils/stable-key.ts +5 -6
- package/types/headless.d.ts +1 -0
- package/types/index.d.ts +5 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { parse, postprocess, preprocess } from 'micromark';
|
|
2
2
|
import { analyzeMarkup } from '../markup/parse';
|
|
3
|
-
import {
|
|
3
|
+
import { boundedMarkdownOptions } from '../markup/markdown';
|
|
4
4
|
import type { PairedNode } from '../markup/pair';
|
|
5
5
|
import { sourceLines } from './source-lines';
|
|
6
6
|
|
|
@@ -38,7 +38,7 @@ export function markdownLines(
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
const events = postprocess(
|
|
41
|
-
parse(
|
|
41
|
+
parse(boundedMarkdownOptions(markdown))
|
|
42
42
|
.document()
|
|
43
43
|
.write(preprocess()(markdown, undefined, true)),
|
|
44
44
|
);
|
package/src/headless.ts
CHANGED
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { boot } from './index';
|
|
11
11
|
import { readOnlyValue } from './execute-mutation';
|
|
12
|
+
import { deepClone } from './structural';
|
|
12
13
|
import { isSpecialPassage, useStoryStore } from './store';
|
|
13
14
|
import { noPassageError, specialPassageError } from './runtime-errors';
|
|
14
15
|
import type { StoryAPI } from './story-api';
|
|
@@ -134,6 +135,7 @@ export interface TranscriptOptions {
|
|
|
134
135
|
export interface Transcript {
|
|
135
136
|
/**
|
|
136
137
|
* `Story.step()`, recording the observation it acted on and the choice.
|
|
138
|
+
* Choice values are copied before the action runs.
|
|
137
139
|
* Await each step before the next: steps are not queued.
|
|
138
140
|
*/
|
|
139
141
|
step(id: string, value?: unknown): Promise<Observation>;
|
|
@@ -168,13 +170,16 @@ export function createTranscript(
|
|
|
168
170
|
const action =
|
|
169
171
|
before.actions.find((a) => a.id === id) ??
|
|
170
172
|
story.getActions().find((a) => a.id === id);
|
|
173
|
+
// Snapshot before the action can mutate its input, including before
|
|
174
|
+
// awaiting an asynchronous action (#691).
|
|
175
|
+
const choice = deepClone(value);
|
|
171
176
|
const next = await story.step(id, value);
|
|
172
177
|
|
|
173
178
|
const chose: TranscriptEntry['chose'] = {
|
|
174
179
|
id,
|
|
175
180
|
label: action?.label ?? id,
|
|
176
181
|
};
|
|
177
|
-
if (value !== undefined) chose.value =
|
|
182
|
+
if (value !== undefined) chose.value = choice;
|
|
178
183
|
const entry: TranscriptEntry = {
|
|
179
184
|
turn: before.turn,
|
|
180
185
|
passage: before.passage,
|
|
@@ -90,9 +90,11 @@ function isTabStop(el: HTMLElement): boolean {
|
|
|
90
90
|
const input = el as HTMLInputElement;
|
|
91
91
|
if (el.localName === 'input' && input.type === 'radio' && input.name) {
|
|
92
92
|
const group = Array.from(
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
).querySelectorAll<HTMLInputElement>(
|
|
93
|
+
// Form ownership does not imply DOM ancestry: `form="id"` controls
|
|
94
|
+
// elsewhere in the same tree belong to the group too (#688).
|
|
95
|
+
(el.getRootNode() as ParentNode).querySelectorAll<HTMLInputElement>(
|
|
96
|
+
'input[type="radio"]',
|
|
97
|
+
),
|
|
96
98
|
).filter(
|
|
97
99
|
(r) =>
|
|
98
100
|
r.name === input.name &&
|
package/src/interpolation.ts
CHANGED
|
@@ -282,12 +282,13 @@ class TextEvaluator {
|
|
|
282
282
|
});
|
|
283
283
|
}
|
|
284
284
|
|
|
285
|
-
// Registered widgets take precedence over stray companion markers.
|
|
286
|
-
if (isSubMacro(node.name)) return '';
|
|
287
|
-
|
|
288
285
|
const text = getMacroText(node.name);
|
|
289
286
|
if (!text) {
|
|
290
|
-
const
|
|
287
|
+
const macro = getMacro(node.name);
|
|
288
|
+
// Explicit implementations take precedence over stray companions,
|
|
289
|
+
// including their missing-text diagnostics (#689).
|
|
290
|
+
if (!macro && isSubMacro(node.name)) return '';
|
|
291
|
+
const message = macro
|
|
291
292
|
? `{${node.name}} has no text form, so it can't be used in an attribute value or label`
|
|
292
293
|
: `unknown macro {${node.name}}`;
|
|
293
294
|
this.errors.push({ macro: node.name, error: new Error(message) });
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* it holds no second copy: the generated parser imports this module's
|
|
4
4
|
* default export as `shared` (see scripts/peggy.ts).
|
|
5
5
|
*/
|
|
6
|
+
import { HTML_TEXT_ELEMENTS } from './html-elements';
|
|
6
7
|
import { SIGIL_SCOPES, withSelectors } from './tokens';
|
|
7
8
|
|
|
8
|
-
export default { SIGIL_SCOPES, withSelectors };
|
|
9
|
+
export default { SIGIL_SCOPES, withSelectors, HTML_TEXT_ELEMENTS };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** HTML raw-text bodies preserve character references as written. */
|
|
2
|
+
export const RAW_TEXT_ELEMENTS = new Set([
|
|
3
|
+
'script',
|
|
4
|
+
'style',
|
|
5
|
+
'xmp',
|
|
6
|
+
'iframe',
|
|
7
|
+
'noembed',
|
|
8
|
+
'noframes',
|
|
9
|
+
]);
|
|
10
|
+
|
|
11
|
+
/** Text-only bodies recognize their own end tag, never nested HTML tags. */
|
|
12
|
+
export const HTML_TEXT_ELEMENTS = new Set([
|
|
13
|
+
...RAW_TEXT_ELEMENTS,
|
|
14
|
+
'textarea',
|
|
15
|
+
'title',
|
|
16
|
+
]);
|
package/src/markup/markdown.ts
CHANGED
|
@@ -54,11 +54,19 @@ export function markdownToHtml(
|
|
|
54
54
|
text: string,
|
|
55
55
|
options?: { inline?: boolean },
|
|
56
56
|
): string {
|
|
57
|
+
return micromark(text, boundedMarkdownOptions(text, options));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Share bounded tokenization with scans that need source token offsets. */
|
|
61
|
+
export function boundedMarkdownOptions(
|
|
62
|
+
text: string,
|
|
63
|
+
options?: { inline?: boolean },
|
|
64
|
+
): Options {
|
|
57
65
|
const base = markdownOptions(options);
|
|
58
|
-
return
|
|
66
|
+
return {
|
|
59
67
|
...base,
|
|
60
68
|
extensions: [...(base.extensions ?? []), unclosedHtml(text), textData],
|
|
61
|
-
}
|
|
69
|
+
};
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
/** Join a data token just read to a data token right before it. */
|
package/src/markup/render.tsx
CHANGED
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
type TextScope,
|
|
23
23
|
} from '../interpolation';
|
|
24
24
|
import { MacroError } from '../components/macros/MacroError';
|
|
25
|
+
import { HTML_TEXT_ELEMENTS, RAW_TEXT_ELEMENTS } from './html-elements';
|
|
25
26
|
import { isCodeAttribute } from './code-attributes';
|
|
26
27
|
import { errorMessage } from '../utils/error-message';
|
|
27
28
|
import { EMPTY_NAMESPACE } from '../utils/namespace';
|
|
@@ -113,7 +114,7 @@ const STRUCTURAL_ELEMENTS = new Set([
|
|
|
113
114
|
]);
|
|
114
115
|
|
|
115
116
|
/** Elements whose content is literal text, not markdown. */
|
|
116
|
-
const PREFORMATTED_ELEMENTS = new Set(['pre',
|
|
117
|
+
const PREFORMATTED_ELEMENTS = new Set(['pre', ...HTML_TEXT_ELEMENTS]);
|
|
117
118
|
export const WidgetChildrenContext = createContext<WidgetChildren | null>(null);
|
|
118
119
|
|
|
119
120
|
/**
|
|
@@ -778,7 +779,7 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
778
779
|
let children: preact.ComponentChildren = undefined;
|
|
779
780
|
let structural = false;
|
|
780
781
|
if (node.children.length > 0) {
|
|
781
|
-
if (tag
|
|
782
|
+
if (RAW_TEXT_ELEMENTS.has(tag)) {
|
|
782
783
|
// Raw-text elements keep script text and character-reference lookalikes
|
|
783
784
|
// (`?a=1¬ch=2`) as written (#453)
|
|
784
785
|
children = node.children.map((n) =>
|
|
@@ -903,8 +904,6 @@ function renderMacro(node: MacroNode, key: string) {
|
|
|
903
904
|
);
|
|
904
905
|
}
|
|
905
906
|
|
|
906
|
-
if (isSubMacro(node.name)) return null;
|
|
907
|
-
|
|
908
907
|
const Component = getMacro(node.name);
|
|
909
908
|
if (Component) {
|
|
910
909
|
return (
|
|
@@ -917,6 +916,8 @@ function renderMacro(node: MacroNode, key: string) {
|
|
|
917
916
|
);
|
|
918
917
|
}
|
|
919
918
|
|
|
919
|
+
if (isSubMacro(node.name)) return null;
|
|
920
|
+
|
|
920
921
|
return (
|
|
921
922
|
<span
|
|
922
923
|
key={key}
|
package/src/markup/spindle.peggy
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
{{
|
|
26
26
|
const LB = String.fromCharCode(123);
|
|
27
27
|
const RB = String.fromCharCode(125);
|
|
28
|
-
const { SIGIL_SCOPES, withSelectors } = shared;
|
|
28
|
+
const { SIGIL_SCOPES, withSelectors, HTML_TEXT_ELEMENTS } = shared;
|
|
29
29
|
const VOID_TAGS = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
|
|
30
30
|
const BRANCH_PARENT = { elseif: 'if', else: 'if', case: 'switch', default: 'switch', next: 'timed' };
|
|
31
31
|
const LINK_SEPARATORS = [['|', false], ['->', false], ['<-', true]];
|
|
@@ -133,12 +133,17 @@ const textMode = options.text === true;
|
|
|
133
133
|
// Only cursor-context parsing accepts EOF in place of an unfinished closer.
|
|
134
134
|
const completionMode = options.completion === true;
|
|
135
135
|
const hooks = options.hooks;
|
|
136
|
+
// Unclosed comment openers are text. Reject them without rescanning each
|
|
137
|
+
// remaining suffix; completion alone accepts EOF as the closer (#690).
|
|
138
|
+
const lastCommentClose = input.lastIndexOf('-->');
|
|
136
139
|
/** End of the stretch a `.|n|` repetition consumes (see skipTo). */
|
|
137
140
|
let skipEnd = -1;
|
|
138
141
|
/** Whether the attribute whose value is being read holds code. */
|
|
139
142
|
let attrCode = false;
|
|
140
143
|
/** The macro whose raw body is being read. */
|
|
141
144
|
let rawName = '';
|
|
145
|
+
/** The HTML text-only body being read; its own closer ends this context. */
|
|
146
|
+
let htmlTextTag = '';
|
|
142
147
|
|
|
143
148
|
/**
|
|
144
149
|
* The index in `inner` (at `at` in the input) of the first `separator` that
|
|
@@ -206,12 +211,18 @@ Tokens
|
|
|
206
211
|
TokenItem
|
|
207
212
|
= TextRun
|
|
208
213
|
/ Escape
|
|
209
|
-
/ Comment
|
|
210
|
-
/ LinkLexeme
|
|
211
|
-
/ EmailAutolink
|
|
212
|
-
/ &{ return !textMode; } UriAutolink
|
|
213
|
-
/ &{ return !textMode; } tag:HtmlOpen {
|
|
214
|
-
|
|
214
|
+
/ &{ return !htmlTextTag; } Comment
|
|
215
|
+
/ &{ return !htmlTextTag; } LinkLexeme
|
|
216
|
+
/ &{ return !htmlTextTag; } EmailAutolink
|
|
217
|
+
/ &{ return !textMode && !htmlTextTag; } UriAutolink
|
|
218
|
+
/ &{ return !textMode && !htmlTextTag; } tag:HtmlOpen {
|
|
219
|
+
if (!tag.isSelfClose && HTML_TEXT_ELEMENTS.has(tag.lower)) htmlTextTag = tag.lower;
|
|
220
|
+
return tag;
|
|
221
|
+
}
|
|
222
|
+
/ &{ return !textMode; } tag:HtmlClose &{ return !htmlTextTag || tag.lower === htmlTextTag; } {
|
|
223
|
+
htmlTextTag = '';
|
|
224
|
+
return VOID_TAGS.has(tag.lower) ? null : tag;
|
|
225
|
+
}
|
|
215
226
|
/ Value
|
|
216
227
|
/ tag:MacroTag
|
|
217
228
|
body:( &{ rawName = tag.lower; return tag.kind === 'open' && hooks.isRaw(rawName); } b:RawTokens { return b; } )?
|
|
@@ -246,7 +257,7 @@ TextRun
|
|
|
246
257
|
* rendering drops it. (An unclosed `<!--` is ordinary text.)
|
|
247
258
|
*/
|
|
248
259
|
Comment
|
|
249
|
-
= value:$("<!--" (">" / "->" / (!"-->" .)* ("-->" / OpenEnd))) {
|
|
260
|
+
= value:$("<!--" (">" / "->" / &{ return completionMode || offset() <= lastCommentClose; } (!"-->" .)* ("-->" / OpenEnd))) {
|
|
250
261
|
const node = lexeme('text', location(), { value });
|
|
251
262
|
node.comment = true;
|
|
252
263
|
return node;
|
|
@@ -279,7 +290,7 @@ Escape
|
|
|
279
290
|
/** A character that starts no markup here. */
|
|
280
291
|
Literal
|
|
281
292
|
= "{" !(Selectors [$_@%(!/a-zA-Z]) { return lexeme('text', location(), { value: LB }); }
|
|
282
|
-
/ !(&{ return !textMode; } HtmlClose) "<" { return lexeme('text', location(), { value: '<' }); }
|
|
293
|
+
/ !(&{ return !textMode && !htmlTextTag; } HtmlClose) "<" { return lexeme('text', location(), { value: '<' }); }
|
|
283
294
|
/ "[" { return lexeme('text', location(), { value: '[' }); }
|
|
284
295
|
|
|
285
296
|
LinkLexeme
|
package/src/parser.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RAW_TEXT_ELEMENTS } from './markup/html-elements';
|
|
1
2
|
import { MarkupError, tokenizeMarkup } from './markup/parse';
|
|
2
3
|
import { isCodeAttribute, splitSigilTemplate } from './markup/code-attributes';
|
|
3
4
|
|
|
@@ -141,7 +142,7 @@ const escapeTextReferences = (text: string) =>
|
|
|
141
142
|
*/
|
|
142
143
|
function isLiteralTextElement(parent: ParentNode): boolean {
|
|
143
144
|
return (
|
|
144
|
-
|
|
145
|
+
RAW_TEXT_ELEMENTS.has((parent as Element).localName) &&
|
|
145
146
|
(parent as Element).namespaceURI === HTML_NAMESPACE
|
|
146
147
|
);
|
|
147
148
|
}
|
package/src/store.ts
CHANGED
|
@@ -285,6 +285,12 @@ function persistSession(get: () => StoryState): { error: unknown } | undefined {
|
|
|
285
285
|
sessionMoments.length = history.length;
|
|
286
286
|
}
|
|
287
287
|
|
|
288
|
+
// Reseeding replaces an existing moment's PRNG snapshot. Keep metadata
|
|
289
|
+
// current without reconstructing the cached variable namespaces (#702).
|
|
290
|
+
for (let i = 0; i < sessionMoments.length; i++) {
|
|
291
|
+
sessionMoments[i] = savedMoment(history[i]!, sessionMoments[i]!.variables);
|
|
292
|
+
}
|
|
293
|
+
|
|
288
294
|
// Append new entries
|
|
289
295
|
if (sessionMoments.length < history.length) {
|
|
290
296
|
const gap = history.length - sessionMoments.length;
|
package/src/story-api.ts
CHANGED
|
@@ -175,7 +175,9 @@ export interface StoryAPI {
|
|
|
175
175
|
event: E,
|
|
176
176
|
callback: StoryEventCallback<E>,
|
|
177
177
|
): () => void;
|
|
178
|
-
waitForActions(
|
|
178
|
+
waitForActions(options?: {
|
|
179
|
+
includeRunningActions?: boolean;
|
|
180
|
+
}): Promise<StoryAction[]>;
|
|
179
181
|
observe(): Observation;
|
|
180
182
|
step(id: string, value?: unknown): Promise<Observation>;
|
|
181
183
|
setObservedActions(rules: ObservedActionRules): void;
|
|
@@ -446,10 +448,14 @@ function createStoryAPI(): StoryAPI {
|
|
|
446
448
|
return trackRuntimeUnsub(unsub);
|
|
447
449
|
},
|
|
448
450
|
|
|
449
|
-
async waitForActions(
|
|
451
|
+
async waitForActions(
|
|
452
|
+
options: { includeRunningActions?: boolean } = {},
|
|
453
|
+
): Promise<StoryAction[]> {
|
|
450
454
|
// An action that finishes later (QuickLoad) has not changed what the
|
|
451
455
|
// checks below look at until it has (#504)
|
|
452
|
-
|
|
456
|
+
// A callback can request a render flush without waiting for its own
|
|
457
|
+
// completion. Keep the default external action-completion barrier.
|
|
458
|
+
if (options.includeRunningActions !== false) await settleRunningActions();
|
|
453
459
|
await new Promise<void>((resolve) => {
|
|
454
460
|
requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
|
|
455
461
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SIGIL_SCOPES,
|
|
3
3
|
lexJs,
|
|
4
|
+
pairMarkup,
|
|
4
5
|
passagePiecesOfTokens,
|
|
5
6
|
pieceOffset,
|
|
6
7
|
type HtmlToken,
|
|
@@ -10,11 +11,15 @@ import {
|
|
|
10
11
|
type Token,
|
|
11
12
|
type VariableScope,
|
|
12
13
|
} from '../tooling';
|
|
14
|
+
import { HTML_TEXT_ELEMENTS } from '../markup/html-elements';
|
|
13
15
|
import { scanMarkupTolerant, tokenizeMarkupTolerant } from '../markup/parse';
|
|
14
16
|
|
|
15
17
|
type ScannedMarkup = ReturnType<typeof scanMarkupTolerant>;
|
|
16
18
|
import { jsCodeEnd } from '../markup/code-end';
|
|
17
19
|
import { builtinMacros } from './builtin-macros';
|
|
20
|
+
import { parameterLookup } from '../code-check';
|
|
21
|
+
|
|
22
|
+
const pairing = parameterLookup(builtinMacros).pairing;
|
|
18
23
|
|
|
19
24
|
/** The CodeMirror 5 token names Twine themes (see the design spec's table). */
|
|
20
25
|
export type CmStyle =
|
|
@@ -230,12 +235,9 @@ function openDo(tokens: readonly Token[]): MacroToken | undefined {
|
|
|
230
235
|
return open;
|
|
231
236
|
}
|
|
232
237
|
|
|
233
|
-
/** Elements whose content is code, where `<!--` is not an HTML comment. */
|
|
234
|
-
const CODE_ELEMENTS = new Set(['script', 'style']);
|
|
235
|
-
|
|
236
238
|
/**
|
|
237
|
-
* The text tokens that are markup, each with the
|
|
238
|
-
*
|
|
239
|
+
* The text tokens that are markup, each with the HTML text-only element
|
|
240
|
+
* it is in, if any. The text of a `{do}` body is code, and is
|
|
239
241
|
* left out. (Strings and expressions in macros are not text tokens.)
|
|
240
242
|
*/
|
|
241
243
|
function* markupText(
|
|
@@ -250,7 +252,7 @@ function* markupText(
|
|
|
250
252
|
const tag = token.tag.toLowerCase();
|
|
251
253
|
if (token.isClose) {
|
|
252
254
|
if (tag === element) element = undefined;
|
|
253
|
-
} else if (!token.isSelfClose &&
|
|
255
|
+
} else if (!token.isSelfClose && HTML_TEXT_ELEMENTS.has(tag)) {
|
|
254
256
|
element ??= tag;
|
|
255
257
|
}
|
|
256
258
|
} else if (token.type === 'text' && !token.comment && !inDo) {
|
|
@@ -260,8 +262,8 @@ function* markupText(
|
|
|
260
262
|
}
|
|
261
263
|
|
|
262
264
|
/**
|
|
263
|
-
* Where the `<!--` that no `-->` closes starts, or -1. In
|
|
264
|
-
*
|
|
265
|
+
* Where the `<!--` that no `-->` closes starts, or -1. In an HTML
|
|
266
|
+
* text-only element, `<!--` is literal text, no HTML comment.
|
|
265
267
|
*/
|
|
266
268
|
function openComment(tokens: readonly Token[]): number {
|
|
267
269
|
for (const { token, element } of markupText(tokens)) {
|
|
@@ -287,7 +289,7 @@ function closesWithGt(text: string, at: number): boolean {
|
|
|
287
289
|
/**
|
|
288
290
|
* Where a tag starts that is text only for want of its `>`, or -1: `</b`,
|
|
289
291
|
* or `<b_r x=` (no error: `<b` is no tag before `_`), which a `>` on a later
|
|
290
|
-
* line makes a tag. In a
|
|
292
|
+
* line makes a tag. In a text-only element only its end tag counts.
|
|
291
293
|
*/
|
|
292
294
|
function openTag(text: string, tokens: readonly Token[]): number {
|
|
293
295
|
for (const { token, element } of markupText(tokens)) {
|
|
@@ -458,14 +460,33 @@ export function openConstructStart(text: string): number {
|
|
|
458
460
|
export function highlightWithOpenStart(text: string): {
|
|
459
461
|
spans: StyledSpan[];
|
|
460
462
|
openStart: number;
|
|
463
|
+
context: string;
|
|
461
464
|
} {
|
|
462
465
|
try {
|
|
463
466
|
const reading = read(text);
|
|
467
|
+
const openStart = openStartOf(text, reading);
|
|
468
|
+
// Retain only the enclosing openers before the lexical tail. Branch
|
|
469
|
+
// arguments depend on their parent even when it began on an earlier
|
|
470
|
+
// line; retaining the whole body would make long blocks quadratic (#699).
|
|
471
|
+
const before = openStart < 0 ? text.length : openStart;
|
|
472
|
+
const { errors } = pairMarkup(
|
|
473
|
+
reading.tokens.filter((token) => token.end <= before),
|
|
474
|
+
pairing,
|
|
475
|
+
);
|
|
476
|
+
const openers = errors
|
|
477
|
+
.filter((error) => error.code === 'unclosed-block')
|
|
478
|
+
.sort((a, b) => a.start - b.start)
|
|
479
|
+
.map((error) => text.slice(error.start, error.end));
|
|
464
480
|
return {
|
|
465
481
|
spans: spansOf(text, reading),
|
|
466
|
-
openStart
|
|
482
|
+
openStart,
|
|
483
|
+
context: openers.length ? openers.join('\n') + '\n' : '',
|
|
467
484
|
};
|
|
468
485
|
} catch {
|
|
469
|
-
return {
|
|
486
|
+
return {
|
|
487
|
+
spans: highlightSpans(text),
|
|
488
|
+
openStart: openConstructStart(text),
|
|
489
|
+
context: '',
|
|
490
|
+
};
|
|
470
491
|
}
|
|
471
492
|
}
|
package/src/twine-editor/mode.ts
CHANGED
|
@@ -10,6 +10,8 @@ import {
|
|
|
10
10
|
export const LOOK_AHEAD_LINES = 200;
|
|
11
11
|
|
|
12
12
|
export interface ModeState {
|
|
13
|
+
/** Enclosing block/element openers, without their previous body text. */
|
|
14
|
+
context: string;
|
|
13
15
|
/**
|
|
14
16
|
* The text of a construct still open at the end of the previous line,
|
|
15
17
|
* from its start, ending in a line break; `''` when nothing is open.
|
|
@@ -68,13 +70,13 @@ function lookAhead(
|
|
|
68
70
|
|
|
69
71
|
/** Read the current line's spans and the tail for the next line. */
|
|
70
72
|
function startLine(stream: StringStream, state: ModeState): void {
|
|
71
|
-
const lineStart = state.tail.length;
|
|
72
|
-
const head = state.tail + stream.string;
|
|
73
|
+
const lineStart = state.context.length + state.tail.length;
|
|
74
|
+
const head = state.context + state.tail + stream.string;
|
|
73
75
|
const line = highlightWithOpenStart(head);
|
|
74
76
|
const open = line.openStart;
|
|
75
77
|
let spans = line.spans;
|
|
76
78
|
// A capped construct still open: no look-ahead until it closes.
|
|
77
|
-
let capped = state.capped && open ===
|
|
79
|
+
let capped = state.capped && open === state.context.length;
|
|
78
80
|
if (open >= 0 && !capped) {
|
|
79
81
|
const window = lookAhead(stream, head);
|
|
80
82
|
capped = window.capped;
|
|
@@ -89,6 +91,7 @@ function startLine(stream: StringStream, state: ModeState): void {
|
|
|
89
91
|
}));
|
|
90
92
|
state.lineStart = lineStart;
|
|
91
93
|
state.capped = capped;
|
|
94
|
+
state.context = line.context;
|
|
92
95
|
const rest = open < 0 ? '' : head.slice(open);
|
|
93
96
|
// A capped tail keeps the construct's first line only, so the cost per
|
|
94
97
|
// line stays flat however long it stays open.
|
|
@@ -104,7 +107,13 @@ function startLine(stream: StringStream, state: ModeState): void {
|
|
|
104
107
|
export function mode(): Mode<ModeState> {
|
|
105
108
|
return {
|
|
106
109
|
name: 'spindle',
|
|
107
|
-
startState: () => ({
|
|
110
|
+
startState: () => ({
|
|
111
|
+
context: '',
|
|
112
|
+
tail: '',
|
|
113
|
+
spans: [],
|
|
114
|
+
lineStart: 0,
|
|
115
|
+
capped: false,
|
|
116
|
+
}),
|
|
108
117
|
copyState: (state) => ({ ...state, spans: [...state.spans] }),
|
|
109
118
|
blankLine: (state) => {
|
|
110
119
|
if (state.tail) state.tail += '\n';
|
|
@@ -129,6 +138,7 @@ export function mode(): Mode<ModeState> {
|
|
|
129
138
|
stream.skipToEnd();
|
|
130
139
|
state.spans = [];
|
|
131
140
|
state.tail = '';
|
|
141
|
+
state.context = '';
|
|
132
142
|
state.capped = false;
|
|
133
143
|
return null;
|
|
134
144
|
}
|
package/src/utils/stable-key.ts
CHANGED
|
@@ -86,12 +86,12 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
86
86
|
return named(keys.length ? `${key}${fieldsOf(obj, keys, seen)}` : key);
|
|
87
87
|
};
|
|
88
88
|
|
|
89
|
-
//
|
|
90
|
-
//
|
|
89
|
+
// Every object participates in the graph, including Date/RegExp leaves
|
|
90
|
+
// whose aliases can affect mount-initialized locals (#704).
|
|
91
|
+
seen.set(obj, seen.size);
|
|
92
|
+
|
|
93
|
+
// Read with the built-in methods: a subclass may override them.
|
|
91
94
|
if (val instanceof Date || val instanceof RegExp) {
|
|
92
|
-
if (className !== undefined && extraKeys(obj).length) {
|
|
93
|
-
seen.set(obj, seen.size);
|
|
94
|
-
}
|
|
95
95
|
return withFields(
|
|
96
96
|
val instanceof Date
|
|
97
97
|
? `Date(${Date.prototype.getTime.call(val)})`
|
|
@@ -99,7 +99,6 @@ function keyOf(val: unknown, seen: Map<object, number>): string {
|
|
|
99
99
|
);
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
seen.set(obj, seen.size);
|
|
103
102
|
if (Array.isArray(val)) {
|
|
104
103
|
// A hole is no element, not an undefined one (#497), as for deepEqual
|
|
105
104
|
// and a save; a plain `map` would skip it, giving `[,]` the key of `[]`
|
package/types/headless.d.ts
CHANGED
|
@@ -64,6 +64,7 @@ export interface TranscriptOptions {
|
|
|
64
64
|
export interface Transcript {
|
|
65
65
|
/**
|
|
66
66
|
* `Story.step()`, recording the observation it acted on and the choice.
|
|
67
|
+
* Choice values are copied before the action runs.
|
|
67
68
|
* Await each step before the next: steps are not queued.
|
|
68
69
|
*/
|
|
69
70
|
step(id: string, value?: unknown): Promise<Observation>;
|
package/types/index.d.ts
CHANGED
|
@@ -905,9 +905,12 @@ export interface StoryAPI {
|
|
|
905
905
|
/**
|
|
906
906
|
* Wait for the actions performed with `performAction()` that are still
|
|
907
907
|
* running, then for the next frame's actions to be registered, and return
|
|
908
|
-
* them.
|
|
908
|
+
* them. Set `includeRunningActions: false` inside an asynchronous action
|
|
909
|
+
* callback to flush rendering without waiting for the callback itself.
|
|
909
910
|
*/
|
|
910
|
-
waitForActions(
|
|
911
|
+
waitForActions(options?: {
|
|
912
|
+
includeRunningActions?: boolean;
|
|
913
|
+
}): Promise<StoryAction[]>;
|
|
911
914
|
|
|
912
915
|
/**
|
|
913
916
|
* What the story shows and offers now: the passage text, the
|