@rohal12/spindle 0.64.16 → 0.64.18

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.
@@ -18,6 +18,7 @@ type ScannedMarkup = ReturnType<typeof scanMarkupTolerant>;
18
18
  import { jsCodeEnd } from '../markup/code-end';
19
19
  import { builtinMacros } from './builtin-macros';
20
20
  import { parameterLookup } from '../code-check';
21
+ import { upperBound } from '../utils/sorted';
21
22
 
22
23
  const pairing = parameterLookup(builtinMacros).pairing;
23
24
 
@@ -407,26 +408,39 @@ function spansOf(text: string, reading: Reading): StyledSpan[] {
407
408
  }
408
409
 
409
410
  /**
410
- * Where the construct starts whose code, starting at `at`, may end
411
- * elsewhere once text follows: the damage of an error the code is in, or
412
- * the tag holding the code (a raw body starts where its opener ends).
411
+ * Owners of code that may end elsewhere once text follows: the damage of
412
+ * an error the code is in, or the tag holding the code (a raw body starts
413
+ * where its opener ends). Index once for all open code positions.
413
414
  */
414
- function openCodeStart(scan: ScannedMarkup, at: number): number {
415
- const i = scan.errors.findIndex(
416
- (e, i) => scan.damage[i]! < at && at <= Math.max(e.offset, e.end),
417
- );
418
- if (i >= 0) return scan.damage[i]!;
419
- const token = scan.tokens.find(
420
- (t) => t.type !== 'text' && t.start < at && at <= t.end,
421
- );
422
- return token ? token.start : -1;
415
+ function openCodeStarts(scan: ScannedMarkup, ends: number[]): number[] {
416
+ if (!ends.length) return [];
417
+ // Damage starts are ordered but their ranges may overlap. Prefix maxima
418
+ // let each query find the first owning error, just as findIndex did.
419
+ const damageEnds: number[] = [];
420
+ for (const error of scan.errors) {
421
+ damageEnds.push(
422
+ Math.max(
423
+ damageEnds[damageEnds.length - 1] ?? -1,
424
+ error.offset,
425
+ error.end,
426
+ ),
427
+ );
428
+ }
429
+ const tokens = scan.tokens.filter((token) => token.type !== 'text');
430
+ const starts = tokens.map((token) => token.start);
431
+ return ends.map((at) => {
432
+ const i = upperBound(damageEnds, at - 1);
433
+ if (i < scan.damage.length && scan.damage[i]! < at) return scan.damage[i]!;
434
+ const token = tokens[upperBound(starts, at - 1) - 1];
435
+ return token && at <= token.end ? token.start : -1;
436
+ });
423
437
  }
424
438
 
425
439
  function openStartOf(text: string, reading: Reading): number {
426
440
  const { tokens } = reading;
427
441
  const starts = [
428
442
  ...unclosedStarts(text, reading),
429
- ...reading.openEnds.map((at) => openCodeStart(reading, at)),
443
+ ...openCodeStarts(reading, reading.openEnds),
430
444
  openComment(tokens),
431
445
  openDo(tokens)?.start ?? -1,
432
446
  openTag(text, tokens),
@@ -5,7 +5,14 @@
5
5
  *
6
6
  * Run: npx tsc --noEmit
7
7
  */
8
- import type { StoryAPI as SourceAPI } from './story-api';
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
@@ -34,7 +34,7 @@ export function* composedElements(root: ParentNode): Generator<HTMLElement> {
34
34
  }
35
35
 
36
36
  /** Elements whose content is never text the reader reads. */
37
- const NOT_TEXT = new Set(['SCRIPT', 'STYLE', 'TEMPLATE', 'NOSCRIPT']);
37
+ const NOT_TEXT = new Set(['script', 'style', 'template', 'noscript']);
38
38
 
39
39
  /**
40
40
  * Whether the element hides its whole subtree, including stylesheet rules.
@@ -46,7 +46,7 @@ export function isHidden(el: Element): boolean {
46
46
  el.getAttribute('aria-hidden') === 'true' ||
47
47
  /(^|;)\s*display\s*:\s*none\b/i.test(el.getAttribute('style') ?? '') ||
48
48
  el.ownerDocument.defaultView?.getComputedStyle(el).display === 'none' ||
49
- NOT_TEXT.has(el.tagName) ||
49
+ NOT_TEXT.has(el.localName.toLowerCase()) ||
50
50
  (el.parentElement?.tagName === 'DETAILS' &&
51
51
  !el.parentElement.hasAttribute('open') &&
52
52
  el !==
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
- beforenavigate: (passageName: string) => void;
140
- afternavigate: (to: string, from: string) => void;
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
@@ -812,6 +830,29 @@ export interface StoryAPI {
812
830
  /** The current passage name. */
813
831
  readonly passage: string;
814
832
 
833
+ /**
834
+ * The history: the moments Back and Forward move through, oldest first.
835
+ * Read-only. `Story.config.maxHistory` limits how many are kept.
836
+ */
837
+ readonly history: {
838
+ /** The number of moments kept. */
839
+ readonly length: number;
840
+ /** The index of the current moment (0 is the oldest kept). */
841
+ readonly index: number;
842
+ /**
843
+ * The passage name of the moment at `index` (the current moment when
844
+ * left out). Throws a `RangeError` if the history has no such moment.
845
+ */
846
+ passage(index?: number): string;
847
+ /**
848
+ * The story variables recorded for the moment at `index` (the current
849
+ * moment when left out): the state on entering it, which Back and
850
+ * Forward restore. A frozen copy, keyed by name without the `$`. Throws a
851
+ * `RangeError` if the history has no such moment.
852
+ */
853
+ variables(index?: number): Record<string, unknown>;
854
+ };
855
+
815
856
  /** The settings API. */
816
857
  readonly settings: SettingsAPI;
817
858