squiffy-runtime 7.0.0-beta.6 → 7.0.0-beta.8

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 (80) hide show
  1. package/dist/assets.d.ts +1 -0
  2. package/dist/classes.test.d.ts +1 -0
  3. package/dist/confirmDialog.d.ts +8 -0
  4. package/dist/events.d.ts +11 -0
  5. package/dist/every.d.ts +12 -0
  6. package/dist/every.test.d.ts +1 -0
  7. package/dist/paths.d.ts +15 -0
  8. package/dist/paths.test.d.ts +1 -0
  9. package/dist/playerSettings.d.ts +17 -0
  10. package/dist/preview-host.d.ts +2 -0
  11. package/dist/preview-host.js +10 -0
  12. package/dist/preview-host.js.map +2 -2
  13. package/dist/preview-html.js +1 -1
  14. package/dist/preview-protocol.d.ts +6 -0
  15. package/dist/settings.test.d.ts +1 -0
  16. package/dist/settingsDialog.d.ts +17 -0
  17. package/dist/skill.test.d.ts +1 -0
  18. package/dist/sound.d.ts +35 -0
  19. package/dist/sound.test.d.ts +1 -0
  20. package/dist/squiffy.page.css +14 -3
  21. package/dist/squiffy.preview.html +680 -82
  22. package/dist/squiffy.runtime.css +531 -1
  23. package/dist/squiffy.runtime.d.ts +5 -1
  24. package/dist/squiffy.runtime.global.js +117 -96
  25. package/dist/squiffy.runtime.global.js.map +1 -1
  26. package/dist/squiffy.runtime.js +5728 -4520
  27. package/dist/squiffy.runtime.js.map +1 -1
  28. package/dist/state.d.ts +4 -1
  29. package/dist/style.test.d.ts +1 -0
  30. package/dist/textProcessor.d.ts +16 -0
  31. package/dist/transitions.test.d.ts +1 -0
  32. package/dist/types.d.ts +54 -0
  33. package/dist/uiLinks.test.d.ts +1 -0
  34. package/dist/utils.d.ts +1 -0
  35. package/dist/utils.test.d.ts +1 -0
  36. package/package.json +3 -3
  37. package/src/__snapshots__/squiffy.runtime.test.ts.snap +10 -10
  38. package/src/animation.test.ts +44 -0
  39. package/src/animation.ts +9 -5
  40. package/src/assets.test.ts +1 -3
  41. package/src/assets.ts +1 -1
  42. package/src/classes.test.ts +201 -0
  43. package/src/confirmDialog.ts +66 -0
  44. package/src/events.ts +18 -0
  45. package/src/every.test.ts +25 -0
  46. package/src/every.ts +94 -0
  47. package/src/panels.test.ts +24 -0
  48. package/src/paths.test.ts +66 -0
  49. package/src/paths.ts +155 -0
  50. package/src/playerSettings.ts +128 -0
  51. package/src/plugins/animate.ts +16 -3
  52. package/src/plugins/live.ts +10 -4
  53. package/src/plugins/plugins.test.ts +51 -6
  54. package/src/preview-host.test.ts +22 -0
  55. package/src/preview-host.ts +15 -0
  56. package/src/preview-protocol.ts +9 -1
  57. package/src/preview.test.ts +114 -3
  58. package/src/preview.ts +97 -3
  59. package/src/settings.test.ts +233 -0
  60. package/src/settingsDialog.ts +160 -0
  61. package/src/skill.test.ts +67 -0
  62. package/src/sound.test.ts +273 -0
  63. package/src/sound.ts +207 -0
  64. package/src/squiffy.page.css +14 -3
  65. package/src/squiffy.runtime.css +531 -1
  66. package/src/squiffy.runtime.test.ts +1130 -17
  67. package/src/squiffy.runtime.ts +600 -38
  68. package/src/state.ts +45 -10
  69. package/src/style.test.ts +89 -0
  70. package/src/textProcessor.ts +484 -35
  71. package/src/transitions.test.ts +302 -0
  72. package/src/types.ts +79 -0
  73. package/src/uiLinks.test.ts +156 -0
  74. package/src/updater.ts +26 -8
  75. package/src/utils.test.ts +60 -0
  76. package/src/utils.ts +44 -14
  77. package/dist/infixExpressions.d.ts +0 -6
  78. package/src/infixExpressions.test.ts +0 -379
  79. package/src/infixExpressions.ts +0 -488
  80. /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
