squiffy-runtime 6.0.0-beta.3 → 6.0.0-beta.5

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.
@@ -222,6 +222,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
222
222
  },
223
223
  story: {
224
224
  go: go,
225
+ seen: (sectionOrPassage: string) => state.getSeen(sectionOrPassage),
225
226
  },
226
227
  element: outputElementContainer,
227
228
  import: imports,
@@ -359,6 +360,8 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
359
360
  if (story.uiJsIndex !== undefined) {
360
361
  runJs(story.uiJsIndex, {
361
362
  registerAnimation: animation.registerAnimation.bind(animation),
363
+ registerHelper: textProcessor.handlebars.registerHelper.bind(textProcessor.handlebars),
364
+ SafeString: textProcessor.handlebars.SafeString,
362
365
  });
363
366
  }
364
367
  };
@@ -391,28 +394,25 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
391
394
 
392
395
  function newSection(sectionName?: string) {
393
396
  if (currentSectionElement) {
394
- currentSectionElement.querySelectorAll("input").forEach(el => {
395
- const attribute = el.getAttribute("data-attribute") || el.id;
396
- if (attribute) set(attribute, el.value);
397
- el.disabled = true;
397
+ // Inputs we disable are marked with data-squiffy-disabled, so goBack() can re-enable them
398
+ // without touching any that the author disabled themselves.
399
+ currentSectionElement.querySelectorAll("input, textarea, select").forEach(el => {
400
+ const input = el as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
401
+ const attribute = input.getAttribute("data-attribute") || input.id;
402
+ if (attribute) set(attribute, input.value);
403
+ if (!input.disabled) {
404
+ input.disabled = true;
405
+ input.setAttribute("data-squiffy-disabled", "");
406
+ }
398
407
  });
399
-
408
+
400
409
  currentSectionElement.querySelectorAll("[contenteditable]").forEach(el => {
401
410
  const attribute = el.getAttribute("data-attribute") || el.id;
402
411
  if (attribute) set(attribute, el.innerHTML);
403
- (el as HTMLElement).contentEditable = "false";
404
- });
405
-
406
- currentSectionElement.querySelectorAll("textarea").forEach(el => {
407
- const attribute = el.getAttribute("data-attribute") || el.id;
408
- if (attribute) set(attribute, el.value);
409
- el.disabled = true;
410
- });
411
-
412
- currentSectionElement.querySelectorAll("select").forEach(el => {
413
- const attribute = el.getAttribute("data-attribute") || el.id;
414
- if (attribute) set(attribute, el.value);
415
- el.disabled = true;
412
+ if (el.getAttribute("contenteditable") !== "false") {
413
+ el.setAttribute("contenteditable", "false");
414
+ el.setAttribute("data-squiffy-disabled", "");
415
+ }
416
416
  });
417
417
  }
418
418
 
@@ -543,10 +543,25 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
543
543
  return;
544
544
  }
545
545
 
546
- updateStory(story, newStory, outputElement, ui, disableLink, (attrs) => processAttributes(attrs));
547
-
546
+ // Switch to the new story before re-rendering, so that text processing (e.g. {{embed}})
547
+ // sees sections that were added in this update.
548
+ const oldStory = story;
548
549
  story = newStory;
549
550
 
551
+ // When re-rendering a section or passage, {{embed}} should look up passages in the
552
+ // (new version of the) section being rendered.
553
+ const processTextInSection = (text: string, section: Section) => {
554
+ const previousSection = currentSection;
555
+ currentSection = section;
556
+ try {
557
+ return ui.processText(text, false);
558
+ } finally {
559
+ currentSection = previousSection;
560
+ }
561
+ };
562
+
563
+ updateStory(oldStory, newStory, outputElement, processTextInSection, disableLink, (attrs) => processAttributes(attrs));
564
+
550
565
  setCurrentSectionElement();
551
566
  setCurrentPassageElement();
552
567
  }
@@ -576,12 +591,26 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
576
591
  return sectionPassageCount + clearStack.children.length;
577
592
  }
578
593
 
594
+ function enableInputs(sectionElement: Element) {
595
+ sectionElement.querySelectorAll("[data-squiffy-disabled]").forEach(el => {
596
+ if (el.hasAttribute("contenteditable")) {
597
+ el.setAttribute("contenteditable", "true");
598
+ }
599
+ else {
600
+ (el as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement).disabled = false;
601
+ }
602
+ el.removeAttribute("data-squiffy-disabled");
603
+ });
604
+ }
605
+
579
606
  function canGoBack() {
580
607
  return getHistoryCount() > 1;
581
608
  }
582
609
 
