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

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.
@@ -359,6 +359,8 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
359
359
  if (story.uiJsIndex !== undefined) {
360
360
  runJs(story.uiJsIndex, {
361
361
  registerAnimation: animation.registerAnimation.bind(animation),
362
+ registerHelper: textProcessor.handlebars.registerHelper.bind(textProcessor.handlebars),
363
+ SafeString: textProcessor.handlebars.SafeString,
362
364
  });
363
365
  }
364
366
  };
@@ -391,28 +393,25 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
391
393
 
392
394
  function newSection(sectionName?: string) {
393
395
  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;
396
+ // Inputs we disable are marked with data-squiffy-disabled, so goBack() can re-enable them
397
+ // without touching any that the author disabled themselves.
398
+ currentSectionElement.querySelectorAll("input, textarea, select").forEach(el => {
399
+ const input = el as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement;
400
+ const attribute = input.getAttribute("data-attribute") || input.id;
401
+ if (attribute) set(attribute, input.value);
402
+ if (!input.disabled) {
403
+ input.disabled = true;
404
+ input.setAttribute("data-squiffy-disabled", "");
405
+ }
398
406
  });
399
-
407
+
400
408
  currentSectionElement.querySelectorAll("[contenteditable]").forEach(el => {
401
409
  const attribute = el.getAttribute("data-attribute") || el.id;
402
410
  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;
411
+ if (el.getAttribute("contenteditable") !== "false") {
412
+ el.setAttribute("contenteditable", "false");
413
+ el.setAttribute("data-squiffy-disabled", "");
414
+ }
416
415
  });
417
416
  }
418
417
 
@@ -543,10 +542,25 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
543
542
  return;
544
543
  }
545
544
 
546
- updateStory(story, newStory, outputElement, ui, disableLink, (attrs) => processAttributes(attrs));
547
-
545
+ // Switch to the new story before re-rendering, so that text processing (e.g. {{embed}})
546
+ // sees sections that were added in this update.
547
+ const oldStory = story;
548
548
  story = newStory;
549
549
 
550
+ // When re-rendering a section or passage, {{embed}} should look up passages in the
551
+ // (new version of the) section being rendered.
552
+ const processTextInSection = (text: string, section: Section) => {
553
+ const previousSection = currentSection;
554
+ currentSection = section;
555
+ try {
556
+ return ui.processText(text, false);
557
+ } finally {
558
+ currentSection = previousSection;
559
+ }
560
+ };
561
+
562
+ updateStory(oldStory, newStory, outputElement, processTextInSection, disableLink, (attrs) => processAttributes(attrs));
563
+
550
564
  setCurrentSectionElement();
551
565
  setCurrentPassageElement();
552
566
  }
@@ -576,12 +590,26 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
576
590
  return sectionPassageCount + clearStack.children.length;
577
591
  }
578
592
 
593
+ function enableInputs(sectionElement: Element) {
594
+ sectionElement.querySelectorAll("[data-squiffy-disabled]").forEach(el => {
595
+ if (el.hasAttribute("contenteditable")) {
596
+ el.setAttribute("contenteditable", "true");
597
+ }
598
+ else {
599
+ (el as HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement).disabled = false;
600
+ }
601
+ el.removeAttribute("data-squiffy-disabled");
602
+ });
603
+ }
604
+
579
605
  function canGoBack() {
580
606
  return getHistoryCount() > 1;
581
607
  }
582
608
 
583
609
  function goBack() {
584
- if (!canGoBack()) {
610
+ // Like link clicks, ignore this while transitions are running. The section or passage being shown
611
+ // hasn't written its undo log yet, and would write it to the wrong element once it resumed.
612
+ if (runningTransitions || !canGoBack()) {
585
613
  return;
586
614
  }
587
615
 
@@ -642,6 +670,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
642
670
 
643
671
  setCurrentSectionElement();
644
672
  setCurrentPassageElement();
673
+ enableInputs(currentSectionElement);
645
674
  }
646
675
 
647
676
  if (!canGoBack()) {
@@ -719,7 +748,7 @@ export const init = async (options: SquiffyInitOptions): Promise<SquiffyApi> =>
719
748
  const get = state.get.bind(state);
720
749
  const set = state.set.bind(state);
721
750
 
722
- const textProcessor = new TextProcessor(story, state, () => currentSection);
751
+ const textProcessor = new TextProcessor(() => story, state, () => currentSection);
723
752
  const linkHandler = new LinkHandler();
724
753
 
725
754
  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/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
  });