@rohal12/spindle 0.60.3 → 0.61.0

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 (55) hide show
  1. package/README.md +1 -1
  2. package/dist/pkg/format.js +1 -1
  3. package/dist/pkg/headless.js +18671 -18510
  4. package/dist/pkg/play-worker.js +181 -0
  5. package/dist/pkg/play.js +398 -0
  6. package/dist/pkg/story-variables.js +11016 -153
  7. package/package.json +19 -1
  8. package/src/action-registry.ts +43 -3
  9. package/src/components/PassageDialog.tsx +29 -27
  10. package/src/components/dialog-close-context.ts +5 -1
  11. package/src/components/macros/Button.tsx +3 -3
  12. package/src/components/macros/Checkbox.tsx +3 -2
  13. package/src/components/macros/Cycle.tsx +3 -4
  14. package/src/components/macros/Dialog.tsx +3 -3
  15. package/src/components/macros/Listbox.tsx +3 -4
  16. package/src/components/macros/MacroLink.tsx +3 -3
  17. package/src/components/macros/MenubarButtons.tsx +3 -3
  18. package/src/components/macros/PassageDisplay.tsx +14 -9
  19. package/src/components/macros/Radiobutton.tsx +3 -2
  20. package/src/components/macros/detached-body.tsx +1 -4
  21. package/src/components/macros/input-macro.ts +10 -8
  22. package/src/define-macro.ts +0 -4
  23. package/src/execute-mutation.ts +10 -6
  24. package/src/format/fenced-code.ts +59 -0
  25. package/src/format/format-passage.ts +100 -11
  26. package/src/headless.ts +25 -1
  27. package/src/hooks/use-action.ts +44 -4
  28. package/src/hooks/use-modal-focus.ts +1 -30
  29. package/src/markup/ast.ts +4 -3
  30. package/src/markup/pair.ts +22 -9
  31. package/src/markup/parse.ts +2 -0
  32. package/src/markup/validate.ts +5 -3
  33. package/src/observe/observable-text.ts +6 -24
  34. package/src/observe/observe.ts +4 -1
  35. package/src/play/index.ts +15 -0
  36. package/src/play/optional.ts +41 -0
  37. package/src/play/protocol.ts +35 -0
  38. package/src/play/resolve-story.ts +265 -0
  39. package/src/play/session.ts +253 -0
  40. package/src/play/worker.ts +169 -0
  41. package/src/registry.ts +36 -1
  42. package/src/runtime-errors.ts +5 -0
  43. package/src/saves/save-manager.ts +48 -7
  44. package/src/saves/storage.ts +3 -1
  45. package/src/saves/types.ts +1 -0
  46. package/src/store.ts +13 -6
  47. package/src/story-init.ts +6 -1
  48. package/src/structural.ts +126 -66
  49. package/src/tooling.ts +2 -0
  50. package/src/types-drift-check.ts +55 -0
  51. package/src/utils/element-visibility.ts +61 -0
  52. package/types/headless.d.ts +14 -0
  53. package/types/index.d.ts +2 -0
  54. package/types/play.d.ts +83 -0
  55. package/types/tooling.d.ts +2 -0
