@rohal12/spindle 0.64.31 → 0.64.33
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 +477 -253
- package/dist/pkg/story-variables.js +357 -255
- package/package.json +1 -1
- package/src/code-check.ts +67 -13
- package/src/components/macros/Checkbox.tsx +10 -6
- package/src/components/macros/Listbox.tsx +11 -7
- package/src/components/macros/Radiobutton.tsx +25 -6
- package/src/components/macros/input-macro.ts +190 -9
- package/src/components/macros/macro-args.ts +29 -10
- package/src/define-macro.ts +2 -0
- package/src/format/format-passage.ts +38 -17
- package/src/hooks/use-modal-focus.ts +23 -10
- package/src/js-bindings.ts +41 -23
- package/src/markup/ast.ts +6 -1
- package/src/markup/code-end.ts +23 -5
- package/src/markup/pair.ts +10 -2
- package/src/markup/parse.ts +40 -1
- package/src/markup/render.tsx +1 -1
- package/src/markup/spindle.d.peggy.ts +2 -0
- package/src/markup/spindle.peggy +5 -6
- package/src/markup/validate.ts +3 -0
- package/src/observe/observable-text.ts +2 -1
- package/src/story-api.ts +6 -1
- package/src/tooling.ts +1 -0
- package/src/utils/variable-writes.ts +24 -0
- package/types/tooling.d.ts +11 -2
|
@@ -189,6 +189,17 @@ function joinSpans(
|
|
|
189
189
|
return out + text.slice(last);
|
|
190
190
|
}
|
|
191
191
|
|
|
192
|
+
/** Collapse the trailing blank lines without probing every whitespace suffix. */
|
|
193
|
+
function trimBlankLines(text: string): string {
|
|
194
|
+
let end = text.length;
|
|
195
|
+
let newline = false;
|
|
196
|
+
while (end > 0 && /[ \t\n]/.test(text[end - 1]!)) {
|
|
197
|
+
newline ||= text[end - 1] === '\n';
|
|
198
|
+
end--;
|
|
199
|
+
}
|
|
200
|
+
return newline ? text.slice(0, end) + '\n' : text;
|
|
201
|
+
}
|
|
202
|
+
|
|
192
203
|
async function formatLf(
|
|
193
204
|
text: string,
|
|
194
205
|
options: PassageFormatOptions,
|
|
@@ -201,10 +212,19 @@ async function formatLf(
|
|
|
201
212
|
const format = options.kind === 'script' ? formatJS : formatCSS;
|
|
202
213
|
const code = body.trim();
|
|
203
214
|
const formatted = await format(code, prettier, prettierOptions);
|
|
204
|
-
return (formatted ?? code).trim()
|
|
215
|
+
return (formatted ?? code).trim() + '\n';
|
|
205
216
|
}
|
|
206
217
|
|
|
207
218
|
const isBlock = options.isBlock ?? defaultIsBlock(body, options.blockMacros);
|
|
219
|
+
// Pairing retains a tolerant tree beyond the runtime's nesting limit.
|
|
220
|
+
// Leave unsupported passages intact before any recursive formatter walks.
|
|
221
|
+
if (
|
|
222
|
+
analyzeMarkup(body, { hooks: { isBlock } }).errors.some(
|
|
223
|
+
(error) => error.code === 'nesting-limit',
|
|
224
|
+
)
|
|
225
|
+
) {
|
|
226
|
+
return text;
|
|
227
|
+
}
|
|
208
228
|
const isDedenting =
|
|
209
229
|
options.isDedentingSubMacro ??
|
|
210
230
|
((name: string) => DEFAULT_DEDENTING.has(name.toLowerCase()));
|
|
@@ -217,17 +237,18 @@ async function formatLf(
|
|
|
217
237
|
const literalMarkers = new Set<string>();
|
|
218
238
|
const fenced = markdownLines(body, isBlock, new Set(['codeFenced']));
|
|
219
239
|
const literalHtml = whitespaceSensitiveLines(body, isBlock);
|
|
220
|
-
body =
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
240
|
+
body = trimBlankLines(
|
|
241
|
+
body
|
|
242
|
+
.split('\n')
|
|
243
|
+
.map((line, index) => {
|
|
244
|
+
if (!fenced.has(index) && !literalHtml.has(index)) return line;
|
|
245
|
+
const marker = `${literalSentinel}L${literalLines.size}${literalSentinel}`;
|
|
246
|
+
literalLines.set(marker, line);
|
|
247
|
+
literalMarkers.add(marker);
|
|
248
|
+
return (line.match(/^[ \t]*/)?.[0] ?? '') + marker;
|
|
249
|
+
})
|
|
250
|
+
.join('\n'),
|
|
251
|
+
);
|
|
231
252
|
const lines: string[] = [];
|
|
232
253
|
/** Indices in lines whose trailing two spaces are a hard line break. */
|
|
233
254
|
const hardBreakLines = new Set<number>();
|
|
@@ -359,12 +380,12 @@ async function formatLf(
|
|
|
359
380
|
// normalized by indentMacros) and ensure single trailing newline
|
|
360
381
|
let output = lines
|
|
361
382
|
.map((l, i) =>
|
|
362
|
-
hardBreakLines.has(i) || preservedLines.has(i)
|
|
363
|
-
? l
|
|
364
|
-
: l.replace(/\s+$/, ''),
|
|
383
|
+
hardBreakLines.has(i) || preservedLines.has(i) ? l : l.trimEnd(),
|
|
365
384
|
)
|
|
366
385
|
.join('\n');
|
|
367
|
-
|
|
386
|
+
let end = output.length;
|
|
387
|
+
while (end > 0 && output[end - 1] === '\n') end--;
|
|
388
|
+
output = output.slice(0, end) + '\n';
|
|
368
389
|
|
|
369
390
|
return replaceAll(
|
|
370
391
|
output
|
|
@@ -613,7 +634,7 @@ function indentMacros(
|
|
|
613
634
|
(body.keepRelative ? line.col - (isRoot ? 0 : body.minCol) : 0);
|
|
614
635
|
}
|
|
615
636
|
outCol.push(col);
|
|
616
|
-
const hardBreak =
|
|
637
|
+
const hardBreak = lines[i]!.endsWith(' ') && info[i + 1] ? ' ' : '';
|
|
617
638
|
result.push(' '.repeat(col) + line.text + hardBreak);
|
|
618
639
|
}
|
|
619
640
|
|
|
@@ -22,6 +22,17 @@ const FOCUSABLE = [
|
|
|
22
22
|
'[tabindex]',
|
|
23
23
|
].join(',');
|
|
24
24
|
|
|
25
|
+
/** Only the first summary child of a details element is its native toggle. */
|
|
26
|
+
function isDetailsToggle(el: HTMLElement): boolean {
|
|
27
|
+
return (
|
|
28
|
+
el.localName === 'summary' &&
|
|
29
|
+
el.parentElement?.localName === 'details' &&
|
|
30
|
+
Array.from(el.parentElement.children).find(
|
|
31
|
+
(c) => c.localName === 'summary',
|
|
32
|
+
) === el
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
25
36
|
/**
|
|
26
37
|
* Whether `el` can be focused at all: shown, rendered, enabled (also by its
|
|
27
38
|
* fieldset) and not inert.
|
|
@@ -38,7 +49,16 @@ function isAvailable(el: HTMLElement): boolean {
|
|
|
38
49
|
if (at.hasAttribute('inert')) return false;
|
|
39
50
|
}
|
|
40
51
|
// Also covers controls disabled by a <fieldset disabled> ancestor
|
|
41
|
-
|
|
52
|
+
if (el.matches(':disabled')) return false;
|
|
53
|
+
// [tabindex] also selects empty/invalid attributes on plain elements.
|
|
54
|
+
// Those do not make the element focusable. Editing hosts can still
|
|
55
|
+
// report tabIndex -1; negative valid tabindexes allow scripted focus.
|
|
56
|
+
return (
|
|
57
|
+
tabIndexAttr(el) !== null ||
|
|
58
|
+
el.tabIndex >= 0 ||
|
|
59
|
+
(el.isContentEditable && !el.parentElement?.isContentEditable) ||
|
|
60
|
+
isDetailsToggle(el)
|
|
61
|
+
);
|
|
42
62
|
}
|
|
43
63
|
|
|
44
64
|
/** The `tabindex` of `el` as an integer, or null when absent or invalid. */
|
|
@@ -92,15 +112,8 @@ function isTabStop(el: HTMLElement, radioStops: RadioStops): boolean {
|
|
|
92
112
|
return false;
|
|
93
113
|
}
|
|
94
114
|
// Only a details element's first summary child is its toggle
|
|
95
|
-
if (el.localName === 'summary')
|
|
96
|
-
|
|
97
|
-
if (
|
|
98
|
-
details?.localName !== 'details' ||
|
|
99
|
-
Array.from(details.children).find((c) => c.localName === 'summary') !== el
|
|
100
|
-
) {
|
|
101
|
-
return tabIndex !== null;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
115
|
+
if (el.localName === 'summary' && !isDetailsToggle(el))
|
|
116
|
+
return tabIndex !== null;
|
|
104
117
|
// localName, not instanceof: elements of an embedded document are of
|
|
105
118
|
// another realm's classes
|
|
106
119
|
const input = el as HTMLInputElement;
|
package/src/js-bindings.ts
CHANGED
|
@@ -336,33 +336,51 @@ export class Bindings {
|
|
|
336
336
|
*/
|
|
337
337
|
private statementEnd(from: number): number {
|
|
338
338
|
const { code } = this;
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
339
|
+
// Suspend enclosing statements on the heap: distributed scripts can
|
|
340
|
+
// contain arbitrarily deep unbraced bodies, even if JS cannot run them.
|
|
341
|
+
const pendingIf: boolean[] = [];
|
|
342
|
+
statements: while (true) {
|
|
343
|
+
const start = this.forward(from);
|
|
344
|
+
const block = this.byOpen.get(start);
|
|
345
|
+
let end: number;
|
|
346
|
+
if (block?.kind === '{') {
|
|
347
|
+
end = Math.min(block.close + 1, code.length);
|
|
348
|
+
} else {
|
|
349
|
+
const word = this.wordAt(start);
|
|
350
|
+
if (/^(?:if|for|while|with)$/.test(word)) {
|
|
351
|
+
let paren = this.forward(start + word.length);
|
|
352
|
+
if (word === 'for' && this.wordAt(paren) === 'await') {
|
|
353
|
+
paren = this.forward(paren + 5);
|
|
354
|
+
}
|
|
355
|
+
const header = this.byOpen.get(paren);
|
|
356
|
+
if (header) {
|
|
357
|
+
pendingIf.push(word === 'if');
|
|
358
|
+
from = header.close + 1;
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
for (end = start; end < code.length; end++) {
|
|
363
|
+
const c = code.charAt(end);
|
|
364
|
+
if (c === ';') {
|
|
365
|
+
end++;
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
if (CLOSERS[c]) break;
|
|
369
|
+
const group = this.byOpen.get(end);
|
|
370
|
+
if (group) end = group.close;
|
|
371
|
+
else if (c === '\n' && this.startsStatement(end)) break;
|
|
372
|
+
}
|
|
347
373
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
const end = this.statementEnd(header.close + 1);
|
|
374
|
+
while (pendingIf.length) {
|
|
375
|
+
const isIf = pendingIf.pop();
|
|
351
376
|
const next = this.forward(end);
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
377
|
+
if (isIf && this.wordAt(next) === 'else') {
|
|
378
|
+
from = next + 4;
|
|
379
|
+
continue statements;
|
|
380
|
+
}
|
|
355
381
|
}
|
|
382
|
+
return end;
|
|
356
383
|
}
|
|
357
|
-
for (let end = start; end < code.length; end++) {
|
|
358
|
-
const c = code.charAt(end);
|
|
359
|
-
if (c === ';') return end + 1;
|
|
360
|
-
if (CLOSERS[c]) return end;
|
|
361
|
-
const group = this.byOpen.get(end);
|
|
362
|
-
if (group) end = group.close;
|
|
363
|
-
else if (c === '\n' && this.startsStatement(end)) return end;
|
|
364
|
-
}
|
|
365
|
-
return code.length;
|
|
366
384
|
}
|
|
367
385
|
|
|
368
386
|
/** Whether the line break at `index` ends a statement, and one follows. */
|
package/src/markup/ast.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { withSelectors, type Selectors, type VariableScope } from './tokens';
|
|
6
6
|
import type { PairedBranch, PairedNode } from './pair';
|
|
7
7
|
import { NameSet } from '../utils/macro-names';
|
|
8
|
-
import { getMacroParameters } from '../registry';
|
|
8
|
+
import { getMacro, getMacroParameters } from '../registry';
|
|
9
9
|
|
|
10
10
|
export interface TextNode {
|
|
11
11
|
type: 'text';
|
|
@@ -108,6 +108,11 @@ export function isBlockMacro(name: string): boolean {
|
|
|
108
108
|
return isBuiltinBlockMacro(name);
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/** Explicit implementations can also be invoked outside a companion owner. */
|
|
112
|
+
export function isStandaloneMacro(name: string): boolean {
|
|
113
|
+
return WIDGETS.has(name) || getMacro(name) !== undefined;
|
|
114
|
+
}
|
|
115
|
+
|
|
111
116
|
/** Built-in defaults without any process-global registrations or widgets. */
|
|
112
117
|
export function isBuiltinBlockMacro(name: string): boolean {
|
|
113
118
|
return BLOCK_MACROS.has(name);
|
package/src/markup/code-end.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { findCodeEnd, parsedCodeEnd } from '../js-lexer';
|
|
2
|
+
import { escapeRegExp } from '../components/macros/arg-utils';
|
|
3
|
+
import { upperBound } from '../utils/sorted';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Where the JavaScript inside markup ends. The grammar (spindle.peggy) leaves
|
|
@@ -161,6 +163,10 @@ export type OnOpenCodeEnd = (input: string, codeStart: number) => void;
|
|
|
161
163
|
* input (see `OnOpenCodeEnd`); the ends it finds are the same.
|
|
162
164
|
*/
|
|
163
165
|
export function jsCodeEnd(onOpenEnd?: OnOpenCodeEnd): CodeEnd {
|
|
166
|
+
// Keep only the latest immutable source. Missing closers are indexed too,
|
|
167
|
+
// so every unclosed raw opener does not search the same suffix again.
|
|
168
|
+
let indexedInput = '';
|
|
169
|
+
const rawClosers = new Map<string, number[]>();
|
|
164
170
|
return {
|
|
165
171
|
closeBrace(input, codeStart, lenientStart) {
|
|
166
172
|
let ranOut = false;
|
|
@@ -172,10 +178,21 @@ export function jsCodeEnd(onOpenEnd?: OnOpenCodeEnd): CodeEnd {
|
|
|
172
178
|
return end;
|
|
173
179
|
},
|
|
174
180
|
rawBodyEnd(input, bodyStart, name) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
181
|
+
if (indexedInput !== input) {
|
|
182
|
+
indexedInput = input;
|
|
183
|
+
rawClosers.clear();
|
|
184
|
+
}
|
|
185
|
+
name = name.toLowerCase();
|
|
186
|
+
const closer = `\\{/${escapeRegExp(name)}\\s*\\}`;
|
|
187
|
+
let candidates = rawClosers.get(name);
|
|
188
|
+
if (!candidates) {
|
|
189
|
+
candidates = Array.from(
|
|
190
|
+
input.matchAll(new RegExp(closer, 'gi')),
|
|
191
|
+
(m) => m.index,
|
|
192
|
+
);
|
|
193
|
+
rawClosers.set(name, candidates);
|
|
194
|
+
}
|
|
195
|
+
const firstAt = candidates[upperBound(candidates, bodyStart - 1)] ?? -1;
|
|
179
196
|
if (firstAt === -1) return -1;
|
|
180
197
|
const at = new RegExp(closer, 'iy');
|
|
181
198
|
let ranOut = false;
|
|
@@ -192,7 +209,8 @@ export function jsCodeEnd(onOpenEnd?: OnOpenCodeEnd): CodeEnd {
|
|
|
192
209
|
// with its `}` follows
|
|
193
210
|
if (
|
|
194
211
|
onOpenEnd &&
|
|
195
|
-
(ranOut ||
|
|
212
|
+
(ranOut ||
|
|
213
|
+
new RegExp(`\\{/${escapeRegExp(name)}\\s*$`, 'i').test(input))
|
|
196
214
|
) {
|
|
197
215
|
onOpenEnd(input, bodyStart);
|
|
198
216
|
}
|
package/src/markup/pair.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { branchParents, getMacroParameters } from '../registry';
|
|
14
14
|
import { sourcePositions } from './line-column';
|
|
15
|
-
import { isBlockMacro, isRawMacro } from './ast';
|
|
15
|
+
import { isBlockMacro, isRawMacro, isStandaloneMacro } from './ast';
|
|
16
16
|
import type { HtmlToken, MacroToken, Span, Token } from './tokens';
|
|
17
17
|
|
|
18
18
|
/** What is wrong with the pairing, by kind. */
|
|
@@ -82,6 +82,8 @@ export interface PairMarkupOptions {
|
|
|
82
82
|
isRaw?(name: string): boolean;
|
|
83
83
|
/** Parents declaring a branching sub-macro (default: registered metadata). */
|
|
84
84
|
branchParents?(name: string): readonly string[];
|
|
85
|
+
/** Whether a companion name also has a standalone macro/widget implementation. */
|
|
86
|
+
isStandalone?(name: string): boolean;
|
|
85
87
|
/**
|
|
86
88
|
* The markup the tokens are of: lets a message say at which line and column
|
|
87
89
|
* an element was opened (else it says at which offset).
|
|
@@ -152,6 +154,7 @@ export function pairMarkup(
|
|
|
152
154
|
): PairedMarkup {
|
|
153
155
|
const isBlock = options.isBlock ?? isBlockMacro;
|
|
154
156
|
const isRaw = options.isRaw ?? isRawMacro;
|
|
157
|
+
const isStandalone = options.isStandalone ?? isStandaloneMacro;
|
|
155
158
|
const parentsOf = (name: string): readonly string[] => {
|
|
156
159
|
if (options.branchParents) return options.branchParents(name);
|
|
157
160
|
const registered = branchParents(name);
|
|
@@ -291,7 +294,12 @@ export function pairMarkup(
|
|
|
291
294
|
if (
|
|
292
295
|
token.type === 'macro' &&
|
|
293
296
|
!token.isClose &&
|
|
294
|
-
parentsOf(token.name).length > 0
|
|
297
|
+
parentsOf(token.name).length > 0 &&
|
|
298
|
+
(!isStandalone(token.name) ||
|
|
299
|
+
(last(stack)?.node.token.type === 'macro' &&
|
|
300
|
+
parentsOf(token.name).includes(
|
|
301
|
+
lowerName(last(stack)!.node.token as MacroToken),
|
|
302
|
+
)))
|
|
295
303
|
) {
|
|
296
304
|
const tag = token as MacroToken;
|
|
297
305
|
const frame = last(stack);
|
package/src/markup/parse.ts
CHANGED
|
@@ -24,6 +24,7 @@ export interface MarkupHooks extends CodeEnd {
|
|
|
24
24
|
/** Whether a macro takes a body closed by `{/name}`. */
|
|
25
25
|
isBlock(name: string): boolean;
|
|
26
26
|
branchParents?(name: string): readonly string[];
|
|
27
|
+
isStandalone?(name: string): boolean;
|
|
27
28
|
/** Whether a macro's body is JavaScript, kept verbatim (`{do}`). */
|
|
28
29
|
isRaw(name: string): boolean;
|
|
29
30
|
/** Whether an attribute's value is code (`onclick`), not markup. */
|
|
@@ -93,6 +94,7 @@ function run(
|
|
|
93
94
|
startRule: 'Tokens' | 'SelectorsPrefix',
|
|
94
95
|
options: ParseMarkupOptions,
|
|
95
96
|
completion = false,
|
|
97
|
+
findDelimiter = indexDelimiters(source),
|
|
96
98
|
): unknown {
|
|
97
99
|
const hooks = { ...defaultHooks, ...options.hooks };
|
|
98
100
|
if (!options.hooks?.closeBrace) {
|
|
@@ -106,6 +108,7 @@ function run(
|
|
|
106
108
|
text: options.text === true,
|
|
107
109
|
hooks,
|
|
108
110
|
completion,
|
|
111
|
+
findDelimiter,
|
|
109
112
|
});
|
|
110
113
|
} catch (err) {
|
|
111
114
|
const { location, code } = err as {
|
|
@@ -128,6 +131,27 @@ function run(
|
|
|
128
131
|
}
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
/** Literal delimiter searches shared by suffix and prefix recovery attempts. */
|
|
135
|
+
function indexDelimiters(source: string) {
|
|
136
|
+
const positions = new Map<string, number[]>();
|
|
137
|
+
return (delimiter: string, from: number, end = source.length): number => {
|
|
138
|
+
let offsets = positions.get(delimiter);
|
|
139
|
+
if (!offsets) {
|
|
140
|
+
offsets = [];
|
|
141
|
+
for (
|
|
142
|
+
let at = source.indexOf(delimiter);
|
|
143
|
+
at >= 0;
|
|
144
|
+
at = source.indexOf(delimiter, at + 1)
|
|
145
|
+
) {
|
|
146
|
+
offsets.push(at);
|
|
147
|
+
}
|
|
148
|
+
positions.set(delimiter, offsets);
|
|
149
|
+
}
|
|
150
|
+
const at = offsets[upperBound(offsets, from - 1)];
|
|
151
|
+
return at !== undefined && at + delimiter.length <= end ? at : -1;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
131
155
|
/** What tolerant parsing makes of markup (see analyzeMarkup). */
|
|
132
156
|
export interface MarkupAnalysis {
|
|
133
157
|
tokens: Token[];
|
|
@@ -156,6 +180,7 @@ export function analyzeMarkup(
|
|
|
156
180
|
isBlock: hooks.isBlock,
|
|
157
181
|
isRaw: hooks.isRaw,
|
|
158
182
|
branchParents: hooks.branchParents,
|
|
183
|
+
isStandalone: hooks.isStandalone,
|
|
159
184
|
source,
|
|
160
185
|
});
|
|
161
186
|
if (errors.length === 0 && paired.errors.length === 0) {
|
|
@@ -289,6 +314,7 @@ export function scanMarkupTolerant(
|
|
|
289
314
|
// Share the index across recovery attempts, whose inputs are suffixes or
|
|
290
315
|
// truncated prefixes. Re-indexing each attempt would itself be quadratic.
|
|
291
316
|
const codeEnd = indexCodeEnds(source);
|
|
317
|
+
const findDelimiter = indexDelimiters(source);
|
|
292
318
|
const customCloseBrace = options.hooks?.closeBrace;
|
|
293
319
|
const lastBrace = source.lastIndexOf('}');
|
|
294
320
|
/** `text` shifted by `base`, tokenized: the tokens or the error. */
|
|
@@ -309,7 +335,20 @@ export function scanMarkupTolerant(
|
|
|
309
335
|
};
|
|
310
336
|
try {
|
|
311
337
|
return (
|
|
312
|
-
run(
|
|
338
|
+
run(
|
|
339
|
+
text,
|
|
340
|
+
'Tokens',
|
|
341
|
+
{ ...options, hooks },
|
|
342
|
+
completion,
|
|
343
|
+
(delimiter, from) => {
|
|
344
|
+
const at = findDelimiter(
|
|
345
|
+
delimiter,
|
|
346
|
+
base + from,
|
|
347
|
+
base + text.length,
|
|
348
|
+
);
|
|
349
|
+
return at < 0 ? -1 : at - base;
|
|
350
|
+
},
|
|
351
|
+
) as Token[]
|
|
313
352
|
).map((token) => shiftOffsets(token, base));
|
|
314
353
|
} catch (error) {
|
|
315
354
|
if (!(error instanceof MarkupError)) throw error;
|
package/src/markup/render.tsx
CHANGED
|
@@ -972,7 +972,7 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
972
972
|
);
|
|
973
973
|
}
|
|
974
974
|
const element = authorElement(
|
|
975
|
-
namespace === 'svg' ? svgTagName(node.tag) :
|
|
975
|
+
namespace === 'svg' ? svgTagName(node.tag) : tag,
|
|
976
976
|
props,
|
|
977
977
|
children,
|
|
978
978
|
namespace === 'math',
|
|
@@ -20,6 +20,8 @@ export interface ParseOptions {
|
|
|
20
20
|
text: boolean;
|
|
21
21
|
/** What the grammar leaves to code (see parse.ts MarkupHooks). */
|
|
22
22
|
hooks: unknown;
|
|
23
|
+
/** Indexed literal search, relative to this parse attempt. */
|
|
24
|
+
findDelimiter: (delimiter: string, from: number) => number;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/**
|
package/src/markup/spindle.peggy
CHANGED
|
@@ -133,9 +133,8 @@ 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
|
-
//
|
|
137
|
-
|
|
138
|
-
const lastCommentClose = input.lastIndexOf('-->');
|
|
136
|
+
// Indexed by parse.ts across recovery suffixes and truncated prefixes.
|
|
137
|
+
const findDelimiter = options.findDelimiter;
|
|
139
138
|
/** End of the stretch a `.|n|` repetition consumes (see skipTo). */
|
|
140
139
|
let skipEnd = -1;
|
|
141
140
|
/** Whether the attribute whose value is being read holds code. */
|
|
@@ -252,7 +251,7 @@ TextRun
|
|
|
252
251
|
* rendering drops it. (An unclosed `<!--` is ordinary text.)
|
|
253
252
|
*/
|
|
254
253
|
Comment
|
|
255
|
-
= value:$("<!--" (">" / "->" / &{ return completionMode || offset()
|
|
254
|
+
= value:$("<!--" (">" / "->" / &{ return completionMode || findDelimiter('-->', offset()) >= 0; } (!"-->" .)* ("-->" / OpenEnd))) {
|
|
256
255
|
const node = lexeme('text', location(), { value });
|
|
257
256
|
node.comment = true;
|
|
258
257
|
return node;
|
|
@@ -303,8 +302,8 @@ LinkLexeme
|
|
|
303
302
|
*/
|
|
304
303
|
LinkText
|
|
305
304
|
= start:Here &{
|
|
306
|
-
const close =
|
|
307
|
-
const nested =
|
|
305
|
+
const close = findDelimiter(']]', start);
|
|
306
|
+
const nested = findDelimiter('[[', start);
|
|
308
307
|
linkTextEnd = Math.min(close < 0 ? input.length : close, nested < 0 ? input.length : nested);
|
|
309
308
|
return true;
|
|
310
309
|
}
|
package/src/markup/validate.ts
CHANGED
|
@@ -220,6 +220,9 @@ export function validateMarkup(
|
|
|
220
220
|
ReturnType<typeof sourcePositions>
|
|
221
221
|
>();
|
|
222
222
|
const hooks = {
|
|
223
|
+
...(options.parametersOf?.pairing?.isStandalone && {
|
|
224
|
+
isStandalone: options.parametersOf.pairing.isStandalone,
|
|
225
|
+
}),
|
|
223
226
|
...(options.isBlockMacro && { isBlock: options.isBlockMacro }),
|
|
224
227
|
...(options.branchParents && { branchParents: options.branchParents }),
|
|
225
228
|
isRaw: (name: string) =>
|
package/src/story-api.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { useStoryStore, trackRuntimeUnsub, recordHookWrite } from './store';
|
|
2
|
+
import { notifyVariableWrite } from './utils/variable-writes';
|
|
2
3
|
import type { StoryState, VariableNamespaces } from './store';
|
|
3
4
|
import { freeze } from 'immer';
|
|
4
5
|
import {
|
|
@@ -350,7 +351,11 @@ function createStoryAPI(): StoryAPI {
|
|
|
350
351
|
for (const [name] of entries) {
|
|
351
352
|
const { isTransient, key } = parseName(name);
|
|
352
353
|
warnIfUndeclared(isTransient, key);
|
|
353
|
-
if (!isTransient)
|
|
354
|
+
if (!isTransient) {
|
|
355
|
+
const path = key.split('.');
|
|
356
|
+
recordHookWrite(path);
|
|
357
|
+
notifyVariableWrite(path);
|
|
358
|
+
}
|
|
354
359
|
}
|
|
355
360
|
// One store update for all keys, in program order with any running
|
|
356
361
|
// mutation. Nested writes use the mutation merge to preserve aliases.
|
package/src/tooling.ts
CHANGED
|
@@ -249,6 +249,7 @@ function tokenizationOptions(
|
|
|
249
249
|
isRawMacro(name, parametersOf.widget?.(name) !== undefined),
|
|
250
250
|
isBlock: blockLookup(macros),
|
|
251
251
|
branchParents: branchParentLookup(macros),
|
|
252
|
+
isStandalone: parametersOf.pairing!.isStandalone,
|
|
252
253
|
...options.hooks,
|
|
253
254
|
},
|
|
254
255
|
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Explicit writes, including assignments that leave a binding unchanged. */
|
|
2
|
+
const listeners = new Set<(path: readonly string[]) => void>();
|
|
3
|
+
|
|
4
|
+
export function notifyVariableWrite(path: readonly string[]): void {
|
|
5
|
+
for (const listener of listeners) listener(path);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** Observe writes to a binding, its parent, or one of its children. */
|
|
9
|
+
export function subscribeVariableWrites(
|
|
10
|
+
binding: readonly string[],
|
|
11
|
+
onWrite: () => void,
|
|
12
|
+
): () => void {
|
|
13
|
+
const listener = (path: readonly string[]) => {
|
|
14
|
+
const length = Math.min(path.length, binding.length);
|
|
15
|
+
for (let i = 0; i < length; i++) {
|
|
16
|
+
if (path[i] !== binding[i]) return;
|
|
17
|
+
}
|
|
18
|
+
onWrite();
|
|
19
|
+
};
|
|
20
|
+
listeners.add(listener);
|
|
21
|
+
return () => {
|
|
22
|
+
listeners.delete(listener);
|
|
23
|
+
};
|
|
24
|
+
}
|
package/types/tooling.d.ts
CHANGED
|
@@ -866,13 +866,14 @@ export interface PieceBase {
|
|
|
866
866
|
* For the code or text of a quoted string with escapes (a `\"`), whose
|
|
867
867
|
* characters are not where `offset` plus their index says: the offset of
|
|
868
868
|
* each character in the source, and of the end of the text, so that
|
|
869
|
-
* `sourceOffsets[i]` is where character `i` of `code
|
|
869
|
+
* `sourceOffsets[i]` is where character `i` of `code`, `text` or a passage
|
|
870
|
+
* `name` is. The final entry maps the end boundary. Absent
|
|
870
871
|
* when `offset + i` is (see {@link pieceOffset}).
|
|
871
872
|
*/
|
|
872
873
|
sourceOffsets?: readonly number[];
|
|
873
874
|
}
|
|
874
875
|
|
|
875
|
-
/** Where
|
|
876
|
+
/** Where boundary `index` of a piece's code, text or name is in the source. */
|
|
876
877
|
export declare function pieceOffset(piece: PieceBase, index: number): number;
|
|
877
878
|
|
|
878
879
|
/**
|
|
@@ -901,6 +902,12 @@ export interface CodePiece extends PieceBase {
|
|
|
901
902
|
export interface PassagePiece extends PieceBase {
|
|
902
903
|
kind: 'passage';
|
|
903
904
|
name: string;
|
|
905
|
+
/**
|
|
906
|
+
* Decoded name boundaries in the original source, including the end before
|
|
907
|
+
* closing quotes. Quotes/escapes do not change the whole-argument offset
|
|
908
|
+
* and length. Both UTF-16 units of a code-point escape map to its start.
|
|
909
|
+
*/
|
|
910
|
+
sourceOffsets?: readonly number[];
|
|
904
911
|
/** A known navigation target, rather than included/dialog content. */
|
|
905
912
|
navigation?: true;
|
|
906
913
|
/** The markup it is in, for an error: `[[Go->Hall]]`. */
|
|
@@ -1064,6 +1071,8 @@ export interface PairMarkupOptions {
|
|
|
1064
1071
|
isRaw?(name: string): boolean;
|
|
1065
1072
|
/** Parents declaring a branching sub-macro (default: registered metadata). */
|
|
1066
1073
|
branchParents?(name: string): readonly string[];
|
|
1074
|
+
/** Whether a companion name also has a standalone macro/widget implementation. */
|
|
1075
|
+
isStandalone?(name: string): boolean;
|
|
1067
1076
|
/**
|
|
1068
1077
|
* The markup the tokens are of: lets a message say at which line and column
|
|
1069
1078
|
* an element was opened (else it says at which offset).
|