squiffy-runtime 7.0.0-beta.6 → 7.0.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +11 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-host.d.ts +2 -0
- package/dist/preview-host.js +10 -0
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +6 -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 +14 -3
- package/dist/squiffy.preview.html +680 -82
- package/dist/squiffy.runtime.css +531 -1
- package/dist/squiffy.runtime.d.ts +5 -1
- package/dist/squiffy.runtime.global.js +117 -96
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +5728 -4520
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +4 -1
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +16 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +54 -0
- package/dist/uiLinks.test.d.ts +1 -0
- package/dist/utils.d.ts +1 -0
- package/dist/utils.test.d.ts +1 -0
- package/package.json +3 -3
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +10 -10
- 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 +18 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/panels.test.ts +24 -0
- package/src/paths.test.ts +66 -0
- package/src/paths.ts +155 -0
- package/src/playerSettings.ts +128 -0
- package/src/plugins/animate.ts +16 -3
- package/src/plugins/live.ts +10 -4
- package/src/plugins/plugins.test.ts +51 -6
- package/src/preview-host.test.ts +22 -0
- package/src/preview-host.ts +15 -0
- package/src/preview-protocol.ts +9 -1
- package/src/preview.test.ts +114 -3
- package/src/preview.ts +97 -3
- 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 +14 -3
- package/src/squiffy.runtime.css +531 -1
- package/src/squiffy.runtime.test.ts +1130 -17
- package/src/squiffy.runtime.ts +600 -38
- package/src/state.ts +45 -10
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +484 -35
- package/src/transitions.test.ts +302 -0
- package/src/types.ts +79 -0
- package/src/uiLinks.test.ts +156 -0
- package/src/updater.ts +26 -8
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +44 -14
- package/dist/infixExpressions.d.ts +0 -6
- package/src/infixExpressions.test.ts +0 -379
- package/src/infixExpressions.ts +0 -488
- /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));
|
package/src/squiffy.page.css
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
{
|
|
5
5
|
/* The width of the story text */
|
|
6
6
|
--squiffy-max-width: 42rem;
|
|
7
|
-
/* The story's font. Charter,
|
|
8
|
-
--squiffy-font-family: Charter, "Bitstream Charter",
|
|
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
9
|
/* The size of the story text. The header's buttons are smaller, and don't change with it */
|
|
10
10
|
--squiffy-font-size: 1.125rem;
|
|
11
11
|
/* The space between lines of text */
|
|
@@ -90,6 +90,14 @@ button#squiffy-theme[data-next="light"] .squiffy-icon-dark
|
|
|
90
90
|
display: none;
|
|
91
91
|
}
|
|
92
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
|
+
|
|
93
101
|
/* With a side panel (squiffy.json's "panels"), the page is wider, with the panel to the right of
|
|
94
102
|
the story. It stays in view as the story scrolls, under the header. On a narrower screen, it's a
|
|
95
103
|
button in the header instead. */
|
|
@@ -109,7 +117,10 @@ button#squiffy-theme[data-next="light"] .squiffy-icon-dark
|
|
|
109
117
|
grid-column: 1 / -1;
|
|
110
118
|
}
|
|
111
119
|
|
|
112
|
-
|
|
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
|
|
113
124
|
{
|
|
114
125
|
position: sticky;
|
|
115
126
|
top: 3.5rem;
|