@rohal12/spindle 0.64.18 → 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.
@@ -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). A throwing
9289
- * handler is logged instead of propagating into Preact and breaking the
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) => {
@@ -20517,6 +20518,24 @@ var useStoryStore = create()(storyStore((set, get) => ({
20517
20518
  })));
20518
20519
  // src/components/StoryInterface.tsx
20519
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
+ }
20520
20539
  function StoryInterface() {
20521
20540
  const { storyData } = useStoryFields("storyData");
20522
20541
  const overridePassage = storyData?.passages.get("StoryInterface");
@@ -20915,6 +20934,27 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
20915
20934
  }, [dismissible, onClose]);
20916
20935
  }
20917
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
+ }
20918
20958
  function PassageDialog({ passageName, fallbackMarkup, panelClass, onClose, label, dismissible = true, showCloseButton = dismissible }) {
20919
20959
  const onCloseRef = useRef(onClose);
20920
20960
  onCloseRef.current = onClose;
@@ -20956,7 +20996,13 @@ function PassageDialog({ passageName, fallbackMarkup, panelClass, onClose, label
20956
20996
  return () => panel.removeAttribute("aria-labelledby");
20957
20997
  }, [label, markup]);
20958
20998
  useLayoutEffect(() => {
20959
- if (panelRef.current) emitFromRender("dialogrender", passageName ?? "", 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);
20960
21006
  }, []);
20961
21007
  const handleBackdrop = (e) => {
20962
21008
  if (!dismissible) return;
@@ -26375,7 +26421,7 @@ function boot() {
26375
26421
  });
26376
26422
  connectTriggersToStore();
26377
26423
  const storyInterfacePassage = storyData.passages.get("StoryInterface");
26378
- if (storyInterfacePassage && !storyInterfacePassage.content.includes("{passage}")) console.warn("spindle: StoryInterface passage does not contain {passage}. The current passage will not be displayed.");
26424
+ if (storyInterfacePassage && !usesPassageMacro(storyInterfacePassage.content)) console.warn("spindle: StoryInterface passage does not contain {passage}. The current passage will not be displayed.");
26379
26425
  const root = document.getElementById("root");
26380
26426
  if (!root) throw new Error("spindle: No <div id=\"root\"> element found.");
26381
26427
  render(/* @__PURE__ */ jsx(App, {}), root);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.64.18",
3
+ "version": "0.64.19",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -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
- emitFromRender('dialogrender', passageName ?? '', panelRef.current);
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
 
@@ -23,6 +23,7 @@ type EventMap = {
23
23
  afternavigate: (to: string, from: string, kind: NavigationKind) => void;
24
24
  passagerender: (passage: string, element: HTMLElement) => void;
25
25
  dialogrender: (passage: string, element: HTMLElement) => void;
26
+ dialogclose: (passage: string) => void;
26
27
  };
27
28
 
28
29
  export type StoryEvent = keyof EventMap;
@@ -41,6 +42,7 @@ const VALID_EVENTS = new Set<string>([
41
42
  'afternavigate',
42
43
  'passagerender',
43
44
  'dialogrender',
45
+ 'dialogclose',
44
46
  ]);
45
47
 
46
48
  // Each event key maps to a Set of callbacks.
@@ -83,9 +85,9 @@ export function emit<E extends StoryEvent>(
83
85
  }
84
86
 
85
87
  /**
86
- * Emit an event from inside a render commit (layout effect). A throwing
87
- * handler is logged instead of propagating into Preact and breaking the
88
- * 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.
89
91
  */
90
92
  export function emitFromRender<E extends StoryEvent>(
91
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 contain {passage}
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.includes('{passage}')
300
+ !usesPassageMacro(storyInterfacePassage.content)
300
301
  ) {
301
302
  console.warn(
302
303
  'spindle: StoryInterface passage does not contain {passage}. ' +
package/types/index.d.ts CHANGED
@@ -168,6 +168,15 @@ export interface StoryEventMap {
168
168
  * without one).
169
169
  */
170
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;
171
180
  }
172
181
 
173
182
  /** Event name that can be passed to `Story.on()`. */