@@ -37,6 +37,10 @@ import type {
37
37
  import type { bootStory as PublishedBootStory } from '../types/headless';
38
38
  import type * as SourceTooling from './tooling';
39
39
  import type * as PublishedTooling from '../types/tooling';
40
+ import type * as SourceHeadless from './headless';
41
+ import type * as PublishedHeadless from '../types/headless';
42
+ import type * as SourcePlay from './play/index';
43
+ import type * as PublishedPlay from '../types/play';
40
44
 
41
45
  /** What the source declares, by published name. */
42
46
  interface Source {
@@ -198,3 +202,54 @@ declare const validateStoryMarkup: typeof SourceValidateStoryMarkup;
198
202
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
199
203
  const _validateMarkup: typeof PublishedValidateMarkup = (passages, options) =>
200
204
  validateStoryMarkup(passages, [], options);
205
+
206
+ // The rest of the headless entry point, and the play entry point
207
+ // (`@rohal12/spindle/play`): each must be the same type in the source and in
208
+ // types/headless.d.ts or types/play.d.ts.
209
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
210
+ const _headlessAndPlayTypes: [
211
+ Same<typeof SourceHeadless.gotoError, typeof PublishedHeadless.gotoError>,
212
+ Same<
213
+ typeof SourceHeadless.storyVariables,
214
+ typeof PublishedHeadless.storyVariables
215
+ >,
216
+ Same<
217
+ typeof SourceHeadless.createTranscript,
218
+ typeof PublishedHeadless.createTranscript
219
+ >,
220
+ Same<SourceHeadless.BootStoryOptions, PublishedHeadless.BootStoryOptions>,
221
+ Same<SourceHeadless.Transcript, PublishedHeadless.Transcript>,
222
+ Same<SourceHeadless.TranscriptEntry, PublishedHeadless.TranscriptEntry>,
223
+ Same<SourceHeadless.TranscriptOptions, PublishedHeadless.TranscriptOptions>,
224
+ Same<typeof SourcePlay.openPlaySession, typeof PublishedPlay.openPlaySession>,
225
+ Same<
226
+ typeof SourcePlay.resolveStoryHtml,
227
+ typeof PublishedPlay.resolveStoryHtml
228
+ >,
229
+ Same<SourcePlay.PlaySession, PublishedPlay.PlaySession>,
230
+ Same<SourcePlay.PlaySessionOptions, PublishedPlay.PlaySessionOptions>,
231
+ Same<SourcePlay.StoryInput, PublishedPlay.StoryInput>,
232
+ Same<SourcePlay.ResolvedStory, PublishedPlay.ResolvedStory>,
233
+ Same<SourcePlay.Observation, PublishedPlay.Observation>,
234
+ Same<SourcePlay.ObservedAction, PublishedPlay.ObservedAction>,
235
+ Same<SourcePlay.TranscriptEntry, PublishedPlay.TranscriptEntry>,
236
+ Same<SourcePlay.TranscriptOptions, PublishedPlay.TranscriptOptions>,
237
+ ] = [
238
+ true,
239
+ true,
240
+ true,
241
+ true,
242
+ true,
243
+ true,
244
+ true,
245
+ true,
246
+ true,
247
+ true,
248
+ true,
249
+ true,
250
+ true,
251
+ true,
252
+ true,
253
+ true,
254
+ true,
255
+ ];
@@ -0,0 +1,61 @@
1
+ /** Elements whose content is never text the reader reads. */
2
+ const NOT_TEXT = new Set(['SCRIPT', 'STYLE', 'TEMPLATE', 'NOSCRIPT']);
3
+
4
+ /**
5
+ * Whether the reader can't see the element. Headless DOMs compute no layout,
6
+ * so only what the markup says counts: the `hidden` attribute, an inline
7
+ * `display: none` and `aria-hidden="true"`.
8
+ */
9
+ export function isHidden(el: Element): boolean {
10
+ return (
11
+ el.hasAttribute('hidden') ||
12
+ el.getAttribute('aria-hidden') === 'true' ||
13
+ /(^|;)\s*display\s*:\s*none\b/i.test(el.getAttribute('style') ?? '') ||
14
+ NOT_TEXT.has(el.tagName) ||
15
+ (el.parentElement?.tagName === 'DETAILS' &&
16
+ !el.parentElement.hasAttribute('open') &&
17
+ el !==
18
+ Array.from(el.parentElement.children).find(
19
+ (c) => c.tagName === 'SUMMARY',
20
+ ))
21
+ );
22
+ }
23
+
24
+ /**
25
+ * Whether `el` is in a closed `<details>`, other than in the summary that
26
+ * toggles it: the browser renders none of that content (#389).
27
+ */
28
+ function inClosedDetails(el: Element): boolean {
29
+ for (let e = el.parentElement; e; e = e.parentElement) {
30
+ if (e.localName !== 'details' || e.hasAttribute('open')) continue;
31
+ const summary = Array.from(e.children).find(
32
+ (c) => c.localName === 'summary',
33
+ );
34
+ if (!summary?.contains(el)) return true;
35
+ }
36
+ return false;
37
+ }
38
+
39
+ /**
40
+ * Whether CSS leaves `el` in the tab sequence: not `visibility: hidden`
41
+ * (inherited, so the computed value covers ancestors), not inside a
42
+ * `display: none` element and not in the hidden content of a closed
43
+ * `<details>`.
44
+ */
45
+ export function isRendered(el: Element): boolean {
46
+ const { visibility } = getComputedStyle(el);
47
+ if (visibility === 'hidden' || visibility === 'collapse') return false;
48
+ for (let e: Element | null = el; e; e = e.parentElement) {
49
+ if (getComputedStyle(e).display === 'none') return false;
50
+ }
51
+ return !inClosedDetails(el);
52
+ }
53
+
54
+ /** Visibility shared by reader observations and native control handling. */
55
+ export function isReaderVisible(el: Element): boolean {
56
+ if (!el.isConnected || !isRendered(el)) return false;
57
+ for (let at: Element | null = el; at; at = at.parentElement) {
58
+ if (isHidden(at) || at.hasAttribute('inert')) return false;
59
+ }
60
+ return true;
61
+ }
@@ -24,6 +24,20 @@ export interface BootStoryOptions {
24
24
  */
25
25
  export function bootStory(options: BootStoryOptions): Promise<StoryAPI>;
26
26
 
27
+ /**
28
+ * The story's `$` variables: a shallow copy of the current values, keyed by
29
+ * name without the `$`. Values are the store's own (frozen) objects.
30
+ */
31
+ export function storyVariables(): Record<string, unknown>;
32
+
33
+ /**
34
+ * Why `Story.goto(name)` would not navigate, or `null` if it would: `name` is
35
+ * a special passage (StoryInit, ...) or names no passage. The message is the
36
+ * one Spindle logs (and, for a missing passage, shows on the page), since
37
+ * `Story.goto()` reports these instead of throwing.
38
+ */
39
+ export function gotoError(name: string): string | null;
40
+
27
41
  /** One recorded step: what the reader saw, and what they chose. */
28
42
  export interface TranscriptEntry {
29
43
  turn: number;
package/types/index.d.ts CHANGED
@@ -471,6 +471,8 @@ export interface UseActionOptions {
471
471
  type: ActionType;
472
472
  key: string;
473
473
  authorId?: string;
474
+ /** The exact DOM control, including when authored action IDs collide. */
475
+ element?: { readonly current: Element | null };
474
476
  label: string;
475
477
  target?: string;
476
478
  variable?: string;
@@ -0,0 +1,83 @@
1
+ import type {
2
+ Observation,
3
+ TranscriptEntry,
4
+ TranscriptOptions,
5
+ } from './headless';
6
+ import type { ObservedAction } from './index';
7
+
8
+ /** A story to play: compiled HTML, or a path to HTML, Twee or a project. */
9
+ export type StoryInput = { html: string } | { path: string };
10
+
11
+ /** The result of {@link resolveStoryHtml}. */
12
+ export interface ResolvedStory {
13
+ html: string;
14
+ /** Things worth telling the caller; never errors. */
15
+ notes: string[];
16
+ }
17
+
18
+ /**
19
+ * Turn a story into HTML: inline HTML as given, an `.html` path read, and
20
+ * anything else (a `.twee` file, a glob or a project directory) compiled with
21
+ * the installed Spindle format. Compiling needs the optional peer dependency
22
+ * `@rohal12/twee-ts`.
23
+ */
24
+ export function resolveStoryHtml(input: StoryInput): Promise<ResolvedStory>;
25
+
26
+ /** Options for {@link openPlaySession}. */
27
+ export interface PlaySessionOptions {
28
+ html: string;
29
+ /** Per call. Default 10_000. */
30
+ stepTimeoutMs?: number;
31
+ /** Passed to createTranscript() inside the worker. Default { limit: 10 }. */
32
+ transcript?: TranscriptOptions;
33
+ /** Keep passage transitions (bootStory's option). Default false. */
34
+ transitions?: boolean;
35
+ }
36
+
37
+ /** A running story, returned by {@link openPlaySession}. */
38
+ export interface PlaySession {
39
+ observe(): Promise<Observation>;
40
+ /** Goes through the transcript, so the step is recorded. */
41
+ step(id: string, value?: unknown): Promise<Observation>;
42
+ /**
43
+ * Story.goto(), then waitForActions(), then observe(). Not recorded in the
44
+ * transcript. Rejects for a passage the story does not have
45
+ * (`spindle: No passage named "<name>" ...`) or a special passage such as
46
+ * StoryInit (`spindle: Cannot navigate to special passage ...`); the session
47
+ * stays open.
48
+ */
49
+ goto(passage: string): Promise<Observation>;
50
+ /**
51
+ * Story variables, by name with or without `$`. All of them when `names`
52
+ * is left out. Each value comes back as a structured clone (Map, Set, Date
53
+ * and cycles survive; class instances arrive as plain objects); one that
54
+ * cannot be cloned (a function, a Map holding one) is the string
55
+ * `"[unserializable: <reason>]"`.
56
+ */
57
+ get(names?: string[]): Promise<Record<string, unknown>>;
58
+ /** Story.set(), then waitForActions(). */
59
+ set(values: Record<string, unknown>): Promise<void>;
60
+ transcript(): Promise<TranscriptEntry[]>;
61
+ close(): Promise<void>;
62
+ readonly closed: boolean;
63
+ }
64
+
65
+ /**
66
+ * Boot a compiled story in its own worker thread on happy-dom (an optional
67
+ * peer dependency) and resolve with a session once the first passage is
68
+ * shown. Calls run one at a time; a call that takes longer than
69
+ * `stepTimeoutMs` closes the session.
70
+ *
71
+ * In the worker, `window` is happy-dom's window, not `globalThis`. Story code
72
+ * sees `Story` and the DOM globals as in a browser, but a property set on
73
+ * `window` (`window.foo = 1`) does not become a bare global `foo`. Sessions do
74
+ * not share a `BroadcastChannel`, `localStorage` or `navigator`, so they never
75
+ * hear each other. The story's `console.log` goes to stderr, not stdout.
76
+ *
77
+ * A session left open does not keep Node running (a call in progress does).
78
+ */
79
+ export function openPlaySession(
80
+ options: PlaySessionOptions,
81
+ ): Promise<PlaySession>;
82
+
83
+ export type { Observation, ObservedAction, TranscriptEntry, TranscriptOptions };
@@ -993,6 +993,8 @@ export interface PairMarkupOptions {
993
993
  * tag, and the markup after it is not its body.
994
994
  */
995
995
  isRaw?(name: string): boolean;
996
+ /** Parents declaring a branching sub-macro (default: registered metadata). */
997
+ branchParents?(name: string): readonly string[];
996
998
  /**
997
999
  * The markup the tokens are of: lets a message say at which line and column
998
1000
  * an element was opened (else it says at which offset).