package/dist/state.d.ts CHANGED
@@ -8,11 +8,14 @@ export declare class State {
8
8
  emitter: Emitter<SquiffyEventMap>;
9
9
  constructor(persist: boolean, storyId: string, onSet: (attribute: string, value: any) => void, emitter: Emitter<SquiffyEventMap>, onSetInternal?: (attribute: string, oldValue: any, newValue: any) => void);
10
10
  private usePersistentStorage;
11
+ playerSettings: () => Record<string, unknown> | null;
11
12
  readOnly: boolean;
12
13
  set(attribute: string, value: any): void;
14
+ setAt(segments: string[], value: any): void;
13
15
  setInternal(attribute: string, value: any, raiseEvents: boolean): void;
14
- private has;
16
+ has(attribute: string): boolean;
15
17
  get(attribute: string): any;
18
+ getAt(segments: string[]): any;
16
19
  getStore(): Record<string, any>;
17
20
  load(): void;
18
21
  reset(): void;
@@ -0,0 +1 @@
1
+ export {};
@@ -7,12 +7,28 @@ export declare class TextProcessor {
7
7
  getCurrentSection: () => Section;
8
8
  handlebars: typeof Handlebars;
9
9
  constructor(story: () => Story, state: State, currentSection: () => Section);
10
+ strings: {
11
+ restart: string;
12
+ saves: string;
13
+ settings: string;
14
+ };
10
15
  evaluate(expression: string): unknown;
11
16
  goto: {
12
17
  section: string;
13
18
  after: number;
14
19
  } | null;
15
20
  deactivate: Set<string>;
21
+ stopEvery: boolean;
22
+ private scopes;
23
+ private embeddedText;
24
+ private get params();
25
+ private isParam;
26
+ hasAttribute(attribute: string): boolean;
27
+ getAttribute(attribute: string): any;
28
+ setAttribute(attribute: string, value: any): void;
29
+ private context;
30
+ private processing;
31
+ private positionKey;
16
32
  private expressionCache;
17
33
  private evaluated;
18
34
  process(text: string, inline: boolean): string;
@@ -0,0 +1 @@
1
+ export {};
package/dist/types.d.ts CHANGED
@@ -13,6 +13,12 @@ export interface SquiffyInitOptions {
13
13
  * left alone. When not provided, paths are used as they are.
14
14
  */
15
15
  resolveAssetUrl?: (path: string) => string;
16
+ /**
17
+ * The page's text, for a story in another language: squiffy.json's "strings", by the same
18
+ * names. Used for the text of {{restart}} and {{saves}} links (#452), and the dialogs they
19
+ * open when the page doesn't handle them itself. Anything left out is in English.
20
+ */
21
+ strings?: Partial<Record<string, string>>;
16
22
  }
17
23
  export interface SquiffySettings {
18
24
  scroll: string;
@@ -35,6 +41,14 @@ export interface SquiffyApi {
35
41
  getSave: () => SavedGame;
36
42
  loadSave: (save: SavedGame) => Promise<LoadSaveResult>;
37
43
  renderPassage: (name: string) => string;
44
+ isMuted: () => boolean;
45
+ setMuted: (muted: boolean) => void;
46
+ usesSound: () => boolean;
47
+ getSettings: () => PlayerSetting[];
48
+ getSetting: (name: string) => SettingValue | null;
49
+ setSetting: (name: string, value: SettingValue) => void;
50
+ resetSettings: () => void;
51
+ dispose: () => void;
38
52
  on<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): () => void;
39
53
  off<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): void;
40
54
  once<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): () => void;
@@ -57,8 +71,10 @@ export interface Story {
57
71
  id?: string | null;
58
72
  projectId?: string;
59
73
  uiJsIndex?: number;
74
+ audio?: string[];
60
75
  expressions?: boolean;
61
76
  panels?: Panel[];
77
+ settings?: PlayerSetting[];
62
78
  sections: Record<string, Section>;
63
79
  }
64
80
  export declare const PANEL_ICONS: readonly ["bag", "person", "map", "help", "info", "list", "book", "star", "heart"];
@@ -69,9 +85,40 @@ export interface Panel {
69
85
  position?: "side" | "button";
70
86
  icon?: PanelIcon;
71
87
  }
88
+ export type PlayerSetting = PlayerSettingBase & {
89
+ type: "toggle";
90
+ default?: boolean;
91
+ } | PlayerSettingBase & {
92
+ type: "list";
93
+ options: PlayerSettingOption[];
94
+ default?: string;
95
+ } | PlayerSettingBase & {
96
+ type: "range";
97
+ min: number;
98
+ max: number;
99
+ step?: number;
100
+ default?: number;
101
+ };
102
+ interface PlayerSettingBase {
103
+ name: string;
104
+ label: string;
105
+ description?: string;
106
+ }
107
+ export interface PlayerSettingOption {
108
+ value: string;
109
+ label: string;
110
+ }
111
+ export type SettingValue = boolean | string | number;
72
112
  export interface Section {
73
113
  text?: string;
74
114
  clear?: boolean;
115
+ music?: string | null;
116
+ sounds?: string[];
117
+ classes?: string[];
118
+ transition?: string;
119
+ transitionTime?: number;
120
+ leave?: string;
121
+ leaveTime?: number;
75
122
  attributes?: string[];
76
123
  jsIndex?: number;
77
124
  passages?: Record<string, Passage>;
@@ -80,6 +127,13 @@ export interface Section {
80
127
  export interface Passage {
81
128
  text?: string;
82
129
  clear?: boolean;
130
+ music?: string | null;
131
+ sounds?: string[];
132
+ classes?: string[];
133
+ transition?: string;
134
+ transitionTime?: number;
135
+ leave?: string;
136
+ leaveTime?: number;
83
137
  repeat?: boolean;
84
138
  attributes?: string[];
85
139
  jsIndex?: number;
@@ -0,0 +1 @@
1
+ export {};
package/dist/utils.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export declare function fadeReplace(element: HTMLElement, text: string): Promise<void>;
2
+ export declare function parseDelay(value: unknown): number | null;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squiffy-runtime",
3
- "version": "7.0.0-beta.6",
3
+ "version": "7.0.0-beta.8",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/textadventures/squiffy.git",
@@ -49,7 +49,7 @@
49
49
  "esbuild": "^0.28.2",
50
50
  "global-jsdom": "^31.0.0",
51
51
  "jsdom": "^31.0.1",
52
- "squiffy-compiler": "^7.0.0-beta.6",
52
+ "squiffy-compiler": "^7.0.0-beta.8",
53
53
  "typescript": "^6.0.3",
54
54
  "vite": "^8.3.8",
55
55
  "vite-plugin-dts": "^5.1.2"
@@ -59,5 +59,5 @@
59
59
  "handlebars": "^4.7.10",
60
60
  "marked": "^18.2.0"
61
61
  },
62
- "gitHead": "90756c3ea9d782aa3f5f6cc41d9946a0a6cb8a45"
62
+ "gitHead": "385947977612d33f310c92ef6ed9e1a423d8cb19"
63
63
  }
@@ -21,12 +21,12 @@ exports[`Click a passage link 2`] = `
21
21
  "
22
22
  <div><div class="squiffy-output-section" data-section="Introduction" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
23
23
  <p>This is <a href="http://textadventures.co.uk">a website link</a>.</p>
24
- <p>This should be <a class="squiffy-link link-passage disabled" data-passage="passage" role="link" tabindex="-1">a link to a passage</a>. Here's <a class="squiffy-link link-passage" data-passage="passage2" role="link" tabindex="0">another one</a>.</p>
24
+ <p>This should be <a class="squiffy-link link-passage disabled" data-passage="passage" role="link" tabindex="-1" data-clicked="1">a link to a passage</a>. Here's <a class="squiffy-link link-passage" data-passage="passage2" role="link" tabindex="0">another one</a>.</p>
25
25
  <p>You don't need to specify a name - for example, this <a class="squiffy-link link-passage" data-passage="link" role="link" tabindex="0">link</a> and this <a class="squiffy-link link-section" data-section="section" role="link" tabindex="0">section</a>.</p>
26
26
  <p>And this goes to the <a class="squiffy-link link-section" data-section="section2" role="link" tabindex="0">next section</a>.</p>
27
27
  <p>This line has links to <a class="squiffy-link link-section" data-section="section 3" role="link" tabindex="0">section 3</a> and <a class="squiffy-link link-section" data-section="section four" role="link" tabindex="0">section 4</a>.</p>
28
28
  <p>This line has links to <a class="squiffy-link link-passage" data-passage="passage 3" role="link" tabindex="0">passage 3</a> and <a class="squiffy-link link-passage" data-passage="passage four" role="link" tabindex="0">passage 4</a>.</p>
29
- <p>Oh look - <a class="squiffy-link link-passage" data-passage="it's a passage with an apostrophe" role="link" tabindex="0">it's a passage with an apostrophe</a>.</p></div></div><div class="squiffy-output-passage" data-passage="passage" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;Introduction&quot;:1},&quot;_seen_sections&quot;:[&quot;Introduction&quot;]}"><div class="squiffy-output-block"><div data-source="[[Introduction]][passage]"><p>Here's some text for the passage.</p>
29
+ <p>Oh look - <a class="squiffy-link link-passage" data-passage="it's a passage with an apostrophe" role="link" tabindex="0">it's a passage with an apostrophe</a>.</p></div></div><div class="squiffy-output-passage" data-passage="passage" data-clicked-link="1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;Introduction&quot;:1},&quot;_seen_sections&quot;:[&quot;Introduction&quot;]}"><div class="squiffy-output-block"><div data-source="[[Introduction]][passage]"><p>Here's some text for the passage.</p>
30
30
  <p>This passage has a picture in it. It's by <a href="https://unsplash.com/@lmablankers?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Lars Blankers</a> on <a href="https://unsplash.com/photos/brown-potato-lot-B0s3Xndk6tw?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a>.</p>
31
31
  <p><img src="lars-blankers-B0s3Xndk6tw-unsplash.jpg" alt=""></p></div></div></div></div></div>"
32
32
  `;
@@ -81,12 +81,12 @@ exports[`Click a section link and go back 1`] = `
81
81
 
82
82
  exports[`Delete passage 1`] = `
83
83
  "
84
- <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a" role="link" tabindex="-1">a</a></p></div></div><div class="squiffy-output-passage" data-passage="a" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a]"><p>New passage</p></div></div></div></div></div>"
84
+ <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a" role="link" tabindex="-1" data-clicked="1">a</a></p></div></div><div class="squiffy-output-passage" data-passage="a" data-clicked-link="1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a]"><p>New passage</p></div></div></div></div></div>"
85
85
  `;
86
86
 
87
87
  exports[`Delete passage 2`] = `
88
88
  "
89
- <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a" role="link" tabindex="-1">a</a></p></div></div></div></div>"
89
+ <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a" role="link" tabindex="-1" data-clicked="1">a</a></p></div></div></div></div>"
90
90
  `;
91
91
 
92
92
  exports[`Delete section 1`] = `
@@ -101,12 +101,12 @@ exports[`Delete section 2`] = `
101
101
 
102
102
  exports[`Going back handling @clear and attribute changes 1`] = `
103
103
  "
104
- <div><div class="squiffy-clear-stack" style="display: none;"><div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Choose: <a class="squiffy-link link-passage" data-passage="a" role="link">a</a>, <a class="squiffy-link link-passage disabled" data-passage="b" role="link" tabindex="-1">b</a></p></div></div><div class="squiffy-output-passage" data-passage="b" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;],&quot;test&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]][b]"><p>You chose b. Now <a class="squiffy-link link-section" data-section="continue" role="link" tabindex="0">continue</a></p></div></div></div></div><div class="squiffy-output-section" data-section="continue" data-undo="{&quot;turns&quot;:1,&quot;previous&quot;:&quot;_default&quot;,&quot;previous_section&quot;:null,&quot;_passage&quot;:&quot;b&quot;,&quot;_section&quot;:&quot;_default&quot;,&quot;_visits&quot;:{&quot;_default&quot;:1,&quot;b&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;,&quot;b&quot;],&quot;test&quot;:456,&quot;_turncount&quot;:1}"><div class="squiffy-output-block"><div data-source="[[continue]]"><p>Now choose: <a class="squiffy-link link-passage disabled" data-passage="c" role="link" tabindex="-1">c</a>, <a class="squiffy-link link-passage" data-passage="d" role="link" tabindex="0">d</a></p></div></div></div></div></div><div class="squiffy-output-section" data-section="continue" data-clear="true"><div class="squiffy-output-passage" data-passage="c" data-undo="{&quot;turns&quot;:2,&quot;_turncount&quot;:0,&quot;previous&quot;:&quot;b&quot;,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1,&quot;b&quot;:1,&quot;continue&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;,&quot;b&quot;,&quot;continue&quot;],&quot;test&quot;:789}"><div class="squiffy-output-block"><div data-source="[[continue]][c]"><p>You chose c. Now <a class="squiffy-link link-section" data-section="finish" role="link" tabindex="0">finish</a></p></div></div></div></div></div>"
104
+ <div><div class="squiffy-clear-stack" style="display: none;"><div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Choose: <a class="squiffy-link link-passage" data-passage="a" role="link">a</a>, <a class="squiffy-link link-passage disabled" data-passage="b" role="link" tabindex="-1" data-clicked="1">b</a></p></div></div><div class="squiffy-output-passage" data-passage="b" data-clicked-link="1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;],&quot;test&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]][b]"><p>You chose b. Now <a class="squiffy-link link-section" data-section="continue" role="link" tabindex="0">continue</a></p></div></div></div></div><div class="squiffy-output-section" data-section="continue" data-undo="{&quot;turns&quot;:1,&quot;previous&quot;:&quot;_default&quot;,&quot;previous_section&quot;:null,&quot;_passage&quot;:&quot;b&quot;,&quot;_section&quot;:&quot;_default&quot;,&quot;_visits&quot;:{&quot;_default&quot;:1,&quot;b&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;,&quot;b&quot;],&quot;test&quot;:456,&quot;_turncount&quot;:1}"><div class="squiffy-output-block"><div data-source="[[continue]]"><p>Now choose: <a class="squiffy-link link-passage disabled" data-passage="c" role="link" tabindex="-1" data-clicked="2">c</a>, <a class="squiffy-link link-passage" data-passage="d" role="link" tabindex="0">d</a></p></div></div></div></div></div><div class="squiffy-output-section" data-section="continue" data-clear="true"><div class="squiffy-output-passage" data-passage="c" data-clicked-link="2" data-undo="{&quot;turns&quot;:2,&quot;_turncount&quot;:0,&quot;previous&quot;:&quot;b&quot;,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1,&quot;b&quot;:1,&quot;continue&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;,&quot;b&quot;,&quot;continue&quot;],&quot;test&quot;:789}"><div class="squiffy-output-block"><div data-source="[[continue]][c]"><p>You chose c. Now <a class="squiffy-link link-section" data-section="finish" role="link" tabindex="0">finish</a></p></div></div></div></div></div>"
105
105
  `;
106
106
 
107
107
  exports[`Going back handling @clear and attribute changes 2`] = `
