squiffy-runtime 6.0.0-alpha.11 → 6.0.0-alpha.13

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.
@@ -1,2 +1,3 @@
1
1
  import { SquiffyApi, SquiffyInitOptions } from './types.js';
2
+ export type { SquiffyApi } from "./types.js";
2
3
  export declare const init: (options: SquiffyInitOptions) => SquiffyApi;
@@ -514,7 +514,7 @@ export const init = (options) => {
514
514
  return;
515
515
  handleClick(event);
516
516
  });
517
- textProcessor = new TextProcessor(get, set, story, ui, seen, processAttributes);
517
+ textProcessor = new TextProcessor(get, set, story, () => currentSection, seen, processAttributes);
518
518
  begin();
519
519
  return {
520
520
  restart: restart,
@@ -164,10 +164,10 @@ function safeQuerySelector(name) {
164
164
  return name.replace(/'/g, "\\'");
165
165
  }
166
166
  function getSectionContent(element, section) {
167
- return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]]'] p`)?.textContent || null;
167
+ return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]]']`)?.textContent || null;
168
168
  }
169
169
  function getPassageContent(element, section, passage) {
170
- return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]][${safeQuerySelector(passage)}]'] p`)?.textContent || null;
170
+ return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]][${safeQuerySelector(passage)}]']`)?.textContent || null;
171
171
  }
172
172
  test('Update default section output', async () => {
173
173
  const { squiffyApi, element } = await initScript("Hello world");
@@ -318,3 +318,27 @@ Passage in section B.`);
318
318
  const linkB1 = findLink(element, 'passage', 'b1');
319
319
  expect(squiffyApi.clickLink(linkB1)).toBe(true);
320
320
  });
321
+ test('Embed text from a section', async () => {
322
+ const script = `
323
+ [[section1]]:
324
+ Here is some text from the next section: {section2}
325
+
326
+ [[section2]]:
327
+ Text from next section.
328
+ `;
329
+ const { element } = await initScript(script);
330
+ let output = getSectionContent(element, 'section1');
331
+ expect(output).toBe('Here is some text from the next section: Text from next section.');
332
+ });
333
+ test('Embed text from a passage', async () => {
334
+ const script = `
335
+ [[section1]]:
336
+ Here is some text from a passage: {passage}
337
+
338
+ [passage]:
339
+ Text from a passage.
340
+ `;
341
+ const { element } = await initScript(script);
342
+ let output = getSectionContent(element, 'section1');
343
+ expect(output).toBe('Here is some text from a passage: Text from a passage.');
344
+ });
@@ -1,11 +1,12 @@
1
+ import { Section } from "./types.js";
1
2
  export declare class TextProcessor {
2
3
  get: (attribute: string) => any;
3
4
  set: (attribute: string, value: any) => void;
4
5
  story: any;
5
- currentSection: any;
6
+ getCurrentSection: () => Section;
6
7
  seen: (section: string) => boolean;
7
8
  processAttributes: (attributes: string[]) => void;
8
- constructor(get: (attribute: string) => any, set: (attribute: string, value: any) => void, story: any, currentSection: any, seen: (section: string) => boolean, processAttributes: (attributes: string[]) => void);
9
+ constructor(get: (attribute: string) => any, set: (attribute: string, value: any) => void, story: any, currentSection: () => Section, seen: (section: string) => boolean, processAttributes: (attributes: string[]) => void);
9
10
  process(text: string, data: any): string;
10
11
  processTextCommand(text: string, data: any): any;
11
12
  processTextCommand_If(section: string, data: any): string;
@@ -4,7 +4,7 @@ export class TextProcessor {
4
4
  this.get = get;
5
5
  this.set = set;
6
6
  this.story = story;
7
- this.currentSection = currentSection;
7
+ this.getCurrentSection = currentSection;
8
8
  this.seen = seen;
9
9
  this.processAttributes = processAttributes;
10
10
  }
@@ -47,6 +47,7 @@ export class TextProcessor {
47
47
  return (text);
48
48
  }
49
49
  processTextCommand(text, data) {
50
+ const currentSection = this.getCurrentSection();
50
51
  if (startsWith(text, 'if ')) {
51
52
  return this.processTextCommand_If(text, data);
52
53
  }
@@ -62,10 +63,12 @@ export class TextProcessor {
62
63
  else if (/^sequence[: ]/.test(text)) {
63
64
  return this.processTextCommand_Rotate('sequence', text);
64
65
  }
65
- else if (this.currentSection.passages && text in this.currentSection.passages) {
66
- return this.process(this.currentSection.passages[text].text || '', data);
66
+ else if (currentSection.passages && text in currentSection.passages) {
67
+ console.log("Found passage");
68
+ return this.process(currentSection.passages[text].text || '', data);
67
69
  }
68
70
  else if (text in this.story.sections) {
71
+ console.log("Found section");
69
72
  return this.process(this.story.sections[text].text || '', data);
70
73
  }
71
74
  else if (startsWith(text, '@') && !startsWith(text, '@replace')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squiffy-runtime",
3
- "version": "6.0.0-alpha.11",
3
+ "version": "6.0.0-alpha.13",
4
4
  "type": "module",
5
5
  "main": "dist/squiffy.runtime.js",
6
6
  "types": "dist/squiffy.runtime.d.ts",
@@ -213,11 +213,11 @@ function safeQuerySelector(name: string) {
213
213
  }
214
214
 
215
215
  function getSectionContent(element: HTMLElement, section: string) {
216
- return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]]'] p`)?.textContent || null;
216
+ return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]]']`)?.textContent || null;
217
217
  }