583
610
  function goBack() {
584
- if (!canGoBack()) {
611
+ // Like link clicks, ignore this while transitions are running. The section or passage being shown
612
+ // hasn't written its undo log yet, and would write it to the wrong element once it resumed.
613
+ if (runningTransitions || !canGoBack()) {
585
614
  return;
586
615
  }
587
616
 
@@ -642,6 +671,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
642
671
 
643
672
  setCurrentSectionElement();
644
673
  setCurrentPassageElement();
674
+ enableInputs(currentSectionElement);
645
675
  }
646
676
 
647
677
  if (!canGoBack()) {
@@ -719,7 +749,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
719
749
  const get = state.get.bind(state);
720
750
  const set = state.set.bind(state);
721
751
 
722
- const textProcessor = new TextProcessor(story, state, () => currentSection);
752
+ const textProcessor = new TextProcessor(() => story, state, () => currentSection);
723
753
  const linkHandler = new LinkHandler();
724
754
 
725
755
  const getSectionText = (sectionName: string) => {
@@ -2,17 +2,18 @@ import * as marked from "marked";
2
2
  import Handlebars from "handlebars";
3
3
  import { Section, Story } from "./types.js";
4
4
  import { State } from "./state.js";
5
+ import { rewriteInfixExpressions } from "./infixExpressions.js";
5
6
 
6
7
  export class TextProcessor {
7
- story: Story;
8
+ getStory: () => Story;
8
9
  state: State;
9
10
  getCurrentSection: () => Section;
10
11
  handlebars: typeof Handlebars;
11
12
 
12
- constructor (story: Story,
13
+ constructor (story: () => Story,
13
14
  state: State,
14
15
  currentSection: () => Section) {
15
- this.story = story;
16
+ this.getStory = story;
16
17
  this.state = state;
17
18
  this.getCurrentSection = currentSection;
18
19
  this.handlebars = Handlebars.create();
@@ -21,8 +22,10 @@ export class TextProcessor {
21
22
  const currentSection = this.getCurrentSection();
22
23
  if (currentSection.passages && name in currentSection.passages) {
23
24
  return new Handlebars.SafeString(this.process(currentSection.passages[name].text || "", true));
24
- } else if (name in this.story.sections) {
25
- return new Handlebars.SafeString(this.process(this.story.sections[name].text || "", true));
25
+ }
26
+ const story = this.getStory();
27
+ if (name in story.sections) {
28
+ return new Handlebars.SafeString(this.process(story.sections[name].text || "", true));
26
29
  }
27
30
  });
28
31
 
@@ -174,7 +177,7 @@ export class TextProcessor {
174
177
  return result;
175
178
  };
176
179
 
177
- const escapeAttr = (s: string) => s.replace(/&/g, "&amp;").replace(/"/g, "&quot;");
180
+ const escapeAttr = (s: unknown) => String(s).replace(/&/g, "&amp;").replace(/"/g, "&quot;");
178
181
 
179
182
  this.handlebars.registerHelper("section", (section: string, options) => {
180
183
  const text = options.hash.text as string || section;
@@ -189,7 +192,7 @@ export class TextProcessor {
189
192
 
190
193
  process(text: string, inline: boolean) {
191
194
  try {
192
- const template = this.handlebars.compile(text);
195
+ const template = this.handlebars.compile(rewriteInfixExpressions(text));
193
196
  text = template(this.state.getStore());
194
197
 
195
198
  if (inline) {
package/src/types.ts CHANGED
@@ -50,6 +50,7 @@ interface SquiffyJsFunctionApi {
50
50
  };
51
51
  story: {
52
52
  go: (section: string) => void;
53
+ seen: (sectionOrPassage: string) => boolean;
53
54
  };
54
55
  }
55
56
 
package/src/updater.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Story } from "./types.js";
1
+ import { Section, Story } from "./types.js";
2
2
 
3
3
  function arraysEqual(a: string[] | undefined, b: string[] | undefined): boolean {
4
4
  if (!a && !b) return true;
@@ -10,7 +10,7 @@ function arraysEqual(a: string[] | undefined, b: string[] | undefined): boolean
10
10
  export function updateStory(oldStory: Story,
11
11
  newStory: Story,
12
12
  outputElement: HTMLElement,
13
- ui: any,
13
+ processText: (text: string, section: Section) => string,
14
14
  disableLink: (link: Element) => void,
15
15
  processAttributes: (attributes: string[]) => void) {
16
16
 
@@ -65,7 +65,7 @@ export function updateStory(oldStory: Story,
65
65
  if (textChanged || attributesChanged) {
66
66
  // Re-render text (attributes may affect text output via helpers)
67
67
  for (const element of elements) {
68
- updateElementTextPreservingDisabledPassageLinks(element, ui.processText(newSection.text, false));
68
+ updateElementTextPreservingDisabledPassageLinks(element, processText(newSection.text || "", newSection));
69
69
  }
70
70
  }
71
71
  }
@@ -98,7 +98,7 @@ export function updateStory(oldStory: Story,
98
98
  if (textChanged || attributesChanged) {
99
99
  // Re-render text (attributes may affect text output via helpers)
100
100
  for (const element of elements) {
101
- updateElementTextPreservingDisabledPassageLinks(element, ui.processText(newPassage.text, false));
101
+ updateElementTextPreservingDisabledPassageLinks(element, processText(newPassage.text || "", newStory.sections[existingSection]));
102
102
  }
103
103
  }
104
104
  }
package/vitest.config.ts CHANGED
@@ -3,7 +3,7 @@ import { defineConfig } from "vitest/config";
3
3
  export default defineConfig({
4
4
  test: {
5
5
  globals: true,
6
- environment: "jsdom",
6
+ environment: "node",
7
7
  setupFiles: "./vitest.setup.ts",
8
8
  },
9
9
  });