108
108
  "
109
- <div><div class="squiffy-clear-stack" style="display: none;"></div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Choose: <a class="squiffy-link link-passage" data-passage="a" role="link">a</a>, <a class="squiffy-link link-passage disabled" data-passage="b" role="link" tabindex="-1">b</a></p></div></div><div class="squiffy-output-passage" data-passage="b" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;],&quot;test&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]][b]"><p>You chose b. Now <a class="squiffy-link link-section" data-section="continue" role="link" tabindex="0">continue</a></p></div></div></div></div><div class="squiffy-output-section" data-section="continue" data-undo="{&quot;turns&quot;:1,&quot;previous&quot;:&quot;_default&quot;,&quot;previous_section&quot;:null,&quot;_passage&quot;:&quot;b&quot;,&quot;_section&quot;:&quot;_default&quot;,&quot;_visits&quot;:{&quot;_default&quot;:1,&quot;b&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;,&quot;b&quot;],&quot;test&quot;:456,&quot;_turncount&quot;:1}"><div class="squiffy-output-block"><div data-source="[[continue]]"><p>Now choose: <a class="squiffy-link link-passage" data-passage="c" role="link">c</a>, <a class="squiffy-link link-passage" data-passage="d" role="link" tabindex="0">d</a></p></div></div></div></div>"
109
+ <div><div class="squiffy-clear-stack" style="display: none;"></div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Choose: <a class="squiffy-link link-passage" data-passage="a" role="link">a</a>, <a class="squiffy-link link-passage disabled" data-passage="b" role="link" tabindex="-1" data-clicked="1">b</a></p></div></div><div class="squiffy-output-passage" data-passage="b" data-clicked-link="1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;],&quot;test&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]][b]"><p>You chose b. Now <a class="squiffy-link link-section" data-section="continue" role="link" tabindex="0">continue</a></p></div></div></div></div><div class="squiffy-output-section" data-section="continue" data-undo="{&quot;turns&quot;:1,&quot;previous&quot;:&quot;_default&quot;,&quot;previous_section&quot;:null,&quot;_passage&quot;:&quot;b&quot;,&quot;_section&quot;:&quot;_default&quot;,&quot;_visits&quot;:{&quot;_default&quot;:1,&quot;b&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;,&quot;b&quot;],&quot;test&quot;:456,&quot;_turncount&quot;:1}"><div class="squiffy-output-block"><div data-source="[[continue]]"><p>Now choose: <a class="squiffy-link link-passage" data-passage="c" role="link">c</a>, <a class="squiffy-link link-passage" data-passage="d" role="link" tabindex="0">d</a></p></div></div></div></div>"
110
110
  `;
111
111
 
112
112
  exports[`Update default section output 1`] = `
