squiffy-runtime 7.0.0-beta.4 → 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.
- package/dist/events.d.ts +1 -0
- package/dist/panels.d.ts +14 -0
- package/dist/panels.test.d.ts +1 -0
- package/dist/preview-host.d.ts +2 -1
- package/dist/preview-host.js +4 -1
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +4 -0
- package/dist/saveSlots.d.ts +16 -0
- package/dist/saves.test.d.ts +1 -0
- package/dist/savesDialog.d.ts +43 -0
- package/dist/savesDialog.test.d.ts +1 -0
- package/dist/squiffy.page.css +46 -22
- package/dist/squiffy.preview.html +612 -109
- package/dist/squiffy.runtime.css +476 -9
- package/dist/squiffy.runtime.d.ts +9 -1
- package/dist/squiffy.runtime.global.js +113 -72
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +4146 -3460
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +4 -0
- package/dist/textProcessor.d.ts +5 -0
- package/dist/types.d.ts +32 -0
- package/package.json +3 -3
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +21 -21
- package/src/events.ts +1 -0
- package/src/panels.test.ts +201 -0
- package/src/panels.ts +187 -0
- package/src/plugins/plugins.test.ts +42 -0
- package/src/plugins/replaceLabel.ts +6 -2
- package/src/preview-host.test.ts +5 -1
- package/src/preview-host.ts +9 -3
- package/src/preview-protocol.ts +6 -2
- package/src/preview.test.ts +71 -3
- package/src/preview.ts +74 -13
- package/src/saveSlots.ts +159 -0
- package/src/saves.test.ts +199 -0
- package/src/savesDialog.test.ts +290 -0
- package/src/savesDialog.ts +499 -0
- package/src/squiffy.page.css +46 -22
- package/src/squiffy.runtime.css +476 -9
- package/src/squiffy.runtime.test.ts +890 -4
- package/src/squiffy.runtime.ts +322 -17
- package/src/state.ts +34 -4
- package/src/textProcessor.ts +76 -9
- package/src/types.ts +57 -1
- package/vite.preview.config.ts +7 -0
package/dist/state.d.ts
CHANGED
|
@@ -8,12 +8,16 @@ 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;
|
|
16
18
|
reset(): void;
|
|
17
19
|
setSeen(sectionName: string): void;
|
|
18
20
|
getSeen(sectionName: string): boolean;
|
|
21
|
+
getVisits(sectionName: string): number;
|
|
22
|
+
private getVisitsFrom;
|
|
19
23
|
}
|
package/dist/textProcessor.d.ts
CHANGED
|
@@ -8,6 +8,11 @@ export declare class TextProcessor {
|
|
|
8
8
|
handlebars: typeof Handlebars;
|
|
9
9
|
constructor(story: () => Story, state: State, currentSection: () => Section);
|
|
10
10
|
evaluate(expression: string): unknown;
|
|
11
|
+
goto: {
|
|
12
|
+
section: string;
|
|
13
|
+
after: number;
|
|
14
|
+
} | null;
|
|
15
|
+
deactivate: Set<string>;
|
|
11
16
|
private expressionCache;
|
|
12
17
|
private evaluated;
|
|
13
18
|
process(text: string, inline: boolean): string;
|
package/dist/types.d.ts
CHANGED
|
@@ -31,6 +31,10 @@ export interface SquiffyApi {
|
|
|
31
31
|
clickLink: (link: HTMLElement) => Promise<boolean>;
|
|
32
32
|
update: (story: Story) => void;
|
|
33
33
|
goBack: () => void;
|
|
34
|
+
canGoBack: () => boolean;
|
|
35
|
+
getSave: () => SavedGame;
|
|
36
|
+
loadSave: (save: SavedGame) => Promise<LoadSaveResult>;
|
|
37
|
+
renderPassage: (name: string) => string;
|
|
34
38
|
on<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): () => void;
|
|
35
39
|
off<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): void;
|
|
36
40
|
once<E extends keyof SquiffyEventMap>(event: E, handler: SquiffyEventHandler<E>): () => void;
|
|
@@ -44,16 +48,27 @@ interface SquiffyJsFunctionApi {
|
|
|
44
48
|
story: {
|
|
45
49
|
go: (section: string) => void;
|
|
46
50
|
seen: (sectionOrPassage: string) => boolean;
|
|
51
|
+
visits: (sectionOrPassage: string) => number;
|
|
47
52
|
};
|
|
48
53
|
}
|
|
49
54
|
export interface Story {
|
|
50
55
|
js: ((squiffy: SquiffyJsFunctionApi, get: (attribute: string) => any, set: (attribute: string, value: any) => void) => void)[];
|
|
51
56
|
start: string;
|
|
52
57
|
id?: string | null;
|
|
58
|
+
projectId?: string;
|
|
53
59
|
uiJsIndex?: number;
|
|
54
60
|
expressions?: boolean;
|
|
61
|
+
panels?: Panel[];
|
|
55
62
|
sections: Record<string, Section>;
|
|
56
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
|
+
}
|
|
57
72
|
export interface Section {
|
|
58
73
|
text?: string;
|
|
59
74
|
clear?: boolean;
|
|
@@ -65,7 +80,24 @@ export interface Section {
|
|
|
65
80
|
export interface Passage {
|
|
66
81
|
text?: string;
|
|
67
82
|
clear?: boolean;
|
|
83
|
+
repeat?: boolean;
|
|
68
84
|
attributes?: string[];
|
|
69
85
|
jsIndex?: number;
|
|
70
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
|
+
};
|
|
71
103
|
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "squiffy-runtime",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
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.
|
|
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": "
|
|
62
|
+
"gitHead": "90756c3ea9d782aa3f5f6cc41d9946a0a6cb8a45"
|
|
63
63
|
}
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
exports[`"Hello world" script should run 1`] = `
|
|
4
4
|
"
|
|
5
|
-
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Hello world</p></div></div></div></div>"
|
|
5
|
+
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Hello world</p></div></div></div></div>"
|
|
6
6
|
`;
|
|
7
7
|
|
|
8
8
|
exports[`Click a passage link 1`] = `
|
|
9
9
|
"
|
|
10
|
-
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
10
|
+
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
11
11
|
<p>This is <a href="http://textadventures.co.uk">a website link</a>.</p>
|
|
12
12
|
<p>This should be <a class="squiffy-link link-passage" data-passage="passage" role="link" tabindex="0">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>
|
|
13
13
|
<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>
|
|
@@ -19,21 +19,21 @@ exports[`Click a passage link 1`] = `
|
|
|
19
19
|
|
|
20
20
|
exports[`Click a passage link 2`] = `
|
|
21
21
|
"
|
|
22
|
-
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
22
|
+
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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
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>
|
|
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="{"_turncount":0,"_seen_sections":["Introduction"]}"><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="{"turns":0,"_turncount":0,"previous":null,"_passage":null,"_visits":{"Introduction":1},"_seen_sections":["Introduction"]}"><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&utm_medium=referral&utm_content=creditCopyText">Lars Blankers</a> on <a href="https://unsplash.com/photos/brown-potato-lot-B0s3Xndk6tw?utm_source=unsplash&utm_medium=referral&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
|
`;
|
|
33
33
|
|
|
34
34
|
exports[`Click a passage link and go back 1`] = `
|
|
35
35
|
"
|
|
36
|
-
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
36
|
+
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
37
37
|
<p>This is <a href="http://textadventures.co.uk">a website link</a>.</p>
|
|
38
38
|
<p>This should be <a class="squiffy-link link-passage" data-passage="passage" role="link">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>
|
|
39
39
|
<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>
|
|
@@ -45,7 +45,7 @@ exports[`Click a passage link and go back 1`] = `
|
|
|
45
45
|
|
|
46
46
|
exports[`Click a section link 1`] = `
|
|
47
47
|
"
|
|
48
|
-
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
48
|
+
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
49
49
|
<p>This is <a href="http://textadventures.co.uk">a website link</a>.</p>
|
|
50
50
|
<p>This should be <a class="squiffy-link link-passage" data-passage="passage" role="link" tabindex="0">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>
|
|
51
51
|
<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>
|
|
@@ -57,19 +57,19 @@ exports[`Click a section link 1`] = `
|
|
|
57
57
|
|
|
58
58
|
exports[`Click a section link 2`] = `
|
|
59
59
|
"
|
|
60
|
-
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
60
|
+
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
61
61
|
<p>This is <a href="http://textadventures.co.uk">a website link</a>.</p>
|
|
62
62
|
<p>This should be <a class="squiffy-link link-passage" data-passage="passage" role="link" tabindex="0">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>
|
|
63
63
|
<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>
|
|
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="{"_section":"Introduction","_seen_sections":["Introduction"],"_turncount":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="{"turns":0,"previous":null,"previous_section":null,"_section":"Introduction","_visits":{"Introduction":1},"_seen_sections":["Introduction"],"_turncount":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`] = `
|
|
71
71
|
"
|
|
72
|
-
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
72
|
+
<div><div class="squiffy-output-section" data-section="Introduction" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[Introduction]]"><h1>Squiffy Test</h1>
|
|
73
73
|
<p>This is <a href="http://textadventures.co.uk">a website link</a>.</p>
|
|
74
74
|
<p>This should be <a class="squiffy-link link-passage" data-passage="passage" role="link" tabindex="0">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>
|
|
75
75
|
<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>
|
|
@@ -81,60 +81,60 @@ 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="{"_section":null,"_seen_sections":null,"_turncount":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="{"_turncount":0,"_seen_sections":["_default"]}"><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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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="{"turns":0,"_turncount":0,"previous":null,"_passage":null,"_visits":{"_default":1},"_seen_sections":["_default"]}"><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="{"_section":null,"_seen_sections":null,"_turncount":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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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>"
|
|
90
90
|
`;
|
|
91
91
|
|
|
92
92
|
exports[`Delete section 1`] = `
|
|
93
93
|
"
|
|
94
|
-
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_seen_sections":null,"_turncount":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="{"_section":"_default","_seen_sections":["_default"],"_turncount":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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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="{"turns":0,"previous":null,"previous_section":null,"_section":"_default","_visits":{"_default":1},"_seen_sections":["_default"],"_turncount":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`] = `
|
|
98
98
|
"
|
|
99
|
-
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_seen_sections":null,"_turncount":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>"
|
|
99
|
+
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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>"
|
|
100
100
|
`;
|
|
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="{"_section":null,"_seen_sections":null,"_turncount":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="{"_turncount":0,"_seen_sections":["_default"],"test":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="{"_section":"_default","_seen_sections":["_default","b"],"test":456,"_turncount":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="{"_turncount":0,"_seen_sections":["_default","b","continue"],"test":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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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="{"turns":0,"_turncount":0,"previous":null,"_passage":null,"_visits":{"_default":1},"_seen_sections":["_default"],"test":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="{"turns":1,"previous":"_default","previous_section":null,"_passage":"b","_section":"_default","_visits":{"_default":1,"b":1},"_seen_sections":["_default","b"],"test":456,"_turncount":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="{"turns":2,"_turncount":0,"previous":"b","_passage":null,"_visits":{"_default":1,"b":1,"continue":1},"_seen_sections":["_default","b","continue"],"test":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="{"_section":null,"_seen_sections":null,"_turncount":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="{"_turncount":0,"_seen_sections":["_default"],"test":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="{"_section":"_default","_seen_sections":["_default","b"],"test":456,"_turncount":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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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="{"turns":0,"_turncount":0,"previous":null,"_passage":null,"_visits":{"_default":1},"_seen_sections":["_default"],"test":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="{"turns":1,"previous":"_default","previous_section":null,"_passage":"b","_section":"_default","_visits":{"_default":1,"b":1},"_seen_sections":["_default","b"],"test":456,"_turncount":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`] = `
|
|
113
113
|
"
|
|
114
|
-
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Hello world</p></div></div></div></div>"
|
|
114
|
+
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Hello world</p></div></div></div></div>"
|
|
115
115
|
`;
|
|
116
116
|
|
|
117
117
|
exports[`Update default section output 2`] = `
|
|
118
118
|
"
|
|
119
|
-
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Updated content</p></div></div></div></div>"
|
|
119
|
+
<div><div class="squiffy-output-section" data-section="_default" data-undo="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":null}"><div class="squiffy-output-block"><div data-source="[[_default]]"><p>Updated content</p></div></div></div></div>"
|
|
120
120
|
`;
|
|
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="{"_section":null,"_seen_sections":null,"_turncount":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="{"_turncount":0,"_seen_sections":["_default"]}"><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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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="{"turns":0,"_turncount":0,"previous":null,"_passage":null,"_visits":{"_default":1},"_seen_sections":["_default"]}"><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="{"_section":null,"_seen_sections":null,"_turncount":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="{"_turncount":0,"_seen_sections":["_default"]}"><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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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="{"turns":0,"_turncount":0,"previous":null,"_passage":null,"_visits":{"_default":1},"_seen_sections":["_default"]}"><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="{"_section":null,"_seen_sections":null,"_turncount":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="{"_turncount":0,"_seen_sections":["_default"]}"><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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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="{"turns":0,"_turncount":0,"previous":null,"_passage":null,"_visits":{"_default":1},"_seen_sections":["_default"]}"><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="{"_section":null,"_seen_sections":null,"_turncount":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="{"_turncount":0,"_seen_sections":["_default"]}"><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="{"_section":null,"_visits":null,"_seen_sections":null,"_turncount":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="{"turns":0,"_turncount":0,"previous":null,"_passage":null,"_visits":{"_default":1},"_seen_sections":["_default"]}"><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
|
+
});
|