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

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 (45) hide show
  1. package/dist/events.d.ts +1 -0
  2. package/dist/panels.d.ts +14 -0
  3. package/dist/panels.test.d.ts +1 -0
  4. package/dist/preview-host.d.ts +2 -1
  5. package/dist/preview-host.js +4 -1
  6. package/dist/preview-host.js.map +2 -2
  7. package/dist/preview-html.js +1 -1
  8. package/dist/preview-protocol.d.ts +4 -0
  9. package/dist/saveSlots.d.ts +16 -0
  10. package/dist/saves.test.d.ts +1 -0
  11. package/dist/savesDialog.d.ts +43 -0
  12. package/dist/savesDialog.test.d.ts +1 -0
  13. package/dist/squiffy.page.css +46 -22
  14. package/dist/squiffy.preview.html +604 -108
  15. package/dist/squiffy.runtime.css +468 -8
  16. package/dist/squiffy.runtime.d.ts +9 -1
  17. package/dist/squiffy.runtime.global.js +113 -72
  18. package/dist/squiffy.runtime.global.js.map +1 -1
  19. package/dist/squiffy.runtime.js +4154 -3534
  20. package/dist/squiffy.runtime.js.map +1 -1
  21. package/dist/state.d.ts +2 -0
  22. package/dist/textProcessor.d.ts +1 -0
  23. package/dist/types.d.ts +30 -0
  24. package/package.json +3 -3
  25. package/src/__snapshots__/squiffy.runtime.test.ts.snap +10 -10
  26. package/src/events.ts +1 -0
  27. package/src/panels.test.ts +201 -0
  28. package/src/panels.ts +187 -0
  29. package/src/preview-host.test.ts +5 -1
  30. package/src/preview-host.ts +9 -3
  31. package/src/preview-protocol.ts +6 -2
  32. package/src/preview.test.ts +68 -3
  33. package/src/preview.ts +74 -13
  34. package/src/saveSlots.ts +159 -0
  35. package/src/saves.test.ts +199 -0
  36. package/src/savesDialog.test.ts +290 -0
  37. package/src/savesDialog.ts +499 -0
  38. package/src/squiffy.page.css +46 -22
  39. package/src/squiffy.runtime.css +468 -8
  40. package/src/squiffy.runtime.test.ts +375 -4
  41. package/src/squiffy.runtime.ts +227 -10
  42. package/src/state.ts +18 -4
  43. package/src/textProcessor.ts +50 -9
  44. package/src/types.ts +54 -1
  45. package/vite.preview.config.ts +7 -0
package/dist/state.d.ts CHANGED
@@ -8,8 +8,10 @@ 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
+ readOnly: boolean;
11
12
  set(attribute: string, value: any): void;
12
13
  setInternal(attribute: string, value: any, raiseEvents: boolean): void;
14
+ private has;
13
15
  get(attribute: string): any;
14
16
  getStore(): Record<string, any>;
15
17
  load(): void;
@@ -12,6 +12,7 @@ export declare class TextProcessor {
12
12
  section: string;
13
13
  after: number;
14
14
  } | null;
15
+ deactivate: Set<string>;
15
16
  private expressionCache;
16
17
  private evaluated;
17
18
  process(text: string, inline: boolean): string;
package/dist/types.d.ts CHANGED
@@ -32,6 +32,9 @@ export interface SquiffyApi {
32
32
  update: (story: Story) => void;
33
33
  goBack: () => void;
34
34
  canGoBack: () => boolean;
35
+ getSave: () => SavedGame;
36
+ loadSave: (save: SavedGame) => Promise<LoadSaveResult>;
37
+ renderPassage: (name: string) => string;
35
38
  on<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): () => void;
36
39
  off<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): void;
37
40
  once<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): () => void;
@@ -52,10 +55,20 @@ export interface Story {
52
55
  js: ((squiffy: SquiffyJsFunctionApi, get: (attribute: string) => any, set: (attribute: string, value: any) => void) => void)[];
53
56
  start: string;
54
57
  id?: string | null;
58
+ projectId?: string;
55
59
  uiJsIndex?: number;
56
60
  expressions?: boolean;
61
+ panels?: Panel[];
57
62
  sections: Record<string, Section>;
58
63
  }