@@ -121,20 +121,20 @@ exports[`Update default section output 2`] = `
121
121
 
122
122
  exports[`Update passage output - passage name "a" 1`] = `
123
123
  "
124
- <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a" role="link" tabindex="-1">a</a></p></div></div><div class="squiffy-output-passage" data-passage="a" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a]"><p>Passage a content</p></div></div></div></div></div>"
124
+ <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a" role="link" tabindex="-1" data-clicked="1">a</a></p></div></div><div class="squiffy-output-passage" data-passage="a" data-clicked-link="1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a]"><p>Passage a content</p></div></div></div></div></div>"
125
125
  `;
126
126
 
127
127
  exports[`Update passage output - passage name "a" 2`] = `
128
128
  "
129
- <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a" role="link" tabindex="-1">a</a></p></div></div><div class="squiffy-output-passage" data-passage="a" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a]"><p>Updated passage content</p></div></div></div></div></div>"
129
+ <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a" role="link" tabindex="-1" data-clicked="1">a</a></p></div></div><div class="squiffy-output-passage" data-passage="a" data-clicked-link="1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a]"><p>Updated passage content</p></div></div></div></div></div>"
130
130
  `;
131
131
 
132
132
  exports[`Update passage output - passage name "a'1" 1`] = `
133
133
  "
