squiffy-runtime 7.0.0-beta.1 → 7.0.0-beta.11
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/assets.d.ts +1 -0
- package/dist/classes.test.d.ts +1 -0
- package/dist/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +12 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/helpers.test.d.ts +1 -0
- package/dist/numbers.d.ts +3 -0
- package/dist/panels.d.ts +14 -0
- package/dist/panels.test.d.ts +1 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.test.d.ts +1 -0
- package/dist/persistent.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-host.d.ts +7 -3
- package/dist/preview-host.js +47 -5
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +16 -0
- package/dist/preview.d.ts +1 -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/scripts.test.d.ts +1 -0
- package/dist/settings.test.d.ts +1 -0
- package/dist/settingsDialog.d.ts +17 -0
- package/dist/skill.test.d.ts +1 -0
- package/dist/sound.d.ts +35 -0
- package/dist/sound.test.d.ts +1 -0
- package/dist/squiffy.page.css +155 -0
- package/dist/squiffy.preview.html +1469 -136
- package/dist/squiffy.runtime.css +1123 -20
- package/dist/squiffy.runtime.d.ts +13 -1
- package/dist/squiffy.runtime.global.js +144 -79
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +6771 -4424
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +17 -0
- package/dist/storyLists.d.ts +4 -0
- package/dist/storyLists.test.d.ts +1 -0
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +27 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +125 -0
- package/dist/types.plugins.d.ts +1 -0
- package/dist/uiLinks.test.d.ts +1 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.test.d.ts +1 -0
- package/package.json +11 -6
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +22 -22
- package/src/animation.test.ts +44 -0
- package/src/animation.ts +9 -5
- package/src/assets.test.ts +1 -3
- package/src/assets.ts +1 -1
- package/src/classes.test.ts +201 -0
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +19 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/helpers.test.ts +34 -0
- package/src/numbers.ts +21 -0
- package/src/panels.test.ts +225 -0
- package/src/panels.ts +187 -0
- package/src/paths.test.ts +66 -0
- package/src/paths.ts +155 -0
- package/src/persistent.test.ts +155 -0
- package/src/playerSettings.ts +128 -0
- package/src/pluginManager.ts +1 -0
- package/src/plugins/animate.ts +28 -4
- package/src/plugins/live.ts +18 -5
- package/src/plugins/plugins.test.ts +93 -6
- package/src/plugins/replaceLabel.ts +7 -3
- package/src/preview-host.test.ts +97 -1
- package/src/preview-host.ts +71 -10
- package/src/preview-protocol.ts +25 -3
- package/src/preview.main.ts +1 -0
- package/src/preview.test.ts +203 -4
- package/src/preview.ts +178 -12
- 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/scripts.test.ts +130 -0
- package/src/settings.test.ts +233 -0
- package/src/settingsDialog.ts +160 -0
- package/src/skill.test.ts +67 -0
- package/src/sound.test.ts +273 -0
- package/src/sound.ts +207 -0
- package/src/squiffy.page.css +155 -0
- package/src/squiffy.runtime.css +1123 -20
- package/src/squiffy.runtime.test.ts +2806 -44
- package/src/squiffy.runtime.ts +1111 -64
- package/src/state.ts +172 -11
- package/src/storyLists.test.ts +134 -0
- package/src/storyLists.ts +57 -0
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +739 -58
- package/src/transitions.test.ts +343 -0
- package/src/types.plugins.ts +3 -0
- package/src/types.ts +209 -1
- package/src/uiLinks.test.ts +156 -0
- package/src/updater.ts +26 -8
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +46 -14
- package/vite.config.ts +3 -4
- package/vite.preview.config.ts +16 -5
- package/dist/infixExpressions.d.ts +0 -2
- package/src/infixExpressions.test.ts +0 -187
- package/src/infixExpressions.ts +0 -323
- /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// Music and sound effects (#444), with a fake Audio that records what's played
|
|
2
|
+
import { expect, test, beforeEach, afterEach, vi } from "vitest";
|
|
3
|
+
import globalJsdom from "global-jsdom";
|
|
4
|
+
import { init } from "./squiffy.runtime.js";
|
|
5
|
+
import { compile as squiffyCompile } from "squiffy-compiler";
|
|
6
|
+
|
|
7
|
+
class FakeAudio {
|
|
8
|
+
static all: FakeAudio[] = [];
|
|
9
|
+
// Whether play() is refused, as a browser does before the player has clicked anything
|
|
10
|
+
static refuse = false;
|
|
11
|
+
src: string;
|
|
12
|
+
loop = false;
|
|
13
|
+
muted = false;
|
|
14
|
+
volume = 1;
|
|
15
|
+
preload = "";
|
|
16
|
+
paused = true;
|
|
17
|
+
listeners: Record<string, (() => void)[]> = {};
|
|
18
|
+
|
|
19
|
+
constructor(src = "") {
|
|
20
|
+
this.src = src;
|
|
21
|
+
FakeAudio.all.push(this);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
play() {
|
|
25
|
+
if (FakeAudio.refuse) {
|
|
26
|
+
const error = new Error("Not allowed");
|
|
27
|
+
error.name = "NotAllowedError";
|
|
28
|
+
return Promise.reject(error);
|
|
29
|
+
}
|
|
30
|
+
this.paused = false;
|
|
31
|
+
return Promise.resolve();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
pause() {
|
|
35
|
+
this.paused = true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
addEventListener(name: string, listener: () => void) {
|
|
39
|
+
(this.listeners[name] ??= []).push(listener);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
end() {
|
|
43
|
+
this.paused = true;
|
|
44
|
+
this.listeners.ended?.forEach(l => l());
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// What's playing: music loops, sounds don't
|
|
49
|
+
const playing = () => FakeAudio.all.filter(a => !a.paused);
|
|
50
|
+
const music = () => playing().filter(a => a.loop).map(a => a.src);
|
|
51
|
+
const sounds = () => playing().filter(a => !a.loop).map(a => a.src);
|
|
52
|
+
|
|
53
|
+
let cleanup: () => void;
|
|
54
|
+
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
cleanup = globalJsdom("<!DOCTYPE html><html><body><div id=\"squiffy\"></div></body></html>");
|
|
57
|
+
FakeAudio.all = [];
|
|
58
|
+
FakeAudio.refuse = false;
|
|
59
|
+
vi.stubGlobal("Audio", FakeAudio);
|
|
60
|
+
vi.useFakeTimers();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
afterEach(() => {
|
|
64
|
+
vi.useRealTimers();
|
|
65
|
+
vi.unstubAllGlobals();
|
|
66
|
+
localStorage.clear();
|
|
67
|
+
cleanup();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const start = async (script: string, options: { persist?: boolean; resolveAssetUrl?: (path: string) => string } = {}) => {
|
|
71
|
+
const result = await squiffyCompile({ script });
|
|
72
|
+
if (!result.success) throw new Error("Compile failed");
|
|
73
|
+
const story = {
|
|
74
|
+
...result.output.story,
|
|
75
|
+
js: result.output.js.map(lines => new Function("squiffy", "get", "set", lines.join("\n"))) as any,
|
|
76
|
+
};
|
|
77
|
+
const element = document.getElementById("squiffy")!;
|
|
78
|
+
const api = await init({ element, story, persist: options.persist ?? false, storyId: "test", ...options });
|
|
79
|
+
await api.begin();
|
|
80
|
+
return { api, element };
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const click = async (element: HTMLElement, text: string) => {
|
|
84
|
+
const link = [...element.querySelectorAll<HTMLElement>("a.squiffy-link")]
|
|
85
|
+
.find(l => l.textContent == text && !l.classList.contains("disabled"));
|
|
86
|
+
if (!link) throw new Error(`No link "${text}"`);
|
|
87
|
+
link.click();
|
|
88
|
+
await vi.runAllTimersAsync();
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const story = `
|
|
92
|
+
[[forest]]:
|
|
93
|
+
@music forest.mp3
|
|
94
|
+
The forest. [[Go to the cave]](cave), [[stay in the forest]](forest2)
|
|
95
|
+
|
|
96
|
+
[[forest2]]:
|
|
97
|
+
@music forest.mp3
|
|
98
|
+
Still the forest. [[Go to the cave]](cave)
|
|
99
|
+
|
|
100
|
+
[[cave]]:
|
|
101
|
+
@music cave.mp3
|
|
102
|
+
@sound drip.mp3
|
|
103
|
+
The cave. [[Leave]](outside)
|
|
104
|
+
|
|
105
|
+
[[outside]]:
|
|
106
|
+
@music stop
|
|
107
|
+
Outside.
|
|
108
|
+
`;
|
|
109
|
+
|
|
110
|
+
test("@music plays music, which carries on in a section with the same music", async () => {
|
|
111
|
+
const { element } = await start(story);
|
|
112
|
+
expect(music()).toEqual(["forest.mp3"]);
|
|
113
|
+
const forest = playing()[0];
|
|
114
|
+
|
|
115
|
+
await click(element, "stay in the forest");
|
|
116
|
+
expect(music()).toEqual(["forest.mp3"]);
|
|
117
|
+
// The same one, not started again
|
|
118
|
+
expect(playing()[0]).toBe(forest);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("@music changes the music, fading out the old one, and @music stop stops it", async () => {
|
|
122
|
+
const { element } = await start(story);
|
|
123
|
+
const forest = playing()[0];
|
|
124
|
+
|
|
125
|
+
await click(element, "Go to the cave");
|
|
126
|
+
expect(forest.paused).toBe(true);
|
|
127
|
+
expect(forest.volume).toBe(0);
|
|
128
|
+
expect(music()).toEqual(["cave.mp3"]);
|
|
129
|
+
expect(playing().find(a => a.loop)!.volume).toBe(1);
|
|
130
|
+
|
|
131
|
+
await click(element, "Leave");
|
|
132
|
+
expect(music()).toEqual([]);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
test("@sound plays a sound once", async () => {
|
|
136
|
+
const { element } = await start(story);
|
|
137
|
+
await click(element, "Go to the cave");
|
|
138
|
+
expect(sounds()).toEqual(["drip.mp3"]);
|
|
139
|
+
const drip = playing().find(a => a.src == "drip.mp3")!;
|
|
140
|
+
expect(drip.loop).toBe(false);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("The music is the _music attribute, so going back puts the music back", async () => {
|
|
144
|
+
const { api, element } = await start(story);
|
|
145
|
+
await click(element, "Go to the cave");
|
|
146
|
+
expect(api.get("_music")).toBe("cave.mp3");
|
|
147
|
+
|
|
148
|
+
api.goBack();
|
|
149
|
+
await vi.runAllTimersAsync();
|
|
150
|
+
expect(api.get("_music")).toBe("forest.mp3");
|
|
151
|
+
expect(music()).toEqual(["forest.mp3"]);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("Loading a saved game plays its music", async () => {
|
|
155
|
+
const { api, element } = await start(story);
|
|
156
|
+
const save = api.getSave();
|
|
157
|
+
await click(element, "Go to the cave");
|
|
158
|
+
await click(element, "Leave");
|
|
159
|
+
expect(music()).toEqual([]);
|
|
160
|
+
|
|
161
|
+
await api.loadSave(save);
|
|
162
|
+
await vi.runAllTimersAsync();
|
|
163
|
+
expect(music()).toEqual(["forest.mp3"]);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("A story carried on from where it was left plays its music", async () => {
|
|
167
|
+
const first = await start(story, { persist: true });
|
|
168
|
+
await click(first.element, "Go to the cave");
|
|
169
|
+
first.api.dispose();
|
|
170
|
+
first.element.innerHTML = "";
|
|
171
|
+
FakeAudio.all = [];
|
|
172
|
+
|
|
173
|
+
await start(story, { persist: true });
|
|
174
|
+
expect(music()).toEqual(["cave.mp3"]);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
test("{{music}}, {{stopmusic}} and {{sound}} in the text", async () => {
|
|
178
|
+
const { element } = await start(`
|
|
179
|
+
Start. {{music "theme.mp3" fade=0}}{{sound "bell.mp3" volume=0.5}} [[Next]](next)
|
|
180
|
+
|
|
181
|
+
[[next]]:
|
|
182
|
+
{{stopmusic fade=0}}Quiet.
|
|
183
|
+
`);
|
|
184
|
+
expect(music()).toEqual(["theme.mp3"]);
|
|
185
|
+
expect(playing().find(a => a.src == "theme.mp3")!.volume).toBe(1);
|
|
186
|
+
expect(playing().find(a => a.src == "bell.mp3")!.volume).toBe(0.5);
|
|
187
|
+
|
|
188
|
+
await click(element, "Next");
|
|
189
|
+
expect(music()).toEqual([]);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test("@sound stop and {{stopsounds}} stop the sounds playing once, and the music carries on", async () => {
|
|
193
|
+
const { element } = await start(`
|
|
194
|
+
@music theme.mp3
|
|
195
|
+
{{sound "bell.mp3"}}{{sound "rain.mp3"}}Start. [[Next]](next)
|
|
196
|
+
|
|
197
|
+
[[next]]:
|
|
198
|
+
@sound stop
|
|
199
|
+
@sound door.mp3
|
|
200
|
+
Next. [[Again]](again)
|
|
201
|
+
|
|
202
|
+
[[again]]:
|
|
203
|
+
{{stopsounds}}Quiet.
|
|
204
|
+
`);
|
|
205
|
+
expect(sounds()).toEqual(["bell.mp3", "rain.mp3"]);
|
|
206
|
+
|
|
207
|
+
await click(element, "Next");
|
|
208
|
+
// Stopped before the section's own sound plays
|
|
209
|
+
expect(sounds()).toEqual(["door.mp3"]);
|
|
210
|
+
expect(music()).toEqual(["theme.mp3"]);
|
|
211
|
+
|
|
212
|
+
await click(element, "Again");
|
|
213
|
+
expect(sounds()).toEqual([]);
|
|
214
|
+
expect(music()).toEqual(["theme.mp3"]);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("{{music}} and {{sound}} on their own are still attributes", async () => {
|
|
218
|
+
const { element } = await start(`
|
|
219
|
+
@set music = jazz
|
|
220
|
+
@set sound = loud
|
|
221
|
+
I like {{music}}, {{sound}}.
|
|
222
|
+
`);
|
|
223
|
+
expect(element.textContent).toContain("I like jazz, loud.");
|
|
224
|
+
expect(FakeAudio.all.filter(a => a.src == "jazz" || a.src == "loud")).toEqual([]);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
test("Music refused before the player has done anything starts when they click", async () => {
|
|
228
|
+
FakeAudio.refuse = true;
|
|
229
|
+
await start(story);
|
|
230
|
+
await vi.runAllTimersAsync();
|
|
231
|
+
expect(music()).toEqual([]);
|
|
232
|
+
|
|
233
|
+
FakeAudio.refuse = false;
|
|
234
|
+
document.dispatchEvent(new window.Event("pointerdown"));
|
|
235
|
+
await vi.runAllTimersAsync();
|
|
236
|
+
expect(music()).toEqual(["forest.mp3"]);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
test("Muting mutes what's playing and what plays next, and is remembered for the story", async () => {
|
|
240
|
+
const { api, element } = await start(story, { persist: true });
|
|
241
|
+
expect(api.isMuted()).toBe(false);
|
|
242
|
+
api.setMuted(true);
|
|
243
|
+
expect(playing()[0].muted).toBe(true);
|
|
244
|
+
|
|
245
|
+
await click(element, "Go to the cave");
|
|
246
|
+
expect(playing().every(a => a.muted)).toBe(true);
|
|
247
|
+
expect(localStorage.getItem("squiffy-muted:test")).toBe("true");
|
|
248
|
+
|
|
249
|
+
// Restarting doesn't forget it
|
|
250
|
+
api.restart();
|
|
251
|
+
expect(api.isMuted()).toBe(true);
|
|
252
|
+
expect(localStorage.getItem("squiffy-muted:test")).toBe("true");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
test("Files are preloaded, through resolveAssetUrl", async () => {
|
|
256
|
+
const { api } = await start(story, { resolveAssetUrl: path => "blob:" + path });
|
|
257
|
+
const preloaded = FakeAudio.all.filter(a => a.preload == "auto").map(a => a.src).sort();
|
|
258
|
+
expect(preloaded).toEqual(["blob:cave.mp3", "blob:drip.mp3", "blob:forest.mp3"]);
|
|
259
|
+
expect(music()).toEqual(["blob:forest.mp3"]);
|
|
260
|
+
expect(api.usesSound()).toBe(true);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
test("A story with no sound doesn't use sound", async () => {
|
|
264
|
+
const { api } = await start("Hello.");
|
|
265
|
+
expect(api.usesSound()).toBe(false);
|
|
266
|
+
expect(FakeAudio.all).toEqual([]);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test("dispose() stops everything", async () => {
|
|
270
|
+
const { api } = await start(story);
|
|
271
|
+
api.dispose();
|
|
272
|
+
expect(playing()).toEqual([]);
|
|
273
|
+
});
|
package/src/sound.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// Music and sound effects (#444). The music playing is the _music attribute, so going back,
|
|
2
|
+
// loading a saved game and restarting all put it back as it was: the runtime calls sync() after
|
|
3
|
+
// each of those, and after anything that sets it. Sound effects play once, and aren't kept.
|
|
4
|
+
|
|
5
|
+
export type AudioUrlResolver = (path: string) => string;
|
|
6
|
+
|
|
7
|
+
export interface SoundOptions {
|
|
8
|
+
// Turns a file in the story's folder into the URL to play it from
|
|
9
|
+
resolveUrl: AudioUrlResolver;
|
|
10
|
+
// The music that should be playing: the _music attribute
|
|
11
|
+
getMusic: () => string | null;
|
|
12
|
+
// Where mute is remembered, if anywhere
|
|
13
|
+
mutedKey?: string;
|
|
14
|
+
// Something started playing, or would have if the player hadn't muted it
|
|
15
|
+
onPlay?: () => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// How long music takes to fade out when it's changed or stopped, and to fade in, unless
|
|
19
|
+
// fade= says otherwise
|
|
20
|
+
export const DEFAULT_FADE = 1;
|
|
21
|
+
|
|
22
|
+
const FADE_STEP_MS = 50;
|
|
23
|
+
|
|
24
|
+
interface Track {
|
|
25
|
+
file: string;
|
|
26
|
+
audio: HTMLAudioElement;
|
|
27
|
+
fadeTimer?: ReturnType<typeof setInterval>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class Sound {
|
|
31
|
+
private music: Track | null = null;
|
|
32
|
+
private effects = new Set<HTMLAudioElement>();
|
|
33
|
+
private preloaded = new Map<string, HTMLAudioElement>();
|
|
34
|
+
private muted = false;
|
|
35
|
+
// Browsers don't play sound until the player has clicked or pressed a key on the page, so
|
|
36
|
+
// music that was refused is started again then
|
|
37
|
+
private waitingForGesture = false;
|
|
38
|
+
private disposed = false;
|
|
39
|
+
|
|
40
|
+
constructor(private options: SoundOptions) {
|
|
41
|
+
this.muted = this.readMuted();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Starts loading files before they're needed, so they play straight away
|
|
45
|
+
preload(files: string[]) {
|
|
46
|
+
if (typeof Audio === "undefined") return;
|
|
47
|
+
for (const file of files) {
|
|
48
|
+
if (this.preloaded.has(file)) continue;
|
|
49
|
+
const audio = new Audio();
|
|
50
|
+
audio.preload = "auto";
|
|
51
|
+
audio.src = this.options.resolveUrl(file);
|
|
52
|
+
this.preloaded.set(file, audio);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Makes the music playing match the _music attribute. Music that's already playing carries on.
|
|
57
|
+
sync(fade = DEFAULT_FADE) {
|
|
58
|
+
if (this.disposed) return;
|
|
59
|
+
const file = this.options.getMusic() || null;
|
|
60
|
+
if (file === (this.music?.file ?? null)) return;
|
|
61
|
+
this.stopMusic(fade);
|
|
62
|
+
if (file) this.startMusic(file, fade);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Plays a sound once
|
|
66
|
+
play(file: string, volume = 1) {
|
|
67
|
+
if (this.disposed || typeof Audio === "undefined") return;
|
|
68
|
+
const audio = this.newAudio(file);
|
|
69
|
+
audio.volume = clampVolume(volume);
|
|
70
|
+
this.effects.add(audio);
|
|
71
|
+
const done = () => this.effects.delete(audio);
|
|
72
|
+
audio.addEventListener("ended", done);
|
|
73
|
+
audio.addEventListener("error", done);
|
|
74
|
+
this.options.onPlay?.();
|
|
75
|
+
// A sound refused before the player has done anything is lost: it would be out of place later
|
|
76
|
+
audio.play()?.catch(done);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Stops the sounds playing once. Music carries on.
|
|
80
|
+
stopSounds() {
|
|
81
|
+
for (const audio of this.effects) audio.pause();
|
|
82
|
+
this.effects.clear();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
isMuted() {
|
|
86
|
+
return this.muted;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
setMuted(muted: boolean) {
|
|
90
|
+
this.muted = muted;
|
|
91
|
+
if (this.music) this.music.audio.muted = muted;
|
|
92
|
+
for (const audio of this.effects) audio.muted = muted;
|
|
93
|
+
this.writeMuted();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Stops everything, for good: the story is being replaced
|
|
97
|
+
dispose() {
|
|
98
|
+
this.disposed = true;
|
|
99
|
+
this.stopMusic(0);
|
|
100
|
+
this.stopSounds();
|
|
101
|
+
this.removeGestureListeners();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private newAudio(file: string) {
|
|
105
|
+
const audio = new Audio(this.options.resolveUrl(file));
|
|
106
|
+
audio.muted = this.muted;
|
|
107
|
+
return audio;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private startMusic(file: string, fade: number) {
|
|
111
|
+
if (typeof Audio === "undefined") return;
|
|
112
|
+
const audio = this.newAudio(file);
|
|
113
|
+
audio.loop = true;
|
|
114
|
+
const track: Track = { file, audio };
|
|
115
|
+
this.music = track;
|
|
116
|
+
this.options.onPlay?.();
|
|
117
|
+
audio.volume = fade > 0 ? 0 : 1;
|
|
118
|
+
this.playTrack(track, fade);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
private playTrack(track: Track, fade: number) {
|
|
122
|
+
const played = track.audio.play();
|
|
123
|
+
if (fade > 0) this.fade(track, 1, fade);
|
|
124
|
+
played?.catch((error: unknown) => {
|
|
125
|
+
if (track === this.music && (error as { name?: string })?.name == "NotAllowedError") {
|
|
126
|
+
this.waitForGesture();
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private stopMusic(fade: number) {
|
|
132
|
+
const track = this.music;
|
|
133
|
+
if (!track) return;
|
|
134
|
+
this.music = null;
|
|
135
|
+
if (fade > 0 && !track.audio.paused) {
|
|
136
|
+
this.fade(track, 0, fade, () => track.audio.pause());
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
clearInterval(track.fadeTimer);
|
|
140
|
+
track.audio.pause();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Changes a track's volume to `to` over `seconds`
|
|
145
|
+
private fade(track: Track, to: number, seconds: number, then?: () => void) {
|
|
146
|
+
clearInterval(track.fadeTimer);
|
|
147
|
+
const from = track.audio.volume;
|
|
148
|
+
const steps = Math.max(1, Math.round(seconds * 1000 / FADE_STEP_MS));
|
|
149
|
+
let step = 0;
|
|
150
|
+
track.fadeTimer = setInterval(() => {
|
|
151
|
+
step++;
|
|
152
|
+
track.audio.volume = clampVolume(from + (to - from) * step / steps);
|
|
153
|
+
if (step >= steps) {
|
|
154
|
+
clearInterval(track.fadeTimer);
|
|
155
|
+
track.fadeTimer = undefined;
|
|
156
|
+
then?.();
|
|
157
|
+
}
|
|
158
|
+
}, FADE_STEP_MS);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
private readonly onGesture = () => {
|
|
162
|
+
this.removeGestureListeners();
|
|
163
|
+
const track = this.music;
|
|
164
|
+
if (track && track.audio.paused) {
|
|
165
|
+
track.audio.volume = 1;
|
|
166
|
+
this.playTrack(track, 0);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
private waitForGesture() {
|
|
171
|
+
if (this.waitingForGesture || typeof document === "undefined") return;
|
|
172
|
+
this.waitingForGesture = true;
|
|
173
|
+
// Capturing, so it's before a link click that might change the music
|
|
174
|
+
document.addEventListener("pointerdown", this.onGesture, true);
|
|
175
|
+
document.addEventListener("keydown", this.onGesture, true);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private removeGestureListeners() {
|
|
179
|
+
if (!this.waitingForGesture) return;
|
|
180
|
+
this.waitingForGesture = false;
|
|
181
|
+
document.removeEventListener("pointerdown", this.onGesture, true);
|
|
182
|
+
document.removeEventListener("keydown", this.onGesture, true);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private readMuted() {
|
|
186
|
+
if (!this.options.mutedKey) return false;
|
|
187
|
+
try {
|
|
188
|
+
return localStorage.getItem(this.options.mutedKey) == "true";
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private writeMuted() {
|
|
196
|
+
if (!this.options.mutedKey) return;
|
|
197
|
+
try {
|
|
198
|
+
if (this.muted) localStorage.setItem(this.options.mutedKey, "true");
|
|
199
|
+
else localStorage.removeItem(this.options.mutedKey);
|
|
200
|
+
}
|
|
201
|
+
catch {
|
|
202
|
+
// It just isn't remembered
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const clampVolume = (volume: number) => Math.min(1, Math.max(0, Number.isFinite(volume) ? volume : 1));
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/* Page layout styles for an exported story. See https://squiffystory.com/styling/ for how to customise these. */
|
|
2
|
+
|
|
3
|
+
:root
|
|
4
|
+
{
|
|
5
|
+
/* The width of the story text */
|
|
6
|
+
--squiffy-max-width: 42rem;
|
|
7
|
+
/* The story's font. Charter, or Georgia if the reader doesn't have it */
|
|
8
|
+
--squiffy-font-family: Charter, "Bitstream Charter", Georgia, serif;
|
|
9
|
+
/* The size of the story text. The header's buttons are smaller, and don't change with it */
|
|
10
|
+
--squiffy-font-size: 1.125rem;
|
|
11
|
+
/* The space between lines of text */
|
|
12
|
+
--squiffy-line-height: 1.6;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/* Follow the reader's light or dark system setting, or what they chose with the theme button.
|
|
16
|
+
To keep a story light (or dark) whatever the setting, set ":root { color-scheme: light; }"
|
|
17
|
+
(or dark). These rules use :where() so that a :root rule like that overrides them, and the
|
|
18
|
+
theme button is hidden. */
|
|
19
|
+
:where(:root)
|
|
20
|
+
{
|
|
21
|
+
color-scheme: light dark;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
:where(:root[data-squiffy-theme="light"])
|
|
25
|
+
{
|
|
26
|
+
color-scheme: light;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
:where(:root[data-squiffy-theme="dark"])
|
|
30
|
+
{
|
|
31
|
+
color-scheme: dark;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
body
|
|
35
|
+
{
|
|
36
|
+
margin: 0;
|
|
37
|
+
background-color: var(--squiffy-background);
|
|
38
|
+
color: var(--squiffy-text);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/* Wraps the header and the story: sets the page width and font */
|
|
42
|
+
div#squiffy-container
|
|
43
|
+
{
|
|
44
|
+
max-width: var(--squiffy-max-width);
|
|
45
|
+
margin-left: auto;
|
|
46
|
+
margin-right: auto;
|
|
47
|
+
padding: 0 1.25rem 3rem;
|
|
48
|
+
font-family: var(--squiffy-font-family);
|
|
49
|
+
line-height: var(--squiffy-line-height);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/* The bar above the story containing the Back, Restart and light/dark buttons. It stays at the
|
|
53
|
+
top of the window as the story scrolls under it. */
|
|
54
|
+
div#squiffy-header
|
|
55
|
+
{
|
|
56
|
+
position: sticky;
|
|
57
|
+
top: 0;
|
|
58
|
+
z-index: 1;
|
|
59
|
+
padding: 0.5rem 0;
|
|
60
|
+
background-color: var(--squiffy-background);
|
|
61
|
+
font-size: 0.875rem;
|
|
62
|
+
text-align: right;
|
|
63
|
+
white-space: nowrap;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/* A short fade under the header, so the story scrolls under it rather than being cut off */
|
|
67
|
+
div#squiffy-header::after
|
|
68
|
+
{
|
|
69
|
+
content: "";
|
|
70
|
+
position: absolute;
|
|
71
|
+
left: 0;
|
|
72
|
+
right: 0;
|
|
73
|
+
top: 100%;
|
|
74
|
+
height: 0.75rem;
|
|
75
|
+
background: linear-gradient(var(--squiffy-background), transparent);
|
|
76
|
+
pointer-events: none;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/* Space between the Back, Restart and light/dark buttons */
|
|
80
|
+
button.squiffy-header-button + button.squiffy-header-button
|
|
81
|
+
{
|
|
82
|
+
margin-left: 0.25em;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/* The button that switches between light and dark mode. It shows a moon in light mode and a sun
|
|
86
|
+
in dark mode. */
|
|
87
|
+
button#squiffy-theme:not([data-next="light"]) .squiffy-icon-light,
|
|
88
|
+
button#squiffy-theme[data-next="light"] .squiffy-icon-dark
|
|
89
|
+
{
|
|
90
|
+
display: none;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/* The mute button (#444), for a story with music or sounds. It shows a speaker, crossed out
|
|
94
|
+
when the story is muted. */
|
|
95
|
+
button#squiffy-mute.squiffy-muted .squiffy-icon-sound,
|
|
96
|
+
button#squiffy-mute:not(.squiffy-muted) .squiffy-icon-muted
|
|
97
|
+
{
|
|
98
|
+
display: none;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* With a side panel (squiffy.json's "panels"), the page is wider, with the panel to the right of
|
|
102
|
+
the story. It stays in view as the story scrolls, under the header. On a narrower screen, it's a
|
|
103
|
+
button in the header instead. */
|
|
104
|
+
@media (min-width: 64rem)
|
|
105
|
+
{
|
|
106
|
+
div#squiffy-container.squiffy-has-side
|
|
107
|
+
{
|
|
108
|
+
display: grid;
|
|
109
|
+
grid-template-columns: minmax(0, 1fr) var(--squiffy-side-width);
|
|
110
|
+
column-gap: 2.5rem;
|
|
111
|
+
align-items: start;
|
|
112
|
+
max-width: calc(var(--squiffy-max-width) + var(--squiffy-side-width) + 2.5rem);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
div#squiffy-container.squiffy-has-side > div#squiffy-header
|
|
116
|
+
{
|
|
117
|
+
grid-column: 1 / -1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/* :where() takes the container's id out of the selector's specificity, so a story's own
|
|
121
|
+
aside.squiffy-panels-side { ... } rule, which comes after this, changes the border and the
|
|
122
|
+
padding too */
|
|
123
|
+
:where(div#squiffy-container.squiffy-has-side) > aside.squiffy-panels-side
|
|
124
|
+
{
|
|
125
|
+
position: sticky;
|
|
126
|
+
top: 3.5rem;
|
|
127
|
+
padding-left: 1.25rem;
|
|
128
|
+
border-left: 1px solid var(--squiffy-border);
|
|
129
|
+
max-height: calc(100vh - 4.5rem);
|
|
130
|
+
overflow-y: auto;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/* The story text */
|
|
135
|
+
div#squiffy
|
|
136
|
+
{
|
|
137
|
+
font-size: var(--squiffy-font-size);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
div#squiffy :is(h1, h2, h3, h4, h5, h6)
|
|
141
|
+
{
|
|
142
|
+
line-height: 1.25;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
div#squiffy p
|
|
146
|
+
{
|
|
147
|
+
text-wrap: pretty;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/* Stop images overflowing the page */
|
|
151
|
+
div#squiffy img
|
|
152
|
+
{
|
|
153
|
+
max-width: 100%;
|
|
154
|
+
height: auto;
|
|
155
|
+
}
|