@rohal12/spindle 0.60.2 → 0.60.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 +18651 -18517
- package/dist/pkg/story-variables.js +11018 -154
- package/package.json +1 -1
- package/src/action-registry.ts +43 -3
- package/src/components/PassageDialog.tsx +29 -27
- package/src/components/dialog-close-context.ts +5 -1
- package/src/components/macros/Button.tsx +3 -3
- package/src/components/macros/Checkbox.tsx +3 -2
- package/src/components/macros/Cycle.tsx +3 -4
- package/src/components/macros/Dialog.tsx +3 -3
- package/src/components/macros/Listbox.tsx +3 -4
- package/src/components/macros/MacroLink.tsx +3 -3
- package/src/components/macros/MenubarButtons.tsx +3 -3
- package/src/components/macros/PassageDisplay.tsx +14 -9
- package/src/components/macros/Radiobutton.tsx +3 -2
- package/src/components/macros/detached-body.tsx +1 -4
- package/src/components/macros/input-macro.ts +10 -8
- package/src/define-macro.ts +0 -4
- package/src/execute-mutation.ts +10 -6
- package/src/format/fenced-code.ts +59 -0
- package/src/format/format-passage.ts +103 -12
- package/src/hooks/use-action.ts +44 -4
- package/src/hooks/use-modal-focus.ts +1 -30
- package/src/markup/ast.ts +4 -3
- package/src/markup/pair.ts +22 -9
- package/src/markup/parse.ts +2 -0
- package/src/markup/validate.ts +5 -3
- package/src/observe/observable-text.ts +6 -24
- package/src/observe/observe.ts +4 -1
- package/src/registry.ts +36 -1
- package/src/saves/save-manager.ts +48 -7
- package/src/saves/storage.ts +3 -1
- package/src/saves/types.ts +1 -0
- package/src/store.ts +1 -1
- package/src/story-init.ts +6 -1
- package/src/structural.ts +126 -66
- package/src/tooling.ts +2 -0
- package/src/utils/element-visibility.ts +61 -0
- package/types/index.d.ts +2 -0
- package/types/tooling.d.ts +2 -0
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { isBlockMacro } from '../markup/ast';
|
|
2
|
-
import { tokenizeMarkupTolerant } from '../markup/parse';
|
|
2
|
+
import { analyzeMarkup, tokenizeMarkupTolerant } from '../markup/parse';
|
|
3
|
+
import type { PairedNode } from '../markup/pair';
|
|
3
4
|
import type { MacroToken } from '../markup/tokens';
|
|
4
5
|
import { segmentRegions } from './segment';
|
|
6
|
+
import { fencedCodeLines } from './fenced-code';
|
|
5
7
|
import {
|
|
6
8
|
formatJS,
|
|
7
9
|
formatCSS,
|
|
@@ -193,7 +195,7 @@ async function formatLf(
|
|
|
193
195
|
): Promise<string> {
|
|
194
196
|
const { prettier, prettierOptions } = options;
|
|
195
197
|
// Trailing blank lines are not part of the passage's text
|
|
196
|
-
|
|
198
|
+
let body = text;
|
|
197
199
|
|
|
198
200
|
if (options.kind === 'script' || options.kind === 'stylesheet') {
|
|
199
201
|
const format = options.kind === 'script' ? formatJS : formatCSS;
|
|
@@ -207,9 +209,29 @@ async function formatLf(
|
|
|
207
209
|
options.isDedentingSubMacro ??
|
|
208
210
|
((name: string) => DEFAULT_DEDENTING.has(name.toLowerCase()));
|
|
209
211
|
const sentinel = newlineSentinel(body);
|
|
212
|
+
// Hide literal lines before any token joining, HTML segmentation or prose
|
|
213
|
+
// formatting. Restore whole lines, including their original indentation.
|
|
214
|
+
// Region newline decoding must not consume literal-line placeholders.
|
|
215
|
+
const literalSentinel = newlineSentinel(body + sentinel);
|
|
216
|
+
const literalLines = new Map<string, string>();
|
|
217
|
+
const literalMarkers = new Set<string>();
|
|
218
|
+
const fenced = fencedCodeLines(body, isBlock);
|
|
219
|
+
const literalHtml = whitespaceSensitiveLines(body, isBlock);
|
|
220
|
+
body = body
|
|
221
|
+
.split('\n')
|
|
222
|
+
.map((line, index) => {
|
|
223
|
+
if (!fenced.has(index) && !literalHtml.has(index)) return line;
|
|
224
|
+
const marker = `${literalSentinel}L${literalLines.size}${literalSentinel}`;
|
|
225
|
+
literalLines.set(marker, line);
|
|
226
|
+
literalMarkers.add(marker);
|
|
227
|
+
return (line.match(/^[ \t]*/)?.[0] ?? '') + marker;
|
|
228
|
+
})
|
|
229
|
+
.join('\n')
|
|
230
|
+
.replace(/(?:[ \t]*\n)+[ \t]*$/, '\n');
|
|
210
231
|
const lines: string[] = [];
|
|
211
232
|
/** Indices in lines whose trailing two spaces are a hard line break. */
|
|
212
233
|
const hardBreakLines = new Set<number>();
|
|
234
|
+
const preservedLines = new Set<number>();
|
|
213
235
|
|
|
214
236
|
// Segment into regions. Multiline tokens are joined onto
|
|
215
237
|
// one line first, so their inner lines are never taken for markup.
|
|
@@ -264,7 +286,12 @@ async function formatLf(
|
|
|
264
286
|
// Whitespace that renders as written: the runtime keeps a line break
|
|
265
287
|
// Prettier puts after `<pre>` (a browser's parser drops it), and
|
|
266
288
|
// Prettier cannot see a `white-space` style. Leave such HTML as written.
|
|
267
|
-
if (
|
|
289
|
+
if (
|
|
290
|
+
WHITESPACE_AS_WRITTEN.test(htmlText) ||
|
|
291
|
+
region.lines.some((line) => literalLines.has(line.trim()))
|
|
292
|
+
) {
|
|
293
|
+
for (let i = 0; i < region.lines.length; i++)
|
|
294
|
+
preservedLines.add(lines.length + i);
|
|
268
295
|
lines.push(...region.lines);
|
|
269
296
|
continue;
|
|
270
297
|
}
|
|
@@ -302,9 +329,14 @@ async function formatLf(
|
|
|
302
329
|
}
|
|
303
330
|
|
|
304
331
|
// Spindle/markdown region — apply macro indentation
|
|
305
|
-
let indented = indentMacros(
|
|
332
|
+
let indented = indentMacros(
|
|
333
|
+
region.lines,
|
|
334
|
+
isBlock,
|
|
335
|
+
isDedenting,
|
|
336
|
+
literalMarkers,
|
|
337
|
+
);
|
|
306
338
|
if (options.maxLineLength) {
|
|
307
|
-
indented = wrapLines(indented, options.maxLineLength);
|
|
339
|
+
indented = wrapLines(indented, options.maxLineLength, literalLines);
|
|
308
340
|
}
|
|
309
341
|
for (const line of indented) {
|
|
310
342
|
if (line.endsWith(' ')) hardBreakLines.add(lines.length);
|
|
@@ -314,16 +346,59 @@ async function formatLf(
|
|
|
314
346
|
// Strip trailing whitespace (except Markdown hard breaks, already
|
|
315
347
|
// normalized by indentMacros) and ensure single trailing newline
|
|
316
348
|
let output = lines
|
|
317
|
-
.map((l, i) =>
|
|
349
|
+
.map((l, i) =>
|
|
350
|
+
hardBreakLines.has(i) || preservedLines.has(i)
|
|
351
|
+
? l
|
|
352
|
+
: l.replace(/\s+$/, ''),
|
|
353
|
+
)
|
|
318
354
|
.join('\n');
|
|
319
355
|
output = output.replace(/\n*$/, '\n');
|
|
320
356
|
|
|
321
|
-
return replaceAll(
|
|
357
|
+
return replaceAll(
|
|
358
|
+
output
|
|
359
|
+
.split('\n')
|
|
360
|
+
.map((line) => literalLines.get(line.trim()) ?? line)
|
|
361
|
+
.join('\n'),
|
|
362
|
+
sentinel,
|
|
363
|
+
'\n',
|
|
364
|
+
);
|
|
322
365
|
}
|
|
323
366
|
|
|
324
367
|
/** HTML whose whitespace renders as written: a `<pre>` or `<textarea>`, or a `white-space` style. */
|
|
325
368
|
const WHITESPACE_AS_WRITTEN = /<(?:pre|textarea)[\s>]|white-space\s*:/i;
|
|
326
369
|
|
|
370
|
+
/** Preserve whole literal elements, including openers beside nested tags. */
|
|
371
|
+
function whitespaceSensitiveLines(
|
|
372
|
+
source: string,
|
|
373
|
+
isBlock: (name: string) => boolean,
|
|
374
|
+
): Set<number> {
|
|
375
|
+
const lines = new Set<number>();
|
|
376
|
+
if (!WHITESPACE_AS_WRITTEN.test(source)) return lines;
|
|
377
|
+
const { nodes } = analyzeMarkup(source, { hooks: { isBlock } });
|
|
378
|
+
function visit(nodes: PairedNode[]): void {
|
|
379
|
+
for (const node of nodes) {
|
|
380
|
+
const { token, body } = node;
|
|
381
|
+
if (
|
|
382
|
+
token.type === 'html' &&
|
|
383
|
+
(['pre', 'textarea'].includes(token.tag.toLowerCase()) ||
|
|
384
|
+
Object.entries(token.attributes).some(
|
|
385
|
+
([name, value]) =>
|
|
386
|
+
name.toLowerCase() === 'style' && /white-space\s*:/i.test(value),
|
|
387
|
+
))
|
|
388
|
+
) {
|
|
389
|
+
const first = source.slice(0, node.start).split('\n').length - 1;
|
|
390
|
+
const last = source.slice(0, node.end - 1).split('\n').length - 1;
|
|
391
|
+
for (let line = first; line <= last; line++) lines.add(line);
|
|
392
|
+
} else if (body) {
|
|
393
|
+
visit(body.children);
|
|
394
|
+
for (const branch of body.branches) visit(branch.children);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
visit(nodes);
|
|
399
|
+
return lines;
|
|
400
|
+
}
|
|
401
|
+
|
|
327
402
|
/** A Markdown list item line (`- a`, `* a`, `+ a`, `1. a`, `1) a`). */
|
|
328
403
|
const LIST_ITEM_REGEX = /^(?:[-+*]|\d{1,9}[.)])(?:[ \t]|$)/;
|
|
329
404
|
|
|
@@ -342,6 +417,8 @@ interface MacroBody {
|
|
|
342
417
|
minCol: number;
|
|
343
418
|
/** True if leading indentation may be Markdown syntax (lists, code fences). */
|
|
344
419
|
keepRelative: boolean;
|
|
420
|
+
/** Literal lines retain the indentation of their owning Markdown document. */
|
|
421
|
+
keepOriginal?: boolean;
|
|
345
422
|
}
|
|
346
423
|
|
|
347
424
|
/**
|
|
@@ -363,6 +440,7 @@ function indentMacros(
|
|
|
363
440
|
lines: string[],
|
|
364
441
|
isBlock: (name: string) => boolean,
|
|
365
442
|
isDedenting: (name: string) => boolean,
|
|
443
|
+
literalMarkers: ReadonlySet<string>,
|
|
366
444
|
): string[] {
|
|
367
445
|
const macrosOnLine = macroTokensByLine(lines);
|
|
368
446
|
// Pass 1: assign each line to the body (Markdown document) it belongs to.
|
|
@@ -390,6 +468,7 @@ function indentMacros(
|
|
|
390
468
|
body.minCol = Math.min(body.minCol, col);
|
|
391
469
|
if (LIST_ITEM_REGEX.test(text) || CODE_FENCE_REGEX.test(text))
|
|
392
470
|
body.keepRelative = true;
|
|
471
|
+
if (literalMarkers.has(text)) body.keepOriginal = true;
|
|
393
472
|
info.push({ body: currentBody(), col, text });
|
|
394
473
|
};
|
|
395
474
|
|
|
@@ -465,8 +544,10 @@ function indentMacros(
|
|
|
465
544
|
const body = bodies[line.body]!;
|
|
466
545
|
const isRoot = body.openLine === -1;
|
|
467
546
|
const base = isRoot ? 0 : outCol[body.openLine]! + 2;
|
|
468
|
-
col =
|
|
469
|
-
|
|
547
|
+
col = body.keepOriginal
|
|
548
|
+
? line.col
|
|
549
|
+
: base +
|
|
550
|
+
(body.keepRelative ? line.col - (isRoot ? 0 : body.minCol) : 0);
|
|
470
551
|
}
|
|
471
552
|
outCol.push(col);
|
|
472
553
|
const hardBreak = / {2,}$/.test(lines[i]!) && info[i + 1] ? ' ' : '';
|
|
@@ -523,13 +604,21 @@ function indentColumn(line: string): number {
|
|
|
523
604
|
* Preserves leading indentation. Skips pure macro lines and choice/link
|
|
524
605
|
* lines, where wrapping would break syntax.
|
|
525
606
|
*/
|
|
526
|
-
function wrapLines(
|
|
607
|
+
function wrapLines(
|
|
608
|
+
lines: string[],
|
|
609
|
+
maxLen: number,
|
|
610
|
+
literalLines: ReadonlyMap<string, string>,
|
|
611
|
+
): string[] {
|
|
527
612
|
const result: string[] = [];
|
|
528
613
|
const javaScript = doBodyLines(lines);
|
|
529
614
|
|
|
530
615
|
for (const [index, line] of lines.entries()) {
|
|
531
616
|
// A `{do}` body is JavaScript: a line break there can change the program
|
|
532
|
-
if (
|
|
617
|
+
if (
|
|
618
|
+
line.length <= maxLen ||
|
|
619
|
+
javaScript.has(index) ||
|
|
620
|
+
literalLines.has(line.trim())
|
|
621
|
+
) {
|
|
533
622
|
result.push(line);
|
|
534
623
|
continue;
|
|
535
624
|
}
|
|
@@ -587,7 +676,9 @@ function wordWrap(text: string, maxLen: number, indent: string): string[] {
|
|
|
587
676
|
if (token.type === 'text' || token.type === 'html') continue;
|
|
588
677
|
inToken.fill(true, token.start, token.end);
|
|
589
678
|
}
|
|
590
|
-
const room = maxLen - indent
|
|
679
|
+
const room = maxLen - indentColumn(indent);
|
|
680
|
+
// With no room after the prefix, keep the line instead of searching before pos.
|
|
681
|
+
if (room <= 0) return [indent + text];
|
|
591
682
|
let pos = 0;
|
|
592
683
|
|
|
593
684
|
while (pos < text.length) {
|
package/src/hooks/use-action.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createContext } from 'preact';
|
|
2
|
-
import { useContext, useLayoutEffect, useRef } from 'preact/hooks';
|
|
2
|
+
import { useCallback, useContext, useLayoutEffect, useRef } from 'preact/hooks';
|
|
3
3
|
import {
|
|
4
4
|
registerAction,
|
|
5
5
|
generateActionId,
|
|
@@ -7,12 +7,49 @@ import {
|
|
|
7
7
|
type ActionRegistration,
|
|
8
8
|
type StoryAction,
|
|
9
9
|
} from '../action-registry';
|
|
10
|
-
import {
|
|
10
|
+
import {
|
|
11
|
+
DialogCloseContext,
|
|
12
|
+
DialogPanelContext,
|
|
13
|
+
} from '../components/dialog-close-context';
|
|
11
14
|
|
|
12
15
|
/** The action to register, and what its ID is generated from. */
|
|
13
16
|
export interface UseActionOptions extends Omit<StoryAction, 'id'> {
|
|
14
17
|
key: string;
|
|
15
18
|
authorId?: string;
|
|
19
|
+
/** The exact DOM control, including when authored action IDs collide. */
|
|
20
|
+
element?: { readonly current: Element | null };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface ControlAction {
|
|
24
|
+
id: string;
|
|
25
|
+
ref: (element: Element | null) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Register a built-in control with its own stable DOM association. */
|
|
29
|
+
export function useControlAction(opts: UseActionOptions): ControlAction {
|
|
30
|
+
const element = useRef<Element | null>(null);
|
|
31
|
+
const ref = useCallback((value: Element | null) => {
|
|
32
|
+
element.current = value;
|
|
33
|
+
}, []);
|
|
34
|
+
return { id: useAction({ ...opts, element }), ref };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Associate a macro control's authored attributes with its runtime action. */
|
|
38
|
+
export function actionAttributes(
|
|
39
|
+
action: ControlAction,
|
|
40
|
+
control: { id?: string; cls?: string },
|
|
41
|
+
): {
|
|
42
|
+
id?: string;
|
|
43
|
+
class?: string;
|
|
44
|
+
'data-action'?: string;
|
|
45
|
+
ref: ControlAction['ref'];
|
|
46
|
+
} {
|
|
47
|
+
return {
|
|
48
|
+
id: control.id,
|
|
49
|
+
class: control.cls,
|
|
50
|
+
'data-action': action.id || undefined,
|
|
51
|
+
ref: action.ref,
|
|
52
|
+
};
|
|
16
53
|
}
|
|
17
54
|
|
|
18
55
|
/**
|
|
@@ -26,6 +63,7 @@ export function useAction(opts: UseActionOptions): string {
|
|
|
26
63
|
const disabled = useContext(ActionsDisabledContext);
|
|
27
64
|
// The dialog the control renders in, for observe() to scope by
|
|
28
65
|
const dialog = useContext(DialogCloseContext);
|
|
66
|
+
const panel = useContext(DialogPanelContext);
|
|
29
67
|
const idRef = useRef<string>('');
|
|
30
68
|
const performRef = useRef(opts.perform);
|
|
31
69
|
performRef.current = opts.perform;
|
|
@@ -58,11 +96,11 @@ export function useAction(opts: UseActionOptions): string {
|
|
|
58
96
|
const fresh = useRef(false);
|
|
59
97
|
useLayoutEffect(() => {
|
|
60
98
|
if (disabled) return;
|
|
61
|
-
const own = registerAction(buildAction(), dialog);
|
|
99
|
+
const own = registerAction(buildAction(), dialog, panel, opts.element);
|
|
62
100
|
registration.current = own;
|
|
63
101
|
fresh.current = true;
|
|
64
102
|
return () => own.unregister();
|
|
65
|
-
}, [id, disabled, dialog]);
|
|
103
|
+
}, [id, disabled, dialog, panel, opts.element]);
|
|
66
104
|
|
|
67
105
|
// When what the action exposes changes (e.g. an input's value), replace it
|
|
68
106
|
// in place: one `actionsChanged` notification, and it keeps its position in
|
|
@@ -80,6 +118,8 @@ export function useAction(opts: UseActionOptions): string {
|
|
|
80
118
|
id,
|
|
81
119
|
disabled,
|
|
82
120
|
dialog,
|
|
121
|
+
panel,
|
|
122
|
+
opts.element,
|
|
83
123
|
opts.type,
|
|
84
124
|
opts.label,
|
|
85
125
|
opts.target,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isRendered } from '../utils/element-visibility';
|
|
1
2
|
import { useLayoutEffect } from 'preact/hooks';
|
|
2
3
|
import type { RefObject } from 'preact';
|
|
3
4
|
|
|
@@ -16,36 +17,6 @@ const FOCUSABLE = [
|
|
|
16
17
|
'[tabindex]',
|
|
17
18
|
].join(',');
|
|
18
19
|
|
|
19
|
-
/**
|
|
20
|
-
* Whether `el` is in a closed `<details>`, other than in the summary that
|
|
21
|
-
* toggles it: the browser renders none of that content (#389).
|
|
22
|
-
*/
|
|
23
|
-
function inClosedDetails(el: HTMLElement): boolean {
|
|
24
|
-
for (let e = el.parentElement; e; e = e.parentElement) {
|
|
25
|
-
if (e.localName !== 'details' || e.hasAttribute('open')) continue;
|
|
26
|
-
const summary = Array.from(e.children).find(
|
|
27
|
-
(c) => c.localName === 'summary',
|
|
28
|
-
);
|
|
29
|
-
if (!summary?.contains(el)) return true;
|
|
30
|
-
}
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Whether CSS leaves `el` in the tab sequence: not `visibility: hidden`
|
|
36
|
-
* (inherited, so the computed value covers ancestors), not inside a
|
|
37
|
-
* `display: none` element and not in the hidden content of a closed
|
|
38
|
-
* `<details>`.
|
|
39
|
-
*/
|
|
40
|
-
function isRendered(el: HTMLElement): boolean {
|
|
41
|
-
const { visibility } = getComputedStyle(el);
|
|
42
|
-
if (visibility === 'hidden' || visibility === 'collapse') return false;
|
|
43
|
-
for (let e: HTMLElement | null = el; e; e = e.parentElement) {
|
|
44
|
-
if (getComputedStyle(e).display === 'none') return false;
|
|
45
|
-
}
|
|
46
|
-
return !inClosedDetails(el);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
20
|
/**
|
|
50
21
|
* Whether `el` can be focused at all: shown, rendered, enabled (also by its
|
|
51
22
|
* fieldset) and not inert.
|
package/src/markup/ast.ts
CHANGED
|
@@ -180,7 +180,7 @@ function astNode({ token, body }: PairedNode): ASTNode {
|
|
|
180
180
|
};
|
|
181
181
|
if (!body) return withSelectors(node, token);
|
|
182
182
|
const children = buildAst(body.children);
|
|
183
|
-
if (!BRANCHING.has(name)) {
|
|
183
|
+
if (!BRANCHING.has(name) && body.branches.length === 0) {
|
|
184
184
|
node.children = children;
|
|
185
185
|
return withSelectors(node, token);
|
|
186
186
|
}
|
|
@@ -193,13 +193,14 @@ function astNode({ token, body }: PairedNode): ASTNode {
|
|
|
193
193
|
{ rawArgs: tag.rawArgs, children: branchChildren },
|
|
194
194
|
tag,
|
|
195
195
|
);
|
|
196
|
+
if (!BRANCHING.has(name)) node.children = children;
|
|
196
197
|
node.branches = [
|
|
197
|
-
branch(token, children),
|
|
198
|
+
...(BRANCHING.has(name) ? [branch(token, children)] : []),
|
|
198
199
|
...body.branches.map(({ tag, children: c }: PairedBranch) =>
|
|
199
200
|
branch(tag, buildAst(c)),
|
|
200
201
|
),
|
|
201
202
|
];
|
|
202
|
-
return node;
|
|
203
|
+
return BRANCHING.has(name) ? node : withSelectors(node, token);
|
|
203
204
|
}
|
|
204
205
|
}
|
|
205
206
|
}
|
package/src/markup/pair.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* runtime reads it, or, when it matches one further out, closes that one and
|
|
11
11
|
* what was open inside it stays unclosed.
|
|
12
12
|
*/
|
|
13
|
+
import { branchParents } from '../registry';
|
|
13
14
|
import { lineColumn } from './line-column';
|
|
14
15
|
import { isBlockMacro, isRawMacro } from './ast';
|
|
15
16
|
import type { HtmlToken, MacroToken, Span, Token } from './tokens';
|
|
@@ -72,6 +73,8 @@ export interface PairMarkupOptions {
|
|
|
72
73
|
* tag, where the body would start, and the markup after it is not its body.
|
|
73
74
|
*/
|
|
74
75
|
isRaw?(name: string): boolean;
|
|
76
|
+
/** Parents declaring a branching sub-macro (default: registered metadata). */
|
|
77
|
+
branchParents?(name: string): readonly string[];
|
|
75
78
|
/**
|
|
76
79
|
* The markup the tokens are of: lets a message say at which line and column
|
|
77
80
|
* an element was opened (else it says at which offset).
|
|
@@ -96,11 +99,6 @@ export const BRANCH_PARENT: Readonly<Record<string, string>> = {
|
|
|
96
99
|
next: 'timed',
|
|
97
100
|
};
|
|
98
101
|
|
|
99
|
-
const isBranchTag = (token: Token): boolean =>
|
|
100
|
-
token.type === 'macro' &&
|
|
101
|
-
!token.isClose &&
|
|
102
|
-
Object.prototype.hasOwnProperty.call(BRANCH_PARENT, token.name.toLowerCase());
|
|
103
|
-
|
|
104
102
|
const lowerName = (token: MacroToken | HtmlToken) =>
|
|
105
103
|
nameOf(token).toLowerCase();
|
|
106
104
|
|
|
@@ -147,6 +145,15 @@ export function pairMarkup(
|
|
|
147
145
|
): PairedMarkup {
|
|
148
146
|
const isBlock = options.isBlock ?? isBlockMacro;
|
|
149
147
|
const isRaw = options.isRaw ?? isRawMacro;
|
|
148
|
+
const parentsOf = (name: string): readonly string[] => {
|
|
149
|
+
const lower = name.toLowerCase();
|
|
150
|
+
const builtin = Object.prototype.hasOwnProperty.call(BRANCH_PARENT, lower)
|
|
151
|
+
? BRANCH_PARENT[lower]
|
|
152
|
+
: undefined;
|
|
153
|
+
return builtin
|
|
154
|
+
? [builtin]
|
|
155
|
+
: (options.branchParents?.(name) ?? branchParents(name));
|
|
156
|
+
};
|
|
150
157
|
const errors: PairingError[] = [];
|
|
151
158
|
const noticedAtEnd = (tokens[tokens.length - 1]?.end ?? 0) + 1;
|
|
152
159
|
const where = (offset: number) => {
|
|
@@ -177,7 +184,7 @@ export function pairMarkup(
|
|
|
177
184
|
inside: MacroToken | HtmlToken | undefined,
|
|
178
185
|
noticedAt: number,
|
|
179
186
|
) => {
|
|
180
|
-
const parent =
|
|
187
|
+
const parent = parentsOf(tag.name).join(' or ');
|
|
181
188
|
const where = inside ? `, not inside ${tagLabel(inside)}` : '';
|
|
182
189
|
report(
|
|
183
190
|
'misplaced-branch',
|
|
@@ -205,8 +212,10 @@ export function pairMarkup(
|
|
|
205
212
|
const open = node.token as MacroToken | HtmlToken;
|
|
206
213
|
const noticedAt = close ? close.end : noticedAtEnd;
|
|
207
214
|
for (const { tag } of body.branches) {
|
|
208
|
-
|
|
209
|
-
|
|
215
|
+
if (
|
|
216
|
+
open.type !== 'macro' ||
|
|
217
|
+
!parentsOf(tag.name).includes(lowerName(open))
|
|
218
|
+
) {
|
|
210
219
|
misplaced(tag, open, noticedAt);
|
|
211
220
|
}
|
|
212
221
|
}
|
|
@@ -260,7 +269,11 @@ export function pairMarkup(
|
|
|
260
269
|
}
|
|
261
270
|
continue;
|
|
262
271
|
}
|
|
263
|
-
if (
|
|
272
|
+
if (
|
|
273
|
+
token.type === 'macro' &&
|
|
274
|
+
!token.isClose &&
|
|
275
|
+
parentsOf(token.name).length > 0
|
|
276
|
+
) {
|
|
264
277
|
const tag = token as MacroToken;
|
|
265
278
|
const frame = last(stack);
|
|
266
279
|
if (!frame) {
|
package/src/markup/parse.ts
CHANGED
|
@@ -22,6 +22,7 @@ export { lineColumn };
|
|
|
22
22
|
export interface MarkupHooks extends CodeEnd {
|
|
23
23
|
/** Whether a macro takes a body closed by `{/name}`. */
|
|
24
24
|
isBlock(name: string): boolean;
|
|
25
|
+
branchParents?(name: string): readonly string[];
|
|
25
26
|
/** Whether a macro's body is JavaScript, kept verbatim (`{do}`). */
|
|
26
27
|
isRaw(name: string): boolean;
|
|
27
28
|
/** Whether an attribute's value is code (`onclick`), not markup. */
|
|
@@ -144,6 +145,7 @@ export function analyzeMarkup(
|
|
|
144
145
|
const paired = pairMarkup(tokens, {
|
|
145
146
|
isBlock: hooks.isBlock,
|
|
146
147
|
isRaw: hooks.isRaw,
|
|
148
|
+
branchParents: hooks.branchParents,
|
|
147
149
|
source,
|
|
148
150
|
});
|
|
149
151
|
if (errors.length === 0 && paired.errors.length === 0) {
|
package/src/markup/validate.ts
CHANGED
|
@@ -85,6 +85,7 @@ export interface MarkupValidationOptions {
|
|
|
85
85
|
macroNames?: Iterable<string>;
|
|
86
86
|
/** Whether a macro takes a body (default: the registered block macros). */
|
|
87
87
|
isBlockMacro?(name: string): boolean;
|
|
88
|
+
branchParents?(name: string): readonly string[];
|
|
88
89
|
/**
|
|
89
90
|
* Report on only the passages for which this holds (default: all). The
|
|
90
91
|
* others are still read for the widgets they define.
|
|
@@ -201,9 +202,10 @@ export function validateMarkup(
|
|
|
201
202
|
options: MarkupValidationOptions,
|
|
202
203
|
): MarkupDiagnostic[] {
|
|
203
204
|
const diagnostics: MarkupDiagnostic[] = [];
|
|
204
|
-
const hooks =
|
|
205
|
-
|
|
206
|
-
:
|
|
205
|
+
const hooks = {
|
|
206
|
+
...(options.isBlockMacro && { isBlock: options.isBlockMacro }),
|
|
207
|
+
...(options.branchParents && { branchParents: options.branchParents }),
|
|
208
|
+
};
|
|
207
209
|
const report = (
|
|
208
210
|
passage: MarkupPassage,
|
|
209
211
|
start: number,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isHidden } from '../utils/element-visibility';
|
|
1
2
|
import { getAction } from '../action-registry';
|
|
2
3
|
|
|
3
4
|
/** Elements that start and end a line. */
|
|
@@ -20,29 +21,6 @@ const BLOCKS = new Set([
|
|
|
20
21
|
'FOOTER',
|
|
21
22
|
]);
|
|
22
23
|
|
|
23
|
-
/** Elements whose content is never text the reader reads. */
|
|
24
|
-
const NOT_TEXT = new Set(['SCRIPT', 'STYLE', 'TEMPLATE', 'NOSCRIPT']);
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Whether the reader can't see the element. Headless DOMs compute no layout,
|
|
28
|
-
* so only what the markup says counts: the `hidden` attribute, an inline
|
|
29
|
-
* `display: none` and `aria-hidden="true"`.
|
|
30
|
-
*/
|
|
31
|
-
function isHidden(el: Element): boolean {
|
|
32
|
-
return (
|
|
33
|
-
el.hasAttribute('hidden') ||
|
|
34
|
-
el.getAttribute('aria-hidden') === 'true' ||
|
|
35
|
-
/(^|;)\s*display\s*:\s*none\b/i.test(el.getAttribute('style') ?? '') ||
|
|
36
|
-
NOT_TEXT.has(el.tagName) ||
|
|
37
|
-
(el.parentElement?.tagName === 'DETAILS' &&
|
|
38
|
-
!el.parentElement.hasAttribute('open') &&
|
|
39
|
-
el !==
|
|
40
|
-
Array.from(el.parentElement.children).find(
|
|
41
|
-
(c) => c.tagName === 'SUMMARY',
|
|
42
|
-
))
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
24
|
function formatValue(value: unknown): string {
|
|
47
25
|
try {
|
|
48
26
|
return JSON.stringify(value) ?? String(value);
|
|
@@ -112,7 +90,11 @@ export function observableText(el: Element, skip?: string): string {
|
|
|
112
90
|
const child = node as Element;
|
|
113
91
|
if (isHidden(child) || (skip && child !== el && child.matches(skip)))
|
|
114
92
|
return;
|
|
115
|
-
if (
|
|
93
|
+
if (
|
|
94
|
+
child.hasAttribute('data-action') &&
|
|
95
|
+
(child.matches('input, textarea, select') ||
|
|
96
|
+
getAction(child.getAttribute('data-action')!)?.type === 'cycle')
|
|
97
|
+
) {
|
|
116
98
|
out += controlMarker(child);
|
|
117
99
|
return;
|
|
118
100
|
}
|
package/src/observe/observe.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
actionMatches,
|
|
3
|
+
isActionVisible,
|
|
3
4
|
checkActionMatcher,
|
|
4
5
|
getActions,
|
|
5
6
|
getDialogActions,
|
|
@@ -117,7 +118,9 @@ const DIALOG = '.dialog-overlay';
|
|
|
117
118
|
*/
|
|
118
119
|
function offeredActions(): StoryAction[] {
|
|
119
120
|
const dialog = innermostOpenDialog();
|
|
120
|
-
return dialog ? getDialogActions(dialog) : getActions()
|
|
121
|
+
return (dialog ? getDialogActions(dialog) : getActions()).filter(
|
|
122
|
+
isActionVisible,
|
|
123
|
+
);
|
|
121
124
|
}
|
|
122
125
|
|
|
123
126
|
/** What the story shows and offers now. */
|
package/src/registry.ts
CHANGED
|
@@ -65,7 +65,7 @@ export function registerSubMacro(name: string): void {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
export function isSubMacro(name: string): boolean {
|
|
68
|
-
return subMacros.has(name);
|
|
68
|
+
return subMacros.has(name) || companionParents.has(name);
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
/**
|
|
@@ -227,12 +227,46 @@ export interface MacroMetadata {
|
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
const metadataRegistry = new NameMap<MacroMetadata>();
|
|
230
|
+
/** Indexed at registration, so parsing each tag does not scan every macro. */
|
|
231
|
+
const companionParents = new NameMap<Set<string>>();
|
|
230
232
|
|
|
231
233
|
export function registerMacroMetadata(
|
|
232
234
|
name: string,
|
|
233
235
|
metadata: MacroMetadata,
|
|
234
236
|
): void {
|
|
237
|
+
const previous = metadataRegistry.get(name);
|
|
238
|
+
if (previous) {
|
|
239
|
+
for (const sub of previous.subMacros) {
|
|
240
|
+
const parents = companionParents.get(sub);
|
|
241
|
+
parents?.delete(previous.name.toLowerCase());
|
|
242
|
+
if (parents?.size === 0) companionParents.delete(sub);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
235
245
|
metadataRegistry.set(name, metadata);
|
|
246
|
+
for (const sub of metadata.subMacros) {
|
|
247
|
+
const parents = companionParents.get(sub) ?? new Set<string>();
|
|
248
|
+
parents.add(metadata.name.toLowerCase());
|
|
249
|
+
companionParents.set(sub, parents);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The parents declaring a branching companion (listbox options are values). */
|
|
254
|
+
export function branchParents(
|
|
255
|
+
name: string,
|
|
256
|
+
macros?: readonly Pick<MacroMetadata, 'name' | 'subMacros'>[],
|
|
257
|
+
): string[] {
|
|
258
|
+
if (!macros) {
|
|
259
|
+
return [...(companionParents.get(name) ?? [])].filter(
|
|
260
|
+
(parent) => parent !== 'listbox',
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
return macros
|
|
264
|
+
.filter(
|
|
265
|
+
(macro) =>
|
|
266
|
+
macro.name.toLowerCase() !== 'listbox' &&
|
|
267
|
+
macro.subMacros.some((sub) => sub.toLowerCase() === name.toLowerCase()),
|
|
268
|
+
)
|
|
269
|
+
.map((macro) => macro.name.toLowerCase());
|
|
236
270
|
}
|
|
237
271
|
|
|
238
272
|
/** The declared parameters of the macro `name`, if it declares any. */
|
|
@@ -248,4 +282,5 @@ export function getMacroRegistry(): MacroMetadata[] {
|
|
|
248
282
|
|
|
249
283
|
export function clearMetadataRegistry(): void {
|
|
250
284
|
metadataRegistry.clear();
|
|
285
|
+
companionParents.clear();
|
|
251
286
|
}
|