@rohal12/spindle 0.59.30 → 0.59.32
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 +588 -253
- package/dist/pkg/story-variables.js +321 -52
- package/package.json +1 -1
- package/src/action-registry.ts +3 -1
- package/src/automation/runner.ts +4 -0
- package/src/components/PassageDialog.tsx +39 -8
- package/src/components/macros/Dialog.tsx +14 -3
- package/src/components/macros/PassageDisplay.tsx +106 -22
- package/src/components/macros/Repeat.tsx +4 -2
- package/src/components/macros/SaveManager.tsx +11 -4
- package/src/components/macros/Unset.tsx +26 -13
- package/src/hooks/use-modal-focus.ts +62 -9
- package/src/index.tsx +10 -9
- package/src/js-bindings.ts +299 -37
- package/src/js-lexer.ts +64 -11
- package/src/macro-discovery.ts +60 -12
- 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/store.ts +79 -5
- package/src/story-api.ts +10 -2
- package/src/structural.ts +7 -0
- package/src/tooling.ts +3 -2
- package/src/utils/macro-names.ts +4 -0
- package/src/utils/namespace.ts +11 -2
- 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 +3 -1
|
@@ -18,6 +18,7 @@ import { emitFromRender } from '../event-emitter';
|
|
|
18
18
|
import { useModalFocus } from '../hooks/use-modal-focus';
|
|
19
19
|
import { registerOpenDialog } from '../triggers';
|
|
20
20
|
import { errorMessage } from '../utils/error-message';
|
|
21
|
+
import { useAction } from '../hooks/use-action';
|
|
21
22
|
|
|
22
23
|
export const DialogCloseContext = createContext<(() => void) | null>(null);
|
|
23
24
|
|
|
@@ -137,14 +138,11 @@ export function PassageDialog({
|
|
|
137
138
|
tabIndex={-1}
|
|
138
139
|
>
|
|
139
140
|
{showCloseButton && (
|
|
140
|
-
<
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
>
|
|
146
|
-
✕
|
|
147
|
-
</button>
|
|
141
|
+
<DialogCloseButton
|
|
142
|
+
dialogName={passageName ?? label ?? 'dialog'}
|
|
143
|
+
passageName={passageName}
|
|
144
|
+
onClose={stableOnClose}
|
|
145
|
+
/>
|
|
148
146
|
)}
|
|
149
147
|
<div class="dialog-body">
|
|
150
148
|
<ViewScopeContext.Provider value={viewScope}>
|
|
@@ -156,3 +154,36 @@ export function PassageDialog({
|
|
|
156
154
|
</DialogCloseContext.Provider>
|
|
157
155
|
);
|
|
158
156
|
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The `✕` button, registered as a `close-dialog` action while the dialog is
|
|
160
|
+
* shown, so whatever opened it through the API can close it too (#540).
|
|
161
|
+
*/
|
|
162
|
+
function DialogCloseButton({
|
|
163
|
+
dialogName,
|
|
164
|
+
passageName,
|
|
165
|
+
onClose,
|
|
166
|
+
}: {
|
|
167
|
+
dialogName: string;
|
|
168
|
+
passageName?: string;
|
|
169
|
+
onClose: () => void;
|
|
170
|
+
}) {
|
|
171
|
+
useAction({
|
|
172
|
+
type: 'close-dialog',
|
|
173
|
+
key: dialogName,
|
|
174
|
+
label: 'Close',
|
|
175
|
+
target: passageName,
|
|
176
|
+
perform: onClose,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<button
|
|
181
|
+
type="button"
|
|
182
|
+
class="dialog-close"
|
|
183
|
+
aria-label="Close"
|
|
184
|
+
onClick={onClose}
|
|
185
|
+
>
|
|
186
|
+
✕
|
|
187
|
+
</button>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
@@ -9,15 +9,26 @@ defineMacro({
|
|
|
9
9
|
{ name: 'label', type: 'text', holds: 'markup', required: true },
|
|
10
10
|
{ name: 'noclose', type: 'flag' },
|
|
11
11
|
],
|
|
12
|
-
render({ children = [] }, ctx) {
|
|
12
|
+
render({ rawArgs, children = [] }, ctx) {
|
|
13
13
|
const [open, setOpen] = ctx.hooks.useState(false);
|
|
14
14
|
|
|
15
15
|
const { noclose } = ctx.args;
|
|
16
|
-
const label = ctx.resolve!(ctx.args.label ?? '');
|
|
16
|
+
const label = ctx.resolve!(ctx.args.label ?? '') ?? '';
|
|
17
17
|
const passageName = ctx
|
|
18
18
|
.collectText(children)
|
|
19
19
|
.trim()
|
|
20
20
|
.replace(/^["']|["']$/g, '');
|
|
21
|
+
const openDialog = () => setOpen(true);
|
|
22
|
+
|
|
23
|
+
// Its own type: the menubar's Saves and Settings are `dialog` (#540)
|
|
24
|
+
ctx.useAction({
|
|
25
|
+
type: 'open-dialog',
|
|
26
|
+
key: rawArgs,
|
|
27
|
+
authorId: ctx.id,
|
|
28
|
+
label,
|
|
29
|
+
target: passageName,
|
|
30
|
+
perform: openDialog,
|
|
31
|
+
});
|
|
21
32
|
|
|
22
33
|
return (
|
|
23
34
|
<>
|
|
@@ -25,7 +36,7 @@ defineMacro({
|
|
|
25
36
|
type="button"
|
|
26
37
|
id={ctx.id}
|
|
27
38
|
class={ctx.cls}
|
|
28
|
-
onClick={
|
|
39
|
+
onClick={openDialog}
|
|
29
40
|
>
|
|
30
41
|
{label}
|
|
31
42
|
</button>
|
|
@@ -15,12 +15,25 @@ import { Passage, renderPassageContent } from '../Passage';
|
|
|
15
15
|
import { defineMacro } from '../../define-macro';
|
|
16
16
|
import { resolveTransition, type ResolvedTransition } from '../../transition';
|
|
17
17
|
|
|
18
|
+
/** Input types whose `value` is not what the user sees or edits. */
|
|
19
|
+
const VALUELESS_INPUTS = new Set([
|
|
20
|
+
'checkbox',
|
|
21
|
+
'radio',
|
|
22
|
+
'file', // its value can not be set
|
|
23
|
+
'hidden',
|
|
24
|
+
'button',
|
|
25
|
+
'submit',
|
|
26
|
+
'reset',
|
|
27
|
+
'image',
|
|
28
|
+
]);
|
|
29
|
+
|
|
18
30
|
/**
|
|
19
31
|
* Copy the live state cloneNode drops from `source` into its clone: the
|
|
20
|
-
* pixels of each canvas
|
|
21
|
-
* attributes only, and a canvas's bitmap is not one)
|
|
22
|
-
*
|
|
23
|
-
*
|
|
32
|
+
* pixels of each canvas, the state of form controls (cloneNode keeps
|
|
33
|
+
* attributes only, and a canvas's bitmap is not one), and the visible frame
|
|
34
|
+
* and position of media. Source and clone have the same structure, so
|
|
35
|
+
* elements pair up by position. Best effort: a canvas or video that can not
|
|
36
|
+
* be drawn from stays as cloned.
|
|
24
37
|
*/
|
|
25
38
|
function copyLiveState(source: Element, clone: Element): void {
|
|
26
39
|
const pairs = <T extends Element>(
|
|
@@ -43,15 +56,92 @@ function copyLiveState(source: Element, clone: Element): void {
|
|
|
43
56
|
}
|
|
44
57
|
});
|
|
45
58
|
|
|
46
|
-
pairs<HTMLInputElement
|
|
47
|
-
|
|
48
|
-
|
|
59
|
+
pairs<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>(
|
|
60
|
+
'input, textarea, select',
|
|
61
|
+
(from, to) => {
|
|
62
|
+
if (from instanceof HTMLSelectElement) {
|
|
63
|
+
// `select.value` is the first selected option only: it loses the other
|
|
64
|
+
// selections of a multiple select (and the pick among options of one
|
|
65
|
+
// value), so a multiple select goes option by option
|
|
66
|
+
const select = to as HTMLSelectElement;
|
|
67
|
+
if (!from.multiple) {
|
|
68
|
+
select.selectedIndex = from.selectedIndex;
|
|
69
|
+
} else {
|
|
70
|
+
for (let i = 0; i < from.options.length; i++) {
|
|
71
|
+
const option = select.options[i];
|
|
72
|
+
if (option) option.selected = from.options[i]!.selected;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
} else if (from instanceof HTMLInputElement) {
|
|
76
|
+
const input = to as HTMLInputElement;
|
|
77
|
+
input.checked = from.checked;
|
|
78
|
+
input.indeterminate = from.indeterminate;
|
|
79
|
+
if (!VALUELESS_INPUTS.has(from.type)) input.value = from.value;
|
|
80
|
+
} else {
|
|
81
|
+
to.value = from.value;
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
pairs<HTMLMediaElement>('audio, video', (from, to) => {
|
|
87
|
+
// The snapshot is a picture: silent, still, and not started by autoplay
|
|
88
|
+
to.pause?.();
|
|
89
|
+
to.muted = true;
|
|
90
|
+
to.removeAttribute('autoplay');
|
|
91
|
+
try {
|
|
92
|
+
// Applied once the metadata is there: the progress of native controls
|
|
93
|
+
to.currentTime = from.currentTime;
|
|
94
|
+
} catch {
|
|
95
|
+
// no such position yet: the snapshot shows the start
|
|
96
|
+
}
|
|
97
|
+
if (!(from instanceof HTMLVideoElement && to instanceof HTMLVideoElement)) {
|
|
98
|
+
return;
|
|
49
99
|
}
|
|
50
|
-
// A
|
|
51
|
-
|
|
52
|
-
|
|
100
|
+
// A fresh clone decodes nothing, so it would show its first frame (or
|
|
101
|
+
// the poster). Draw the frame on screen now into the poster, which is
|
|
102
|
+
// shown at once and needs no load or seek to finish.
|
|
103
|
+
if (from.readyState < 2 || !from.videoWidth || !from.videoHeight) return;
|
|
104
|
+
try {
|
|
105
|
+
const frame = document.createElement('canvas');
|
|
106
|
+
frame.width = from.videoWidth;
|
|
107
|
+
frame.height = from.videoHeight;
|
|
108
|
+
frame.getContext('2d')?.drawImage(from, 0, 0);
|
|
109
|
+
to.setAttribute('poster', frame.toDataURL('image/jpeg', 0.92));
|
|
110
|
+
} catch {
|
|
111
|
+
// a cross-origin video without CORS taints the canvas: keeps its poster
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type ScrollOffset = [element: Element, top: number, left: number];
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Scroll offsets of the elements of `source` (itself included) that are
|
|
120
|
+
* scrolled, keyed by the matching element of `clone`. Applied once the clone
|
|
121
|
+
* is in the page: a detached element has nothing to scroll.
|
|
122
|
+
*/
|
|
123
|
+
function scrollOffsets(source: Element, clone: Element): ScrollOffset[] {
|
|
124
|
+
const from = [source, ...source.querySelectorAll('*')];
|
|
125
|
+
const to = [clone, ...clone.querySelectorAll('*')];
|
|
126
|
+
const offsets: ScrollOffset[] = [];
|
|
127
|
+
from.forEach((el, i) => {
|
|
128
|
+
if (to[i] && (el.scrollTop || el.scrollLeft)) {
|
|
129
|
+
offsets.push([to[i]!, el.scrollTop, el.scrollLeft]);
|
|
53
130
|
}
|
|
54
131
|
});
|
|
132
|
+
return offsets;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const pendingScrolls = new WeakMap<Element, ScrollOffset[]>();
|
|
136
|
+
|
|
137
|
+
/** Add `snapshot` to `container`, scrolled as what it shows was. */
|
|
138
|
+
function appendSnapshot(container: Element, snapshot: HTMLElement): void {
|
|
139
|
+
container.appendChild(snapshot);
|
|
140
|
+
for (const [el, top, left] of pendingScrolls.get(snapshot) ?? []) {
|
|
141
|
+
el.scrollTop = top;
|
|
142
|
+
el.scrollLeft = left;
|
|
143
|
+
}
|
|
144
|
+
pendingScrolls.delete(snapshot);
|
|
55
145
|
}
|
|
56
146
|
|
|
57
147
|
/**
|
|
@@ -59,8 +149,9 @@ function copyLiveState(source: Element, clone: Element): void {
|
|
|
59
149
|
* - Strips `id` attributes from the clone tree
|
|
60
150
|
* - Disables interaction (pointer-events, user-select, inert, aria-hidden)
|
|
61
151
|
* - Pauses/mutes any <audio>/<video>
|
|
62
|
-
* - Copies what cloneNode leaves behind: canvas pixels
|
|
63
|
-
*
|
|
152
|
+
* - Copies what cloneNode leaves behind: canvas pixels, the state of form
|
|
153
|
+
* controls, the visible frame of videos (#512, #525, #536) and, through
|
|
154
|
+
* `appendSnapshot`, scroll offsets
|
|
64
155
|
* - Adds `.passage-snapshot` class and `data-transition` attribute
|
|
65
156
|
*/
|
|
66
157
|
function createSnapshot(
|
|
@@ -69,6 +160,7 @@ function createSnapshot(
|
|
|
69
160
|
): HTMLElement {
|
|
70
161
|
const clone = passageEl.cloneNode(true) as HTMLElement;
|
|
71
162
|
copyLiveState(passageEl, clone);
|
|
163
|
+
pendingScrolls.set(clone, scrollOffsets(passageEl, clone));
|
|
72
164
|
|
|
73
165
|
// Strip id attributes to avoid duplicates
|
|
74
166
|
clone.removeAttribute('id');
|
|
@@ -85,14 +177,6 @@ function createSnapshot(
|
|
|
85
177
|
clone.setAttribute('inert', '');
|
|
86
178
|
clone.setAttribute('aria-hidden', 'true');
|
|
87
179
|
|
|
88
|
-
// Pause/mute media
|
|
89
|
-
const mediaEls = clone.querySelectorAll('audio, video');
|
|
90
|
-
for (let i = 0; i < mediaEls.length; i++) {
|
|
91
|
-
const media = mediaEls[i] as HTMLMediaElement;
|
|
92
|
-
media.pause?.();
|
|
93
|
-
media.muted = true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
180
|
// Mark as snapshot
|
|
97
181
|
clone.classList.remove('passage');
|
|
98
182
|
clone.classList.add('passage-snapshot');
|
|
@@ -333,7 +417,7 @@ defineMacro({
|
|
|
333
417
|
|
|
334
418
|
if (config.type === 'fade-through') {
|
|
335
419
|
// idle → outgoing (fade out snapshot) → paused → incoming (mount new, fade in) → idle
|
|
336
|
-
containerEl
|
|
420
|
+
appendSnapshot(containerEl, snapshot);
|
|
337
421
|
|
|
338
422
|
// Hide the current passage during outgoing phase by setting empty key
|
|
339
423
|
setDisplayedPassage('');
|
|
@@ -361,7 +445,7 @@ defineMacro({
|
|
|
361
445
|
|
|
362
446
|
// Use CSS grid stacking for crossfade
|
|
363
447
|
containerEl.classList.add('passage-container--crossfading');
|
|
364
|
-
containerEl
|
|
448
|
+
appendSnapshot(containerEl, snapshot);
|
|
365
449
|
|
|
366
450
|
// Mount new passage immediately alongside snapshot
|
|
367
451
|
setDisplayedPassage(currentPassage);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext } from 'preact';
|
|
1
|
+
import { createContext, Fragment } from 'preact';
|
|
2
2
|
import { defineMacro } from '../../define-macro';
|
|
3
3
|
import { DELAY_PARAMETER } from './macro-args';
|
|
4
4
|
import { OutgoingContext } from '../../hooks/use-story-fields';
|
|
@@ -37,7 +37,9 @@ defineMacro({
|
|
|
37
37
|
|
|
38
38
|
const content = (
|
|
39
39
|
<RepeatContext.Provider value={{ stop }}>
|
|
40
|
-
|
|
40
|
+
{/* A keyed Fragment remounts the body each tick without a DOM
|
|
41
|
+
wrapper that would break `tbody > tr`, `ul > li`, ... (#520) */}
|
|
42
|
+
<Fragment key={count}>{ctx.renderNodes(children)}</Fragment>
|
|
41
43
|
</RepeatContext.Provider>
|
|
42
44
|
);
|
|
43
45
|
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import {
|
|
9
9
|
useStoryStore,
|
|
10
10
|
resolvePlaythroughId,
|
|
11
|
+
followSavedPlaythrough,
|
|
11
12
|
issueStateReplacement,
|
|
12
13
|
isStateReplacementSuperseded,
|
|
13
14
|
} from '../../store';
|
|
@@ -154,7 +155,8 @@ export function SaveManagerContent({
|
|
|
154
155
|
try {
|
|
155
156
|
await saveWithHooks(undefined, undefined, beginSave, async (payload) => {
|
|
156
157
|
// Queued now, in call order with other storage operations
|
|
157
|
-
|
|
158
|
+
const saved = await createSave(ifid, playthrough, payload);
|
|
159
|
+
await followSavedPlaythrough(playthrough, saved);
|
|
158
160
|
});
|
|
159
161
|
showStatus(temporary ? 'Save created (temporary)' : 'Save created');
|
|
160
162
|
await refresh();
|
|
@@ -168,9 +170,14 @@ export function SaveManagerContent({
|
|
|
168
170
|
try {
|
|
169
171
|
await saveWithHooks(undefined, undefined, beginSave, async (payload) => {
|
|
170
172
|
// Only the current playthrough's saves offer "Save Here"
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
173
|
+
const saved = await overwriteSave(
|
|
174
|
+
saveId,
|
|
175
|
+
payload,
|
|
176
|
+
undefined,
|
|
177
|
+
playthrough,
|
|
178
|
+
);
|
|
179
|
+
if (!saved) throw new Error('Save not found');
|
|
180
|
+
await followSavedPlaythrough(playthrough, saved);
|
|
174
181
|
});
|
|
175
182
|
showStatus('Save overwritten');
|
|
176
183
|
await refresh();
|
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import { useStoryStore } from '../../store';
|
|
2
2
|
import { defineMacro } from '../../define-macro';
|
|
3
|
+
import { CodeSyntaxError, parseReferencePath } from '../../js-lexer';
|
|
4
|
+
import type { ReferencePath } from '../../js-lexer';
|
|
3
5
|
import { useRunOnce } from './MacroError';
|
|
4
6
|
|
|
7
|
+
/** What `name` is as a variable reference, or null if it is no reference. */
|
|
8
|
+
function referencePath(name: string): ReferencePath | null {
|
|
9
|
+
try {
|
|
10
|
+
return parseReferencePath(name);
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (error instanceof CodeSyntaxError) return null;
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
5
17
|
defineMacro({
|
|
6
18
|
name: 'unset',
|
|
7
19
|
parameters: [{ name: 'variable', type: 'variable', required: true }],
|
|
@@ -9,24 +21,25 @@ defineMacro({
|
|
|
9
21
|
return useRunOnce('unset', rawArgs, () => {
|
|
10
22
|
const state = useStoryStore.getState();
|
|
11
23
|
const name = ctx.args.variable ?? '';
|
|
24
|
+
const path = referencePath(name);
|
|
12
25
|
|
|
13
|
-
if (
|
|
26
|
+
if (!path) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`{unset} expects a variable ($name, _name, %name, or @name, or a field of one such as $name.field or $name["field"]), got "${name}"`,
|
|
29
|
+
);
|
|
30
|
+
} else if (path.nested) {
|
|
14
31
|
// A field of a variable: delete it as mutation code does, which
|
|
15
32
|
// resolves the path in every namespace
|
|
16
33
|
ctx.mutate(`delete ${name}`);
|
|
17
|
-
} else if (
|
|
18
|
-
state.deleteVariable(name
|
|
19
|
-
} else if (
|
|
20
|
-
state.deleteTemporary(name
|
|
21
|
-
} else if (
|
|
22
|
-
state.deleteTransient(name
|
|
23
|
-
} else if (name.startsWith('@')) {
|
|
24
|
-
// Throws outside a locals scope ({for}, widget, {link}, {button})
|
|
25
|
-
ctx.update(name.slice(1), undefined);
|
|
34
|
+
} else if (path.sigil === '$') {
|
|
35
|
+
state.deleteVariable(path.name);
|
|
36
|
+
} else if (path.sigil === '_') {
|
|
37
|
+
state.deleteTemporary(path.name);
|
|
38
|
+
} else if (path.sigil === '%') {
|
|
39
|
+
state.deleteTransient(path.name);
|
|
26
40
|
} else {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
);
|
|
41
|
+
// Throws outside a locals scope ({for}, widget, {link}, {button})
|
|
42
|
+
ctx.update(path.name, undefined);
|
|
30
43
|
}
|
|
31
44
|
});
|
|
32
45
|
},
|
|
@@ -138,8 +138,21 @@ function focusables(root: ParentNode): HTMLElement[] {
|
|
|
138
138
|
];
|
|
139
139
|
}
|
|
140
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
|
+
|
|
141
152
|
interface ModalEntry {
|
|
142
153
|
panel: HTMLElement;
|
|
154
|
+
/** Where focus returns on close; handed down when a lower dialog closes. */
|
|
155
|
+
opener: OpenerChain;
|
|
143
156
|
}
|
|
144
157
|
|
|
145
158
|
/** Open modal dialogs, innermost last. Only the topmost handles keys. */
|
|
@@ -223,6 +236,43 @@ function nextStop(
|
|
|
223
236
|
return backwards ? items[items.length - 1] : items[0];
|
|
224
237
|
}
|
|
225
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
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
226
276
|
export function useModalFocus(
|
|
227
277
|
panelRef: RefObject<HTMLElement | null>,
|
|
228
278
|
bodySelector: string,
|
|
@@ -233,14 +283,7 @@ export function useModalFocus(
|
|
|
233
283
|
const panel = panelRef.current;
|
|
234
284
|
if (!panel) return;
|
|
235
285
|
|
|
236
|
-
|
|
237
|
-
// (#483)
|
|
238
|
-
const focused = document.activeElement;
|
|
239
|
-
const opener =
|
|
240
|
-
focused instanceof HTMLElement || focused instanceof SVGElement
|
|
241
|
-
? focused
|
|
242
|
-
: null;
|
|
243
|
-
const entry: ModalEntry = { panel };
|
|
286
|
+
const entry: ModalEntry = { panel, opener: activeChain() };
|
|
244
287
|
openModals.push(entry);
|
|
245
288
|
|
|
246
289
|
const body = panel.querySelector<HTMLElement>(bodySelector) ?? panel;
|
|
@@ -257,12 +300,22 @@ export function useModalFocus(
|
|
|
257
300
|
return () => {
|
|
258
301
|
const i = openModals.indexOf(entry);
|
|
259
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
|
+
}
|
|
260
313
|
// Restore focus only if it was inside this dialog (or lost to <body>);
|
|
261
314
|
// don't steal it from somewhere author code moved it on purpose.
|
|
262
315
|
const active = document.activeElement;
|
|
263
316
|
const focusWasHere =
|
|
264
317
|
!active || active === document.body || panel.contains(active);
|
|
265
|
-
if (focusWasHere
|
|
318
|
+
if (focusWasHere) restoreOpener(entry.opener);
|
|
266
319
|
};
|
|
267
320
|
}, []);
|
|
268
321
|
|
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';
|
|
@@ -176,14 +176,15 @@ export function boot() {
|
|
|
176
176
|
}
|
|
177
177
|
|
|
178
178
|
const schema = orStop(() => parseStoryVariables(storyVarsPassage.content));
|
|
179
|
-
// Pass 1:
|
|
180
|
-
//
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
|
|
184
|
-
|
|
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()),
|
|
185
186
|
)) {
|
|
186
|
-
|
|
187
|
+
setBlockWidget(name, block);
|
|
187
188
|
}
|
|
188
189
|
|
|
189
190
|
const errors = variableErrors(storyData, schema);
|