64
+ export declare const PANEL_ICONS: readonly ["bag", "person", "map", "help", "info", "list", "book", "star", "heart"];
65
+ export type PanelIcon = typeof PANEL_ICONS[number];
66
+ export interface Panel {
67
+ passage: string;
68
+ label: string;
69
+ position?: "side" | "button";
70
+ icon?: PanelIcon;
71
+ }
59
72
  export interface Section {
60
73
  text?: string;
61
74
  clear?: boolean;
@@ -67,7 +80,24 @@ export interface Section {
67
80
  export interface Passage {
68
81
  text?: string;
69
82
  clear?: boolean;
83
+ repeat?: boolean;
70
84
  attributes?: string[];
71
85
  jsIndex?: number;
72
86
  }
87
+ export interface SavedGame {
88
+ format: "squiffy-save";
89
+ version: 1;
90
+ storyId: string | null;
91
+ section: string | null;
92
+ passage: string | null;
93
+ summary: string;
94
+ savedAt: string;
95
+ state: Record<string, unknown>;
96
+ }
97
+ export type LoadSaveResult = {
98
+ success: true;
99
+ } | {
100
+ success: false;
101
+ error: "notASave" | "wrongStory";
102
+ };
73
103
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squiffy-runtime",
3
- "version": "7.0.0-beta.5",
3
+ "version": "7.0.0-beta.6",
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.5",
52
+ "squiffy-compiler": "^7.0.0-beta.6",
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": "9495ea44822999ad3e5c8fa305a88c5f5e43f1d8"
62
+ "gitHead": "90756c3ea9d782aa3f5f6cc41d9946a0a6cb8a45"
63
63
  }
@@ -26,7 +26,7 @@ exports[`Click a passage link 2`] = `
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;_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-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
  `;
@@ -64,7 +64,7 @@ exports[`Click a section link 2`] = `
64
64
  <p>And this goes to the <a class="squiffy-link link-section" data-section="section2" role="link" tabindex="0">next section</a>.</p>
65
65
  <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>
66
66
  <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>
67
- <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><div class="squiffy-output-section" data-section="section 3" data-undo="{&quot;turns&quot;:0,&quot;_section&quot;:&quot;Introduction&quot;,&quot;_visits&quot;:{&quot;Introduction&quot;:1},&quot;_seen_sections&quot;:[&quot;Introduction&quot;],&quot;_turncount&quot;:0}"><div class="squiffy-output-block"><div data-source="[[section 3]]"><p>Another section is here, with passages <a class="squiffy-link link-passage" data-passage="a" role="link" tabindex="0">a</a> and <a class="squiffy-link link-passage" data-passage="b" role="link" tabindex="0">b</a>.</p></div></div></div></div>"
67
+ <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><div class="squiffy-output-section" data-section="section 3" data-undo="{&quot;turns&quot;:0,&quot;previous&quot;:null,&quot;previous_section&quot;:null,&quot;_section&quot;:&quot;Introduction&quot;,&quot;_visits&quot;:{&quot;Introduction&quot;:1},&quot;_seen_sections&quot;:[&quot;Introduction&quot;],&quot;_turncount&quot;:0}"><div class="squiffy-output-block"><div data-source="[[section 3]]"><p>Another section is here, with passages <a class="squiffy-link link-passage" data-passage="a" role="link" tabindex="0">a</a> and <a class="squiffy-link link-passage" data-passage="b" role="link" tabindex="0">b</a>.</p></div></div></div></div>"
68
68
  `;
69
69
 
70
70
  exports[`Click a section link and go back 1`] = `
@@ -81,7 +81,7 @@ 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;_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">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>"
85
85
  `;
86
86
 
87
87
  exports[`Delete passage 2`] = `
@@ -91,7 +91,7 @@ exports[`Delete passage 2`] = `
91
91
 
92
92
  exports[`Delete section 1`] = `
93
93
  "
94
- <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-section" data-section="a" role="link" tabindex="0">a</a></p></div></div></div><div class="squiffy-output-section" data-section="a" data-undo="{&quot;turns&quot;:0,&quot;_section&quot;:&quot;_default&quot;,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;],&quot;_turncount&quot;:0}"><div class="squiffy-output-block"><div data-source="[[a]]"><p>New section</p></div></div></div></div>"
94
+ <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-section" data-section="a" role="link" tabindex="0">a</a></p></div></div></div><div class="squiffy-output-section" data-section="a" data-undo="{&quot;turns&quot;:0,&quot;previous&quot;:null,&quot;previous_section&quot;:null,&quot;_section&quot;:&quot;_default&quot;,&quot;_visits&quot;:{&quot;_default&quot;:1},&quot;_seen_sections&quot;:[&quot;_default&quot;],&quot;_turncount&quot;:0}"><div class="squiffy-output-block"><div data-source="[[a]]"><p>New section</p></div></div></div></div>"
95
95
  `;
96
96
 
97
97
  exports[`Delete section 2`] = `
@@ -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;_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;_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;_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">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>"
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;_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;_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">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>"
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;_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">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>"
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;_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">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>"
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;_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">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>"
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;_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">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>"
140
140
  `;
package/src/events.ts CHANGED
@@ -5,6 +5,7 @@ export type SquiffyEventMap = {
5
5
  set: { attribute: string, value: any }; // an attribute was set
6
6
  canGoBackChanged: { canGoBack: boolean }; // whether the "back" button should be enabled
7
7
  navigated: { section: string, passage?: string }; // a section or passage was shown, or Back went to one
8
+ storyUpdated: Record<string, never>; // update() changed the story, e.g. after an edit in the editor
8
9
  };
9
10
 
10
11
  export type SquiffyEventHandler<E extends keyof SquiffyEventMap> =
@@ -0,0 +1,201 @@
1
+ import { expect, test, beforeEach, afterEach } from "vitest";
2
+ import globalJsdom from "global-jsdom";
3
+ import { init } from "./squiffy.runtime.js";
4
+ import { compile as squiffyCompile, PANEL_ICONS as COMPILER_PANEL_ICONS } from "squiffy-compiler";
5
+ import { createPanels } from "./panels.js";
6
+ import { PANEL_ICONS, type Panel, type SquiffyApi } from "./types.js";
7
+
8
+ let cleanup: () => void;
9
+
10
+ beforeEach(() => {
11
+ cleanup = globalJsdom("<!DOCTYPE html><html><body><div id=\"container\"><div id=\"header\"><button id=\"restart\"></button></div><div id=\"squiffy\"></div></div></body></html>");
12
+ // jsdom has no showModal()
13
+ window.HTMLDialogElement.prototype.showModal = function () { this.setAttribute("open", ""); };
14
+ window.HTMLDialogElement.prototype.close = function () {
15
+ this.removeAttribute("open");
16
+ this.dispatchEvent(new window.Event("close"));
17
+ };
18
+ });
19
+
20
+ afterEach(() => {
21
+ cleanup();
22
+ });
23
+
24
+ const script = `[[]]:
25
+
26
+ [status]:
27
+ Gold: {{gold}}
28
+
29
+ [lamp]:
30
+ {{#if lamp}}You have a lamp.{{/if}}
31
+
32
+ [inventory]:
33
+ {{#if lamp}}You have a lamp.{{/if}}{{set "gold" 1000}}{{goto "end"}} Go to [[end]] or [here](b).
34
+
35
+ [[start]]:
36
+ @set gold = 5
37
+ Welcome. [Get the lamp](b, lamp, gold += 10) or go to [[end]].
38
+
39
+ [b]:
40
+ You have it.
41
+
42
+ [[end]]:
43
+ The end.`;
44
+
45
+ const start = async () => {
46
+ const element = document.getElementById("squiffy")!;
47
+ const result = await squiffyCompile({ script });
48
+ if (!result.success) throw new Error("Compile failed");
49
+ const js = result.output.js.map(lines => new Function("squiffy", "get", "set", lines.join("\n")));
50
+ const api = await init({ element, story: { js: js as any, ...result.output.story }, persist: false });
51
+ await api.begin();
52
+ return { api, element };
53
+ };
54
+
55
+ const click = async (api: SquiffyApi, element: HTMLElement, text: string) => {
56
+ const link = [...element.querySelectorAll<HTMLElement>("a.squiffy-link")]
57
+ .filter(l => l.textContent === text && !l.classList.contains("disabled")).pop()!;
58
+ expect(await api.clickLink(link)).toBe(true);
59
+ };
60
+
61
+ // Lets the panels' queued refresh happen
62
+ const settle = () => new Promise(resolve => setTimeout(resolve, 0));
63
+
64
+ const panelsFor = (api: SquiffyApi, panels: Panel[]) => createPanels(api, {
65
+ panels,
66
+ buttons: document.getElementById("header")!,
67
+ side: document.getElementById("container")!,
68
+ });
69
+
70
+ test("renderPassage renders a master section passage with the attributes as they are", async () => {
71
+ const { api } = await start();
72
+ expect(api.renderPassage("status")).toBe("<p>Gold: 5</p>");
73
+ });
74
+
75
+ test("renderPassage changes nothing, and disables its links", async () => {
76
+ const { api, element } = await start();
77
+ const html = api.renderPassage("inventory");
78
+ expect(api.get("gold")).toBe(5);
79
+ expect(api.get("_section")).toBe("start");
80
+ expect(html).toContain("class=\"squiffy-link link-section disabled\"");
81
+ expect(html).toContain("class=\"squiffy-link link-passage disabled\"");
82
+ // Its {{goto}} isn't followed when the story moves on
83
+ await click(api, element, "Get the lamp");
84
+ expect(api.get("_section")).toBe("start");
85
+ expect(api.get("gold")).toBe(15);
86
+ });
87
+
88
+ test("renderPassage shows an error for a passage that isn't in the master section", async () => {
89
+ const { api } = await start();
90
+ expect(api.renderPassage("b")).toContain("there is no passage called \"b\" in the master section [[]]");
91
+ });
92
+
93
+ test("a side panel is shown beside the story, and rendered again as the story goes on", async () => {
94
+ const { api, element } = await start();
95
+ panelsFor(api, [{ passage: "status", label: "Status", position: "side" }]);
96
+ const container = document.getElementById("container")!;
97
+ expect(container.classList.contains("squiffy-has-side")).toBe(true);
98
+ const panel = container.querySelector("aside.squiffy-panels-side > section.squiffy-panel")!;
99
+ expect(panel.getAttribute("aria-label")).toBe("Status");
100
+ expect(panel.innerHTML).toBe("<p>Gold: 5</p>");
101
+
102
+ await click(api, element, "Get the lamp");
103
+ await settle();
104
+ expect(panel.innerHTML).toBe("<p>Gold: 15</p>");
105
+
106
+ api.goBack();
107
+ await settle();
108
+ expect(panel.innerHTML).toBe("<p>Gold: 5</p>");
109
+
110
+ // Its button is for narrow screens, where CSS hides the panel
111
+ const button = document.querySelector<HTMLButtonElement>("#header button.squiffy-panel-button")!;
112
+ expect(button.classList.contains("squiffy-panel-side-button")).toBe(true);
113
+ });
114
+
115
+ test("a side panel whose text comes out empty is hidden", async () => {
116
+ const { api, element } = await start();
117
+ panelsFor(api, [{ passage: "lamp", label: "Lamp", position: "side" }]);
118
+ const aside = document.querySelector<HTMLElement>("aside.squiffy-panels-side")!;
119
+ const section = aside.querySelector<HTMLElement>("section")!;
120
+ expect(section.hidden).toBe(true);
121
+ expect(aside.hidden).toBe(true);
122
+ await click(api, element, "Get the lamp");
123
+ await settle();
124
+ expect(section.hidden).toBe(false);
125
+ expect(aside.hidden).toBe(false);
126
+ });
127
+
128
+ test("a button panel's button opens it in a dialog, rendered fresh", async () => {
129
+ const { api, element } = await start();
130
+ panelsFor(api, [{ passage: "status", label: "Status" }, { passage: "inventory", label: "Bag", icon: "bag" }]);
131
+ const buttons = [...document.querySelectorAll<HTMLButtonElement>("#header button")];
132
+ // Before the page's own buttons, in the order they're listed
133
+ expect(buttons.map(b => b.id || b.dataset.passage)).toEqual(["status", "inventory", "restart"]);
134
+ expect(buttons[0].textContent).toBe("Status");
135
+ expect(buttons[0].classList.contains("squiffy-panel-text-button")).toBe(true);
136
+ expect(buttons[1].getAttribute("aria-label")).toBe("Bag");
137
+ expect(buttons[1].querySelector("svg")).not.toBeNull();
138
+ // No side panels, so no aside
139
+ expect(document.querySelector("aside")).toBeNull();
140
+
141
+ buttons[0].click();
142
+ const dialog = document.querySelector<HTMLDialogElement>("dialog.squiffy-panel-dialog")!;
143
+ expect(dialog.hasAttribute("open")).toBe(true);
144
+ expect(dialog.querySelector("h2")!.textContent).toBe("Status");
145
+ expect(dialog.querySelector(".squiffy-panel-dialog-content")!.innerHTML).toBe("<p>Gold: 5</p>");
146
+ // The story's text isn't touched
147
+ expect(element.textContent).not.toContain("Gold:");
148
+
149
+ // While it's open, it follows the story
150
+ api.set("gold", 7);
151
+ await settle();
152
+ expect(dialog.querySelector(".squiffy-panel-dialog-content")!.innerHTML).toBe("<p>Gold: 7</p>");
153
+
154
+ dialog.querySelector<HTMLButtonElement>(".squiffy-panel-dialog-close")!.click();
155
+ expect(dialog.hasAttribute("open")).toBe(false);
156
+ });
157
+
158
+ test("opening a panel doesn't count as a move", async () => {
159
+ const { api } = await start();
160
+ const panels = panelsFor(api, [{ passage: "status", label: "Status" }]);
161
+ panels.open("status");
162
+ expect(api.canGoBack()).toBe(false);
163
+ });
164
+
165
+ test("without a side element, a side panel only has its button", async () => {
166
+ const { api } = await start();
167
+ createPanels(api, { panels: [{ passage: "status", label: "Status", position: "side" }], buttons: document.getElementById("header")! });
168
+ expect(document.querySelector("aside")).toBeNull();
169
+ const button = document.querySelector<HTMLButtonElement>("button.squiffy-panel-button")!;
170
+ expect(button.classList.contains("squiffy-panel-side-button")).toBe(false);
171
+ });
172
+
173
+ test("dispose takes the panels off the page", async () => {
174
+ const { api } = await start();
175
+ const panels = panelsFor(api, [{ passage: "status", label: "Status", position: "side" }, { passage: "inventory", label: "Bag" }]);
176
+ panels.dispose();
177
+ expect(document.querySelector("aside, dialog, button.squiffy-panel-button")).toBeNull();
178
+ expect(document.getElementById("container")!.classList.contains("squiffy-has-side")).toBe(false);
179
+ expect(document.getElementById("restart")).not.toBeNull();
180
+ });
181
+
182
+ test("the dialog's Close button can be translated", async () => {
183
+ const { api } = await start();
184
+ createPanels(api, { panels: [{ passage: "status", label: "État" }], buttons: document.getElementById("header")!, strings: { close: "Fermer" } });
185
+ expect(document.querySelector(".squiffy-panel-dialog-close")!.getAttribute("aria-label")).toBe("Fermer");
186
+ });
187
+
188
+ test("a hot update renders the panels again", async () => {
189
+ const { api } = await start();
190
+ panelsFor(api, [{ passage: "status", label: "Status", position: "side" }]);
191
+ const result = await squiffyCompile({ script: script.replace("Gold: {{gold}}", "Coins: {{gold}}") });
192
+ if (!result.success) throw new Error("Compile failed");
193
+ const js = result.output.js.map(lines => new Function("squiffy", "get", "set", lines.join("\n")));
194
+ api.update({ js: js as any, ...result.output.story });
195
+ await settle();
196
+ expect(document.querySelector("aside section")!.innerHTML).toBe("<p>Coins: 5</p>");
197
+ });
198
+
199
+ test("the compiler checks for the same icons the runtime has", () => {
200
+ expect([...COMPILER_PANEL_ICONS]).toEqual([...PANEL_ICONS]);
201
+ });
package/src/panels.ts ADDED
@@ -0,0 +1,187 @@
1
+ // Panels (#14, #443): passages in the master section that are always to hand, rendered again with
2
+ // the attributes as they are whenever the story moves on, e.g. the player's stats, inventory or
3
+ // a map. A "side" panel is shown beside the story, and a "button" panel is shown in a dialog,
4
+ // opened by a button in the header. Neither is part of the story's text, so they don't count as
5
+ // moves for Back or saved games. On a screen too narrow for a side panel, it has a button too.
6
+ //
7
+ // const panels = createPanels(squiffyApi, {
8
+ // panels: story.panels,
9
+ // buttons: document.getElementById("squiffy-header"),
10
+ // side: document.getElementById("squiffy-container"),
11
+ // });
12
+
13
+ import { PANEL_ICONS, type Panel, type PanelIcon, type SquiffyApi } from "./types.js";
14
+
15
+ export interface PanelsOptions {
16
+ // From the story's squiffy.json "panels" (story.panels)
17
+ panels: Panel[];
18
+ // Where the panels' buttons go: at the start of this element, e.g. the page's header
19
+ buttons: HTMLElement;
20
+ // Where side panels go: an <aside class="squiffy-panels-side"> is added at the end of this
21
+ // element, which gets the class squiffy-has-side, so its CSS can make room. Without it,
22
+ // side panels only have their buttons.
23
+ side?: HTMLElement;
24
+ // The dialog's text, for a story in another language: "close". Anything left out is in English.
25
+ strings?: Partial<Record<string, string>>;
26
+ // Where the dialog goes in the page: the end of the body by default
27
+ container?: HTMLElement;
28
+ }
29
+
30
+ export interface Panels {
31
+ // Renders the panels again, e.g. after changing an attribute from outside the story
32
+ refresh: () => void;
33
+ // Opens a panel's dialog, by its passage name
34
+ open: (passage: string) => void;
35
+ // Takes the panels, their buttons and their dialog off the page
36
+ dispose: () => void;
37
+ }
38
+
39
+ const ICON_CLOSE = "<path d=\"M18 6 6 18M6 6l12 12\"/>";
40
+
41
+ // Lucide icons (ISC licence), as are the page's other buttons
42
+ const ICONS: Record<PanelIcon, string> = {
43
+ bag: "<path d=\"M4 10a4 4 0 0 1 4-4h8a4 4 0 0 1 4 4v10a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2z\"/><path d=\"M8 10h8\"/><path d=\"M8 18h8\"/><path d=\"M8 22v-6a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v6\"/><path d=\"M9 6V4a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v2\"/>",
44
+ person: "<path d=\"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2\"/><circle cx=\"12\" cy=\"7\" r=\"4\"/>",
45
+ map: "<path d=\"M14.106 5.553a2 2 0 0 0 1.788 0l3.659-1.83A1 1 0 0 1 21 4.619v12.764a1 1 0 0 1-.553.894l-4.553 2.277a2 2 0 0 1-1.788 0l-4.212-2.106a2 2 0 0 0-1.788 0l-3.659 1.83A1 1 0 0 1 3 19.381V6.618a1 1 0 0 1 .553-.894l4.553-2.277a2 2 0 0 1 1.788 0z\"/><path d=\"M15 5.764v15\"/><path d=\"M9 3.236v15\"/>",
46
+ help: "<circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3\"/><path d=\"M12 17h.01\"/>",
47
+ info: "<circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"M12 16v-4\"/><path d=\"M12 8h.01\"/>",
48
+ list: "<path d=\"M3 6h.01M3 12h.01M3 18h.01M8 6h13M8 12h13M8 18h13\"/>",
49
+ book: "<path d=\"M12 7v14\"/><path d=\"M3 18a1 1 0 0 1-1-1V4a1 1 0 0 1 1-1h5a4 4 0 0 1 4 4 4 4 0 0 1 4-4h5a1 1 0 0 1 1 1v13a1 1 0 0 1-1 1h-6a3 3 0 0 0-3 3 3 3 0 0 0-3-3z\"/>",
50
+ star: "<path d=\"M11.525 2.295a.53.53 0 0 1 .95 0l2.31 4.679a2.123 2.123 0 0 0 1.595 1.16l5.166.756a.53.53 0 0 1 .294.904l-3.736 3.638a2.123 2.123 0 0 0-.611 1.878l.882 5.14a.53.53 0 0 1-.771.56l-4.618-2.428a2.122 2.122 0 0 0-1.973 0L6.396 21.01a.53.53 0 0 1-.77-.56l.881-5.139a2.122 2.122 0 0 0-.611-1.879L2.16 9.795a.53.53 0 0 1 .294-.906l5.165-.755a2.122 2.122 0 0 0 1.597-1.16z\"/>",
51
+ heart: "<path d=\"M19 14c1.49-1.46 3-3.21 3-5.5A5.5 5.5 0 0 0 16.5 3c-1.76 0-3 .5-4.5 2-1.5-1.5-2.74-2-4.5-2A5.5 5.5 0 0 0 2 8.5c0 2.3 1.5 4.05 3 5.5l7 7Z\"/>",
52
+ };
53
+
54
+ const svg = (paths: string) => `<svg viewBox="0 0 24 24" aria-hidden="true">${paths}</svg>`;
55
+
56
+ let dialogCount = 0;
57
+
58
+ export function createPanels(api: SquiffyApi, options: PanelsOptions): Panels {
59
+ const close = typeof options.strings?.close == "string" ? options.strings.close : "Close";
60
+ const id = `squiffy-panel-${++dialogCount}`;
61
+ const cleanups: (() => void)[] = [];
62
+
63
+ // The dialog that button panels are shown in, one at a time
64
+ const dialog = document.createElement("dialog");
65
+ dialog.className = "squiffy-panel-dialog";
66
+ dialog.setAttribute("aria-labelledby", `${id}-heading`);
67
+ dialog.innerHTML = `
68
+ <div class="squiffy-panel-dialog-header">
69
+ <h2 id="${id}-heading"></h2>
70
+ <button type="button" class="squiffy-panel-dialog-close">${svg(ICON_CLOSE)}</button>
71
+ </div>
72
+ <div class="squiffy-panel squiffy-panel-dialog-content"></div>`;
73
+ const heading = dialog.querySelector("h2")!;
74
+ const closeButton = dialog.querySelector<HTMLButtonElement>(".squiffy-panel-dialog-close")!;
75
+ closeButton.title = close;
76
+ closeButton.setAttribute("aria-label", close);
77
+ closeButton.addEventListener("click", () => dialog.close());
78
+ const content = dialog.querySelector<HTMLDivElement>(".squiffy-panel-dialog-content")!;
79
+ // Clicking the backdrop closes it too
80
+ dialog.addEventListener("click", event => {
81
+ if (event.target === dialog) dialog.close();
82
+ });
83
+ (options.container ?? document.body).appendChild(dialog);
84
+ cleanups.push(() => dialog.remove());
85
+ // The panel the dialog is showing
86
+ let shown: Panel | null = null;
87
+
88
+ const panels = options.panels.filter(panel => panel && typeof panel.passage == "string");
89
+
90
+ // The side panels, beside the story
91
+ const sidePanels = options.side ? panels.filter(panel => panel.position == "side") : [];
92
+ const sideSections = new Map<Panel, HTMLElement>();
93
+ let aside: HTMLElement | null = null;
94
+ if (sidePanels.length && options.side) {
95
+ aside = document.createElement("aside");
96
+ aside.className = "squiffy-panels-side";
97
+ for (const panel of sidePanels) {
98
+ const section = document.createElement("section");
99
+ section.className = "squiffy-panel";
100
+ section.dataset.passage = panel.passage;
101
+ section.setAttribute("aria-label", panel.label || panel.passage);
102
+ aside.appendChild(section);
103
+ sideSections.set(panel, section);
104
+ }
105
+ const side = options.side;
106
+ side.appendChild(aside);
107
+ side.classList.add("squiffy-has-side");
108
+ cleanups.push(() => {
109
+ aside!.remove();
110
+ side.classList.remove("squiffy-has-side");
111
+ });
112
+ }
113
+
114
+ // A button for each panel, in the order they're listed, before anything already in the header.
115
+ // A side panel's button is only shown on screens too narrow for the panel.
116
+ const first = options.buttons.firstChild;
117
+ for (const panel of panels) {
118
+ const label = panel.label || panel.passage;
119
+ const button = document.createElement("button");
120
+ button.type = "button";
121
+ button.className = "squiffy-header-button squiffy-panel-button";
122
+ if (panel.position == "side" && sideSections.has(panel)) button.classList.add("squiffy-panel-side-button");
123
+ button.dataset.passage = panel.passage;
124
+ button.setAttribute("aria-haspopup", "dialog");
125
+ const icon = panel.icon && (PANEL_ICONS as readonly string[]).includes(panel.icon) ? ICONS[panel.icon] : null;
126
+ if (icon) {
127
+ button.innerHTML = svg(icon);
128
+ button.title = label;
129
+ button.setAttribute("aria-label", label);
130
+ }
131
+ else {
132
+ button.classList.add("squiffy-panel-text-button");
133
+ button.textContent = label;
134
+ }
135
+ button.addEventListener("click", () => open(panel));
136
+ options.buttons.insertBefore(button, first);
137
+ cleanups.push(() => button.remove());
138
+ }
139
+
140
+ function open(panel: Panel) {
141
+ shown = panel;
142
+ heading.textContent = panel.label || panel.passage;
143
+ content.innerHTML = api.renderPassage(panel.passage);
144
+ if (!dialog.open) dialog.showModal();
145
+ }
146
+
147
+ function refresh() {
148
+ for (const [panel, section] of sideSections) {
149
+ section.innerHTML = api.renderPassage(panel.passage);
150
+ // Text that comes out empty, e.g. from an {{#if}}, hides the panel
151
+ section.hidden = !section.textContent?.trim() && !section.querySelector("img, svg, video, canvas, iframe");
152
+ }
153
+ if (aside) aside.hidden = [...sideSections.values()].every(section => section.hidden);
154
+ if (dialog.open && shown) content.innerHTML = api.renderPassage(shown.passage);
155
+ }
156
+
157
+ // Rendered again once whatever changed has finished changing, rather than after every attribute
158
+ let queued = false;
159
+ const queueRefresh = () => {
160
+ if (queued) return;
161
+ queued = true;
162
+ queueMicrotask(() => {
163
+ queued = false;
164
+ if (!disposed) refresh();
165
+ });
166
+ };
167
+ for (const event of ["set", "navigated", "storyUpdated"] as const) {
168
+ cleanups.push(api.on(event, queueRefresh));
169
+ }
170
+ dialog.addEventListener("close", () => { shown = null; });
171
+
172
+ let disposed = false;
173
+ refresh();
174
+
175
+ return {
176
+ refresh,
177
+ open(passage: string) {
178
+ const panel = panels.find(p => p.passage == passage);
179
+ if (panel) open(panel);
180
+ },
181
+ dispose() {
182
+ disposed = true;
183
+ if (dialog.open) dialog.close();
184
+ for (const cleanup of cleanups.splice(0)) cleanup();
185
+ },
186
+ };
187
+ }