@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.
Files changed (62) hide show
  1. package/README.md +1 -0
  2. package/dist/pkg/format.js +1 -1
  3. package/dist/pkg/headless.js +947 -351
  4. package/dist/pkg/macro-registry.js +19 -0
  5. package/dist/pkg/macro-registry.json +19 -0
  6. package/dist/pkg/story-variables.js +780 -107
  7. package/package.json +1 -1
  8. package/src/action-registry.ts +59 -5
  9. package/src/automation/load-yaml.ts +111 -20
  10. package/src/automation/runner.ts +19 -13
  11. package/src/automation/types.ts +7 -5
  12. package/src/class-registry.ts +8 -5
  13. package/src/code-check.ts +87 -20
  14. package/src/components/macros/Checkbox.tsx +5 -2
  15. package/src/components/macros/Cycle.tsx +1 -0
  16. package/src/components/macros/For.tsx +1 -7
  17. package/src/components/macros/Listbox.tsx +1 -0
  18. package/src/components/macros/MenubarButtons.tsx +6 -1
  19. package/src/components/macros/Nobr.tsx +1 -0
  20. package/src/components/macros/PassageDisplay.tsx +136 -10
  21. package/src/components/macros/Radiobutton.tsx +1 -0
  22. package/src/components/macros/Repeat.tsx +4 -2
  23. package/src/components/macros/SaveManager.tsx +30 -8
  24. package/src/components/macros/SettingsControls.tsx +19 -4
  25. package/src/components/macros/Span.tsx +1 -0
  26. package/src/components/macros/StoryTitle.tsx +1 -0
  27. package/src/components/macros/Unset.tsx +26 -13
  28. package/src/components/macros/VarDisplay.tsx +2 -3
  29. package/src/components/macros/WidgetInvocation.tsx +5 -6
  30. package/src/components/macros/input-macro.ts +1 -0
  31. package/src/components/macros/locals-scope.tsx +19 -8
  32. package/src/components/macros/macro-args.ts +11 -3
  33. package/src/expression.ts +34 -18
  34. package/src/hooks/use-merged-locals.ts +17 -2
  35. package/src/hooks/use-modal-focus.ts +247 -133
  36. package/src/index.tsx +32 -21
  37. package/src/js-bindings.ts +594 -0
  38. package/src/js-lexer.ts +76 -14
  39. package/src/macro-discovery.ts +83 -32
  40. package/src/markup/ast.ts +22 -1
  41. package/src/markup/render.tsx +44 -15
  42. package/src/parser.ts +41 -10
  43. package/src/prng.ts +24 -1
  44. package/src/saves/save-manager.ts +36 -9
  45. package/src/static-literal.ts +87 -16
  46. package/src/store.ts +83 -6
  47. package/src/story-api.ts +23 -6
  48. package/src/story-variables.ts +91 -28
  49. package/src/structural.ts +26 -15
  50. package/src/tooling.ts +24 -10
  51. package/src/transform.ts +22 -7
  52. package/src/utils/macro-names.ts +4 -0
  53. package/src/utils/namespace.ts +15 -2
  54. package/src/utils/object-path.ts +2 -2
  55. package/src/utils/stable-key.ts +17 -6
  56. package/src/utils/value-kinds.ts +38 -1
  57. package/src/widgets/ast-scanner.ts +4 -1
  58. package/src/widgets/register-widget-def.ts +2 -3
  59. package/src/widgets/widget-def.ts +36 -6
  60. package/src/widgets/widget-registry.ts +6 -2
  61. package/types/index.d.ts +12 -5
  62. package/types/tooling.d.ts +12 -2
@@ -7,6 +7,7 @@ import { STRING_HOLDS, type MacroArgs, type StringHolds } from '../registry';
7
7
  import { tokenizeMarkupTolerant } from '../markup/parse';
8
8
  import type { MacroToken } from '../markup/tokens';
9
9
  import type { ParametersOf } from '../code-check';
10
+ import { NameMap } from '../utils/macro-names';
10
11
  import { registeredParameters, tokenTextContainsChildren } from './ast-scanner';
11
12
 
12
13
  /** A {widget} definition's name, then its `@` parameters. */
@@ -182,16 +183,45 @@ export function widgetDefinitions(
182
183
  }
183
184
 
184
185
  /**
185
- * The names of the block widgets (those whose body renders `{@children}`)
186
- * that StoryInit and the passages tagged `widget` define, closed. They must
187
- * be known as block macros before any passage is parsed, so that passages
188
- * invoking them nest their content whatever the passage order.
186
+ * The definitions the runtime ends up with, one per widget name: the closed
187
+ * ones, the latest winning as registration replaces a widget. The widget
188
+ * passages register before StoryInit runs, so StoryInit's definitions come
189
+ * last whatever the passage order. Read with the flat scanner, so a nested
190
+ * definition owns its own `{@children}`.
189
191
  */