134
- <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a'1" role="link" tabindex="-1">a'1</a></p></div></div><div class="squiffy-output-passage" data-passage="a'1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a'1]"><p>Passage a content</p></div></div></div></div></div>"
134
+ <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a'1" role="link" tabindex="-1" data-clicked="1">a'1</a></p></div></div><div class="squiffy-output-passage" data-passage="a'1" data-clicked-link="1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a'1]"><p>Passage a content</p></div></div></div></div></div>"
135
135
  `;
136
136
 
137
137
  exports[`Update passage output - passage name "a'1" 2`] = `
138
138
  "
139
- <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a'1" role="link" tabindex="-1">a'1</a></p></div></div><div class="squiffy-output-passage" data-passage="a'1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a'1]"><p>Updated passage content</p></div></div></div></div></div>"
139
+ <div><div class="squiffy-output-section" data-section="_default" data-undo="{&quot;_section&quot;:null,&quot;_visits&quot;:null,&quot;_seen_sections&quot;:null,&quot;_turncount&quot;:null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Click this: <a class="squiffy-link link-passage disabled" data-passage="a'1" role="link" tabindex="-1" data-clicked="1">a'1</a></p></div></div><div class="squiffy-output-passage" data-passage="a'1" data-clicked-link="1" data-undo="{&quot;turns&quot;:0,&quot;_turncount&quot;:0,&quot;previous&quot;:null,&quot;_passage&quot;:null,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;]}"><div class="squiffy-output-block"><div data-source="[[_default]][a'1]"><p>Updated passage content</p></div></div></div></div></div>"
140
140
  `;
@@ -0,0 +1,44 @@
1
+ // The built-in animations in animation.ts
2
+ import { afterAll, beforeAll, expect, test } from "vitest";
3
+ import globalJsdom from "global-jsdom";
4
+ import type { Animation as AnimationClass } from "./animation.js";
5
+
6
+ let cleanup: () => void;
7
+ let Animation: typeof AnimationClass;
8
+
9
+ // anime.js keeps the document it finds when it's first imported, so there's one DOM for the
10
+ // whole file, made before it's imported
11
+ beforeAll(async () => {
12
+ cleanup = globalJsdom("<!DOCTYPE html><html><body></body></html>");
13
+ // jsdom has no ResizeObserver or document.fonts, which anime.js's splitText uses to split text
14
+ // into lines again when it's resized, and once the fonts have loaded
15
+ (globalThis as any).ResizeObserver ??= class { observe() {} unobserve() {} disconnect() {} };
16
+ Object.defineProperty(document, "fonts", { value: { status: "loaded", ready: Promise.resolve() } });
17
+ ({ Animation } = await import("./animation.js"));
18
+ });
19
+
20
+ afterAll(() => {
21
+ cleanup();
22
+ });
23
+
24
+ // The characters the animation split the text into, grouped by the element that holds them
25
+ const charsByParent = (el: HTMLElement) => {
26
+ const groups = new Map<Element, string>();
27
+ for (const char of el.querySelectorAll("[data-char]")) {
28
+ groups.set(char.parentElement!, (groups.get(char.parentElement!) ?? "") + char.textContent);
29
+ }
30
+ return [...groups.values()];
31
+ };
32
+
33
+ test.each([
34
+ ["typewriter", {}],
35
+ ["continue", { style: "wave" }],
36
+ ])("%s keeps each word's characters together, so a line can't break inside a word", (name, params) => {
37
+ const el = document.createElement("span");
38
+ el.textContent = "Now, let's spin...";
39
+ document.body.appendChild(el);
40
+
41
+ new Animation().runAnimation(name, el, params, () => {}, false);
42
+
43
+ expect(charsByParent(el)).toEqual(["Now,", "let's", "spin..."]);
44
+ });
package/src/animation.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { animate, stagger, text } from "animejs";
1
+ import { animate, splitText, stagger } from "animejs";
2
2
 
3
3
  export type AnimationHandler = (el: HTMLElement, params: Record<string, any>, onComplete: () => void, loop: boolean) => void;
4
4
 
@@ -50,7 +50,10 @@ export class Animation {
50
50
 
51
51
  constructor() {
52
52
  this.registerAnimation("typewriter", function(el, params, onComplete, loop) {
53
- const { chars } = text.split(el, { words: false, chars: true });
53
+ // Each character in a span, inside a span for its word, so a line only breaks
54
+ // between words: without the words, it could break anywhere, e.g. at the
55
+ // apostrophe in "let's"
56
+ const { chars } = splitText(el, { words: true, chars: true });
54
57
 
55
58
  const fadeDuration = params.fadeDuration || 100; // ms fade-in per character
56
59
  const interval = params.interval || 100; // ms between each character appearing
@@ -66,7 +69,7 @@ export class Animation {
66
69
  }, { initiallyHidden: true });
67
70
 
68
71
  this.registerAnimation("toast", function(el, params, onComplete, loop) {
69
- const { words } = text.split(el, { words: true, chars: false });
72
+ const { words } = splitText(el, { words: true, chars: false });
70
73
 
71
74
  const fadeDuration = params.fadeDuration || 100; // ms fade-in per word
72
75
  const interval = params.interval || 200; // ms between each word appearing
@@ -106,9 +109,10 @@ export class Animation {
106
109
  let chars: HTMLElement[] | null = null;
107
110
 
108
111
  if (style === "wave") {
109
- // Wave style needs JS animation - split into chars and animate opacity
112
+ // Wave style needs JS animation - split into chars and animate opacity. Words too,
113
+ // so a line only breaks between words, as with typewriter.
110
114
  el.classList.add("squiffy-continue-wave");
111
- const split = text.split(el, { words: false, chars: true });
115
+ const split = splitText(el, { words: true, chars: true });
112
116
  chars = split.chars as HTMLElement[];
113
117
 
114
118
  waveAnimation = animate(chars, {
@@ -11,9 +11,7 @@ vi.mock("animejs", () => ({
11
11
  setTimeout(() => params.onComplete?.());
12
12
  },
13
13
  stagger: () => 0,
14
- text: {
15
- split: (el: HTMLElement) => ({ chars: [el], words: [el], lines: [el] }),
16
- },
14
+ splitText: (el: HTMLElement) => ({ chars: [el], words: [el], lines: [el] }),
17
15
  }));
18
16
 
19
17
  const html = `
