@rohal12/spindle 0.59.23 → 0.59.24
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 +190 -56
- package/dist/pkg/story-variables.js +66 -22
- package/dist/pkg/tooling.js +2 -0
- package/package.json +1 -1
- package/src/components/PassageDialog.tsx +28 -1
- package/src/components/macros/MenubarButtons.tsx +3 -0
- package/src/components/macros/SettingsControls.tsx +10 -6
- package/src/components/macros/Type.tsx +1 -1
- package/src/components/macros/detached-body.tsx +62 -3
- package/src/hooks/use-modal-focus.ts +62 -19
- package/src/markup/render.tsx +15 -4
- package/src/settings.ts +9 -0
- package/src/tooling.ts +22 -1
- package/src/widgets/widget-def.ts +93 -22
- package/types/tooling.d.ts +49 -0
|
@@ -77,6 +77,9 @@ export function defineMenubarAction(config: MenubarActionConfig) {
|
|
|
77
77
|
passageName={config.dialog.passageName}
|
|
78
78
|
fallbackMarkup={config.dialog.fallbackMarkup}
|
|
79
79
|
panelClass={config.dialog.panelClass}
|
|
80
|
+
label={
|
|
81
|
+
typeof config.label === 'string' ? config.label : config.name
|
|
82
|
+
}
|
|
80
83
|
onClose={() => setDialogOpen(false)}
|
|
81
84
|
showCloseButton={config.dialog.showCloseButton}
|
|
82
85
|
/>
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { useState } from 'preact/hooks';
|
|
2
|
-
import { settings, type SettingDef } from '../../settings';
|
|
1
|
+
import { useEffect, useState } from 'preact/hooks';
|
|
2
|
+
import { settings, subscribeSettings, type SettingDef } from '../../settings';
|
|
3
3
|
import { defineMacro } from '../../define-macro';
|
|
4
4
|
|
|
5
5
|
function SettingControl({ name, def }: { name: string; def: SettingDef }) {
|
|
6
6
|
const [value, setValue] = useState(() => settings.get(name));
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
settings.
|
|
11
|
-
|
|
8
|
+
// Follow changes made elsewhere: Story.settings.set(), another view
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
const sync = () => setValue(settings.get(name));
|
|
11
|
+
sync();
|
|
12
|
+
return subscribeSettings(sync);
|
|
13
|
+
}, [name]);
|
|
14
|
+
|
|
15
|
+
const update = (newValue: unknown) => settings.set(name, newValue);
|
|
12
16
|
|
|
13
17
|
switch (def.type) {
|
|
14
18
|
case 'toggle':
|
|
@@ -20,19 +20,67 @@ import { runWithCommittedMutations } from '../../execute-mutation';
|
|
|
20
20
|
import { subscribeStateReplacement } from '../../store';
|
|
21
21
|
import { RepeatContext } from './Repeat';
|
|
22
22
|
import { DialogCloseContext } from '../PassageDialog';
|
|
23
|
+
import { NameSet } from '../../utils/macro-names';
|
|
23
24
|
import { ItemEditContext } from '../../utils/control-edits';
|
|
24
25
|
|
|
25
26
|
const frozenAlways = () => true;
|
|
26
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Macros whose work is over once their effects have run: a body made of
|
|
30
|
+
* these alone has nothing left to wait for. Any other macro (a timer, a
|
|
31
|
+
* widget, an author's macro) may keep working after rendering.
|
|
32
|
+
*/
|
|
33
|
+
const SETTLING_MACROS = new NameSet([
|
|
34
|
+
'set',
|
|
35
|
+
'unset',
|
|
36
|
+
'if',
|
|
37
|
+
'for',
|
|
38
|
+
'switch',
|
|
39
|
+
'do',
|
|
40
|
+
'print',
|
|
41
|
+
'goto',
|
|
42
|
+
'nobr',
|
|
43
|
+
'span',
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
/** Whether `nodes` use no macro outside SETTLING_MACROS (see above). */
|
|
47
|
+
function settlesAfterRender(nodes: ASTNode[]): boolean {
|
|
48
|
+
return nodes.every((n) => {
|
|
49
|
+
switch (n.type) {
|
|
50
|
+
case 'macro':
|
|
51
|
+
return (
|
|
52
|
+
SETTLING_MACROS.has(n.name) &&
|
|
53
|
+
settlesAfterRender(n.children) &&
|
|
54
|
+
(n.branches ?? []).every((b) => settlesAfterRender(b.children))
|
|
55
|
+
);
|
|
56
|
+
case 'html':
|
|
57
|
+
return settlesAfterRender(n.children);
|
|
58
|
+
default:
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Calls `onSettled` once the effects of the content before it have run. */
|
|
65
|
+
function Settled({ onSettled }: { onSettled: () => void }) {
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
// After the effects of the whole body, and what they render in turn
|
|
68
|
+
const timer = setTimeout(onSettled, 0);
|
|
69
|
+
return () => clearTimeout(timer);
|
|
70
|
+
}, []);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
27
74
|
/**
|
|
28
75
|
* Return a function that runs a macro body once, outside the passage tree:
|
|
29
76
|
* it renders `children` into a detached node, so the body's macros ({set},
|
|
30
77
|
* {if}, {goto}, ...) fire their side effects through the normal Preact
|
|
31
78
|
* pipeline. Used by {button} and {link} on click. The body's state is frozen
|
|
32
79
|
* once it has rendered, so it runs once per click. It stays mounted
|
|
33
|
-
* until
|
|
34
|
-
*
|
|
35
|
-
*
|
|
80
|
+
* until it has nothing left to do: a body of plain macros ({set}, {if},
|
|
81
|
+
* {goto}...) once its effects have run, any other (with the timer of a
|
|
82
|
+
* {timed} or {repeat}, say) until the owning macro unmounts, e.g. its
|
|
83
|
+
* passage changes, so that work it starts in an effect can finish; the passage leaving cancels it, and so does a restart or a load
|
|
36
84
|
* replacing the game, which a control of the story interface outlives
|
|
37
85
|
* (#401).
|
|
38
86
|
*
|
|
@@ -80,6 +128,16 @@ export function useDetachedBody(): (children: ASTNode[]) => void {
|
|
|
80
128
|
const run = (children: ASTNode[]) => {
|
|
81
129
|
const locals = liveLocalsView(updater.getValues);
|
|
82
130
|
const container = document.createElement('div');
|
|
131
|
+
// A body that settles is released once it has: it is not kept (with its
|
|
132
|
+
// detached nodes) for every click until the control goes away
|
|
133
|
+
const release = settlesAfterRender(children)
|
|
134
|
+
? () => {
|
|
135
|
+
const at = mounted.current.indexOf(container);
|
|
136
|
+
if (at === -1) return;
|
|
137
|
+
mounted.current.splice(at, 1);
|
|
138
|
+
render(null, container);
|
|
139
|
+
}
|
|
140
|
+
: null;
|
|
83
141
|
render(
|
|
84
142
|
<FrozenStateContext.Provider value={frozenAlways}>
|
|
85
143
|
<LocalsUpdateContext.Provider value={updater}>
|
|
@@ -98,6 +156,7 @@ export function useDetachedBody(): (children: ASTNode[]) => void {
|
|
|
98
156
|
structural,
|
|
99
157
|
locals,
|
|
100
158
|
})}
|
|
159
|
+
{release && <Settled onSettled={release} />}
|
|
101
160
|
</OutgoingContext.Provider>
|
|
102
161
|
</ItemEditContext.Provider>
|
|
103
162
|
</DialogCloseContext.Provider>
|
|
@@ -147,6 +147,33 @@ const openModals: ModalEntry[] = [];
|
|
|
147
147
|
* - closes the topmost dialog on Escape when it is dismissible;
|
|
148
148
|
* - returns focus to the previously focused element on close.
|
|
149
149
|
*/
|
|
150
|
+
/** The input types made of several segments, each a native Tab stop. */
|
|
151
|
+
const SEGMENTED_INPUTS = new Set([
|
|
152
|
+
'date',
|
|
153
|
+
'time',
|
|
154
|
+
'datetime-local',
|
|
155
|
+
'month',
|
|
156
|
+
'week',
|
|
157
|
+
]);
|
|
158
|
+
|
|
159
|
+
/** Whether the element has native tab stops inside it, in its shadow tree. */
|
|
160
|
+
function hasNativeStops(el: Element | null): el is HTMLElement {
|
|
161
|
+
if (el instanceof HTMLMediaElement) return el.controls;
|
|
162
|
+
return (
|
|
163
|
+
el instanceof HTMLInputElement &&
|
|
164
|
+
SEGMENTED_INPUTS.has(el.type) &&
|
|
165
|
+
!el.readOnly
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** The tab stops of an embedded document, in tab order. */
|
|
170
|
+
function frameStops(doc: Document | null): HTMLElement[] {
|
|
171
|
+
if (!doc) return [];
|
|
172
|
+
return Array.from(doc.querySelectorAll<HTMLElement>(FOCUSABLE)).filter(
|
|
173
|
+
(el) => isAvailable(el) && isTabStop(el),
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
150
177
|
export function useModalFocus(
|
|
151
178
|
panelRef: RefObject<HTMLElement>,
|
|
152
179
|
bodySelector: string,
|
|
@@ -200,6 +227,24 @@ export function useModalFocus(
|
|
|
200
227
|
* (#424). `from` may be an element of an embedded document, whose frame
|
|
201
228
|
* element then stands in for it.
|
|
202
229
|
*/
|
|
230
|
+
/** Focus `el`; a frame is entered at its first (or last) control (#460). */
|
|
231
|
+
const enter = (el: HTMLElement, backwards: boolean) => {
|
|
232
|
+
if (el instanceof HTMLIFrameElement) {
|
|
233
|
+
let inner: HTMLElement[] = [];
|
|
234
|
+
try {
|
|
235
|
+
inner = frameStops(el.contentDocument);
|
|
236
|
+
} catch {
|
|
237
|
+
// Not accessible: the frame itself takes focus
|
|
238
|
+
}
|
|
239
|
+
const edge = backwards ? inner[inner.length - 1] : inner[0];
|
|
240
|
+
if (edge) {
|
|
241
|
+
edge.focus();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
el.focus();
|
|
246
|
+
};
|
|
247
|
+
|
|
203
248
|
const moveFocus = (from: Element | null, backwards: boolean) => {
|
|
204
249
|
const items = focusables(panel);
|
|
205
250
|
const first = items[0];
|
|
@@ -224,12 +269,12 @@ export function useModalFocus(
|
|
|
224
269
|
} else {
|
|
225
270
|
next = backwards ? last : first;
|
|
226
271
|
}
|
|
227
|
-
(next ?? (backwards ? last : first))
|
|
272
|
+
enter(next ?? (backwards ? last : first), backwards);
|
|
228
273
|
};
|
|
229
274
|
|
|
230
|
-
// The
|
|
231
|
-
|
|
232
|
-
|
|
275
|
+
// The element whose native inner controls (a media element's, a date
|
|
276
|
+
// input's segments) Tab is stepping through
|
|
277
|
+
let steppingNative: { el: HTMLElement; backwards: boolean } | null = null;
|
|
233
278
|
|
|
234
279
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
235
280
|
if (!top()) return;
|
|
@@ -243,13 +288,13 @@ export function useModalFocus(
|
|
|
243
288
|
}
|
|
244
289
|
|
|
245
290
|
if (e.key !== 'Tab') return;
|
|
246
|
-
// The controls of a media element
|
|
247
|
-
// where the element stays the active
|
|
248
|
-
// natively, and onFocusIn takes over where
|
|
249
|
-
// (#444).
|
|
291
|
+
// The controls of a media element and the segments of a date input are
|
|
292
|
+
// in its user-agent shadow tree, where the element stays the active
|
|
293
|
+
// one: they step through them natively, and onFocusIn takes over where
|
|
294
|
+
// focus leaves the element (#444, #461).
|
|
250
295
|
const active = document.activeElement;
|
|
251
|
-
if (active
|
|
252
|
-
|
|
296
|
+
if (hasNativeStops(active)) {
|
|
297
|
+
steppingNative = { el: active, backwards: e.shiftKey };
|
|
253
298
|
return;
|
|
254
299
|
}
|
|
255
300
|
e.preventDefault();
|
|
@@ -257,7 +302,7 @@ export function useModalFocus(
|
|
|
257
302
|
};
|
|
258
303
|
// Clicking elsewhere ends the stepping through the controls
|
|
259
304
|
const onPointerDown = () => {
|
|
260
|
-
|
|
305
|
+
steppingNative = null;
|
|
261
306
|
};
|
|
262
307
|
|
|
263
308
|
// Key events inside an embedded document never reach this one (#425).
|
|
@@ -279,9 +324,7 @@ export function useModalFocus(
|
|
|
279
324
|
const onFrameKeyDown = (e: KeyboardEvent) => {
|
|
280
325
|
if (!top()) return;
|
|
281
326
|
if (e.key !== 'Tab') return onKeyDown(e);
|
|
282
|
-
const inner =
|
|
283
|
-
doc!.querySelectorAll<HTMLElement>(FOCUSABLE),
|
|
284
|
-
).filter((el) => isAvailable(el) && isTabStop(el));
|
|
327
|
+
const inner = frameStops(doc!);
|
|
285
328
|
const active = doc!.activeElement;
|
|
286
329
|
const edge = e.shiftKey ? inner[0] : inner[inner.length - 1];
|
|
287
330
|
if (!edge || active === edge || active === doc!.body) {
|
|
@@ -307,10 +350,10 @@ export function useModalFocus(
|
|
|
307
350
|
// document can't be reached, is brought back in.
|
|
308
351
|
const onFocusIn = (e: FocusEvent) => {
|
|
309
352
|
const target = e.target;
|
|
310
|
-
if (
|
|
353
|
+
if (steppingNative && target !== steppingNative.el && top()) {
|
|
311
354
|
// Focus left the media controls: on to the modal's next control
|
|
312
|
-
const { el, backwards } =
|
|
313
|
-
|
|
355
|
+
const { el, backwards } = steppingNative;
|
|
356
|
+
steppingNative = null;
|
|
314
357
|
moveFocus(el, backwards);
|
|
315
358
|
return;
|
|
316
359
|
}
|
|
@@ -330,11 +373,11 @@ export function useModalFocus(
|
|
|
330
373
|
// with no tab stop after it in the document) sends no focusin: the
|
|
331
374
|
// modal's next control takes it at once.
|
|
332
375
|
const onFocusOut = (e: FocusEvent) => {
|
|
333
|
-
const pending =
|
|
376
|
+
const pending = steppingNative;
|
|
334
377
|
if (!pending || e.target !== pending.el || e.relatedTarget || !top()) {
|
|
335
378
|
return;
|
|
336
379
|
}
|
|
337
|
-
|
|
380
|
+
steppingNative = null;
|
|
338
381
|
moveFocus(pending.el, pending.backwards);
|
|
339
382
|
};
|
|
340
383
|
document.addEventListener('focusout', onFocusOut, { signal });
|
package/src/markup/render.tsx
CHANGED
|
@@ -680,9 +680,14 @@ function resolveAttributeValue(
|
|
|
680
680
|
return result.text;
|
|
681
681
|
}
|
|
682
682
|
|
|
683
|
+
/** Whether a node shows nothing as text: whitespace, or a closed HTML comment. */
|
|
684
|
+
function isBlankText(n: ASTNode): boolean {
|
|
685
|
+
return n.type === 'text' && (n.comment === true || n.value.trim() === '');
|
|
686
|
+
}
|
|
687
|
+
|
|
683
688
|
/** The index of the summary element opening `nodes`, or -1 when none does. */
|
|
684
689
|
function leadingSummaryIndex(nodes: ASTNode[]): number {
|
|
685
|
-
const at = nodes.findIndex((n) => n
|
|
690
|
+
const at = nodes.findIndex((n) => !isBlankText(n));
|
|
686
691
|
const first = nodes[at];
|
|
687
692
|
return first?.type === 'html' && first.tag.toLowerCase() === 'summary'
|
|
688
693
|
? at
|
|
@@ -719,7 +724,13 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
719
724
|
let children: preact.ComponentChildren = undefined;
|
|
720
725
|
let structural = false;
|
|
721
726
|
if (node.children.length > 0) {
|
|
722
|
-
if (
|
|
727
|
+
if (tag === 'style') {
|
|
728
|
+
// A raw-text element: its CSS keeps character-reference lookalikes
|
|
729
|
+
// (`?a=1¬ch=2`) as written (#453)
|
|
730
|
+
children = node.children.map((n) =>
|
|
731
|
+
n.type === 'text' ? n.value : renderSingleNode(n),
|
|
732
|
+
);
|
|
733
|
+
} else if (inRaw || isRawRoot) {
|
|
723
734
|
children = renderInlineNodes(node.children);
|
|
724
735
|
} else {
|
|
725
736
|
// A details element's summary stays its direct child: it is rendered
|
|
@@ -730,7 +741,7 @@ function HtmlNodeRenderer({ node }: { node: HtmlNode }) {
|
|
|
730
741
|
renderNodes(nodes, { nobr, locals, inline: isInline });
|
|
731
742
|
structural =
|
|
732
743
|
STRUCTURAL_ELEMENTS.has(tag) &&
|
|
733
|
-
node.children.every((n) => n.type !== 'text' || n
|
|
744
|
+
node.children.every((n) => n.type !== 'text' || isBlankText(n));
|
|
734
745
|
children = structural ? (
|
|
735
746
|
node.children
|
|
736
747
|
.filter((n) => n.type !== 'text')
|
|
@@ -1017,7 +1028,7 @@ export function renderNodes(
|
|
|
1017
1028
|
// paragraphs would pull the elements they produce out of it (#436)
|
|
1018
1029
|
if (
|
|
1019
1030
|
options?.structural &&
|
|
1020
|
-
nodes.every((n) => n.type !== 'text' || n
|
|
1031
|
+
nodes.every((n) => n.type !== 'text' || isBlankText(n))
|
|
1021
1032
|
) {
|
|
1022
1033
|
return nodes.filter((n) => n.type !== 'text').map(renderSingleNode);
|
|
1023
1034
|
}
|
package/src/settings.ts
CHANGED
|
@@ -32,6 +32,14 @@ let values: Record<string, unknown> = createNamespace();
|
|
|
32
32
|
let storageLoaded = false;
|
|
33
33
|
let unsubscribeStoryData: (() => void) | null = null;
|
|
34
34
|
|
|
35
|
+
const listeners = new Set<() => void>();
|
|
36
|
+
|
|
37
|
+
/** Call `listener` after a setting changes; returns the unsubscribe. */
|
|
38
|
+
export function subscribeSettings(listener: () => void): () => void {
|
|
39
|
+
listeners.add(listener);
|
|
40
|
+
return () => listeners.delete(listener);
|
|
41
|
+
}
|
|
42
|
+
|
|
35
43
|
function storageKey(): string {
|
|
36
44
|
const storyData = useStoryStore.getState().storyData;
|
|
37
45
|
const ifid = storyData?.ifid || 'unknown';
|
|
@@ -125,6 +133,7 @@ export const settings = {
|
|
|
125
133
|
set(name: string, value: unknown): void {
|
|
126
134
|
values[name] = value;
|
|
127
135
|
persist();
|
|
136
|
+
for (const listener of [...listeners]) listener();
|
|
128
137
|
},
|
|
129
138
|
|
|
130
139
|
getAll(): Record<string, unknown> {
|
package/src/tooling.ts
CHANGED
|
@@ -19,7 +19,11 @@ import {
|
|
|
19
19
|
type PassageReference,
|
|
20
20
|
type Piece,
|
|
21
21
|
} from './code-check';
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
blockWidgetNames,
|
|
24
|
+
widgetDefinitions as readWidgetDefinitions,
|
|
25
|
+
type WidgetDefinition,
|
|
26
|
+
} from './widgets/widget-def';
|
|
23
27
|
import type { ParameterDef } from './registry';
|
|
24
28
|
|
|
25
29
|
export { parseDeclarations, parseStoryVariables } from './story-variables';
|
|
@@ -33,6 +37,8 @@ export type {
|
|
|
33
37
|
} from './story-variables';
|
|
34
38
|
export { checkParameterTypes } from './registry';
|
|
35
39
|
export { formatDiagnostic } from './markup/validate';
|
|
40
|
+
export { parseWidgetDef } from './widgets/widget-def';
|
|
41
|
+
export type { WidgetDef, WidgetDefinition } from './widgets/widget-def';
|
|
36
42
|
|
|
37
43
|
// The parsing rules (see "Tooling API" in docs/tooling.md): the leaf rules
|
|
38
44
|
// the runtime parses with, so editor tooling needn't mirror them.
|
|
@@ -210,3 +216,18 @@ export function collectStoryPassageReferences(
|
|
|
210
216
|
): PassageReference[] {
|
|
211
217
|
return referencesOf(passagePieces(source, macros));
|
|
212
218
|
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The widgets the story defines, as the runtime reads them: those of
|
|
222
|
+
* StoryInit and of the passages tagged `widget`, with their parameters,
|
|
223
|
+
* whether their body renders `{@children}` (a block widget) and where they
|
|
224
|
+
* are written, in source order. Half-typed definitions are reported without a
|
|
225
|
+
* `closeStart`. The macros are those the markup may use, built-in and
|
|
226
|
+
* user-defined (a `{@children}` in an attribute or label counts).
|
|
227
|
+
*/
|
|
228
|
+
export function widgetDefinitions(
|
|
229
|
+
passages: Iterable<MarkupPassage>,
|
|
230
|
+
macros: Iterable<ToolingMacro> = [],
|
|
231
|
+
): WidgetDefinition[] {
|
|
232
|
+
return readWidgetDefinitions(passages, parameterLookup([...macros]));
|
|
233
|
+
}
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
* Reading widget definitions without rendering anything: the `{widget}`
|
|
3
3
|
* macro, startup and markup validation (also in tooling) share these.
|
|
4
4
|
*/
|
|
5
|
-
import { parseMacroArgs } from '../components/macros/macro-args';
|
|
5
|
+
import { parseMacroArgs, type ArgSpans } from '../components/macros/macro-args';
|
|
6
6
|
import type { MacroArgs } from '../registry';
|
|
7
7
|
import { tokenizeMarkupTolerant } from '../markup/parse';
|
|
8
|
+
import type { MacroToken } from '../markup/tokens';
|
|
8
9
|
import type { ParametersOf } from '../code-check';
|
|
9
10
|
import { registeredParameters, tokenTextContainsChildren } from './ast-scanner';
|
|
10
11
|
|
|
@@ -39,39 +40,74 @@ export function parseWidgetDef(rawArgs: string): WidgetDef {
|
|
|
39
40
|
return widgetDef(parseMacroArgs(rawArgs, WIDGET_PARAMETERS));
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
/** A passage that may define widgets, as the tooling reads it. */
|
|
44
|
+
export interface WidgetPassage {
|
|
45
|
+
name: string;
|
|
46
|
+
tags?: string[];
|
|
47
|
+
content: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
42
50
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
* A widget defined by a `{widget}` macro, with where it is written (UTF-16
|
|
52
|
+
* offsets into the passage content).
|
|
53
|
+
*/
|
|
54
|
+
export interface WidgetDefinition extends WidgetDef {
|
|
55
|
+
/** Whether its body renders `{@children}`. */
|
|
56
|
+
block: boolean;
|
|
57
|
+
/** The passage defining it. */
|
|
58
|
+
passage: string;
|
|
59
|
+
/** The opening `{widget …}` tag. */
|
|
60
|
+
start: number;
|
|
61
|
+
end: number;
|
|
62
|
+
/** The name as written, without quotes. */
|
|
63
|
+
nameStart: number;
|
|
64
|
+
nameEnd: number;
|
|
65
|
+
/** The `{/widget}` closer, absent while the definition is not closed. */
|
|
66
|
+
closeStart?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Where the name is written in the arguments, without its quotes. */
|
|
70
|
+
function nameSpan(rawArgs: string): [number, number] {
|
|
71
|
+
const spans: ArgSpans = new Map();
|
|
72
|
+
parseMacroArgs(rawArgs, WIDGET_PARAMETERS, spans);
|
|
73
|
+
const [start, end] = spans.get(WIDGET_PARAMETERS[0]) ?? [0, 0];
|
|
74
|
+
const quote = rawArgs[start];
|
|
75
|
+
return (quote === '"' || quote === "'") && end - start >= 2
|
|
76
|
+
? [start + 1, rawArgs[end - 1] === quote ? end - 1 : end]
|
|
77
|
+
: [start, end];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The widgets that StoryInit and the passages tagged `widget` define, in
|
|
82
|
+
* source order: the ones the runtime registers, read tolerantly (a half-typed
|
|
83
|
+
* definition is still reported, without a `closeStart`). Definitions whose
|
|
84
|
+
* arguments cannot be read are left out.
|
|
47
85
|
*
|
|
48
86
|
* The passages are read as flat tokens, which need no block macros to be
|
|
49
87
|
* known, so a `{@children}` in an HTML comment or in a `{do}` body is text
|
|
50
|
-
* here as it is to the parser, and does not make a block widget (#387).
|
|
88
|
+
* here as it is to the parser, and does not make a block widget (#387). In
|
|
89
|
+
* nested definitions, it belongs to the innermost.
|
|
51
90
|
*/
|
|
52
|
-
export function
|
|
53
|
-
passages: Iterable<
|
|
91
|
+
export function widgetDefinitions(
|
|
92
|
+
passages: Iterable<WidgetPassage>,
|
|
54
93
|
parametersOf: ParametersOf = registeredParameters,
|
|
55
|
-
):
|
|
56
|
-
const
|
|
94
|
+
): WidgetDefinition[] {
|
|
95
|
+
const found: WidgetDefinition[] = [];
|
|
57
96
|
for (const passage of passages) {
|
|
58
97
|
if (passage.name !== 'StoryInit' && !passage.tags?.includes('widget')) {
|
|
59
98
|
continue;
|
|
60
99
|
}
|
|
61
|
-
// The open definitions: their
|
|
62
|
-
const open: {
|
|
100
|
+
// The open definitions: their tags, and whether a slot was seen
|
|
101
|
+
const open: { token: MacroToken; isBlock: boolean; closeStart?: number }[] =
|
|
102
|
+
[];
|
|
103
|
+
const done: typeof open = [];
|
|
63
104
|
for (const token of tokenizeMarkupTolerant(passage.content).tokens) {
|
|
64
105
|
if (token.type === 'macro' && token.name.toLowerCase() === 'widget') {
|
|
65
106
|
if (!token.isClose) {
|
|
66
|
-
open.push({
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (!def?.isBlock) continue;
|
|
71
|
-
try {
|
|
72
|
-
names.push(parseWidgetDef(def.rawArgs).name);
|
|
73
|
-
} catch {
|
|
74
|
-
// The {widget} macro reports a definition it cannot read
|
|
107
|
+
open.push({ token, isBlock: false });
|
|
108
|
+
} else {
|
|
109
|
+
const def = open.pop();
|
|
110
|
+
if (def) done.push({ ...def, closeStart: token.start });
|
|
75
111
|
}
|
|
76
112
|
} else if (
|
|
77
113
|
open.length > 0 &&
|
|
@@ -83,6 +119,41 @@ export function blockWidgetNames(
|
|
|
83
119
|
open[open.length - 1]!.isBlock = true;
|
|
84
120
|
}
|
|
85
121
|
}
|
|
122
|
+
done.push(...open);
|
|
123
|
+
const defs: WidgetDefinition[] = [];
|
|
124
|
+
for (const { token, isBlock, closeStart } of done) {
|
|
125
|
+
try {
|
|
126
|
+
const [from, to] = nameSpan(token.rawArgs);
|
|
127
|
+
defs.push({
|
|
128
|
+
...parseWidgetDef(token.rawArgs),
|
|
129
|
+
block: isBlock,
|
|
130
|
+
passage: passage.name,
|
|
131
|
+
start: token.start,
|
|
132
|
+
end: token.end,
|
|
133
|
+
nameStart: token.argsStart + from,
|
|
134
|
+
nameEnd: token.argsStart + to,
|
|
135
|
+
...(closeStart === undefined ? {} : { closeStart }),
|
|
136
|
+
});
|
|
137
|
+
} catch {
|
|
138
|
+
// The {widget} macro reports a definition it cannot read
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
found.push(...defs.sort((a, b) => a.start - b.start));
|
|
86
142
|
}
|
|
87
|
-
return
|
|
143
|
+
return found;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The names of the block widgets (those whose body renders `{@children}`)
|
|
148
|
+
* that StoryInit and the passages tagged `widget` define, closed. They must
|
|
149
|
+
* be known as block macros before any passage is parsed, so that passages
|
|
150
|
+
* invoking them nest their content whatever the passage order.
|
|
151
|
+
*/
|
|
152
|
+
export function blockWidgetNames(
|
|
153
|
+
passages: Iterable<WidgetPassage>,
|
|
154
|
+
parametersOf: ParametersOf = registeredParameters,
|
|
155
|
+
): string[] {
|
|
156
|
+
return widgetDefinitions(passages, parametersOf)
|
|
157
|
+
.filter((def) => def.block && def.closeStart !== undefined)
|
|
158
|
+
.map((def) => def.name);
|
|
88
159
|
}
|
package/types/tooling.d.ts
CHANGED
|
@@ -680,6 +680,55 @@ export declare function collectStoryPassageReferences(
|
|
|
680
680
|
macros: Iterable<ToolingMacro>,
|
|
681
681
|
): PassageReference[];
|
|
682
682
|
|
|
683
|
+
// ---------------------------------------------------------------------------
|
|
684
|
+
// Widget definitions
|
|
685
|
+
// ---------------------------------------------------------------------------
|
|
686
|
+
|
|
687
|
+
/** What a `{widget}` definition's arguments declare. */
|
|
688
|
+
export interface WidgetDef {
|
|
689
|
+
name: string;
|
|
690
|
+
/** The `@` parameters. */
|
|
691
|
+
params: string[];
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/**
|
|
695
|
+
* A widget defined by a `{widget}` macro. The offsets are UTF-16 offsets into
|
|
696
|
+
* the content of `passage`.
|
|
697
|
+
*/
|
|
698
|
+
export interface WidgetDefinition extends WidgetDef {
|
|
699
|
+
/** Whether its body renders `{@children}`: it takes a closing tag. */
|
|
700
|
+
block: boolean;
|
|
701
|
+
passage: string;
|
|
702
|
+
/** The opening `{widget …}` tag. */
|
|
703
|
+
start: number;
|
|
704
|
+
end: number;
|
|
705
|
+
/** The name as written, without quotes. */
|
|
706
|
+
nameStart: number;
|
|
707
|
+
nameEnd: number;
|
|
708
|
+
/** The `{/widget}` closer; absent while the definition is not closed. */
|
|
709
|
+
closeStart?: number;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* The name and `@` parameters a `{widget}` macro reads from its arguments
|
|
714
|
+
* (`"Wrap" @a @b`), as the runtime reads them. Throws where the macro
|
|
715
|
+
* reports the arguments.
|
|
716
|
+
*/
|
|
717
|
+
export declare function parseWidgetDef(rawArgs: string): WidgetDef;
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* The widgets the story defines, as the runtime registers them: those of
|
|
721
|
+
* `StoryInit` and of the passages tagged `widget`, in source order. Block-ness
|
|
722
|
+
* is decided on tokens, so a `{@children}` in an HTML comment or a `{do}` body
|
|
723
|
+
* does not count; one in an attribute value or label of the `macros` does.
|
|
724
|
+
* Tolerant: a half-typed definition is reported without `closeStart`, and one
|
|
725
|
+
* whose arguments cannot be read is left out.
|
|
726
|
+
*/
|
|
727
|
+
export declare function widgetDefinitions(
|
|
728
|
+
passages: Iterable<MarkupPassage>,
|
|
729
|
+
macros?: Iterable<ToolingMacro>,
|
|
730
|
+
): WidgetDefinition[];
|
|
731
|
+
|
|
683
732
|
// ---------------------------------------------------------------------------
|
|
684
733
|
// Pieces of a passage
|
|
685
734
|
// ---------------------------------------------------------------------------
|