192
+ export function effectiveWidgetDefinitions(
193
+ definitions: readonly WidgetDefinition[],
194
+ ): WidgetDefinition[] {
195
+ const inInit = (def: WidgetDefinition) => def.passage === 'StoryInit';
196
+ const effective = new NameMap<WidgetDefinition>();
197
+ for (const def of [
198
+ ...definitions.filter((def) => !inInit(def)),
199
+ ...definitions.filter(inInit),
200
+ ]) {
201
+ if (def.closeStart !== undefined) effective.set(def.name, def);
202
+ }
203
+ return [...effective.values()];
204
+ }
205
+
206
+ /**
207
+ * The widgets that StoryInit and the passages tagged `widget` define, as the
208
+ * runtime ends up with them (see effectiveWidgetDefinitions). Whether each
209
+ * takes a body must be known before any passage is parsed, so that passages
210
+ * invoking them nest their content (or don't) whatever the passage order.
211
+ */
212
+ export function effectiveWidgets(
213
+ passages: Iterable<WidgetPassage>,
214
+ parametersOf: ParametersOf = registeredParameters,
215
+ ): WidgetDefinition[] {
216
+ return effectiveWidgetDefinitions(widgetDefinitions(passages, parametersOf));
217
+ }
218
+
219
+ /** The names of the block widgets among the effectiveWidgets. */
190
220
  export function blockWidgetNames(
191
221
  passages: Iterable<WidgetPassage>,
192
222
  parametersOf: ParametersOf = registeredParameters,
193
223
  ): string[] {
194
- return widgetDefinitions(passages, parametersOf)
195
- .filter((def) => def.block && def.closeStart !== undefined)
224
+ return effectiveWidgets(passages, parametersOf)
225
+ .filter((def) => def.block)
196
226
  .map((def) => def.name);
197
227
  }
@@ -1,4 +1,4 @@
1
- import type { ASTNode } from '../markup/ast';
1
+ import { clearBlockWidgets, setBlockWidget, type ASTNode } from '../markup/ast';
2
2
  import { checkVariableName } from '../utils/namespace';
3
3
  import { NameMap } from '../utils/macro-names';
4
4
  import type { StringHolds } from '../registry';
@@ -15,7 +15,9 @@ interface WidgetEntry {
15
15
  const widgets = new NameMap<WidgetEntry>();
16
16
 
17
17
  /**
18
- * Register a widget. Its `@` parameters become locals of its body, so one
18
+ * Register a widget. A widget whose body renders {@children} takes a closing
19
+ * tag, so it is a block macro to the parser too, for the passages parsed
20
+ * later; the latest definition decides. Its `@` parameters become locals of its body, so one
19
21
  * that no namespace can hold (`@__proto__`) throws a TypeError and the
20
22
  * widget is not registered.
21
23
  */
@@ -36,6 +38,7 @@ export function registerWidget(
36
38
  holds,
37
39
  isBlock,
38
40
  });
41
+ setBlockWidget(name, isBlock);
39
42
  }
40
43
 
41
44
  export function getWidget(name: string): WidgetEntry | undefined {
@@ -56,4 +59,5 @@ export function isBlockWidget(name: string): boolean {
56
59
 
57
60
  export function clearWidgets(): void {
58
61
  widgets.clear();
62
+ clearBlockWidgets();
59
63
  }
package/types/index.d.ts CHANGED
@@ -215,7 +215,7 @@ export interface StoryAction {
215
215
  options?: string[];
216
216
  value?: unknown;
217
217
  disabled?: boolean;
218
- perform: (value?: unknown) => void;
218
+ perform: (value?: unknown) => void | Promise<void>;
219
219
  }
220
220
 
221
221
  /**
@@ -425,7 +425,7 @@ export interface UseActionOptions {
425
425
  options?: string[];
426
426
  value?: unknown;
427
427
  disabled?: boolean;
428
- perform: (value?: unknown) => void;
428
+ perform: (value?: unknown) => void | Promise<void>;
429
429
  }
430
430
 
431
431
  /**
@@ -825,8 +825,11 @@ export interface StoryAPI {
825
825
  /** Return all registered interactive actions. */
826
826
  getActions(): StoryAction[];
827
827
 
828
- /** Perform a registered action by ID. */
829
- performAction(id: string, value?: unknown): void;
828
+ /**
829
+ * Perform a registered action by ID. An action that finishes later
830
+ * (`{quicksave}`, `{quickload}`) returns a promise that settles when it has.
831
+ */
832
+ performAction(id: string, value?: unknown): void | Promise<void>;
830
833
 
831
834
  /** Subscribe to a story event. Returns an unsubscribe function. */
832
835
  on<E extends StoryEvent>(
@@ -834,7 +837,11 @@ export interface StoryAPI {
834
837
  callback: StoryEventCallback<E>,
835
838
  ): () => void;
836
839
 
837
- /** Wait for the next frame's actions to be registered, then return them. */
840
+ /**
841
+ * Wait for the actions performed with `performAction()` that are still
842
+ * running, then for the next frame's actions to be registered, and return
843
+ * them.
844
+ */
838
845
  waitForActions(): Promise<StoryAction[]>;
839
846
 
840
847
  /** Register a trigger that fires when a condition expression becomes truthy. Returns an unsubscribe function. */
@@ -257,8 +257,18 @@ export interface JsLexHandlers {
257
257
  * comment, or a piece of a template literal.
258
258
  */
259
259
  literal?(text: string, index: number, nesting: number): void;
260
- /** A sigil variable reference (`$name`), covering the sigil and name. */
261
- variable?(sigil: Sigil, name: string, index: number, nesting: number): void;
260
+ /**
261
+ * A sigil variable reference (`$name`), covering the sigil and name, which
262
+ * is the name JavaScript reads (an escaped spelling is decoded). It is
263
+ * written from `index` up to `end`.
264
+ */
265
+ variable?(
266
+ sigil: Sigil,
267
+ name: string,
268
+ index: number,
269
+ nesting: number,
270
+ end: number,
271
+ ): void;
262
272
  }
263
273
 
264
274
  /**