@rohal12/spindle 0.61.2 → 0.61.3
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 +569 -331
- package/dist/pkg/story-variables.js +448 -216
- package/package.json +1 -1
- package/src/format/format-passage.ts +26 -3
- package/src/format/{fenced-code.ts → markdown-lines.ts} +6 -4
- package/src/markup/spindle.peggy +6 -0
- package/src/observe/observable-text.ts +19 -7
- package/src/observe/observe.ts +30 -5
- package/src/story-context-passage.ts +2 -1
- package/src/utils/element-visibility.ts +15 -10
- package/types/index.d.ts +13 -2
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@ import { analyzeMarkup, tokenizeMarkupTolerant } from '../markup/parse';
|
|
|
3
3
|
import type { PairedNode } from '../markup/pair';
|
|
4
4
|
import type { MacroToken } from '../markup/tokens';
|
|
5
5
|
import { segmentRegions } from './segment';
|
|
6
|
-
import {
|
|
6
|
+
import { markdownLines } from './markdown-lines';
|
|
7
7
|
import {
|
|
8
8
|
formatJS,
|
|
9
9
|
formatCSS,
|
|
@@ -215,7 +215,7 @@ async function formatLf(
|
|
|
215
215
|
const literalSentinel = newlineSentinel(body + sentinel);
|
|
216
216
|
const literalLines = new Map<string, string>();
|
|
217
217
|
const literalMarkers = new Set<string>();
|
|
218
|
-
const fenced =
|
|
218
|
+
const fenced = markdownLines(body, isBlock, new Set(['codeFenced']));
|
|
219
219
|
const literalHtml = whitespaceSensitiveLines(body, isBlock);
|
|
220
220
|
body = body
|
|
221
221
|
.split('\n')
|
|
@@ -336,7 +336,12 @@ async function formatLf(
|
|
|
336
336
|
literalMarkers,
|
|
337
337
|
);
|
|
338
338
|
if (options.maxLineLength) {
|
|
339
|
-
indented = wrapLines(
|
|
339
|
+
indented = wrapLines(
|
|
340
|
+
indented,
|
|
341
|
+
options.maxLineLength,
|
|
342
|
+
literalLines,
|
|
343
|
+
isBlock,
|
|
344
|
+
);
|
|
340
345
|
}
|
|
341
346
|
for (const line of indented) {
|
|
342
347
|
if (line.endsWith(' ')) hardBreakLines.add(lines.length);
|
|
@@ -599,6 +604,21 @@ function indentColumn(line: string): number {
|
|
|
599
604
|
// Line wrapping
|
|
600
605
|
// ---------------------------------------------------------------------------
|
|
601
606
|
|
|
607
|
+
/** Constructs whose line breaks or continuation prefixes carry Markdown syntax. */
|
|
608
|
+
const NO_WRAP = new Set([
|
|
609
|
+
'atxHeading',
|
|
610
|
+
'setextHeading',
|
|
611
|
+
'table',
|
|
612
|
+
'listItem',
|
|
613
|
+
'blockQuote',
|
|
614
|
+
'definition',
|
|
615
|
+
'thematicBreak',
|
|
616
|
+
'htmlFlow',
|
|
617
|
+
'codeText',
|
|
618
|
+
'link',
|
|
619
|
+
'image',
|
|
620
|
+
]);
|
|
621
|
+
|
|
602
622
|
/**
|
|
603
623
|
* Wrap lines that exceed maxLen at word boundaries.
|
|
604
624
|
* Preserves leading indentation. Skips pure macro lines and choice/link
|
|
@@ -608,15 +628,18 @@ function wrapLines(
|
|
|
608
628
|
lines: string[],
|
|
609
629
|
maxLen: number,
|
|
610
630
|
literalLines: ReadonlyMap<string, string>,
|
|
631
|
+
isBlock: (name: string) => boolean,
|
|
611
632
|
): string[] {
|
|
612
633
|
const result: string[] = [];
|
|
613
634
|
const javaScript = doBodyLines(lines);
|
|
635
|
+
const structured = markdownLines(lines.join('\n'), isBlock, NO_WRAP);
|
|
614
636
|
|
|
615
637
|
for (const [index, line] of lines.entries()) {
|
|
616
638
|
// A `{do}` body is JavaScript: a line break there can change the program
|
|
617
639
|
if (
|
|
618
640
|
line.length <= maxLen ||
|
|
619
641
|
javaScript.has(index) ||
|
|
642
|
+
structured.has(index) ||
|
|
620
643
|
literalLines.has(line.trim())
|
|
621
644
|
) {
|
|
622
645
|
result.push(line);
|
|
@@ -3,14 +3,16 @@ import { analyzeMarkup } from '../markup/parse';
|
|
|
3
3
|
import { markdownOptions } from '../markup/markdown';
|
|
4
4
|
import type { PairedNode } from '../markup/pair';
|
|
5
5
|
|
|
6
|
-
/** Source lines
|
|
7
|
-
export function
|
|
6
|
+
/** Source lines belonging to the given constructs in runtime Markdown documents. */
|
|
7
|
+
export function markdownLines(
|
|
8
8
|
source: string,
|
|
9
9
|
isBlock: (name: string) => boolean,
|
|
10
|
+
types: ReadonlySet<string>,
|
|
10
11
|
): Set<number> {
|
|
11
12
|
const found = new Set<number>();
|
|
12
13
|
// Most passages contain no fence; avoid parsing their Markdown again.
|
|
13
|
-
if (!/`{3}|~{3}/.test(source))
|
|
14
|
+
if (types.size === 1 && types.has('codeFenced') && !/`{3}|~{3}/.test(source))
|
|
15
|
+
return found;
|
|
14
16
|
const { nodes } = analyzeMarkup(source, { hooks: { isBlock } });
|
|
15
17
|
|
|
16
18
|
function visit(nodes: PairedNode[]): void {
|
|
@@ -39,7 +41,7 @@ export function fencedCodeLines(
|
|
|
39
41
|
.write(preprocess()(markdown, undefined, true)),
|
|
40
42
|
);
|
|
41
43
|
for (const [event, token] of events) {
|
|
42
|
-
if (event !== 'enter' || token.type
|
|
44
|
+
if (event !== 'enter' || !types.has(token.type)) continue;
|
|
43
45
|
for (const span of spans) {
|
|
44
46
|
const start = Math.max(token.start.offset, span.start);
|
|
45
47
|
const end = Math.min(token.end.offset, span.end);
|
package/src/markup/spindle.peggy
CHANGED
|
@@ -207,6 +207,7 @@ TokenItem
|
|
|
207
207
|
/ Comment
|
|
208
208
|
/ LinkLexeme
|
|
209
209
|
/ EmailAutolink
|
|
210
|
+
/ &{ return !textMode; } UriAutolink
|
|
210
211
|
/ &{ return !textMode; } tag:HtmlOpen { return tag; }
|
|
211
212
|
/ &{ return !textMode; } tag:HtmlClose { return VOID_TAGS.has(tag.lower) ? null : tag; }
|
|
212
213
|
/ Value
|
|
@@ -369,6 +370,11 @@ EmailAutolink
|
|
|
369
370
|
&{ return EMAIL_AUTOLINK.test(value); }
|
|
370
371
|
{ return lexeme('text', location(), { value }); }
|
|
371
372
|
|
|
373
|
+
/** URI autolinks take precedence over HTML; Spindle markup stays active. */
|
|
374
|
+
UriAutolink
|
|
375
|
+
= value:$("<" [a-zA-Z] [a-zA-Z0-9+.-]|1..31| ":" (!"[[" [^\x00-\x20\x7F<>{}])* ">")
|
|
376
|
+
{ return lexeme('text', location(), { value }); }
|
|
377
|
+
|
|
372
378
|
HtmlOpen
|
|
373
379
|
= "<" tagStart:Here tag:TagName list:Attribute* _ slash:"/"? ">" {
|
|
374
380
|
const lower = tag.toLowerCase();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isHidden } from '../utils/element-visibility';
|
|
1
|
+
import { isHidden, isRendered } from '../utils/element-visibility';
|
|
2
2
|
import { getAction } from '../action-registry';
|
|
3
3
|
|
|
4
4
|
/** Elements that start and end a line. */
|
|
@@ -66,9 +66,14 @@ function controlMarker(el: Element): string {
|
|
|
66
66
|
* The text a reader sees in the element, as lines: blocks and `<br>` break
|
|
67
67
|
* lines, hidden elements are left out, and a control with a `data-action`
|
|
68
68
|
* becomes a marker such as `[textbox $name: "Alice"]`. Elements inside `el`
|
|
69
|
-
* that match `skip` are left out with their content.
|
|
69
|
+
* that match `skip` are left out with their content. `visibilityRoot` bounds
|
|
70
|
+
* ancestor checks when reading StoryContext inside its hidden host.
|
|
70
71
|
*/
|
|
71
|
-
export function observableText(
|
|
72
|
+
export function observableText(
|
|
73
|
+
el: Element,
|
|
74
|
+
skip?: string,
|
|
75
|
+
visibilityRoot?: Element,
|
|
76
|
+
): string {
|
|
72
77
|
let out = '';
|
|
73
78
|
const endLine = () => {
|
|
74
79
|
if (out !== '' && !out.endsWith('\n')) out += '\n';
|
|
@@ -76,6 +81,8 @@ export function observableText(el: Element, skip?: string): string {
|
|
|
76
81
|
|
|
77
82
|
const walk = (node: Node, inPre: boolean) => {
|
|
78
83
|
if (node.nodeType === 3) {
|
|
84
|
+
if (node.parentElement && !isRendered(node.parentElement, visibilityRoot))
|
|
85
|
+
return;
|
|
79
86
|
const data = node.textContent ?? '';
|
|
80
87
|
if (inPre) out += data;
|
|
81
88
|
else {
|
|
@@ -95,11 +102,12 @@ export function observableText(el: Element, skip?: string): string {
|
|
|
95
102
|
(child.matches('input, textarea, select') ||
|
|
96
103
|
getAction(child.getAttribute('data-action')!)?.type === 'cycle')
|
|
97
104
|
) {
|
|
105
|
+
if (!isRendered(child, visibilityRoot)) return;
|
|
98
106
|
out += controlMarker(child);
|
|
99
107
|
return;
|
|
100
108
|
}
|
|
101
109
|
if (child.tagName === 'BR') {
|
|
102
|
-
out += '\n';
|
|
110
|
+
if (isRendered(child, visibilityRoot)) out += '\n';
|
|
103
111
|
return;
|
|
104
112
|
}
|
|
105
113
|
const block = BLOCKS.has(child.tagName);
|
|
@@ -134,7 +142,11 @@ export function observableText(el: Element, skip?: string): string {
|
|
|
134
142
|
* the reader can't see (inside a hidden element, as observableText() does)
|
|
135
143
|
* and those inside an element within `el` that matches `skip`.
|
|
136
144
|
*/
|
|
137
|
-
export function observableErrors(
|
|
145
|
+
export function observableErrors(
|
|
146
|
+
el: Element,
|
|
147
|
+
skip?: string,
|
|
148
|
+
visibilityRoot?: Element,
|
|
149
|
+
): string[] {
|
|
138
150
|
const left = (e: Element) => {
|
|
139
151
|
for (let at: Element | null = e; at; at = at.parentElement) {
|
|
140
152
|
if (isHidden(at)) return true;
|
|
@@ -144,6 +156,6 @@ export function observableErrors(el: Element, skip?: string): string[] {
|
|
|
144
156
|
return false;
|
|
145
157
|
};
|
|
146
158
|
return Array.from(el.querySelectorAll('.error'))
|
|
147
|
-
.filter((e) => !left(e))
|
|
148
|
-
.map((e) => (e
|
|
159
|
+
.filter((e) => !left(e) && isRendered(e, visibilityRoot))
|
|
160
|
+
.map((e) => observableText(e, skip, visibilityRoot).trim());
|
|
149
161
|
}
|
package/src/observe/observe.ts
CHANGED
|
@@ -15,9 +15,31 @@ import { storyContextElement } from '../story-context-passage';
|
|
|
15
15
|
import { innermostOpenDialog, openDialogPanels } from '../triggers';
|
|
16
16
|
import { checkObject, type Fail } from '../utils/field-checks';
|
|
17
17
|
import { observableErrors, observableText } from './observable-text';
|
|
18
|
+
import { stableKey } from '../utils/stable-key';
|
|
18
19
|
|
|
19
|
-
/**
|
|
20
|
-
export type
|
|
20
|
+
/** A JSON value in an observation, detached from live story state. */
|
|
21
|
+
export type ObservedValue =
|
|
22
|
+
| string
|
|
23
|
+
| number
|
|
24
|
+
| boolean
|
|
25
|
+
| null
|
|
26
|
+
| ObservedValue[]
|
|
27
|
+
| { [key: string]: ObservedValue };
|
|
28
|
+
|
|
29
|
+
/** An action as an observation lists it, with a portable value and no perform. */
|
|
30
|
+
export type ObservedAction = Omit<StoryAction, 'perform' | 'value'> & {
|
|
31
|
+
value?: ObservedValue;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** JSON trees as snapshots; other values use the shared value description. */
|
|
35
|
+
function observedValue(value: unknown): ObservedValue {
|
|
36
|
+
const description = stableKey(value);
|
|
37
|
+
try {
|
|
38
|
+
return JSON.parse(description);
|
|
39
|
+
} catch {
|
|
40
|
+
return description;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
21
43
|
|
|
22
44
|
/** Which actions an observation offers, besides the default ones. */
|
|
23
45
|
export interface ObservedActionRules {
|
|
@@ -76,7 +98,9 @@ export function filterObservedActions(
|
|
|
76
98
|
(!DEFAULT_EXCLUDED.has(action.type) ||
|
|
77
99
|
matchesAny(action, rules.include)),
|
|
78
100
|
)
|
|
79
|
-
.map(({ perform: _perform, ...rest }) =>
|
|
101
|
+
.map(({ perform: _perform, value, ...rest }) =>
|
|
102
|
+
value === undefined ? rest : { ...rest, value: observedValue(value) },
|
|
103
|
+
);
|
|
80
104
|
}
|
|
81
105
|
|
|
82
106
|
let observedRules: ObservedActionRules = {};
|
|
@@ -145,7 +169,8 @@ export function observe(): Observation {
|
|
|
145
169
|
text: body ? observableText(body, DIALOG) : '',
|
|
146
170
|
}));
|
|
147
171
|
const errors = [passageEl, ...bodies.map((d) => d.body), contextEl].flatMap(
|
|
148
|
-
(el) =>
|
|
172
|
+
(el) =>
|
|
173
|
+
el ? observableErrors(el, DIALOG, el === contextEl ? el : undefined) : [],
|
|
149
174
|
);
|
|
150
175
|
// The runtime errors shown (as banners) until dismissed, with what failed
|
|
151
176
|
for (const { context, message } of getRuntimeErrors()) {
|
|
@@ -154,7 +179,7 @@ export function observe(): Observation {
|
|
|
154
179
|
return {
|
|
155
180
|
passage: currentPassage,
|
|
156
181
|
text: passageEl ? observableText(passageEl, DIALOG) : '',
|
|
157
|
-
context: contextEl ? observableText(contextEl) : null,
|
|
182
|
+
context: contextEl ? observableText(contextEl, undefined, contextEl) : null,
|
|
158
183
|
dialogs,
|
|
159
184
|
actions: filterObservedActions(offeredActions(), observedRules),
|
|
160
185
|
errors,
|
|
@@ -78,7 +78,8 @@ function StoryContextView({ ast }: { ast: ASTNode[] }) {
|
|
|
78
78
|
* story has none. It is mounted on the first call, hidden from the page and
|
|
79
79
|
* with its controls registering no actions, and stays mounted: it follows the
|
|
80
80
|
* story's variables, also across a restart. The element itself is not hidden
|
|
81
|
-
* (its parent is), so
|
|
81
|
+
* (its parent is), so observations bound visibility checks at the container.
|
|
82
|
+
* A passage that can't be
|
|
82
83
|
* parsed or rendered shows its error as text in the element; this never
|
|
83
84
|
* throws.
|
|
84
85
|
*/
|
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
const NOT_TEXT = new Set(['SCRIPT', 'STYLE', 'TEMPLATE', 'NOSCRIPT']);
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Whether the
|
|
6
|
-
*
|
|
7
|
-
* `display: none` and `aria-hidden="true"`.
|
|
5
|
+
* Whether the element hides its whole subtree, including stylesheet rules.
|
|
6
|
+
* This needs computed styles but no layout, so it also works headlessly.
|
|
8
7
|
*/
|
|
9
8
|
export function isHidden(el: Element): boolean {
|
|
10
9
|
return (
|
|
11
10
|
el.hasAttribute('hidden') ||
|
|
12
11
|
el.getAttribute('aria-hidden') === 'true' ||
|
|
13
12
|
/(^|;)\s*display\s*:\s*none\b/i.test(el.getAttribute('style') ?? '') ||
|
|
13
|
+
el.ownerDocument.defaultView?.getComputedStyle(el).display === 'none' ||
|
|
14
14
|
NOT_TEXT.has(el.tagName) ||
|
|
15
15
|
(el.parentElement?.tagName === 'DETAILS' &&
|
|
16
16
|
!el.parentElement.hasAttribute('open') &&
|
|
@@ -25,8 +25,9 @@ export function isHidden(el: Element): boolean {
|
|
|
25
25
|
* Whether `el` is in a closed `<details>`, other than in the summary that
|
|
26
26
|
* toggles it: the browser renders none of that content (#389).
|
|
27
27
|
*/
|
|
28
|
-
function inClosedDetails(el: Element): boolean {
|
|
29
|
-
|
|
28
|
+
function inClosedDetails(el: Element, root?: Element): boolean {
|
|
29
|
+
const stop = root?.parentElement;
|
|
30
|
+
for (let e = el.parentElement; e && e !== stop; e = e.parentElement) {
|
|
30
31
|
if (e.localName !== 'details' || e.hasAttribute('open')) continue;
|
|
31
32
|
const summary = Array.from(e.children).find(
|
|
32
33
|
(c) => c.localName === 'summary',
|
|
@@ -40,15 +41,19 @@ function inClosedDetails(el: Element): boolean {
|
|
|
40
41
|
* Whether CSS leaves `el` in the tab sequence: not `visibility: hidden`
|
|
41
42
|
* (inherited, so the computed value covers ancestors), not inside a
|
|
42
43
|
* `display: none` element and not in the hidden content of a closed
|
|
43
|
-
* `<details>`.
|
|
44
|
+
* `<details>`. An optional root bounds ancestor checks, so observations can
|
|
45
|
+
* read StoryContext inside its deliberately hidden host.
|
|
44
46
|
*/
|
|
45
|
-
export function isRendered(el: Element): boolean {
|
|
46
|
-
const
|
|
47
|
+
export function isRendered(el: Element, root?: Element): boolean {
|
|
48
|
+
const style = (e: Element) =>
|
|
49
|
+
e.ownerDocument.defaultView?.getComputedStyle(e);
|
|
50
|
+
const visibility = style(el)?.visibility;
|
|
47
51
|
if (visibility === 'hidden' || visibility === 'collapse') return false;
|
|
48
52
|
for (let e: Element | null = el; e; e = e.parentElement) {
|
|
49
|
-
if (
|
|
53
|
+
if (style(e)?.display === 'none') return false;
|
|
54
|
+
if (e === root) break;
|
|
50
55
|
}
|
|
51
|
-
return !inClosedDetails(el);
|
|
56
|
+
return !inClosedDetails(el, root);
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
/** Visibility shared by reader observations and native control handling. */
|
package/types/index.d.ts
CHANGED
|
@@ -235,8 +235,19 @@ export interface ActionMatcher {
|
|
|
235
235
|
disabled?: boolean;
|
|
236
236
|
}
|
|
237
237
|
|
|
238
|
-
/**
|
|
239
|
-
export type
|
|
238
|
+
/** A JSON value in an observation, detached from live story state. */
|
|
239
|
+
export type ObservedValue =
|
|
240
|
+
| string
|
|
241
|
+
| number
|
|
242
|
+
| boolean
|
|
243
|
+
| null
|
|
244
|
+
| ObservedValue[]
|
|
245
|
+
| { [key: string]: ObservedValue };
|
|
246
|
+
|
|
247
|
+
/** An action as `Story.observe()` lists it, with a portable value and no perform. */
|
|
248
|
+
export type ObservedAction = Omit<StoryAction, 'perform' | 'value'> & {
|
|
249
|
+
value?: ObservedValue;
|
|
250
|
+
};
|
|
240
251
|
|
|
241
252
|
/**
|
|
242
253
|
* Which actions `Story.observe()` offers. `exclude` removes what it
|