@rohal12/spindle 0.59.29 → 0.59.31
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/README.md +1 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +947 -351
- package/dist/pkg/macro-registry.js +19 -0
- package/dist/pkg/macro-registry.json +19 -0
- package/dist/pkg/story-variables.js +780 -107
- package/package.json +1 -1
- package/src/action-registry.ts +59 -5
- package/src/automation/load-yaml.ts +111 -20
- package/src/automation/runner.ts +19 -13
- package/src/automation/types.ts +7 -5
- package/src/class-registry.ts +8 -5
- package/src/code-check.ts +87 -20
- package/src/components/macros/Checkbox.tsx +5 -2
- package/src/components/macros/Cycle.tsx +1 -0
- package/src/components/macros/For.tsx +1 -7
- package/src/components/macros/Listbox.tsx +1 -0
- package/src/components/macros/MenubarButtons.tsx +6 -1
- package/src/components/macros/Nobr.tsx +1 -0
- package/src/components/macros/PassageDisplay.tsx +136 -10
- package/src/components/macros/Radiobutton.tsx +1 -0
- package/src/components/macros/Repeat.tsx +4 -2
- package/src/components/macros/SaveManager.tsx +30 -8
- package/src/components/macros/SettingsControls.tsx +19 -4
- package/src/components/macros/Span.tsx +1 -0
- package/src/components/macros/StoryTitle.tsx +1 -0
- package/src/components/macros/Unset.tsx +26 -13
- package/src/components/macros/VarDisplay.tsx +2 -3
- package/src/components/macros/WidgetInvocation.tsx +5 -6
- package/src/components/macros/input-macro.ts +1 -0
- package/src/components/macros/locals-scope.tsx +19 -8
- package/src/components/macros/macro-args.ts +11 -3
- package/src/expression.ts +34 -18
- package/src/hooks/use-merged-locals.ts +17 -2
- package/src/hooks/use-modal-focus.ts +247 -133
- package/src/index.tsx +32 -21
- package/src/js-bindings.ts +594 -0
- package/src/js-lexer.ts +76 -14
- package/src/macro-discovery.ts +83 -32
- package/src/markup/ast.ts +22 -1
- package/src/markup/render.tsx +44 -15
- package/src/parser.ts +41 -10
- package/src/prng.ts +24 -1
- package/src/saves/save-manager.ts +36 -9
- package/src/static-literal.ts +87 -16
- package/src/store.ts +83 -6
- package/src/story-api.ts +23 -6
- package/src/story-variables.ts +91 -28
- package/src/structural.ts +26 -15
- package/src/tooling.ts +24 -10
- package/src/transform.ts +22 -7
- package/src/utils/macro-names.ts +4 -0
- package/src/utils/namespace.ts +15 -2
- package/src/utils/object-path.ts +2 -2
- package/src/utils/stable-key.ts +17 -6
- package/src/utils/value-kinds.ts +38 -1
- package/src/widgets/ast-scanner.ts +4 -1
- package/src/widgets/register-widget-def.ts +2 -3
- package/src/widgets/widget-def.ts +36 -6
- package/src/widgets/widget-registry.ts +6 -2
- package/types/index.d.ts +12 -5
- package/types/tooling.d.ts +12 -2
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { useContext, useMemo } from 'preact/hooks';
|
|
2
2
|
import { useStoryFields } from './use-story-fields';
|
|
3
|
-
import { LocalsValuesContext } from '../markup/render';
|
|
3
|
+
import { LocalsUpdateContext, LocalsValuesContext } from '../markup/render';
|
|
4
|
+
import { liveLocalsView } from '../utils/live-locals';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The locals in scope, read as they are when code looks (a local the
|
|
8
|
+
* preceding {set} assigned in this pass included, which the context value
|
|
9
|
+
* only gains in the re-render that follows). The returned view is renewed
|
|
10
|
+
* whenever the context value changes, so what depends on it re-renders and
|
|
11
|
+
* recomputes as before.
|
|
12
|
+
*/
|
|
13
|
+
export function useLiveLocals(): Record<string, unknown> {
|
|
14
|
+
const values = useContext(LocalsValuesContext);
|
|
15
|
+
const { getValues } = useContext(LocalsUpdateContext);
|
|
16
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps -- `values` renews the view
|
|
17
|
+
return useMemo(() => liveLocalsView(getValues), [getValues, values]);
|
|
18
|
+
}
|
|
4
19
|
|
|
5
20
|
/**
|
|
6
21
|
* Return store variables, temporary, locals from context, and transient.
|
|
@@ -20,7 +35,7 @@ export function useMergedLocals(): readonly [
|
|
|
20
35
|
'transient',
|
|
21
36
|
'renderCounts',
|
|
22
37
|
);
|
|
23
|
-
const localsValues =
|
|
38
|
+
const localsValues = useLiveLocals();
|
|
24
39
|
|
|
25
40
|
return useMemo(() => {
|
|
26
41
|
return [variables, temporary, localsValues, transient] as const;
|
|
@@ -76,9 +76,11 @@ function tabIndexAttr(el: HTMLElement): number | null {
|
|
|
76
76
|
function isTabStop(el: HTMLElement): boolean {
|
|
77
77
|
const tabIndex = tabIndexAttr(el);
|
|
78
78
|
if (tabIndex !== null && tabIndex < 0) return false;
|
|
79
|
-
// Only an editing host takes focus, not `contenteditable="false"`
|
|
80
|
-
// content nested in a host
|
|
79
|
+
// Only an editing host takes focus of its own, not `contenteditable="false"`
|
|
80
|
+
// or content nested in a host; an explicit tabindex makes any of them a
|
|
81
|
+
// stop (#507)
|
|
81
82
|
if (
|
|
83
|
+
tabIndex === null &&
|
|
82
84
|
el.hasAttribute('contenteditable') &&
|
|
83
85
|
!el.matches('a[href], button, input, select, textarea, iframe, summary') &&
|
|
84
86
|
(!el.isContentEditable || el.parentElement?.isContentEditable)
|
|
@@ -95,15 +97,18 @@ function isTabStop(el: HTMLElement): boolean {
|
|
|
95
97
|
return tabIndex !== null;
|
|
96
98
|
}
|
|
97
99
|
}
|
|
98
|
-
|
|
100
|
+
// localName, not instanceof: elements of an embedded document are of
|
|
101
|
+
// another realm's classes
|
|
102
|
+
const input = el as HTMLInputElement;
|
|
103
|
+
if (el.localName === 'input' && input.type === 'radio' && input.name) {
|
|
99
104
|
const group = Array.from(
|
|
100
105
|
(
|
|
101
|
-
|
|
106
|
+
input.form ?? (el.getRootNode() as ParentNode)
|
|
102
107
|
).querySelectorAll<HTMLInputElement>('input[type="radio"]'),
|
|
103
108
|
).filter(
|
|
104
109
|
(r) =>
|
|
105
|
-
r.name ===
|
|
106
|
-
r.form ===
|
|
110
|
+
r.name === input.name &&
|
|
111
|
+
r.form === input.form &&
|
|
107
112
|
// Of the members Tab can reach (#390)
|
|
108
113
|
(tabIndexAttr(r) ?? 0) >= 0 &&
|
|
109
114
|
isAvailable(r),
|
|
@@ -115,10 +120,11 @@ function isTabStop(el: HTMLElement): boolean {
|
|
|
115
120
|
}
|
|
116
121
|
|
|
117
122
|
/**
|
|
118
|
-
* The elements of `root`
|
|
119
|
-
* first, ascending, then the
|
|
123
|
+
* The elements of `root` (the dialog panel, or an embedded document) in the
|
|
124
|
+
* order Tab visits them: positive tabindexes first, ascending, then the
|
|
125
|
+
* others in document order.
|
|
120
126
|
*/
|
|
121
|
-
function focusables(root:
|
|
127
|
+
function focusables(root: ParentNode): HTMLElement[] {
|
|
122
128
|
const stops = Array.from(
|
|
123
129
|
root.querySelectorAll<HTMLElement>(FOCUSABLE),
|
|
124
130
|
).filter((el) => isAvailable(el) && isTabStop(el));
|
|
@@ -132,8 +138,21 @@ function focusables(root: HTMLElement): HTMLElement[] {
|
|
|
132
138
|
];
|
|
133
139
|
}
|
|
134
140
|
|
|
141
|
+
/** An element that can take focus: HTML or SVG, of this realm or another. */
|
|
142
|
+
type Focusable = HTMLElement | SVGElement;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The opener of a dialog, outermost first: the focused element of the
|
|
146
|
+
* document, then the focused element of the frame it is, and so on. The last
|
|
147
|
+
* is the deepest one; the others are what to fall back on when it is gone
|
|
148
|
+
* (#530).
|
|
149
|
+
*/
|
|
150
|
+
type OpenerChain = Focusable[];
|
|
151
|
+
|
|
135
152
|
interface ModalEntry {
|
|
136
153
|
panel: HTMLElement;
|
|
154
|
+
/** Where focus returns on close; handed down when a lower dialog closes. */
|
|
155
|
+
opener: OpenerChain;
|
|
137
156
|
}
|
|
138
157
|
|
|
139
158
|
/** Open modal dialogs, innermost last. Only the topmost handles keys. */
|
|
@@ -158,20 +177,100 @@ const SEGMENTED_INPUTS = new Set([
|
|
|
158
177
|
|
|
159
178
|
/** Whether the element has native tab stops inside it, in its shadow tree. */
|
|
160
179
|
function hasNativeStops(el: Element | null): el is HTMLElement {
|
|
161
|
-
if (el
|
|
180
|
+
if (!el) return false;
|
|
181
|
+
if (el.localName === 'audio' || el.localName === 'video') {
|
|
182
|
+
return (el as HTMLMediaElement).controls;
|
|
183
|
+
}
|
|
184
|
+
const input = el as HTMLInputElement;
|
|
162
185
|
return (
|
|
163
|
-
el
|
|
164
|
-
SEGMENTED_INPUTS.has(
|
|
165
|
-
!
|
|
186
|
+
el.localName === 'input' &&
|
|
187
|
+
SEGMENTED_INPUTS.has(input.type) &&
|
|
188
|
+
!input.readOnly
|
|
166
189
|
);
|
|
167
190
|
}
|
|
168
191
|
|
|
169
|
-
/**
|
|
170
|
-
function
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
192
|
+
/** Whether `el` is an iframe element, of this realm or of an embedded one. */
|
|
193
|
+
function isFrame(el: Element): el is HTMLIFrameElement {
|
|
194
|
+
return el.localName === 'iframe';
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** The document of a frame, or null when it is not accessible. */
|
|
198
|
+
function frameDocument(frame: HTMLIFrameElement): Document | null {
|
|
199
|
+
try {
|
|
200
|
+
return frame.contentDocument;
|
|
201
|
+
} catch {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** The iframe element that holds `doc`, null for the top document. */
|
|
207
|
+
function frameOf(doc: Document): HTMLElement | null {
|
|
208
|
+
try {
|
|
209
|
+
return doc.defaultView?.frameElement as HTMLElement | null;
|
|
210
|
+
} catch {
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* The stop after (or before) `from` among `items`, the tab stops of `root`,
|
|
217
|
+
* undefined past the end. `from` may be no stop of its own: then the next one
|
|
218
|
+
* in document order. Outside `root`, the first (or last) stop.
|
|
219
|
+
*/
|
|
220
|
+
function nextStop(
|
|
221
|
+
items: HTMLElement[],
|
|
222
|
+
root: ParentNode,
|
|
223
|
+
from: Element | null,
|
|
224
|
+
backwards: boolean,
|
|
225
|
+
): HTMLElement | undefined {
|
|
226
|
+
const at = from ? items.indexOf(from as HTMLElement) : -1;
|
|
227
|
+
if (at !== -1) return items[at + (backwards ? -1 : 1)];
|
|
228
|
+
if (from && from !== root && root.contains(from)) {
|
|
229
|
+
// Inside the root but not a tab stop: continue from its position
|
|
230
|
+
const after = (el: HTMLElement) =>
|
|
231
|
+
!!(from.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_FOLLOWING);
|
|
232
|
+
return backwards
|
|
233
|
+
? items.filter((el) => !after(el)).pop()
|
|
234
|
+
: items.find(after);
|
|
235
|
+
}
|
|
236
|
+
return backwards ? items[items.length - 1] : items[0];
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The focused element, through the accessible frame documents: a frame holding
|
|
241
|
+
* the focus is followed to its own focused element, unless that is its body
|
|
242
|
+
* (nothing focused in it) or its document can't be reached (then the frame
|
|
243
|
+
* itself is the deepest). Elements of an embedded document are of another
|
|
244
|
+
* realm's classes, hence the duck typing. A focusable SVG element (a link in an
|
|
245
|
+
* illustration) counts (#483).
|
|
246
|
+
*/
|
|
247
|
+
function activeChain(): OpenerChain {
|
|
248
|
+
const chain: OpenerChain = [];
|
|
249
|
+
let doc: Document | null = document;
|
|
250
|
+
while (doc) {
|
|
251
|
+
const el: Element | null = doc.activeElement;
|
|
252
|
+
if (!el || (chain.length > 0 && el === doc.body)) break;
|
|
253
|
+
if (typeof (el as HTMLElement).focus !== 'function') break;
|
|
254
|
+
chain.push(el as Focusable);
|
|
255
|
+
doc = isFrame(el) ? frameDocument(el) : null;
|
|
256
|
+
}
|
|
257
|
+
return chain;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Whether `el` is still part of a live document. */
|
|
261
|
+
function isLive(el: Element): boolean {
|
|
262
|
+
return el.isConnected && !!el.ownerDocument.defaultView;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** Focus the deepest element of `chain` that is still there. */
|
|
266
|
+
function restoreOpener(chain: OpenerChain): void {
|
|
267
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
268
|
+
const el = chain[i]!;
|
|
269
|
+
if (isLive(el)) {
|
|
270
|
+
el.focus();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
175
274
|
}
|
|
176
275
|
|
|
177
276
|
export function useModalFocus(
|
|
@@ -184,11 +283,7 @@ export function useModalFocus(
|
|
|
184
283
|
const panel = panelRef.current;
|
|
185
284
|
if (!panel) return;
|
|
186
285
|
|
|
187
|
-
const opener
|
|
188
|
-
document.activeElement instanceof HTMLElement
|
|
189
|
-
? document.activeElement
|
|
190
|
-
: null;
|
|
191
|
-
const entry: ModalEntry = { panel };
|
|
286
|
+
const entry: ModalEntry = { panel, opener: activeChain() };
|
|
192
287
|
openModals.push(entry);
|
|
193
288
|
|
|
194
289
|
const body = panel.querySelector<HTMLElement>(bodySelector) ?? panel;
|
|
@@ -205,12 +300,22 @@ export function useModalFocus(
|
|
|
205
300
|
return () => {
|
|
206
301
|
const i = openModals.indexOf(entry);
|
|
207
302
|
if (i !== -1) openModals.splice(i, 1);
|
|
303
|
+
// Dialogs closing together unmount the lower one first, while focus is
|
|
304
|
+
// still in the upper: it can't restore anything yet, and the upper one's
|
|
305
|
+
// own opener is in this panel, about to be gone. The upper dialogs take
|
|
306
|
+
// over this one's opener (#529).
|
|
307
|
+
for (const upper of openModals.slice(i === -1 ? openModals.length : i)) {
|
|
308
|
+
const [outer] = upper.opener;
|
|
309
|
+
if (!outer || panel.contains(outer) || !upper.opener.some(isLive)) {
|
|
310
|
+
upper.opener = entry.opener;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
208
313
|
// Restore focus only if it was inside this dialog (or lost to <body>);
|
|
209
314
|
// don't steal it from somewhere author code moved it on purpose.
|
|
210
315
|
const active = document.activeElement;
|
|
211
316
|
const focusWasHere =
|
|
212
317
|
!active || active === document.body || panel.contains(active);
|
|
213
|
-
if (focusWasHere
|
|
318
|
+
if (focusWasHere) restoreOpener(entry.opener);
|
|
214
319
|
};
|
|
215
320
|
}, []);
|
|
216
321
|
|
|
@@ -220,32 +325,36 @@ export function useModalFocus(
|
|
|
220
325
|
|
|
221
326
|
const top = () => openModals[openModals.length - 1]?.panel === panel;
|
|
222
327
|
|
|
223
|
-
/**
|
|
224
|
-
* Move focus to the next (or previous) modal control from `from`. The
|
|
225
|
-
* order is computed here, never left to the browser: its positive-tabindex
|
|
226
|
-
* sequence spans the whole document and would lead out of the modal
|
|
227
|
-
* (#424). `from` may be an element of an embedded document, whose frame
|
|
228
|
-
* element then stands in for it.
|
|
229
|
-
*/
|
|
230
328
|
/** Focus `el`; a frame is entered at its first (or last) control (#460). */
|
|
231
329
|
const enter = (el: HTMLElement, backwards: boolean) => {
|
|
232
|
-
if (el
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
inner = frameStops(el.contentDocument);
|
|
236
|
-
} catch {
|
|
237
|
-
// Not accessible: the frame itself takes focus
|
|
238
|
-
}
|
|
330
|
+
if (isFrame(el)) {
|
|
331
|
+
const doc = frameDocument(el);
|
|
332
|
+
const inner = doc ? focusables(doc) : [];
|
|
239
333
|
const edge = backwards ? inner[inner.length - 1] : inner[0];
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
334
|
+
// A frame inside the frame is entered in turn
|
|
335
|
+
if (edge) return enter(edge, backwards);
|
|
336
|
+
// Not accessible or without controls: the frame itself takes focus
|
|
244
337
|
}
|
|
245
338
|
el.focus();
|
|
246
339
|
};
|
|
247
340
|
|
|
341
|
+
/**
|
|
342
|
+
* Move focus to the next (or previous) modal control from `from`. The
|
|
343
|
+
* order is computed here, never left to the browser: its positive-tabindex
|
|
344
|
+
* sequence spans the whole document and would lead out of the modal
|
|
345
|
+
* (#424). `from` is an element of the dialog, or of an embedded document;
|
|
346
|
+
* past the last control of a frame, focus goes on after the frame element
|
|
347
|
+
* in the document that holds it.
|
|
348
|
+
*/
|
|
248
349
|
const moveFocus = (from: Element | null, backwards: boolean) => {
|
|
350
|
+
const doc = from?.ownerDocument;
|
|
351
|
+
const frame = doc && doc !== document ? frameOf(doc) : null;
|
|
352
|
+
if (doc && frame) {
|
|
353
|
+
const next = nextStop(focusables(doc), doc, from, backwards);
|
|
354
|
+
if (next) enter(next, backwards);
|
|
355
|
+
else moveFocus(frame, backwards);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
249
358
|
const items = focusables(panel);
|
|
250
359
|
const first = items[0];
|
|
251
360
|
const last = items[items.length - 1];
|
|
@@ -253,108 +362,75 @@ export function useModalFocus(
|
|
|
253
362
|
panel.focus();
|
|
254
363
|
return;
|
|
255
364
|
}
|
|
256
|
-
const
|
|
257
|
-
let next: HTMLElement | undefined;
|
|
258
|
-
if (at !== -1) {
|
|
259
|
-
next = items[at + (backwards ? -1 : 1)];
|
|
260
|
-
} else if (from && from !== panel && panel.contains(from)) {
|
|
261
|
-
// Inside the panel but not a tab stop: continue from its position
|
|
262
|
-
const after = (el: HTMLElement) =>
|
|
263
|
-
!!(
|
|
264
|
-
from.compareDocumentPosition(el) & Node.DOCUMENT_POSITION_FOLLOWING
|
|
265
|
-
);
|
|
266
|
-
next = backwards
|
|
267
|
-
? items.filter((el) => !after(el)).pop()
|
|
268
|
-
: items.find(after);
|
|
269
|
-
} else {
|
|
270
|
-
next = backwards ? last : first;
|
|
271
|
-
}
|
|
365
|
+
const next = nextStop(items, panel, from, backwards);
|
|
272
366
|
enter(next ?? (backwards ? last : first), backwards);
|
|
273
367
|
};
|
|
274
368
|
|
|
275
369
|
// The element whose native inner controls (a media element's, a date
|
|
276
|
-
// input's segments) Tab is stepping through
|
|
277
|
-
|
|
370
|
+
// input's segments) Tab is stepping through, and the element of the
|
|
371
|
+
// dialog it stands in for (its frame element, for one in an embedded
|
|
372
|
+
// document)
|
|
373
|
+
let steppingNative: {
|
|
374
|
+
el: HTMLElement;
|
|
375
|
+
from: Element;
|
|
376
|
+
backwards: boolean;
|
|
377
|
+
} | null = null;
|
|
278
378
|
|
|
279
|
-
|
|
280
|
-
|
|
379
|
+
// Key events inside an embedded document never reach this one (#425), so
|
|
380
|
+
// every document of the dialog, the top one and the frames in it (#508),
|
|
381
|
+
// gets the handler. Escape closes the dialog. Tab moves on natively
|
|
382
|
+
// within a frame and is taken over at its first and last controls; `frame`
|
|
383
|
+
// is the frame element of an embedded `doc`.
|
|
384
|
+
const keyHandler = (doc: Document, frame: HTMLElement | null) => {
|
|
385
|
+
return (e: KeyboardEvent) => {
|
|
386
|
+
if (!top()) return;
|
|
281
387
|
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
388
|
+
if (e.key === 'Escape') {
|
|
389
|
+
if (!dismissible) return;
|
|
390
|
+
e.preventDefault();
|
|
391
|
+
e.stopPropagation();
|
|
392
|
+
onClose();
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
289
395
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
396
|
+
if (e.key !== 'Tab') return;
|
|
397
|
+
const active = doc.activeElement;
|
|
398
|
+
if (frame) {
|
|
399
|
+
steppingNative = null;
|
|
400
|
+
const inner = focusables(doc);
|
|
401
|
+
const edge = e.shiftKey ? inner[0] : inner[inner.length - 1];
|
|
402
|
+
if (edge && active !== edge && active !== doc.body) return;
|
|
403
|
+
}
|
|
404
|
+
// The controls of a media element and the segments of a date input
|
|
405
|
+
// are in its user-agent shadow tree, where the element stays the
|
|
406
|
+
// active one: they step through them natively, and onFocusIn takes
|
|
407
|
+
// over where focus leaves the element (#444, #461, #506).
|
|
408
|
+
if (hasNativeStops(active)) {
|
|
409
|
+
steppingNative = {
|
|
410
|
+
el: active,
|
|
411
|
+
from: frame ?? active,
|
|
412
|
+
backwards: e.shiftKey,
|
|
413
|
+
};
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
e.preventDefault();
|
|
417
|
+
moveFocus(frame ?? active, e.shiftKey);
|
|
418
|
+
};
|
|
302
419
|
};
|
|
303
420
|
// Clicking elsewhere ends the stepping through the controls
|
|
304
421
|
const onPointerDown = () => {
|
|
305
422
|
steppingNative = null;
|
|
306
423
|
};
|
|
307
424
|
|
|
308
|
-
// Key events inside an embedded document never reach this one (#425).
|
|
309
|
-
// Escape closes the dialog from there too; Tab moves on natively within
|
|
310
|
-
// the frame and is taken over at its first and last controls.
|
|
311
|
-
const controller = new AbortController();
|
|
312
|
-
const { signal } = controller;
|
|
313
|
-
const frameDocs = new Map<HTMLIFrameElement, Document>();
|
|
314
|
-
const attachFrame = (frame: HTMLIFrameElement) => {
|
|
315
|
-
let doc: Document | null = null;
|
|
316
|
-
try {
|
|
317
|
-
doc = frame.contentDocument;
|
|
318
|
-
} catch {
|
|
319
|
-
// Not accessible
|
|
320
|
-
}
|
|
321
|
-
// Already listening to this document
|
|
322
|
-
if (!doc || frameDocs.get(frame) === doc) return;
|
|
323
|
-
frameDocs.set(frame, doc);
|
|
324
|
-
const onFrameKeyDown = (e: KeyboardEvent) => {
|
|
325
|
-
if (!top()) return;
|
|
326
|
-
if (e.key !== 'Tab') return onKeyDown(e);
|
|
327
|
-
const inner = frameStops(doc!);
|
|
328
|
-
const active = doc!.activeElement;
|
|
329
|
-
const edge = e.shiftKey ? inner[0] : inner[inner.length - 1];
|
|
330
|
-
if (!edge || active === edge || active === doc!.body) {
|
|
331
|
-
e.preventDefault();
|
|
332
|
-
moveFocus(frame, e.shiftKey);
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
doc.addEventListener('keydown', onFrameKeyDown, { signal });
|
|
336
|
-
};
|
|
337
|
-
const attachFrames = () => {
|
|
338
|
-
panel.querySelectorAll('iframe').forEach(attachFrame);
|
|
339
|
-
};
|
|
340
|
-
// load doesn't bubble; a frame's document is replaced when it navigates
|
|
341
|
-
const onLoad = (e: Event) => {
|
|
342
|
-
if (e.target instanceof HTMLIFrameElement) attachFrame(e.target);
|
|
343
|
-
};
|
|
344
|
-
panel.addEventListener('load', onLoad, { capture: true, signal });
|
|
345
|
-
const frames = new MutationObserver(attachFrames);
|
|
346
|
-
frames.observe(panel, { childList: true, subtree: true });
|
|
347
|
-
attachFrames();
|
|
348
|
-
|
|
349
425
|
// Focus that lands outside the dialog anyway, e.g. leaving a frame whose
|
|
350
426
|
// document can't be reached, is brought back in.
|
|
351
427
|
const onFocusIn = (e: FocusEvent) => {
|
|
352
428
|
const target = e.target;
|
|
353
429
|
if (steppingNative && target !== steppingNative.el && top()) {
|
|
354
430
|
// Focus left the media controls: on to the modal's next control
|
|
355
|
-
const {
|
|
431
|
+
const { from, backwards } = steppingNative;
|
|
356
432
|
steppingNative = null;
|
|
357
|
-
moveFocus(
|
|
433
|
+
moveFocus(from, backwards);
|
|
358
434
|
return;
|
|
359
435
|
}
|
|
360
436
|
if (!top() || !(target instanceof Node) || panel.contains(target)) return;
|
|
@@ -364,11 +440,6 @@ export function useModalFocus(
|
|
|
364
440
|
moveFocus(null, false);
|
|
365
441
|
};
|
|
366
442
|
|
|
367
|
-
document.addEventListener('keydown', onKeyDown, { signal });
|
|
368
|
-
document.addEventListener('pointerdown', onPointerDown, {
|
|
369
|
-
capture: true,
|
|
370
|
-
signal,
|
|
371
|
-
});
|
|
372
443
|
// Focus that leaves the media element for nothing (past its last control,
|
|
373
444
|
// with no tab stop after it in the document) sends no focusin: the
|
|
374
445
|
// modal's next control takes it at once.
|
|
@@ -378,13 +449,56 @@ export function useModalFocus(
|
|
|
378
449
|
return;
|
|
379
450
|
}
|
|
380
451
|
steppingNative = null;
|
|
381
|
-
moveFocus(pending.
|
|
452
|
+
moveFocus(pending.from, pending.backwards);
|
|
382
453
|
};
|
|
383
|
-
|
|
454
|
+
|
|
455
|
+
const controller = new AbortController();
|
|
456
|
+
const { signal } = controller;
|
|
457
|
+
// The listeners of the dialog's document and of each embedded one
|
|
458
|
+
const listen = (doc: Document, frame: HTMLElement | null) => {
|
|
459
|
+
doc.addEventListener('keydown', keyHandler(doc, frame), { signal });
|
|
460
|
+
doc.addEventListener('pointerdown', onPointerDown, {
|
|
461
|
+
capture: true,
|
|
462
|
+
signal,
|
|
463
|
+
});
|
|
464
|
+
doc.addEventListener('focusout', onFocusOut, { signal });
|
|
465
|
+
};
|
|
466
|
+
const frameDocs = new Map<HTMLIFrameElement, Document>();
|
|
467
|
+
const observers: MutationObserver[] = [];
|
|
468
|
+
const attachFrame = (frame: HTMLIFrameElement) => {
|
|
469
|
+
const doc = frameDocument(frame);
|
|
470
|
+
// Already listening to this document
|
|
471
|
+
if (!doc || frameDocs.get(frame) === doc) return;
|
|
472
|
+
frameDocs.set(frame, doc);
|
|
473
|
+
listen(doc, frame);
|
|
474
|
+
watchFrames(doc);
|
|
475
|
+
};
|
|
476
|
+
// Attach to the frames in `root`, now and as they come and navigate.
|
|
477
|
+
// load doesn't bubble; a frame's document is replaced when it navigates
|
|
478
|
+
const watchFrames = (root: HTMLElement | Document) => {
|
|
479
|
+
const attachAll = () =>
|
|
480
|
+
root.querySelectorAll('iframe').forEach(attachFrame);
|
|
481
|
+
root.addEventListener(
|
|
482
|
+
'load',
|
|
483
|
+
(e) => {
|
|
484
|
+
if (e.target instanceof Node && isFrame(e.target as Element)) {
|
|
485
|
+
attachFrame(e.target as HTMLIFrameElement);
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
{ capture: true, signal },
|
|
489
|
+
);
|
|
490
|
+
const observer = new MutationObserver(attachAll);
|
|
491
|
+
observer.observe(root, { childList: true, subtree: true });
|
|
492
|
+
observers.push(observer);
|
|
493
|
+
attachAll();
|
|
494
|
+
};
|
|
495
|
+
watchFrames(panel);
|
|
496
|
+
|
|
497
|
+
listen(document, null);
|
|
384
498
|
document.addEventListener('focusin', onFocusIn, { signal });
|
|
385
499
|
return () => {
|
|
386
500
|
controller.abort();
|
|
387
|
-
|
|
501
|
+
observers.forEach((o) => o.disconnect());
|
|
388
502
|
};
|
|
389
503
|
}, [dismissible, onClose]);
|
|
390
504
|
}
|
package/src/index.tsx
CHANGED
|
@@ -24,9 +24,9 @@ import {
|
|
|
24
24
|
validateMarkup,
|
|
25
25
|
type MarkupPassage,
|
|
26
26
|
} from './markup/validate';
|
|
27
|
-
import {
|
|
27
|
+
import { effectiveWidgets, parseWidgetDef } from './widgets/widget-def';
|
|
28
28
|
import { parseMarkup } from './markup/parse';
|
|
29
|
-
import {
|
|
29
|
+
import { setBlockWidget } from './markup/ast';
|
|
30
30
|
import { registerWidgetDefinitions } from './widgets/register-widget-def';
|
|
31
31
|
import { errorMessage } from './utils/error-message';
|
|
32
32
|
import type { ASTNode } from './markup/ast';
|
|
@@ -76,6 +76,15 @@ function orStop<T>(parse: () => T, where = ''): T {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
/**
|
|
80
|
+
* What reading arguments depends on, with the macros and widgets known now:
|
|
81
|
+
* the declared parameters of macros and what the parameters of widgets hold.
|
|
82
|
+
* (The markup and the variable references are read by the same.)
|
|
83
|
+
*/
|
|
84
|
+
function knownParameters() {
|
|
85
|
+
return parameterLookup([...getMacroRegistry(), ...widgetMacros()]);
|
|
86
|
+
}
|
|
87
|
+
|
|
79
88
|
/**
|
|
80
89
|
* The undeclared variables the passages refer to, with the macros known now:
|
|
81
90
|
* the input macros that bind a variable (registered by author JS or
|
|
@@ -88,7 +97,12 @@ function variableErrors(
|
|
|
88
97
|
const storeVarMacros = getMacroRegistry()
|
|
89
98
|
.filter((m) => m.storeVar)
|
|
90
99
|
.map((m) => m.name);
|
|
91
|
-
return validatePassages(
|
|
100
|
+
return validatePassages(
|
|
101
|
+
storyData.passages,
|
|
102
|
+
schema,
|
|
103
|
+
storeVarMacros,
|
|
104
|
+
knownParameters(),
|
|
105
|
+
);
|
|
92
106
|
}
|
|
93
107
|
|
|
94
108
|
/**
|
|
@@ -100,12 +114,11 @@ function markupErrors(
|
|
|
100
114
|
storyData: StoryData,
|
|
101
115
|
only: (passage: MarkupPassage) => boolean,
|
|
102
116
|
): string[] {
|
|
103
|
-
const macros = getMacroRegistry();
|
|
104
117
|
return validateMarkup(storyData.passages.values(), {
|
|
105
118
|
isKnownMacro: (name) =>
|
|
106
119
|
!!getMacro(name) || isSubMacro(name) || !!getWidget(name),
|
|
107
|
-
macroNames:
|
|
108
|
-
parametersOf:
|
|
120
|
+
macroNames: getMacroRegistry().map((m) => m.name),
|
|
121
|
+
parametersOf: knownParameters(),
|
|
109
122
|
only,
|
|
110
123
|
}).map(formatDiagnostic);
|
|
111
124
|
}
|
|
@@ -163,14 +176,15 @@ export function boot() {
|
|
|
163
176
|
}
|
|
164
177
|
|
|
165
178
|
const schema = orStop(() => parseStoryVariables(storyVarsPassage.content));
|
|
166
|
-
// Pass 1:
|
|
167
|
-
//
|
|
168
|
-
//
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
|
|
179
|
+
// Pass 1: Record which widgets are block widgets BEFORE any passage is
|
|
180
|
+
// parsed (validation and StoryInit included), so that passages invoking
|
|
181
|
+
// widgets and widget bodies using other block widgets parse correctly
|
|
182
|
+
// regardless of passage or definition order. A widget defined more than
|
|
183
|
+
// once is what its latest definition is.
|
|
184
|
+
for (const { name, block } of orStop(() =>
|
|
185
|
+
effectiveWidgets(storyData.passages.values()),
|
|
172
186
|
)) {
|
|
173
|
-
|
|
187
|
+
setBlockWidget(name, block);
|
|
174
188
|
}
|
|
175
189
|
|
|
176
190
|
const errors = variableErrors(storyData, schema);
|
|
@@ -265,13 +279,10 @@ export function boot() {
|
|
|
265
279
|
|
|
266
280
|
render(<App />, root);
|
|
267
281
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
document.dispatchEvent(new CustomEvent(':storyready'));
|
|
273
|
-
});
|
|
274
|
-
} else {
|
|
282
|
+
// The event follows the render of the passage the story is at: after
|
|
283
|
+
// `ready()` when rendering is deferred (#430), and after a `{goto}` made
|
|
284
|
+
// while the first passage rendered has shown its destination (#505)
|
|
285
|
+
(getReadyPromise() ?? Promise.resolve()).then(passageShown).then(() => {
|
|
275
286
|
document.dispatchEvent(new CustomEvent(':storyready'));
|
|
276
|
-
}
|
|
287
|
+
});
|
|
277
288
|
}
|