218
218
 
219
219
  function getPassageContent(element: HTMLElement, section: string, passage: string) {
220
- return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]][${safeQuerySelector(passage)}]'] p`)?.textContent || null;
220
+ return element.querySelector(`[data-source='[[${safeQuerySelector(section)}]][${safeQuerySelector(passage)}]']`)?.textContent || null;
221
221
  }
222
222
 
223
223
  test('Update default section output', async () => {
@@ -410,4 +410,34 @@ Passage in section B.`);
410
410
  // and the passage [b1] within it should be clickable
411
411
  const linkB1 = findLink(element, 'passage', 'b1');
412
412
  expect(squiffyApi.clickLink(linkB1)).toBe(true);
413
+ });
414
+
415
+ test('Embed text from a section', async () => {
416
+ const script = `
417
+ [[section1]]:
418
+ Here is some text from the next section: {section2}
419
+
420
+ [[section2]]:
421
+ Text from next section.
422
+ `;
423
+
424
+ const { element } = await initScript(script);
425
+
426
+ let output = getSectionContent(element, 'section1');
427
+ expect(output).toBe('Here is some text from the next section: Text from next section.');
428
+ });
429
+
430
+ test('Embed text from a passage', async () => {
431
+ const script = `
432
+ [[section1]]:
433
+ Here is some text from a passage: {passage}
434
+
435
+ [passage]:
436
+ Text from a passage.
437
+ `;
438
+
439
+ const { element } = await initScript(script);
440
+
441
+ let output = getSectionContent(element, 'section1');
442
+ expect(output).toBe('Here is some text from a passage: Text from a passage.');
413
443
  });
@@ -3,6 +3,8 @@ import { startsWith, rotate } from "./utils.js";
3
3
  import { TextProcessor } from './textProcessor.js';
4
4
  import { Emitter, SquiffyEventMap } from './events.js';
5
5
 
6
+ export type { SquiffyApi } from "./types.js"
7
+
6
8
  export const init = (options: SquiffyInitOptions): SquiffyApi => {
7
9
  let story: Story;
8
10
  let currentSection: Section;
@@ -552,7 +554,7 @@ export const init = (options: SquiffyInitOptions): SquiffyApi => {
552
554
  handleClick(event);
553
555
  });
554
556
 
555
- textProcessor = new TextProcessor(get, set, story, ui, seen, processAttributes);
557
+ textProcessor = new TextProcessor(get, set, story, () => currentSection, seen, processAttributes);
556
558
 
557
559
  begin();
558
560
 
@@ -1,22 +1,23 @@
1
1
  import { startsWith, rotate } from "./utils.js";
2
+ import { Section } from "./types.js";
2
3
 
3
4
  export class TextProcessor {
4
5
  get: (attribute: string) => any;
5
6
  set: (attribute: string, value: any) => void;
6
7
  story: any;
7
- currentSection: any;
8
+ getCurrentSection: () => Section;
8
9
  seen: (section: string) => boolean;
9
10
  processAttributes: (attributes: string[]) => void;
10
11
 
11
12
  constructor (get: (attribute: string) => any,
12
13
  set: (attribute: string, value: any) => void,
13
- story: any, currentSection: any,
14
+ story: any, currentSection: () => Section,
14
15
  seen: (section: string) => boolean,
15
16
  processAttributes: (attributes: string[]) => void) {
16
17
  this.get = get;
17
18
  this.set = set;
18
19
  this.story = story;
19
- this.currentSection = currentSection;
20
+ this.getCurrentSection = currentSection;
20
21
  this.seen = seen;
21
22
  this.processAttributes = processAttributes;
22
23
  }
@@ -64,6 +65,7 @@ export class TextProcessor {
64
65
  }
65
66
 
66
67
  processTextCommand(text: string, data: any) {
68
+ const currentSection = this.getCurrentSection();
67
69
  if (startsWith(text, 'if ')) {
68
70
  return this.processTextCommand_If(text, data);
69
71
  } else if (startsWith(text, 'else:')) {
@@ -74,9 +76,11 @@ export class TextProcessor {
74
76
  return this.processTextCommand_Rotate('rotate', text);
75
77
  } else if (/^sequence[: ]/.test(text)) {
76
78
  return this.processTextCommand_Rotate('sequence', text);
77
- } else if (this.currentSection.passages && text in this.currentSection.passages) {
78
- return this.process(this.currentSection.passages[text].text || '', data);
79
+ } else if (currentSection.passages && text in currentSection.passages) {
80
+ console.log("Found passage");
81
+ return this.process(currentSection.passages[text].text || '', data);
79
82
  } else if (text in this.story.sections) {
83
+ console.log("Found section");
80
84
  return this.process(this.story.sections[text].text || '', data);
81
85
  } else if (startsWith(text, '@') && !startsWith(text, '@replace')) {
82
86
  this.processAttributes(text.substring(1).split(","));