@rohal12/spindle 0.59.31 → 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.
@@ -19357,6 +19357,161 @@ function useModalFocus(panelRef, bodySelector, dismissible, onClose) {
19357
19357
  }, [dismissible, onClose]);
19358
19358
  }
19359
19359
  //#endregion
19360
+ //#region src/action-registry.ts
19361
+ /** The fields of a StoryAction that are left out when unset. */
19362
+ var OPTIONAL_ACTION_FIELDS = [
19363
+ "target",
19364
+ "variable",
19365
+ "options",
19366
+ "value",
19367
+ "disabled"
19368
+ ];
19369
+ /**
19370
+ * The registrations holding each ID, oldest first; the newest is the one
19371
+ * listed. Generated IDs don't collide, but author IDs can, and one control's
19372
+ * cleanup must not remove another control holding the same ID (#233).
19373
+ */
19374
+ var actions = /* @__PURE__ */ new Map();
19375
+ var idCounters = /* @__PURE__ */ new Map();
19376
+ /**
19377
+ * IDs handed out and not yet registered, or registered: a control registers
19378
+ * in a layout effect, after every control of the same render has allocated its
19379
+ * ID, so the registry alone can't tell that "link:Room:2" is taken (#486).
19380
+ * Released when the last registration of an ID goes.
19381
+ */
19382
+ var reserved = /* @__PURE__ */ new Set();
19383
+ /**
19384
+ * The counters reset on every navigation, so a passage's controls get the
19385
+ * same IDs each time it is shown. Controls that stay mounted across
19386
+ * navigations (e.g. in StoryInterface) keep theirs, so allocation skips every
19387
+ * ID still registered (#233) and every one allocated but not registered yet
19388
+ * (#486).
19389
+ */
19390
+ function generateActionId(type, key, authorId) {
19391
+ if (authorId) {
19392
+ reserved.add(authorId);
19393
+ return authorId;
19394
+ }
19395
+ const base = `${type}:${key}`;
19396
+ let count = idCounters.get(base) ?? 0;
19397
+ let id;
19398
+ do {
19399
+ count++;
19400
+ id = count === 1 ? base : `${base}:${count}`;
19401
+ } while (actions.has(id) || reserved.has(id));
19402
+ idCounters.set(base, count);
19403
+ reserved.add(id);
19404
+ return id;
19405
+ }
19406
+ function registerAction(action) {
19407
+ const id = action.id;
19408
+ const entry = { action };
19409
+ const entries = actions.get(id);
19410
+ if (entries) {
19411
+ console.warn(`spindle: two mounted controls share the action ID "${id}"; Story.getActions() lists only the newer one.`);
19412
+ entries.push(entry);
19413
+ } else actions.set(id, [entry]);
19414
+ notify();
19415
+ return {
19416
+ update(next) {
19417
+ const current = actions.get(id);
19418
+ if (!current?.includes(entry)) return;
19419
+ entry.action = next;
19420
+ if (current[current.length - 1] === entry) notify();
19421
+ },
19422
+ unregister() {
19423
+ const current = actions.get(id);
19424
+ const index = current ? current.indexOf(entry) : -1;
19425
+ if (!current || index === -1) return;
19426
+ current.splice(index, 1);
19427
+ if (current.length === 0) {
19428
+ actions.delete(id);
19429
+ reserved.delete(id);
19430
+ }
19431
+ if (index === current.length) notify();
19432
+ }
19433
+ };
19434
+ }
19435
+ /** The promises of the performed actions that have not settled yet. */
19436
+ var running = /* @__PURE__ */ new Set();
19437
+ /**
19438
+ * Perform an action, as `Story.performAction()` does. One that returns a
19439
+ * promise is tracked until it settles, for `settleRunningActions()`, and the
19440
+ * promise is returned for the caller to await (its rejection stays the
19441
+ * caller's to handle).
19442
+ */
19443
+ function performRegisteredAction(action, value) {
19444
+ const result = action.perform(value);
19445
+ if (!result || typeof result.then !== "function") return result;
19446
+ const done = () => void running.delete(result);
19447
+ running.add(result);
19448
+ result.then(done, done);
19449
+ return result.then((v) => v);
19450
+ }
19451
+ /** Wait until every performed action has finished, whether or not it failed. */
19452
+ async function settleRunningActions() {
19453
+ while (running.size > 0) await Promise.allSettled(running);
19454
+ }
19455
+ function getActions() {
19456
+ return Array.from(actions.values(), listed);
19457
+ }
19458
+ function getAction(id) {
19459
+ const entries = actions.get(id);
19460
+ return entries && listed(entries);
19461
+ }
19462
+ function listed(entries) {
19463
+ return entries[entries.length - 1].action;
19464
+ }
19465
+ function resetIdCounters() {
19466
+ idCounters.clear();
19467
+ for (const id of reserved) if (!actions.has(id)) reserved.delete(id);
19468
+ }
19469
+ function notify() {
19470
+ emit("actionsChanged");
19471
+ }
19472
+ //#endregion
19473
+ //#region src/hooks/use-action.ts
19474
+ function useAction(opts) {
19475
+ const idRef = useRef("");
19476
+ const performRef = useRef(opts.perform);
19477
+ performRef.current = opts.perform;
19478
+ if (!idRef.current) idRef.current = generateActionId(opts.type, opts.key, opts.authorId);
19479
+ const id = idRef.current;
19480
+ const buildAction = () => {
19481
+ const action = {
19482
+ id,
19483
+ type: opts.type,
19484
+ label: opts.label,
19485
+ perform: (...args) => performRef.current(...args)
19486
+ };
19487
+ for (const field of OPTIONAL_ACTION_FIELDS) if (opts[field] !== void 0) Object.assign(action, { [field]: opts[field] });
19488
+ return action;
19489
+ };
19490
+ const registration = useRef(null);
19491
+ useLayoutEffect(() => {
19492
+ const own = registerAction(buildAction());
19493
+ registration.current = own;
19494
+ return () => own.unregister();
19495
+ }, [id]);
19496
+ const registered = useRef(false);
19497
+ useLayoutEffect(() => {
19498
+ if (!registered.current) {
19499
+ registered.current = true;
19500
+ return;
19501
+ }
19502
+ registration.current?.update(buildAction());
19503
+ }, [
19504
+ opts.type,
19505
+ opts.label,
19506
+ opts.target,
19507
+ opts.variable,
19508
+ opts.disabled,
19509
+ opts.value,
19510
+ JSON.stringify(opts.options)
19511
+ ]);
19512
+ return id;
19513
+ }
19514
+ //#endregion
19360
19515
  //#region src/components/PassageDialog.tsx
