@rohal12/spindle 0.64.8 → 0.64.9
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 +192 -169
- package/dist/pkg/story-variables.js +323 -163
- 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/markdown.ts +10 -2
- package/src/markup/render.tsx +2 -2
- package/src/markup/spindle.peggy +4 -1
- package/src/twine-editor/highlight.ts +25 -2
- package/src/twine-editor/mode.ts +14 -4
- package/types/headless.d.ts +1 -0
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) });
|
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
|
@@ -903,8 +903,6 @@ function renderMacro(node: MacroNode, key: string) {
|
|
|
903
903
|
);
|
|
904
904
|
}
|
|
905
905
|
|
|
906
|
-
if (isSubMacro(node.name)) return null;
|
|
907
|
-
|
|
908
906
|
const Component = getMacro(node.name);
|
|
909
907
|
if (Component) {
|
|
910
908
|
return (
|
|
@@ -917,6 +915,8 @@ function renderMacro(node: MacroNode, key: string) {
|
|
|
917
915
|
);
|
|
918
916
|
}
|
|
919
917
|
|
|
918
|
+
if (isSubMacro(node.name)) return null;
|
|
919
|
+
|
|
920
920
|
return (
|
|
921
921
|
<span
|
|
922
922
|
key={key}
|
package/src/markup/spindle.peggy
CHANGED
|
@@ -133,6 +133,9 @@ 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. */
|
|
@@ -246,7 +249,7 @@ TextRun
|
|
|
246
249
|
* rendering drops it. (An unclosed `<!--` is ordinary text.)
|
|
247
250
|
*/
|
|
248
251
|
Comment
|
|
249
|
-
= value:$("<!--" (">" / "->" / (!"-->" .)* ("-->" / OpenEnd))) {
|
|
252
|
+
= value:$("<!--" (">" / "->" / &{ return completionMode || offset() <= lastCommentClose; } (!"-->" .)* ("-->" / OpenEnd))) {
|
|
250
253
|
const node = lexeme('text', location(), { value });
|
|
251
254
|
node.comment = true;
|
|
252
255
|
return node;
|
|
@@ -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,
|
|
@@ -15,6 +16,9 @@ import { scanMarkupTolerant, tokenizeMarkupTolerant } from '../markup/parse';
|
|
|
15
16
|
type ScannedMarkup = ReturnType<typeof scanMarkupTolerant>;
|
|
16
17
|
import { jsCodeEnd } from '../markup/code-end';
|
|
17
18
|
import { builtinMacros } from './builtin-macros';
|
|
19
|
+
import { parameterLookup } from '../code-check';
|
|
20
|
+
|
|
21
|
+
const pairing = parameterLookup(builtinMacros).pairing;
|
|
18
22
|
|
|
19
23
|
/** The CodeMirror 5 token names Twine themes (see the design spec's table). */
|
|
20
24
|
export type CmStyle =
|
|
@@ -458,14 +462,33 @@ export function openConstructStart(text: string): number {
|
|
|
458
462
|
export function highlightWithOpenStart(text: string): {
|
|
459
463
|
spans: StyledSpan[];
|
|
460
464
|
openStart: number;
|
|
465
|
+
context: string;
|
|
461
466
|
} {
|
|
462
467
|
try {
|
|
463
468
|
const reading = read(text);
|
|
469
|
+
const openStart = openStartOf(text, reading);
|
|
470
|
+
// Retain only the enclosing openers before the lexical tail. Branch
|
|
471
|
+
// arguments depend on their parent even when it began on an earlier
|
|
472
|
+
// line; retaining the whole body would make long blocks quadratic (#699).
|
|
473
|
+
const before = openStart < 0 ? text.length : openStart;
|
|
474
|
+
const { errors } = pairMarkup(
|
|
475
|
+
reading.tokens.filter((token) => token.end <= before),
|
|
476
|
+
pairing,
|
|
477
|
+
);
|
|
478
|
+
const openers = errors
|
|
479
|
+
.filter((error) => error.code === 'unclosed-block')
|
|
480
|
+
.sort((a, b) => a.start - b.start)
|
|
481
|
+
.map((error) => text.slice(error.start, error.end));
|
|
464
482
|
return {
|
|
465
483
|
spans: spansOf(text, reading),
|
|
466
|
-
openStart
|
|
484
|
+
openStart,
|
|
485
|
+
context: openers.length ? openers.join('\n') + '\n' : '',
|
|
467
486
|
};
|
|
468
487
|
} catch {
|
|
469
|
-
return {
|
|
488
|
+
return {
|
|
489
|
+
spans: highlightSpans(text),
|
|
490
|
+
openStart: openConstructStart(text),
|
|
491
|
+
context: '',
|
|
492
|
+
};
|
|
470
493
|
}
|
|
471
494
|
}
|
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/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>;
|