package/src/assets.ts CHANGED
@@ -18,7 +18,7 @@ const selector = [...urlAttributes, "srcset", "style"].map(a => `[${a}]`).join("
18
18
 
19
19
  // Anything with a scheme (http:, https:, data:, blob:, mailto: etc.), protocol-relative URLs,
20
20
  // and fragment-only references are left alone.
21
- const isAbsolute = (url: string) => /^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith("//") || url.startsWith("#");
21
+ export const isAbsolute = (url: string) => /^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith("//") || url.startsWith("#");
22
22
 
23
23
  const resolveUrl = (url: string, resolve: AssetUrlResolver): string => {
24
24
  const trimmed = url.trim();
@@ -0,0 +1,201 @@
1
+ // Classes on the story's element (#449): @class for the current section, and {{addclass}},
2
+ // {{removeclass}}, {{toggleclass}} and {{hasclass}}
3
+ import { expect, test, beforeEach, afterEach, vi } from "vitest";
4
+ import globalJsdom from "global-jsdom";
5
+ import { init } from "./squiffy.runtime.js";
6
+ import { compile as squiffyCompile } from "squiffy-compiler";
7
+
8
+ let cleanup: () => void;
9
+
10
+ beforeEach(() => {
11
+ cleanup = globalJsdom("<!DOCTYPE html><html><body><div id=\"squiffy\" class=\"mine\"></div></body></html>");
12
+ vi.useFakeTimers();
13
+ });
14
+
15
+ afterEach(() => {
16
+ vi.useRealTimers();
17
+ localStorage.clear();
18
+ cleanup();
19
+ });
20
+
21
+ const start = async (script: string, options: { persist?: boolean } = {}) => {
22
+ const result = await squiffyCompile({ script });
23
+ if (!result.success) throw new Error("Compile failed");
24
+ const story = {
25
+ ...result.output.story,
26
+ js: result.output.js.map(lines => new Function("squiffy", "get", "set", lines.join("\n"))) as any,
27
+ };
28
+ const element = document.getElementById("squiffy")!;
29
+ const api = await init({ element, story, persist: options.persist ?? false, storyId: "test", scroll: "none" });
30
+ await api.begin();
31
+ return { api, element };
32
+ };
33
+
34
+ const click = async (element: HTMLElement, text: string) => {
35
+ const link = [...element.querySelectorAll<HTMLElement>("a.squiffy-link")]
36
+ .find(l => l.textContent == text && !l.classList.contains("disabled"));
37
+ if (!link) throw new Error(`No link "${text}"`);
38
+ link.click();
39
+ await vi.runAllTimersAsync();
40
+ };
41
+
42
+ // The classes on the story's element, other than the page's own
43
+ const classes = (element: HTMLElement) => [...element.classList].filter(c => c != "mine").sort();
44
+
45
+ const story = `
46
+ [[forest]]:
47
+ @class night forest
48
+ The forest. [[Go to the cave]](cave), [look up]
49
+
50
+ [look up]:
51
+ @class stars
52
+ The stars are out.
53
+
54
+ [[cave]]:
55
+ @class dark
56
+ The cave. [[Leave]](outside)
57
+
58
+ [[outside]]:
59
+ Outside.
60
+ `;
61
+
62
+ test("@class puts classes on the story's element while the section is the current one", async () => {
63
+ const { element } = await start(story);
64
+ expect(classes(element)).toEqual(["forest", "night"]);
65
+
66
+ await click(element, "Go to the cave");
67
+ expect(classes(element)).toEqual(["dark"]);
68
+
69
+ await click(element, "Leave");
70
+ expect(classes(element)).toEqual([]);
71
+ // The page's own class is left alone
72
+ expect(element.classList.contains("mine")).toBe(true);
73
+ });
74
+
75
+ test("@class in a passage adds to the section's classes, until the section changes", async () => {
76
+ const { element } = await start(story);
77
+ await click(element, "look up");
78
+ expect(classes(element)).toEqual(["forest", "night", "stars"]);
79
+
80
+ await click(element, "Go to the cave");
81
+ expect(classes(element)).toEqual(["dark"]);
82
+ });
83
+
84
+ test("Going back puts the classes back", async () => {
85
+ const { api, element } = await start(story);
86
+ await click(element, "look up");
87
+ await click(element, "Go to the cave");
88
+
89
+ api.goBack();
90
+ await vi.runAllTimersAsync();
91
+ expect(classes(element)).toEqual(["forest", "night", "stars"]);
92
+
93
+ api.goBack();
94
+ await vi.runAllTimersAsync();
95
+ expect(classes(element)).toEqual(["forest", "night"]);
96
+ });
97
+
98
+ test("Loading a saved game puts its classes on", async () => {
99
+ const { api, element } = await start(story);
100
+ const save = api.getSave();
101
+ await click(element, "Go to the cave");
102
+ expect(classes(element)).toEqual(["dark"]);
103
+
104
+ await api.loadSave(save);
105
+ await vi.runAllTimersAsync();
106
+ expect(classes(element)).toEqual(["forest", "night"]);
107
+ });
108
+
109
+ test("A story carried on from where it was left has its classes", async () => {
110
+ const first = await start(story, { persist: true });
111
+ await click(first.element, "Go to the cave");
112
+ first.api.dispose();
113
+ expect(classes(first.element)).toEqual([]);
114
+ first.element.innerHTML = "";
115
+
116
+ const second = await start(story, { persist: true });
117
+ expect(classes(second.element)).toEqual(["dark"]);
118
+ });
119
+
120
+ test("The master section's @class is on every section", async () => {
121
+ const { element } = await start(`
122
+ [[]]:
123
+ @class story
124
+
125
+ [[a]]:
126
+ @class one
127
+ [[b]]
128
+
129
+ [[b]]:
130
+ B
131
+ `);
132
+ expect(classes(element)).toEqual(["one", "story"]);
133
+ await click(element, "b");
134
+ expect(classes(element)).toEqual(["story"]);
135
+ });
136
+
137
+ test("{{addclass}} and {{removeclass}} change classes that stay until they're removed", async () => {
138
+ const { api, element } = await start(`
139
+ [[a]]:
140
+ {{addclass "dark" "rain"}}[[b]]
141
+
142
+ [[b]]:
143
+ @class cave
144
+ [[c]]
145
+
146
+ [[c]]:
147
+ {{removeclass "dark"}}{{#if (hasclass "rain")}}Still raining.{{/if}}
148
+ `);
149
+ expect(classes(element)).toEqual(["dark", "rain"]);
150
+ await click(element, "b");
151
+ expect(classes(element)).toEqual(["cave", "dark", "rain"]);
152
+ await click(element, "c");
153
+ expect(classes(element)).toEqual(["rain"]);
154
+ expect(element.textContent).toContain("Still raining.");
155
+
156
+ api.goBack();
157
+ await vi.runAllTimersAsync();
158
+ expect(classes(element)).toEqual(["cave", "dark", "rain"]);
159
+ });
160
+
161
+ test("{{toggleclass}} turns a class on and off, and takes several separated by spaces", async () => {
162
+ const { element } = await start(`
163
+ [[a]]:
164
+ {{toggleclass "big loud"}}[[b]]
165
+
166
+ [[b]]:
167
+ {{toggleclass "loud"}}B
168
+ `);
169
+ expect(classes(element)).toEqual(["big", "loud"]);
170
+ await click(element, "b");
171
+ expect(classes(element)).toEqual(["big"]);
172
+ });
173
+
174
+ test("{{hasclass}} checks classes from @class and {{addclass}}", async () => {
175
+ const { element } = await start(`
176
+ [[a]]:
177
+ @class night
178
+ {{#if (hasclass "night")}}It's night.{{/if}}
179
+ {{#if (hasclass "day")}}It's day.{{/if}}
180
+ {{#if (hasclass "night day")}}Both.{{/if}}
181
+ `);
182
+ expect(element.textContent).toContain("It's night.");
183
+ expect(element.textContent).not.toContain("It's day.");
184
+ expect(element.textContent).not.toContain("Both.");
185
+ });
186
+
187
+ test("Restarting takes the classes off", async () => {
188
+ const { api, element } = await start(`
189
+ [[a]]:
190
+ {{addclass "dark"}}[[b]]
191
+
192
+ [[b]]:
193
+ @class cave
194
+ B
195
+ `);
196
+ await click(element, "b");
197
+ expect(classes(element)).toEqual(["cave", "dark"]);
198
+ api.restart();
199
+ await vi.runAllTimersAsync();
200
+ expect(classes(element)).toEqual(["dark"]);
201
+ });