@rohal12/spindle 0.64.17 → 0.64.19
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 +86 -10
- package/package.json +1 -1
- package/src/components/PassageDialog.tsx +38 -4
- package/src/components/StoryInterface.tsx +25 -1
- package/src/event-emitter.ts +14 -5
- package/src/index.tsx +3 -2
- package/src/store.ts +6 -5
- package/src/story-api.ts +48 -0
- package/src/types-drift-check.ts +24 -1
- package/types/index.d.ts +52 -2
package/dist/pkg/headless.js
CHANGED
|
@@ -9260,7 +9260,8 @@ var VALID_EVENTS = /* @__PURE__ */ new Set([
|
|
|
9260
9260
|
"beforenavigate",
|
|
9261
9261
|
"afternavigate",
|
|
9262
9262
|
"passagerender",
|
|
9263
|
-
"dialogrender"
|
|
9263
|
+
"dialogrender",
|
|
9264
|
+
"dialogclose"
|
|
9264
9265
|
]);
|
|
9265
9266
|
var listeners$2 = /* @__PURE__ */ new Map();
|
|
9266
9267
|
function on(event, cb) {
|
|
@@ -9285,9 +9286,9 @@ function emit(event, ...args) {
|
|
|
9285
9286
|
forEachListener(event, (cb) => cb(...args));
|
|
9286
9287
|
}
|
|
9287
9288
|
/**
|
|
9288
|
-
* Emit an event from inside a render commit (layout effect)
|
|
9289
|
-
* handler is logged instead of propagating
|
|
9290
|
-
* render; the remaining handlers still run.
|
|
9289
|
+
* Emit an event from inside a render commit (layout effect), or once one is
|
|
9290
|
+
* over (`dialogclose`). A throwing handler is logged instead of propagating
|
|
9291
|
+
* into Preact and breaking the render; the remaining handlers still run.
|
|
9291
9292
|
*/
|
|
9292
9293
|
function emitFromRender(event, ...args) {
|
|
9293
9294
|
forEachListener(event, (cb) => {
|
|
@@ -19605,7 +19606,8 @@ function moveInHistory(get, set, step) {
|
|
|
19605
19606
|
const previousPassage = get().currentPassage;
|
|
19606
19607
|
const targetPassage = history[target].passage;
|
|
19607
19608
|
const navigation = get().navigationId;
|
|
19608
|
-
|
|
19609
|
+
const kind = step < 0 ? "back" : "forward";
|
|
19610
|
+
emit("beforenavigate", targetPassage, kind);
|
|
19609
19611
|
if (get().navigationId !== navigation) return;
|
|
19610
19612
|
target = get().historyIndex + step;
|
|
19611
19613
|
if (target < 0 || target >= get().history.length) return;
|
|
@@ -19621,7 +19623,7 @@ function moveInHistory(get, set, step) {
|
|
|
19621
19623
|
lastNavigationVars = get().variables;
|
|
19622
19624
|
restorePRNGFromMoment(get().history[get().historyIndex]);
|
|
19623
19625
|
keepDrawsSoFar();
|
|
19624
|
-
persistSessionThen(get, () => emit("afternavigate", targetPassage, previousPassage));
|
|
19626
|
+
persistSessionThen(get, () => emit("afternavigate", targetPassage, previousPassage, kind));
|
|
19625
19627
|
}
|
|
19626
19628
|
/**
|
|
19627
19629
|
* Record the state StoryInit left behind as the start moment: its variable
|
|
@@ -20180,7 +20182,7 @@ var useStoryStore = create()(storyStore((set, get) => ({
|
|
|
20180
20182
|
}
|
|
20181
20183
|
const previousPassage = get().currentPassage;
|
|
20182
20184
|
const navigation = get().navigationId;
|
|
20183
|
-
if (replacesState(() => emit("beforenavigate", passageName)) || get().navigationId !== navigation) return;
|
|
20185
|
+
if (replacesState(() => emit("beforenavigate", passageName, "navigate")) || get().navigationId !== navigation) return;
|
|
20184
20186
|
const patchEntry = computeVarPatches(lastNavigationVars, get().variables);
|
|
20185
20187
|
set((state) => {
|
|
20186
20188
|
state.temporary = createNamespace();
|
|
@@ -20215,7 +20217,7 @@ var useStoryStore = create()(storyStore((set, get) => ({
|
|
|
20215
20217
|
finishEnteredMoment(get, set);
|
|
20216
20218
|
keepDrawsSoFar();
|
|
20217
20219
|
persistSessionThen(get, () => {
|
|
20218
|
-
if (replacesState(() => emit("afternavigate", passageName, previousPassage))) return;
|
|
20220
|
+
if (replacesState(() => emit("afternavigate", passageName, previousPassage, "navigate"))) return;
|
|
20219
20221
|
for (const next of deferred) get().navigate(next);
|
|
20220
20222
|
});
|
|
20221
20223
|
},
|
|
@@ -20516,6 +20518,24 @@ var useStoryStore = create()(storyStore((set, get) => ({
|
|
|
20516
20518
|
})));
|
|
20517
20519
|
// src/components/StoryInterface.tsx
|
|
20518
20520
|
var DEFAULT_MARKUP = "<header class=\"story-menubar\">{story-title}{back}{forward}{restart}{quicksave}{quickload}{saves}{settings}</header>\n{passage}";
|
|
20521
|
+
/**
|
|
20522
|
+
* Whether interface markup uses the `{passage}` macro, however it is
|
|
20523
|
+
* written: with selectors (`{#story.main passage}`) and in any case
|
|
20524
|
+
* (`{Passage}`; macro names are case-insensitive). The text in an escaped
|
|
20525
|
+
* `\{passage}`, a comment, an attribute value, a label or code is no use
|
|
20526
|
+
* of the macro. Malformed markup counts as using it: the interface shows
|
|
20527
|
+
* its error then, which is the problem to report.
|
|
20528
|
+
*/
|
|
20529
|
+
function usesPassageMacro(markup) {
|
|
20530
|
+
let tokens;
|
|
20531
|
+
try {
|
|
20532
|
+
tokens = tokenizeMarkup(markup);
|
|
20533
|
+
} catch (err) {
|
|
20534
|
+
if (err instanceof MarkupError) return true;
|
|
20535
|
+
throw err;
|
|
20536
|
+
}
|
|
20537
|
+
return tokens.some((token) => token.type === "macro" && !token.isClose && token.name.toLowerCase() === "passage");
|
|
20538
|
+
}
|
|
20519
20539
|
function StoryInterface() {
|
|
20520
20540
|
const { storyData } = useStoryFields("storyData");
|
|
20521
20541
|
const overridePassage = storyData?.passages.get("StoryInterface");
|
|
@@ -20914,6 +20934,27 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
|
|
|
20914
20934
|
}, [dismissible, onClose]);
|
|
20915
20935
|
}
|
|
20916
20936
|
// src/components/PassageDialog.tsx
|
|
20937
|
+
/** How many dialogs have rendered: the place of the last in opening order. */
|
|
20938
|
+
var dialogsOpened = 0;
|
|
20939
|
+
/** The dialogs unmounted by the render pass under way, not announced yet. */
|
|
20940
|
+
var closedDialogs = [];
|
|
20941
|
+
/**
|
|
20942
|
+
* Emit `dialogclose` for a dialog that is unmounting, once the render pass
|
|
20943
|
+
* that unmounts it is over. The effect cleanups run before the dialog's DOM is
|
|
20944
|
+
* removed, and of dialogs closing together the lower one unmounts first,
|
|
20945
|
+
* while only the last restores focus (see useModalFocus): by the next
|
|
20946
|
+
* microtask the dialogs are gone, focus is back and `Story.isDialogOpen()`
|
|
20947
|
+
* tells the close. Dialogs that closed together are announced innermost
|
|
20948
|
+
* first.
|
|
20949
|
+
*/
|
|
20950
|
+
function announceDialogClose(closed) {
|
|
20951
|
+
if (closedDialogs.length === 0) queueMicrotask(() => {
|
|
20952
|
+
const dialogs = closedDialogs.sort((a, b) => b.opened - a.opened);
|
|
20953
|
+
closedDialogs = [];
|
|
20954
|
+
for (const { passage } of dialogs) emitFromRender("dialogclose", passage);
|
|
20955
|
+
});
|
|
20956
|
+
closedDialogs.push(closed);
|
|
20957
|
+
}
|
|
20917
20958
|
function PassageDialog({ passageName, fallbackMarkup, panelClass, onClose, label, dismissible = true, showCloseButton = dismissible }) {
|
|
20918
20959
|
const onCloseRef = useRef(onClose);
|
|
20919
20960
|
onCloseRef.current = onClose;
|
|
@@ -20955,7 +20996,13 @@ function PassageDialog({ passageName, fallbackMarkup, panelClass, onClose, label
|
|
|
20955
20996
|
return () => panel.removeAttribute("aria-labelledby");
|
|
20956
20997
|
}, [label, markup]);
|
|
20957
20998
|
useLayoutEffect(() => {
|
|
20958
|
-
if (panelRef.current)
|
|
20999
|
+
if (!panelRef.current) return;
|
|
21000
|
+
const closed = {
|
|
21001
|
+
passage: passageName ?? "",
|
|
21002
|
+
opened: ++dialogsOpened
|
|
21003
|
+
};
|
|
21004
|
+
emitFromRender("dialogrender", closed.passage, panelRef.current);
|
|
21005
|
+
return () => announceDialogClose(closed);
|
|
20959
21006
|
}, []);
|
|
20960
21007
|
const handleBackdrop = (e) => {
|
|
20961
21008
|
if (!dismissible) return;
|
|
@@ -22096,6 +22143,19 @@ function setOne(draft, name, value) {
|
|
|
22096
22143
|
const { isTransient, key } = parseName(name);
|
|
22097
22144
|
setByPath(isTransient ? draft.transient : draft.variables, key.split("."), value);
|
|
22098
22145
|
}
|
|
22146
|
+
/**
|
|
22147
|
+
* The history index `index` names (the current moment's when left out), or
|
|
22148
|
+
* a RangeError naming the moments the history holds.
|
|
22149
|
+
*/
|
|
22150
|
+
function momentIndex(index) {
|
|
22151
|
+
const { history, historyIndex } = useStoryStore.getState();
|
|
22152
|
+
const at = index ?? historyIndex;
|
|
22153
|
+
if (!Number.isInteger(at) || at < 0 || at >= history.length) {
|
|
22154
|
+
const held = history.length === 0 ? "no moments yet" : `moments 0 to ${history.length - 1}`;
|
|
22155
|
+
throw new RangeError(`spindle: History index ${String(at)} is out of range: the history holds ${held}.`);
|
|
22156
|
+
}
|
|
22157
|
+
return at;
|
|
22158
|
+
}
|
|
22099
22159
|
/** A method calling the store action `name`, as the store holds it then. */
|
|
22100
22160
|
function storeAction(name) {
|
|
22101
22161
|
return ((...args) => useStoryStore.getState()[name](...args));
|
|
@@ -22146,6 +22206,22 @@ function createStoryAPI() {
|
|
|
22146
22206
|
get passage() {
|
|
22147
22207
|
return useStoryStore.getState().currentPassage;
|
|
22148
22208
|
},
|
|
22209
|
+
history: {
|
|
22210
|
+
get length() {
|
|
22211
|
+
return useStoryStore.getState().history.length;
|
|
22212
|
+
},
|
|
22213
|
+
get index() {
|
|
22214
|
+
return useStoryStore.getState().historyIndex;
|
|
22215
|
+
},
|
|
22216
|
+
passage(index) {
|
|
22217
|
+
const at = momentIndex(index);
|
|
22218
|
+
return useStoryStore.getState().history[at].passage;
|
|
22219
|
+
},
|
|
22220
|
+
variables(index) {
|
|
22221
|
+
const at = momentIndex(index);
|
|
22222
|
+
return freeze(useStoryStore.getState().getHistoryVariables(at), true);
|
|
22223
|
+
}
|
|
22224
|
+
},
|
|
22149
22225
|
settings,
|
|
22150
22226
|
registerClass(name, ctor) {
|
|
22151
22227
|
registerClass(name, ctor);
|
|
@@ -26345,7 +26421,7 @@ function boot() {
|
|
|
26345
26421
|
});
|
|
26346
26422
|
connectTriggersToStore();
|
|
26347
26423
|
const storyInterfacePassage = storyData.passages.get("StoryInterface");
|
|
26348
|
-
if (storyInterfacePassage && !storyInterfacePassage.content
|
|
26424
|
+
if (storyInterfacePassage && !usesPassageMacro(storyInterfacePassage.content)) console.warn("spindle: StoryInterface passage does not contain {passage}. The current passage will not be displayed.");
|
|
26349
26425
|
const root = document.getElementById("root");
|
|
26350
26426
|
if (!root) throw new Error("spindle: No <div id=\"root\"> element found.");
|
|
26351
26427
|
render(/* @__PURE__ */ jsx(App, {}), root);
|
package/package.json
CHANGED
|
@@ -24,6 +24,38 @@ import { DialogCloseContext, DialogPanelContext } from './dialog-close-context';
|
|
|
24
24
|
|
|
25
25
|
export { DialogCloseContext };
|
|
26
26
|
|
|
27
|
+
/** A dialog that closed, by its passage and its place in the opening order. */
|
|
28
|
+
interface ClosedDialog {
|
|
29
|
+
passage: string;
|
|
30
|
+
opened: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** How many dialogs have rendered: the place of the last in opening order. */
|
|
34
|
+
let dialogsOpened = 0;
|
|
35
|
+
|
|
36
|
+
/** The dialogs unmounted by the render pass under way, not announced yet. */
|
|
37
|
+
let closedDialogs: ClosedDialog[] = [];
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Emit `dialogclose` for a dialog that is unmounting, once the render pass
|
|
41
|
+
* that unmounts it is over. The effect cleanups run before the dialog's DOM is
|
|
42
|
+
* removed, and of dialogs closing together the lower one unmounts first,
|
|
43
|
+
* while only the last restores focus (see useModalFocus): by the next
|
|
44
|
+
* microtask the dialogs are gone, focus is back and `Story.isDialogOpen()`
|
|
45
|
+
* tells the close. Dialogs that closed together are announced innermost
|
|
46
|
+
* first.
|
|
47
|
+
*/
|
|
48
|
+
function announceDialogClose(closed: ClosedDialog): void {
|
|
49
|
+
if (closedDialogs.length === 0) {
|
|
50
|
+
queueMicrotask(() => {
|
|
51
|
+
const dialogs = closedDialogs.sort((a, b) => b.opened - a.opened);
|
|
52
|
+
closedDialogs = [];
|
|
53
|
+
for (const { passage } of dialogs) emitFromRender('dialogclose', passage);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
closedDialogs.push(closed);
|
|
57
|
+
}
|
|
58
|
+
|
|
27
59
|
interface PassageDialogProps {
|
|
28
60
|
passageName?: string;
|
|
29
61
|
fallbackMarkup?: string;
|
|
@@ -112,11 +144,13 @@ export function PassageDialog({
|
|
|
112
144
|
return () => panel.removeAttribute('aria-labelledby');
|
|
113
145
|
}, [label, markup]);
|
|
114
146
|
|
|
115
|
-
// Signal that the dialog's DOM is committed (once per open)
|
|
147
|
+
// Signal that the dialog's DOM is committed (once per open), and that the
|
|
148
|
+
// dialog has closed (once for each dialogrender, with the same passage).
|
|
116
149
|
useLayoutEffect(() => {
|
|
117
|
-
if (panelRef.current)
|
|
118
|
-
|
|
119
|
-
|
|
150
|
+
if (!panelRef.current) return;
|
|
151
|
+
const closed = { passage: passageName ?? '', opened: ++dialogsOpened };
|
|
152
|
+
emitFromRender('dialogrender', closed.passage, panelRef.current);
|
|
153
|
+
return () => announceDialogClose(closed);
|
|
120
154
|
}, []);
|
|
121
155
|
|
|
122
156
|
const handleBackdrop = (e: MouseEvent) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useLayoutEffect, useMemo } from 'preact/hooks';
|
|
2
2
|
import { useStoryFields } from '../hooks/use-story-fields';
|
|
3
|
-
import { parseMarkup } from '../markup/parse';
|
|
3
|
+
import { MarkupError, parseMarkup, tokenizeMarkup } from '../markup/parse';
|
|
4
4
|
import {
|
|
5
5
|
renderInlineNodes,
|
|
6
6
|
InterfaceContext,
|
|
@@ -12,6 +12,30 @@ import { errorMessage } from '../utils/error-message';
|
|
|
12
12
|
const DEFAULT_MARKUP =
|
|
13
13
|
'<header class="story-menubar">{story-title}{back}{forward}{restart}{quicksave}{quickload}{saves}{settings}</header>\n{passage}';
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Whether interface markup uses the `{passage}` macro, however it is
|
|
17
|
+
* written: with selectors (`{#story.main passage}`) and in any case
|
|
18
|
+
* (`{Passage}`; macro names are case-insensitive). The text in an escaped
|
|
19
|
+
* `\{passage}`, a comment, an attribute value, a label or code is no use
|
|
20
|
+
* of the macro. Malformed markup counts as using it: the interface shows
|
|
21
|
+
* its error then, which is the problem to report.
|
|
22
|
+
*/
|
|
23
|
+
export function usesPassageMacro(markup: string): boolean {
|
|
24
|
+
let tokens;
|
|
25
|
+
try {
|
|
26
|
+
tokens = tokenizeMarkup(markup);
|
|
27
|
+
} catch (err) {
|
|
28
|
+
if (err instanceof MarkupError) return true;
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
31
|
+
return tokens.some(
|
|
32
|
+
(token) =>
|
|
33
|
+
token.type === 'macro' &&
|
|
34
|
+
!token.isClose &&
|
|
35
|
+
token.name.toLowerCase() === 'passage',
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
15
39
|
export function StoryInterface() {
|
|
16
40
|
const { storyData } = useStoryFields('storyData');
|
|
17
41
|
|
package/src/event-emitter.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the story moved: `'navigate'` recorded a new history moment (a link,
|
|
3
|
+
* `{goto}`, `Story.goto()`, a watcher's goto); `'back'` and `'forward'`
|
|
4
|
+
* moved through the moments already recorded.
|
|
5
|
+
*/
|
|
6
|
+
export type NavigationKind = 'navigate' | 'back' | 'forward';
|
|
7
|
+
|
|
1
8
|
type EventMap = {
|
|
2
9
|
storyinit: () => void;
|
|
3
10
|
beforerestart: () => void;
|
|
@@ -12,10 +19,11 @@ type EventMap = {
|
|
|
12
19
|
aftersave: (slot: string | undefined) => void;
|
|
13
20
|
beforeload: (slot: string | undefined) => void;
|
|
14
21
|
afterload: (slot: string | undefined) => void;
|
|
15
|
-
beforenavigate: (passageName: string) => void;
|
|
16
|
-
afternavigate: (to: string, from: string) => void;
|
|
22
|
+
beforenavigate: (passageName: string, kind: NavigationKind) => void;
|
|
23
|
+
afternavigate: (to: string, from: string, kind: NavigationKind) => void;
|
|
17
24
|
passagerender: (passage: string, element: HTMLElement) => void;
|
|
18
25
|
dialogrender: (passage: string, element: HTMLElement) => void;
|
|
26
|
+
dialogclose: (passage: string) => void;
|
|
19
27
|
};
|
|
20
28
|
|
|
21
29
|
export type StoryEvent = keyof EventMap;
|
|
@@ -34,6 +42,7 @@ const VALID_EVENTS = new Set<string>([
|
|
|
34
42
|
'afternavigate',
|
|
35
43
|
'passagerender',
|
|
36
44
|
'dialogrender',
|
|
45
|
+
'dialogclose',
|
|
37
46
|
]);
|
|
38
47
|
|
|
39
48
|
// Each event key maps to a Set of callbacks.
|
|
@@ -76,9 +85,9 @@ export function emit<E extends StoryEvent>(
|
|
|
76
85
|
}
|
|
77
86
|
|
|
78
87
|
/**
|
|
79
|
-
* Emit an event from inside a render commit (layout effect)
|
|
80
|
-
* handler is logged instead of propagating
|
|
81
|
-
* render; the remaining handlers still run.
|
|
88
|
+
* Emit an event from inside a render commit (layout effect), or once one is
|
|
89
|
+
* over (`dialogclose`). A throwing handler is logged instead of propagating
|
|
90
|
+
* into Preact and breaking the render; the remaining handlers still run.
|
|
82
91
|
*/
|
|
83
92
|
export function emitFromRender<E extends StoryEvent>(
|
|
84
93
|
event: E,
|
package/src/index.tsx
CHANGED
|
@@ -32,6 +32,7 @@ import { registerWidgetDefinitions } from './widgets/register-widget-def';
|
|
|
32
32
|
import { errorMessage } from './utils/error-message';
|
|
33
33
|
import type { ASTNode } from './markup/ast';
|
|
34
34
|
import { passageShown } from './components/macros/PassageDisplay';
|
|
35
|
+
import { usesPassageMacro } from './components/StoryInterface';
|
|
35
36
|
import './macros/register-builtins';
|
|
36
37
|
import builtinCSS from './styles.css?inline';
|
|
37
38
|
|
|
@@ -292,11 +293,11 @@ export function boot() {
|
|
|
292
293
|
// Wire up trigger system: check triggers on variable mutations, reinit on history nav/load
|
|
293
294
|
connectTriggersToStore();
|
|
294
295
|
|
|
295
|
-
// Warn if StoryInterface passage exists but doesn't
|
|
296
|
+
// Warn if StoryInterface passage exists but doesn't use {passage}
|
|
296
297
|
const storyInterfacePassage = storyData.passages.get('StoryInterface');
|
|
297
298
|
if (
|
|
298
299
|
storyInterfacePassage &&
|
|
299
|
-
!storyInterfacePassage.content
|
|
300
|
+
!usesPassageMacro(storyInterfacePassage.content)
|
|
300
301
|
) {
|
|
301
302
|
console.warn(
|
|
302
303
|
'spindle: StoryInterface passage does not contain {passage}. ' +
|
package/src/store.ts
CHANGED
|
@@ -18,7 +18,7 @@ import type {
|
|
|
18
18
|
SaveExport,
|
|
19
19
|
} from './saves/types';
|
|
20
20
|
import { executeStoryInit } from './story-init';
|
|
21
|
-
import { emit } from './event-emitter';
|
|
21
|
+
import { emit, type NavigationKind } from './event-emitter';
|
|
22
22
|
import {
|
|
23
23
|
resetTriggers,
|
|
24
24
|
checkTriggersOnNavigation,
|
|
@@ -520,7 +520,8 @@ function moveInHistory(get: StoreGet, set: StoreSet, step: -1 | 1): void {
|
|
|
520
520
|
// the game on, and this traversal is dropped rather than applied to a
|
|
521
521
|
// history that is no longer the one it was computed from (#516)
|
|
522
522
|
const navigation = get().navigationId;
|
|
523
|
-
|
|
523
|
+
const kind: NavigationKind = step < 0 ? 'back' : 'forward';
|
|
524
|
+
emit('beforenavigate', targetPassage, kind);
|
|
524
525
|
if (get().navigationId !== navigation) return;
|
|
525
526
|
|
|
526
527
|
// Lowering maxHistory in the callback can trim either end without a new
|
|
@@ -544,7 +545,7 @@ function moveInHistory(get: StoreGet, set: StoreSet, step: -1 | 1): void {
|
|
|
544
545
|
restorePRNGFromMoment(get().history[get().historyIndex]);
|
|
545
546
|
keepDrawsSoFar();
|
|
546
547
|
persistSessionThen(get, () =>
|
|
547
|
-
emit('afternavigate', targetPassage, previousPassage),
|
|
548
|
+
emit('afternavigate', targetPassage, previousPassage, kind),
|
|
548
549
|
);
|
|
549
550
|
}
|
|
550
551
|
|
|
@@ -1436,7 +1437,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1436
1437
|
// traversal, this navigation must then give way to the callback's.
|
|
1437
1438
|
const navigation = get().navigationId;
|
|
1438
1439
|
if (
|
|
1439
|
-
replacesState(() => emit('beforenavigate', passageName)) ||
|
|
1440
|
+
replacesState(() => emit('beforenavigate', passageName, 'navigate')) ||
|
|
1440
1441
|
get().navigationId !== navigation
|
|
1441
1442
|
)
|
|
1442
1443
|
return;
|
|
@@ -1493,7 +1494,7 @@ export const useStoryStore = create<StoryState>()(
|
|
|
1493
1494
|
persistSessionThen(get, () => {
|
|
1494
1495
|
if (
|
|
1495
1496
|
replacesState(() =>
|
|
1496
|
-
emit('afternavigate', passageName, previousPassage),
|
|
1497
|
+
emit('afternavigate', passageName, previousPassage, 'navigate'),
|
|
1497
1498
|
)
|
|
1498
1499
|
) {
|
|
1499
1500
|
return;
|
package/src/story-api.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { useStoryStore, trackRuntimeUnsub, recordHookWrite } from './store';
|
|
2
2
|
import type { StoryState, VariableNamespaces } from './store';
|
|
3
|
+
import { freeze } from 'immer';
|
|
3
4
|
import {
|
|
4
5
|
on as emitterOn,
|
|
5
6
|
emit,
|
|
7
|
+
type NavigationKind,
|
|
6
8
|
type StoryEvent,
|
|
7
9
|
type StoryEventCallback,
|
|
8
10
|
} from './event-emitter';
|
|
@@ -70,6 +72,7 @@ import type { TransitionConfig } from './transition';
|
|
|
70
72
|
|
|
71
73
|
export type { StoryAction };
|
|
72
74
|
export type { MacroMetadata };
|
|
75
|
+
export type { NavigationKind };
|
|
73
76
|
|
|
74
77
|
// Deferred-render promise lifecycle.
|
|
75
78
|
// deferRender() creates a promise; ready() resolves it. A deferRender() while
|
|
@@ -154,6 +157,12 @@ export interface StoryAPI {
|
|
|
154
157
|
previousPassage(): Passage | undefined;
|
|
155
158
|
readonly title: string;
|
|
156
159
|
readonly passage: string;
|
|
160
|
+
readonly history: {
|
|
161
|
+
readonly length: number;
|
|
162
|
+
readonly index: number;
|
|
163
|
+
passage(index?: number): string;
|
|
164
|
+
variables(index?: number): Record<string, unknown>;
|
|
165
|
+
};
|
|
157
166
|
readonly settings: typeof settings;
|
|
158
167
|
registerClass(name: string, ctor: new (...args: any[]) => any): void;
|
|
159
168
|
defineMacro(config: MacroDefinition): void;
|
|
@@ -280,6 +289,25 @@ function setOne(draft: VariableNamespaces, name: string, value: unknown): void {
|
|
|
280
289
|
setByPath(namespace, key.split('.'), value);
|
|
281
290
|
}
|
|
282
291
|
|
|
292
|
+
/**
|
|
293
|
+
* The history index `index` names (the current moment's when left out), or
|
|
294
|
+
* a RangeError naming the moments the history holds.
|
|
295
|
+
*/
|
|
296
|
+
function momentIndex(index: number | undefined): number {
|
|
297
|
+
const { history, historyIndex } = useStoryStore.getState();
|
|
298
|
+
const at = index ?? historyIndex;
|
|
299
|
+
if (!Number.isInteger(at) || at < 0 || at >= history.length) {
|
|
300
|
+
const held =
|
|
301
|
+
history.length === 0
|
|
302
|
+
? 'no moments yet'
|
|
303
|
+
: `moments 0 to ${history.length - 1}`;
|
|
304
|
+
throw new RangeError(
|
|
305
|
+
`spindle: History index ${String(at)} is out of range: the history holds ${held}.`,
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
return at;
|
|
309
|
+
}
|
|
310
|
+
|
|
283
311
|
/** A method calling the store action `name`, as the store holds it then. */
|
|
284
312
|
function storeAction<K extends keyof StoryState>(name: K): StoryState[K] {
|
|
285
313
|
return ((...args: unknown[]) =>
|
|
@@ -364,6 +392,26 @@ function createStoryAPI(): StoryAPI {
|
|
|
364
392
|
return useStoryStore.getState().currentPassage;
|
|
365
393
|
},
|
|
366
394
|
|
|
395
|
+
history: {
|
|
396
|
+
get length(): number {
|
|
397
|
+
return useStoryStore.getState().history.length;
|
|
398
|
+
},
|
|
399
|
+
get index(): number {
|
|
400
|
+
return useStoryStore.getState().historyIndex;
|
|
401
|
+
},
|
|
402
|
+
passage(index?: number): string {
|
|
403
|
+
const at = momentIndex(index);
|
|
404
|
+
return useStoryStore.getState().history[at]!.passage;
|
|
405
|
+
},
|
|
406
|
+
variables(index?: number): Record<string, unknown> {
|
|
407
|
+
const at = momentIndex(index);
|
|
408
|
+
// The copy Back and Forward would restore, frozen like the values
|
|
409
|
+
// Story.get() hands out: it is the caller's own, and a write to it
|
|
410
|
+
// fails instead of seeming to change the recorded moment
|
|
411
|
+
return freeze(useStoryStore.getState().getHistoryVariables(at), true);
|
|
412
|
+
},
|
|
413
|
+
},
|
|
414
|
+
|
|
367
415
|
settings,
|
|
368
416
|
|
|
369
417
|
registerClass(name: string, ctor: new (...args: any[]) => any): void {
|
package/src/types-drift-check.ts
CHANGED
|
@@ -5,7 +5,14 @@
|
|
|
5
5
|
*
|
|
6
6
|
* Run: npx tsc --noEmit
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type {
|
|
9
|
+
NavigationKind as SourceNavigationKind,
|
|
10
|
+
StoryAPI as SourceAPI,
|
|
11
|
+
} from './story-api';
|
|
12
|
+
import type {
|
|
13
|
+
StoryEvent as SourceStoryEvent,
|
|
14
|
+
StoryEventCallback as SourceStoryEventCallback,
|
|
15
|
+
} from './event-emitter';
|
|
9
16
|
import type { parseStoryVariables as SourceParse } from './story-variables';
|
|
10
17
|
import type {
|
|
11
18
|
MacroContext as SourceMacroContext,
|
|
@@ -16,6 +23,9 @@ import type { ASTNode as SourceASTNode } from './markup/ast';
|
|
|
16
23
|
import type { bootStory as SourceBootStory } from './headless';
|
|
17
24
|
import type {
|
|
18
25
|
StoryAPI as PublishedAPI,
|
|
26
|
+
NavigationKind as PublishedNavigationKind,
|
|
27
|
+
StoryEvent as PublishedStoryEvent,
|
|
28
|
+
StoryEventCallback as PublishedStoryEventCallback,
|
|
19
29
|
MacroContext as PublishedMacroContext,
|
|
20
30
|
MacroDefinition as PublishedMacroDefinition,
|
|
21
31
|
MacroProps as PublishedMacroProps,
|
|
@@ -165,6 +175,19 @@ type ToolingChecks = [
|
|
|
165
175
|
];
|
|
166
176
|
type _toolingTypes = AssertTrue<ToolingChecks>;
|
|
167
177
|
|
|
178
|
+
// The story events and what each passes to its callbacks. The `on()` of the
|
|
179
|
+
// two StoryAPI types above stay assignable to each other when a callback
|
|
180
|
+
// gains or loses an argument, so the arguments are compared here.
|
|
181
|
+
type EventChecks = [
|
|
182
|
+
Same<SourceStoryEvent, PublishedStoryEvent>,
|
|
183
|
+
Same<
|
|
184
|
+
{ [E in SourceStoryEvent]: Parameters<SourceStoryEventCallback<E>> },
|
|
185
|
+
{ [E in PublishedStoryEvent]: Parameters<PublishedStoryEventCallback<E>> }
|
|
186
|
+
>,
|
|
187
|
+
Same<SourceNavigationKind, PublishedNavigationKind>,
|
|
188
|
+
];
|
|
189
|
+
type _eventTypes = AssertTrue<EventChecks>;
|
|
190
|
+
|
|
168
191
|
// A typical custom macro written against the published types must type-check,
|
|
169
192
|
// and misuse of the hooks must not (no `any` leaking through).
|
|
170
193
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
package/types/index.d.ts
CHANGED
|
@@ -118,6 +118,16 @@ export interface Passage {
|
|
|
118
118
|
content: string;
|
|
119
119
|
}
|
|
120
120
|
|
|
121
|
+
/**
|
|
122
|
+
* How the story moved, as the navigation events report it: `'navigate'`
|
|
123
|
+
* recorded a new history moment (a link, `{goto}`, `Story.goto()`, a
|
|
124
|
+
* watcher's goto); `'back'` and `'forward'` moved through the moments already
|
|
125
|
+
* recorded (`Story.back()`, `Story.forward()`, the `{back}` and `{forward}`
|
|
126
|
+
* buttons).
|
|
127
|
+
* @see {@link ../../src/event-emitter.ts} for the implementation.
|
|
128
|
+
*/
|
|
129
|
+
export type NavigationKind = 'navigate' | 'back' | 'forward';
|
|
130
|
+
|
|
121
131
|
/**
|
|
122
132
|
* Map of story event names to their callback signatures.
|
|
123
133
|
* @see {@link ../../src/event-emitter.ts} for the implementation.
|
|
@@ -136,8 +146,16 @@ export interface StoryEventMap {
|
|
|
136
146
|
aftersave: (slot: string | undefined) => void;
|
|
137
147
|
beforeload: (slot: string | undefined) => void;
|
|
138
148
|
afterload: (slot: string | undefined) => void;
|
|
139
|
-
|
|
140
|
-
|
|
149
|
+
/**
|
|
150
|
+
* The story is about to move to `passageName`. `Story.passage` and
|
|
151
|
+
* `Story.history` still describe the moment being left.
|
|
152
|
+
*/
|
|
153
|
+
beforenavigate: (passageName: string, kind: NavigationKind) => void;
|
|
154
|
+
/**
|
|
155
|
+
* The story moved from passage `from` to passage `to`. `Story.passage` and
|
|
156
|
+
* `Story.history` describe the moment arrived at.
|
|
157
|
+
*/
|
|
158
|
+
afternavigate: (to: string, from: string, kind: NavigationKind) => void;
|
|
141
159
|
/**
|
|
142
160
|
* A passage's `.passage` element was committed to the DOM (first render,
|
|
143
161
|
* navigation, back/forward, restart, load). Fires after the transition
|
|
@@ -150,6 +168,15 @@ export interface StoryEventMap {
|
|
|
150
168
|
* without one).
|
|
151
169
|
*/
|
|
152
170
|
dialogrender: (passage: string, element: HTMLElement) => void;
|
|
171
|
+
/**
|
|
172
|
+
* A dialog has closed, whichever way: fires once for each `dialogrender`,
|
|
173
|
+
* with the same `passage`, after the dialog is removed from the DOM (a
|
|
174
|
+
* passage transition may still show an inert copy of the outgoing passage,
|
|
175
|
+
* dialog included) and focus is back on the element that had it before the
|
|
176
|
+
* dialog opened. Not fired for dialogs still open when the page is closed
|
|
177
|
+
* or reloaded.
|
|
178
|
+
*/
|
|
179
|
+
dialogclose: (passage: string) => void;
|
|
153
180
|
}
|
|
154
181
|
|
|
155
182
|
/** Event name that can be passed to `Story.on()`. */
|
|
@@ -812,6 +839,29 @@ export interface StoryAPI {
|
|
|
812
839
|
/** The current passage name. */
|
|
813
840
|
readonly passage: string;
|
|
814
841
|
|
|
842
|
+
/**
|
|
843
|
+
* The history: the moments Back and Forward move through, oldest first.
|
|
844
|
+
* Read-only. `Story.config.maxHistory` limits how many are kept.
|
|
845
|
+
*/
|
|
846
|
+
readonly history: {
|
|
847
|
+
/** The number of moments kept. */
|
|
848
|
+
readonly length: number;
|
|
849
|
+
/** The index of the current moment (0 is the oldest kept). */
|
|
850
|
+
readonly index: number;
|
|
851
|
+
/**
|
|
852
|
+
* The passage name of the moment at `index` (the current moment when
|
|
853
|
+
* left out). Throws a `RangeError` if the history has no such moment.
|
|
854
|
+
*/
|
|
855
|
+
passage(index?: number): string;
|
|
856
|
+
/**
|
|
857
|
+
* The story variables recorded for the moment at `index` (the current
|
|
858
|
+
* moment when left out): the state on entering it, which Back and
|
|
859
|
+
* Forward restore. A frozen copy, keyed by name without the `$`. Throws a
|
|
860
|
+
* `RangeError` if the history has no such moment.
|
|
861
|
+
*/
|
|
862
|
+
variables(index?: number): Record<string, unknown>;
|
|
863
|
+
};
|
|
864
|
+
|
|
815
865
|
/** The settings API. */
|
|
816
866
|
readonly settings: SettingsAPI;
|
|
817
867
|
|