19361
19516
  var DialogCloseContext = createContext(null);
19362
19517
  function PassageDialog({ passageName, fallbackMarkup, panelClass, onClose, label, dismissible = true, showCloseButton = dismissible }) {
@@ -19419,12 +19574,10 @@ function PassageDialog({ passageName, fallbackMarkup, panelClass, onClose, label
19419
19574
  "aria-modal": "true",
19420
19575
  "aria-label": label ?? passageName,
19421
19576
  tabIndex: -1,
19422
- children: [showCloseButton && /* @__PURE__ */ jsx("button", {
19423
- type: "button",
19424
- class: "dialog-close",
19425
- "aria-label": "Close",
19426
- onClick: stableOnClose,
19427
- children: "✕"
19577
+ children: [showCloseButton && /* @__PURE__ */ jsx(DialogCloseButton, {
19578
+ dialogName: passageName ?? label ?? "dialog",
19579
+ passageName,
19580
+ onClose: stableOnClose
19428
19581
  }), /* @__PURE__ */ jsx("div", {
19429
19582
  class: "dialog-body",
19430
19583
  children: /* @__PURE__ */ jsx(ViewScopeContext.Provider, {
@@ -19436,6 +19589,26 @@ function PassageDialog({ passageName, fallbackMarkup, panelClass, onClose, label
19436
19589
  })
19437
19590
  });
19438
19591
  }
19592
+ /**
19593
+ * The `✕` button, registered as a `close-dialog` action while the dialog is
19594
+ * shown, so whatever opened it through the API can close it too (#540).
19595
+ */
19596
+ function DialogCloseButton({ dialogName, passageName, onClose }) {
19597
+ useAction({
19598
+ type: "close-dialog",
19599
+ key: dialogName,
19600
+ label: "Close",
19601
+ target: passageName,
19602
+ perform: onClose
19603
+ });
19604
+ return /* @__PURE__ */ jsx("button", {
19605
+ type: "button",
19606
+ class: "dialog-close",
19607
+ "aria-label": "Close",
19608
+ onClick: onClose,
19609
+ children: "✕"
19610
+ });
19611
+ }
19439
19612
  //#endregion
19440
19613
  //#region src/components/TriggerDialogHost.tsx
19441
19614
  function TriggerDialogHost() {
@@ -19923,161 +20096,6 @@ function editedItems(since, state) {
19923
20096
  return (list, item, index) => typeof item === "object" && item !== null ? through.has(item) : ends.get(list)?.has(String(index)) === true;
19924
20097
  }
19925
20098
  //#endregion
19926
- //#region src/action-registry.ts
19927
- /** The fields of a StoryAction that are left out when unset. */
19928
- var OPTIONAL_ACTION_FIELDS = [
19929
- "target",
19930
- "variable",
19931
- "options",
19932
- "value",
19933
- "disabled"
19934
- ];
19935
- /**
19936
- * The registrations holding each ID, oldest first; the newest is the one
19937
- * listed. Generated IDs don't collide, but author IDs can, and one control's
19938
- * cleanup must not remove another control holding the same ID (#233).
19939
- */
19940
- var actions = /* @__PURE__ */ new Map();
19941
- var idCounters = /* @__PURE__ */ new Map();
19942
- /**
19943
- * IDs handed out and not yet registered, or registered: a control registers
19944
- * in a layout effect, after every control of the same render has allocated its
19945
- * ID, so the registry alone can't tell that "link:Room:2" is taken (#486).
19946
- * Released when the last registration of an ID goes.
19947
- */
19948
- var reserved = /* @__PURE__ */ new Set();
19949
- /**
19950
- * The counters reset on every navigation, so a passage's controls get the
19951
- * same IDs each time it is shown. Controls that stay mounted across
19952
- * navigations (e.g. in StoryInterface) keep theirs, so allocation skips every
19953
- * ID still registered (#233) and every one allocated but not registered yet
19954
- * (#486).
19955
- */
19956
- function generateActionId(type, key, authorId) {
19957
- if (authorId) {
19958
- reserved.add(authorId);
19959
- return authorId;
19960
- }
19961
- const base = `${type}:${key}`;
19962
- let count = idCounters.get(base) ?? 0;
19963
- let id;
19964
- do {
19965
- count++;
19966
- id = count === 1 ? base : `${base}:${count}`;
19967
- } while (actions.has(id) || reserved.has(id));
19968
- idCounters.set(base, count);
19969
- reserved.add(id);
19970
- return id;
19971
- }
19972
- function registerAction(action) {
19973
- const id = action.id;
19974
- const entry = { action };
19975
- const entries = actions.get(id);
19976
- if (entries) {
19977
- console.warn(`spindle: two mounted controls share the action ID "${id}"; Story.getActions() lists only the newer one.`);
19978
- entries.push(entry);
19979
- } else actions.set(id, [entry]);
19980
- notify();
19981
- return {
19982
- update(next) {
19983
- const current = actions.get(id);
19984
- if (!current?.includes(entry)) return;
19985
- entry.action = next;
19986
- if (current[current.length - 1] === entry) notify();
19987
- },
19988
- unregister() {
19989
- const current = actions.get(id);
19990
- const index = current ? current.indexOf(entry) : -1;
19991
- if (!current || index === -1) return;
19992
- current.splice(index, 1);
19993
- if (current.length === 0) {
19994
- actions.delete(id);
19995
- reserved.delete(id);
19996
- }
19997
- if (index === current.length) notify();
19998
- }
19999
- };
20000
- }
20001
- /** The promises of the performed actions that have not settled yet. */
20002
- var running = /* @__PURE__ */ new Set();
20003
- /**
20004
- * Perform an action, as `Story.performAction()` does. One that returns a
20005
- * promise is tracked until it settles, for `settleRunningActions()`, and the
20006
- * promise is returned for the caller to await (its rejection stays the
20007
- * caller's to handle).
20008
- */
20009
- function performRegisteredAction(action, value) {
20010
- const result = action.perform(value);
20011
- if (!result || typeof result.then !== "function") return result;
20012
- const done = () => void running.delete(result);
20013
- running.add(result);
20014
- result.then(done, done);
20015
- return result.then((v) => v);
20016
- }
20017
- /** Wait until every performed action has finished, whether or not it failed. */
20018
- async function settleRunningActions() {
20019
- while (running.size > 0) await Promise.allSettled(running);
20020
- }
20021
- function getActions() {
20022
- return Array.from(actions.values(), listed);
20023
- }
20024
- function getAction(id) {
20025
- const entries = actions.get(id);
20026
- return entries && listed(entries);
20027
- }
20028
- function listed(entries) {
20029
- return entries[entries.length - 1].action;
20030
- }
20031
- function resetIdCounters() {
20032
- idCounters.clear();
20033
- for (const id of reserved) if (!actions.has(id)) reserved.delete(id);
20034
- }
20035
- function notify() {
20036
- emit("actionsChanged");
20037
- }
20038
- //#endregion
20039
- //#region src/hooks/use-action.ts
20040
- function useAction(opts) {
20041
- const idRef = useRef("");
20042
- const performRef = useRef(opts.perform);
20043
- performRef.current = opts.perform;
20044
- if (!idRef.current) idRef.current = generateActionId(opts.type, opts.key, opts.authorId);
20045
- const id = idRef.current;
20046
- const buildAction = () => {
20047
- const action = {
20048
- id,
20049
- type: opts.type,
20050
- label: opts.label,
20051
- perform: (...args) => performRef.current(...args)
20052
- };
20053
- for (const field of OPTIONAL_ACTION_FIELDS) if (opts[field] !== void 0) Object.assign(action, { [field]: opts[field] });
20054
- return action;
20055
- };
20056
- const registration = useRef(null);
20057
- useLayoutEffect(() => {
20058
- const own = registerAction(buildAction());
20059
- registration.current = own;
20060
- return () => own.unregister();
20061
- }, [id]);
20062
- const registered = useRef(false);
20063
- useLayoutEffect(() => {
20064
- if (!registered.current) {
20065
- registered.current = true;
20066
- return;
20067
- }
20068
- registration.current?.update(buildAction());
20069
- }, [
20070
- opts.type,
20071
- opts.label,
20072
- opts.target,
20073
- opts.variable,
20074
- opts.disabled,
20075
- opts.value,
20076
- JSON.stringify(opts.options)
20077
- ]);
20078
- return id;
20079
- }
20080
- //#endregion
20081
20099
  //#region src/utils/extract-text.ts
20082
20100
  /**
20083
20101
  * Collect plain text from AST nodes, ignoring non-text nodes.
@@ -23303,16 +23321,25 @@ defineMacro({
23303
23321
  name: "noclose",
23304
23322
  type: "flag"
23305
23323
  }],
23306
- render({ children = [] }, ctx) {
23324
+ render({ rawArgs, children = [] }, ctx) {
23307
23325
  const [open, setOpen] = ctx.hooks.useState(false);
23308
23326
  const { noclose } = ctx.args;
23309
- const label = ctx.resolve(ctx.args.label ?? "");
23327
+ const label = ctx.resolve(ctx.args.label ?? "") ?? "";
23310
23328
  const passageName = ctx.collectText(children).trim().replace(/^["']|["']$/g, "");
23329
+ const openDialog = () => setOpen(true);
23330
+ ctx.useAction({
23331
+ type: "open-dialog",
23332
+ key: rawArgs,
23333
+ authorId: ctx.id,
23334
+ label,
23335
+ target: passageName,
23336
+ perform: openDialog
23337
+ });
23311
23338
  return /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("button", {
23312
23339
  type: "button",
23313
23340
  id: ctx.id,
23314
23341
  class: ctx.cls,
23315
- onClick: () => setOpen(true),
23342
+ onClick: openDialog,
23316
23343
  children: label
23317
23344
  }), open && /* @__PURE__ */ jsx(PassageDialog, {
23318
23345
  passageName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.59.31",
3
+ "version": "0.59.32",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -15,7 +15,9 @@ export type ActionType =
15
15
  | 'restart'
16
16
  | 'save'
17
17
  | 'load'
18
- | 'dialog';
18
+ | 'dialog'
19
+ | 'open-dialog'
20
+ | 'close-dialog';
19
21
 
20
22
  export interface StoryAction {
21
23
  id: string;
@@ -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
- <button
141
- type="button"
142
- class="dialog-close"
143
- aria-label="Close"
144
- onClick={stableOnClose}
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={() => setOpen(true)}
39
+ onClick={openDialog}
29
40
  >
30
41
  {label}
31
42
  </button>
package/types/index.d.ts CHANGED
@@ -200,7 +200,9 @@ export type ActionType =
200
200
  | 'restart'
201
201
  | 'save'
202
202
  | 'load'
203
- | 'dialog';
203
+ | 'dialog'
204
+ | 'open-dialog'
205
+ | 'close-dialog';
204
206
 
205
207
  /**
206
208
  * A registered interactive action (link, button, input, etc.).