squiffy-runtime 7.0.0-beta.7 → 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/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +10 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-html.js +1 -1
- package/dist/settings.test.d.ts +1 -0
- package/dist/settingsDialog.d.ts +17 -0
- package/dist/squiffy.page.css +4 -1
- package/dist/squiffy.preview.html +673 -83
- package/dist/squiffy.runtime.css +531 -1
- package/dist/squiffy.runtime.d.ts +5 -1
- package/dist/squiffy.runtime.global.js +106 -85
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +4743 -4157
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +1 -0
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +8 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +43 -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/animation.test.ts +44 -0
- package/src/animation.ts +7 -3
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +17 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/panels.test.ts +24 -0
- package/src/playerSettings.ts +128 -0
- package/src/plugins/animate.ts +9 -0
- package/src/plugins/live.ts +3 -1
- package/src/plugins/plugins.test.ts +17 -0
- package/src/preview.test.ts +53 -3
- package/src/preview.ts +60 -1
- package/src/settings.test.ts +233 -0
- package/src/settingsDialog.ts +160 -0
- package/src/squiffy.page.css +4 -1
- package/src/squiffy.runtime.css +531 -1
- package/src/squiffy.runtime.test.ts +613 -16
- package/src/squiffy.runtime.ts +372 -19
- package/src/state.ts +16 -1
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +141 -6
- package/src/transitions.test.ts +302 -0
- package/src/types.ts +56 -0
- package/src/uiLinks.test.ts +156 -0
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +44 -14
- package/dist/infixExpressions.d.ts +0 -7
- package/src/infixExpressions.test.ts +0 -516
- package/src/infixExpressions.ts +0 -672
- /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
|
@@ -506,6 +506,23 @@ You clicked!
|
|
|
506
506
|
expect(animateSpan?.getAttribute("data-interval")).toBe("50");
|
|
507
507
|
});
|
|
508
508
|
|
|
509
|
+
test("Animate plugin: times can be expressions or written in seconds (#451)", async () => {
|
|
510
|
+
const script = `
|
|
511
|
+
@set n = 3
|
|
512
|
+
{{#animate "typewriter" trigger="link" start="1.5s" interval=n * 10 duration="200ms"}}[Click me]{{/animate}}
|
|
513
|
+
|
|
514
|
+
[Click me]:
|
|
515
|
+
You clicked!
|
|
516
|
+
`;
|
|
517
|
+
|
|
518
|
+
const { element } = await initScript(script);
|
|
519
|
+
|
|
520
|
+
const animateSpan = element.querySelector<HTMLElement>(".squiffy-animate");
|
|
521
|
+
expect(animateSpan?.getAttribute("data-start")).toBe("1500");
|
|
522
|
+
expect(animateSpan?.getAttribute("data-interval")).toBe("30");
|
|
523
|
+
expect(animateSpan?.getAttribute("data-duration")).toBe("200");
|
|
524
|
+
});
|
|
525
|
+
|
|
509
526
|
test("Animate plugin: trigger on link", async () => {
|
|
510
527
|
const script = `
|
|
511
528
|
{{#animate "typewriter" trigger="link"}}[Click me]{{/animate}}
|
package/src/preview.test.ts
CHANGED
|
@@ -3,7 +3,7 @@ import globalJsdom from "global-jsdom";
|
|
|
3
3
|
import { compile } from "squiffy-compiler";
|
|
4
4
|
import { createPreview } from "./preview.js";
|
|
5
5
|
import type { PreviewMessage } from "./preview-protocol.js";
|
|
6
|
-
import type { Panel, Story } from "./types.js";
|
|
6
|
+
import type { Panel, PlayerSetting, Story } from "./types.js";
|
|
7
7
|
|
|
8
8
|
beforeAll(() => {
|
|
9
9
|
vi.stubGlobal("crypto", { randomUUID: () => "00000000-0000-0000-0000-000000000000" });
|
|
@@ -31,8 +31,8 @@ afterEach(() => {
|
|
|
31
31
|
});
|
|
32
32
|
|
|
33
33
|
// The compiled story module, as a host would send it
|
|
34
|
-
const compileJs = async (script: string, panels?: Panel[]) => {
|
|
35
|
-
const result = await compile({ script, panels });
|
|
34
|
+
const compileJs = async (script: string, panels?: Panel[], settings?: PlayerSetting[]) => {
|
|
35
|
+
const result = await compile({ script, panels, settings });
|
|
36
36
|
if (!result.success) throw new Error("Compile failed");
|
|
37
37
|
return result.getJs();
|
|
38
38
|
};
|
|
@@ -289,6 +289,56 @@ body { background: url("blob:preview/1"), url(https://example.com/x.png); }`);
|
|
|
289
289
|
expect(document.querySelectorAll("dialog.squiffy-panel-dialog")).toHaveLength(0);
|
|
290
290
|
});
|
|
291
291
|
|
|
292
|
+
test("shows a Settings button for a story with settings, which opens the Settings dialog", async () => {
|
|
293
|
+
const { preview, element } = setUp();
|
|
294
|
+
const settings: PlayerSetting[] = [{ name: "warnings", label: "Content warnings", type: "toggle" }];
|
|
295
|
+
const panels: Panel[] = [{ passage: "status", label: "Status" }];
|
|
296
|
+
const panelScript = "[[]]:\n\n[status]:\nScore: {{score}}\n\n" + script;
|
|
297
|
+
await preview.receive({ type: "load", story: { js: await compileJs(panelScript, panels, settings) } });
|
|
298
|
+
const buttons = () => [...element.querySelectorAll<HTMLButtonElement>(".squiffy-preview-header button")];
|
|
299
|
+
expect(buttons().map(b => b.title || b.textContent)).toEqual(["Status", "Settings"]);
|
|
300
|
+
buttons()[1].click();
|
|
301
|
+
expect(document.querySelector("dialog.squiffy-settings label")!.textContent).toBe("Content warnings");
|
|
302
|
+
|
|
303
|
+
// Still after the panels' buttons when they change
|
|
304
|
+
await preview.receive({ type: "update", js: await compileJs(panelScript, [{ passage: "status", label: "Score" }], settings) });
|
|
305
|
+
expect(buttons().map(b => b.title || b.textContent)).toEqual(["Score", "Settings"]);
|
|
306
|
+
|
|
307
|
+
await preview.receive({ type: "update", js: await compileJs(panelScript, panels) });
|
|
308
|
+
expect(buttons().map(b => b.title || b.textContent)).toEqual(["Status"]);
|
|
309
|
+
|
|
310
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) } });
|
|
311
|
+
expect(document.querySelectorAll("dialog.squiffy-settings")).toHaveLength(0);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test("puts the story's classes on #squiffy, as a published story does", async () => {
|
|
315
|
+
const { preview, element } = setUp();
|
|
316
|
+
const settings: PlayerSetting[] = [{ name: "warnings", label: "Content warnings", type: "toggle" }];
|
|
317
|
+
const classScript = "[[start]]:\n@class dark\nIn the [[cellar]].\n\n[[cellar]]:\nDown here.\n";
|
|
318
|
+
await preview.receive({ type: "load", story: { js: await compileJs(classScript, undefined, settings) } });
|
|
319
|
+
expect(element.id).toBe("squiffy");
|
|
320
|
+
expect(element.classList.contains("dark")).toBe(true);
|
|
321
|
+
expect(document.querySelector("div#squiffy.dark")).toBe(element);
|
|
322
|
+
|
|
323
|
+
// A setting's class, when the player changes it
|
|
324
|
+
element.querySelector<HTMLButtonElement>(".squiffy-settings-button")!.click();
|
|
325
|
+
const toggle = document.querySelector<HTMLInputElement>("dialog.squiffy-settings input[type=checkbox]")!;
|
|
326
|
+
toggle.checked = true;
|
|
327
|
+
toggle.dispatchEvent(new window.Event("change"));
|
|
328
|
+
await Promise.resolve();
|
|
329
|
+
expect(element.classList.contains("setting-warnings")).toBe(true);
|
|
330
|
+
|
|
331
|
+
// A section's class goes when the player leaves it
|
|
332
|
+
element.querySelector<HTMLAnchorElement>("a.squiffy-link")!.click();
|
|
333
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
334
|
+
expect(element.classList.contains("dark")).toBe(false);
|
|
335
|
+
expect(element.classList.contains("setting-warnings")).toBe(true);
|
|
336
|
+
|
|
337
|
+
// And a new story starts with none
|
|
338
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) } });
|
|
339
|
+
expect([...element.classList]).toEqual([]);
|
|
340
|
+
});
|
|
341
|
+
|
|
292
342
|
test("reports errors", async () => {
|
|
293
343
|
const messages: PreviewMessage[] = [];
|
|
294
344
|
const preview = createPreview({
|
package/src/preview.ts
CHANGED
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
|
|
4
4
|
import { init } from "./squiffy.runtime.js";
|
|
5
5
|
import { createPanels, type Panels } from "./panels.js";
|
|
6
|
+
import { createSettingsDialog, SETTINGS_ICON, type SettingsDialog } from "./settingsDialog.js";
|
|
6
7
|
import type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory } from "./preview-protocol.js";
|
|
7
|
-
import type { Panel, SquiffyApi, Story } from "./types.js";
|
|
8
|
+
import type { Panel, PlayerSetting, SquiffyApi, Story } from "./types.js";
|
|
8
9
|
|
|
9
10
|
export interface PreviewOptions {
|
|
10
11
|
// Where to show the story
|
|
@@ -57,7 +58,16 @@ export function createPreview(options: PreviewOptions) {
|
|
|
57
58
|
let panels: Panels | undefined;
|
|
58
59
|
let panelsJson = "";
|
|
59
60
|
let panelButtons: HTMLElement | undefined;
|
|
61
|
+
// The Settings button (#452), after the panels' buttons, for a story with "settings"
|
|
62
|
+
let settingsButton: HTMLButtonElement | undefined;
|
|
63
|
+
let settingsDialog: SettingsDialog | undefined;
|
|
60
64
|
let panelSide: HTMLElement | undefined;
|
|
65
|
+
// The runtime puts the story's classes (@class, {{addclass}}, settings such as
|
|
66
|
+
// setting-textSize-Large) on the element it's given, which here is inside the header and side
|
|
67
|
+
// panels' layout. They're copied to the preview's own element, which is #squiffy as on a
|
|
68
|
+
// published page, so stylesheets with rules such as div#squiffy.dark work here too.
|
|
69
|
+
let classObserver: MutationObserver | undefined;
|
|
70
|
+
let mirroredClasses: string[] = [];
|
|
61
71
|
// The host's mute button (#444), which carries on to the next story loaded
|
|
62
72
|
let muted = false;
|
|
63
73
|
let usesSound = false;
|
|
@@ -159,10 +169,22 @@ export function createPreview(options: PreviewOptions) {
|
|
|
159
169
|
}
|
|
160
170
|
}
|
|
161
171
|
|
|
172
|
+
function mirrorClasses(from: HTMLElement | undefined) {
|
|
173
|
+
const classes = from ? [...from.classList].filter(c => c != "squiffy-preview-story") : [];
|
|
174
|
+
for (const c of mirroredClasses) {
|
|
175
|
+
if (!classes.includes(c)) options.element.classList.remove(c);
|
|
176
|
+
}
|
|
177
|
+
for (const c of classes) options.element.classList.add(c);
|
|
178
|
+
mirroredClasses = classes;
|
|
179
|
+
}
|
|
180
|
+
|
|
162
181
|
async function load(message: Extract<HostMessage, { type: "load" }>) {
|
|
163
182
|
const thisLoad = ++loadCount;
|
|
164
183
|
api?.dispose();
|
|
165
184
|
api = undefined;
|
|
185
|
+
classObserver?.disconnect();
|
|
186
|
+
classObserver = undefined;
|
|
187
|
+
mirrorClasses(undefined);
|
|
166
188
|
options.element.innerHTML = "";
|
|
167
189
|
await addStoryFiles(message.story);
|
|
168
190
|
const { story } = await loadModule(message.story.js);
|
|
@@ -172,6 +194,9 @@ export function createPreview(options: PreviewOptions) {
|
|
|
172
194
|
panels?.dispose();
|
|
173
195
|
panels = undefined;
|
|
174
196
|
panelsJson = "";
|
|
197
|
+
settingsDialog?.element.remove();
|
|
198
|
+
settingsDialog = undefined;
|
|
199
|
+
settingsButton = undefined;
|
|
175
200
|
panelButtons = document.createElement("div");
|
|
176
201
|
panelButtons.className = "squiffy-preview-header";
|
|
177
202
|
panelSide = document.createElement("div");
|
|
@@ -180,6 +205,8 @@ export function createPreview(options: PreviewOptions) {
|
|
|
180
205
|
container.className = "squiffy-preview-story";
|
|
181
206
|
panelSide.appendChild(container);
|
|
182
207
|
options.element.append(panelButtons, panelSide);
|
|
208
|
+
classObserver = new MutationObserver(() => mirrorClasses(container));
|
|
209
|
+
classObserver.observe(container, { attributes: true, attributeFilter: ["class"] });
|
|
183
210
|
|
|
184
211
|
const newApi = await init({
|
|
185
212
|
element: container,
|
|
@@ -208,8 +235,11 @@ export function createPreview(options: PreviewOptions) {
|
|
|
208
235
|
send({ type: "usesSound", usesSound });
|
|
209
236
|
api = newApi;
|
|
210
237
|
setPanels(story.panels);
|
|
238
|
+
setSettingsButton(story.settings);
|
|
211
239
|
send({ type: "canGoBackChanged", canGoBack: false });
|
|
212
240
|
await newApi.begin(message.startAt);
|
|
241
|
+
// Now, rather than waiting for the observer, so they're there when the host hears it's loaded
|
|
242
|
+
if (thisLoad == loadCount) mirrorClasses(container);
|
|
213
243
|
if (thisLoad == loadCount) send({ type: "loaded" });
|
|
214
244
|
}
|
|
215
245
|
|
|
@@ -222,6 +252,34 @@ export function createPreview(options: PreviewOptions) {
|
|
|
222
252
|
panels = list?.length ? createPanels(api, { panels: list, buttons: panelButtons, side: panelSide }) : undefined;
|
|
223
253
|
}
|
|
224
254
|
|
|
255
|
+
// Shows the Settings button if the story has settings
|
|
256
|
+
function setSettingsButton(list: PlayerSetting[] | undefined) {
|
|
257
|
+
if (!api || !panelButtons) return;
|
|
258
|
+
if (!list?.length) {
|
|
259
|
+
settingsButton?.remove();
|
|
260
|
+
settingsButton = undefined;
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (settingsButton) {
|
|
264
|
+
panelButtons.appendChild(settingsButton);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const button = document.createElement("button");
|
|
268
|
+
button.type = "button";
|
|
269
|
+
button.className = "squiffy-header-button squiffy-settings-button";
|
|
270
|
+
button.title = "Settings";
|
|
271
|
+
button.setAttribute("aria-label", "Settings");
|
|
272
|
+
button.setAttribute("aria-haspopup", "dialog");
|
|
273
|
+
button.innerHTML = SETTINGS_ICON;
|
|
274
|
+
button.addEventListener("click", () => {
|
|
275
|
+
if (!api) return;
|
|
276
|
+
settingsDialog ??= createSettingsDialog(api);
|
|
277
|
+
settingsDialog.open();
|
|
278
|
+
});
|
|
279
|
+
panelButtons.appendChild(button);
|
|
280
|
+
settingsButton = button;
|
|
281
|
+
}
|
|
282
|
+
|
|
225
283
|
async function receive(message: HostMessage) {
|
|
226
284
|
try {
|
|
227
285
|
switch (message.type) {
|
|
@@ -236,6 +294,7 @@ export function createPreview(options: PreviewOptions) {
|
|
|
236
294
|
const { story } = await loadModule(message.js);
|
|
237
295
|
api.update(story);
|
|
238
296
|
setPanels(story.panels);
|
|
297
|
+
setSettingsButton(story.settings);
|
|
239
298
|
}
|
|
240
299
|
break;
|
|
241
300
|
case "restart":
|
|
@@ -0,0 +1,233 @@
|
|
|
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 } from "squiffy-compiler";
|
|
5
|
+
import { createSettingsDialog } from "./settingsDialog.js";
|
|
6
|
+
import type { PlayerSetting, SquiffyInitOptions } from "./types.js";
|
|
7
|
+
|
|
8
|
+
// Player settings (#452): squiffy.json's "settings", which the story reads as settings.name
|
|
9
|
+
|
|
10
|
+
let cleanup: () => void;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
cleanup = globalJsdom("<!DOCTYPE html><html><body><div id=\"squiffy\"></div></body></html>");
|
|
14
|
+
// jsdom has no showModal()
|
|
15
|
+
window.HTMLDialogElement.prototype.showModal = function () { this.setAttribute("open", ""); };
|
|
16
|
+
window.HTMLDialogElement.prototype.close = function () {
|
|
17
|
+
this.removeAttribute("open");
|
|
18
|
+
this.dispatchEvent(new window.Event("close"));
|
|
19
|
+
};
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
localStorage.clear();
|
|
24
|
+
cleanup();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const SETTINGS: PlayerSetting[] = [
|
|
28
|
+
{ name: "warnings", label: "Content warnings", type: "toggle", default: true, description: "Before scenes some may find upsetting" },
|
|
29
|
+
{ name: "textSize", label: "Text size", type: "list", options: [
|
|
30
|
+
{ value: "small", label: "Small" }, { value: "medium", label: "Medium" }, { value: "large", label: "Large" },
|
|
31
|
+
], default: "medium" },
|
|
32
|
+
{ name: "speed", label: "Speed", type: "range", min: 1, max: 5, step: 1, default: 3 },
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const script = `[[start]]:
|
|
36
|
+
@set score = 1
|
|
37
|
+
{{#if settings.warnings}}Warning!{{/if}} Size {{settings.textSize}}, speed {{settings.speed}}.
|
|
38
|
+
{{#if settings.textSize == "large"}}Big.{{/if}}
|
|
39
|
+
Live: {{live "settings.speed"}}
|
|
40
|
+
{{settings}} {{settings text="Options"}} [[Next]](end)
|
|
41
|
+
|
|
42
|
+
[[end]]:
|
|
43
|
+
@set score = 2
|
|
44
|
+
The end.`;
|
|
45
|
+
|
|
46
|
+
const start = async (options: Partial<SquiffyInitOptions> = {}, settings: PlayerSetting[] | undefined = SETTINGS) => {
|
|
47
|
+
const element = document.getElementById("squiffy")!;
|
|
48
|
+
const result = await squiffyCompile({ script, settings });
|
|
49
|
+
if (!result.success) throw new Error("Compile failed");
|
|
50
|
+
const js = result.output.js.map(lines => new Function("squiffy", "get", "set", lines.join("\n")));
|
|
51
|
+
const api = await init({ element, story: { js: js as any, ...result.output.story }, persist: false, scroll: "none", ...options });
|
|
52
|
+
await api.begin();
|
|
53
|
+
return { api, element };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const link = (element: HTMLElement, text: string) =>
|
|
57
|
+
[...element.querySelectorAll<HTMLElement>("a.squiffy-link")].filter(l => l.textContent === text).pop()!;
|
|
58
|
+
|
|
59
|
+
const settle = () => new Promise(resolve => setTimeout(resolve, 0));
|
|
60
|
+
|
|
61
|
+
const text = (element: HTMLElement) => element.textContent!.replace(/\s+/g, " ");
|
|
62
|
+
|
|
63
|
+
test("The story reads its settings' defaults as settings.name", async () => {
|
|
64
|
+
const { api, element } = await start();
|
|
65
|
+
expect(text(element)).toContain("Warning! Size medium, speed 3.");
|
|
66
|
+
expect(text(element)).not.toContain("Big.");
|
|
67
|
+
expect(api.get("settings")).toEqual({ warnings: true, textSize: "medium", speed: 3 });
|
|
68
|
+
expect(api.get("settings.textSize")).toBe("medium");
|
|
69
|
+
expect(api.getSettings().map(s => s.name)).toEqual(["warnings", "textSize", "speed"]);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("Changed settings are read as the player left them, and only values a setting can have are taken", async () => {
|
|
73
|
+
const { api, element } = await start();
|
|
74
|
+
api.setSetting("warnings", false);
|
|
75
|
+
api.setSetting("textSize", "large");
|
|
76
|
+
api.setSetting("speed", 4.6);
|
|
77
|
+
expect(api.get("settings")).toEqual({ warnings: false, textSize: "large", speed: 5 });
|
|
78
|
+
|
|
79
|
+
api.setSetting("textSize", "enormous");
|
|
80
|
+
api.setSetting("speed", "fast" as any);
|
|
81
|
+
api.setSetting("nonsense", true);
|
|
82
|
+
expect(api.get("settings")).toEqual({ warnings: false, textSize: "large", speed: 5 });
|
|
83
|
+
|
|
84
|
+
// Text shown after the change uses it
|
|
85
|
+
api.restart();
|
|
86
|
+
await settle();
|
|
87
|
+
expect(text(element)).toContain("Size large, speed 5.");
|
|
88
|
+
expect(text(element)).toContain("Big.");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("Settings put classes on the story's element, for its CSS", async () => {
|
|
92
|
+
const { api, element } = await start();
|
|
93
|
+
expect([...element.classList]).toEqual(expect.arrayContaining(["setting-warnings", "setting-textSize-medium", "setting-speed-3"]));
|
|
94
|
+
api.setSetting("warnings", false);
|
|
95
|
+
api.setSetting("textSize", "large");
|
|
96
|
+
expect(element.classList.contains("setting-warnings")).toBe(false);
|
|
97
|
+
expect(element.classList.contains("setting-textSize-medium")).toBe(false);
|
|
98
|
+
expect(element.classList.contains("setting-textSize-large")).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("The story can't change a setting", async () => {
|
|
102
|
+
const { api } = await start();
|
|
103
|
+
api.set("settings.warnings", false);
|
|
104
|
+
api.set("settings", { warnings: false });
|
|
105
|
+
expect(api.getSetting("warnings")).toBe(true);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("Back, Restart and saved games leave settings alone, and saved games don't include them", async () => {
|
|
109
|
+
const { api, element } = await start();
|
|
110
|
+
const save = api.getSave();
|
|
111
|
+
expect(save.state).not.toHaveProperty("settings");
|
|
112
|
+
|
|
113
|
+
await api.clickLink(link(element, "Next"));
|
|
114
|
+
api.setSetting("textSize", "small");
|
|
115
|
+
api.goBack();
|
|
116
|
+
expect(api.get("score")).toBe(1);
|
|
117
|
+
expect(api.getSetting("textSize")).toBe("small");
|
|
118
|
+
|
|
119
|
+
await api.loadSave(save);
|
|
120
|
+
expect(api.getSetting("textSize")).toBe("small");
|
|
121
|
+
|
|
122
|
+
api.restart();
|
|
123
|
+
await settle();
|
|
124
|
+
expect(api.getSetting("textSize")).toBe("small");
|
|
125
|
+
expect(text(element)).toContain("Size small");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("Settings are kept for the story when persist is on, and Reset to defaults puts them back", async () => {
|
|
129
|
+
const first = await start({ persist: true, storyId: "mystory" });
|
|
130
|
+
first.api.setSetting("speed", 2);
|
|
131
|
+
first.api.dispose();
|
|
132
|
+
document.getElementById("squiffy")!.innerHTML = "";
|
|
133
|
+
|
|
134
|
+
const { api, element } = await start({ persist: true, storyId: "mystory" });
|
|
135
|
+
expect(api.getSetting("speed")).toBe(2);
|
|
136
|
+
expect(element.classList.contains("setting-speed-2")).toBe(true);
|
|
137
|
+
|
|
138
|
+
const changes: unknown[] = [];
|
|
139
|
+
api.on("settingChanged", e => changes.push(e));
|
|
140
|
+
api.resetSettings();
|
|
141
|
+
expect(api.getSetting("speed")).toBe(3);
|
|
142
|
+
expect(changes).toEqual([{ name: "speed", value: 3 }]);
|
|
143
|
+
expect(localStorage.getItem("squiffy-settings:mystory")).toBeNull();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("A story without settings can have an attribute called settings", async () => {
|
|
147
|
+
const { api } = await start({}, []);
|
|
148
|
+
api.set("settings", { a: 1 });
|
|
149
|
+
expect(api.get("settings.a")).toBe(1);
|
|
150
|
+
expect(api.getSettings()).toEqual([]);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("Changing a setting updates {{live}} text and raises settingChanged", async () => {
|
|
154
|
+
const { api } = await start();
|
|
155
|
+
const events: unknown[] = [];
|
|
156
|
+
api.on("settingChanged", e => events.push(e));
|
|
157
|
+
api.on("set", e => events.push(e.attribute));
|
|
158
|
+
api.setSetting("warnings", false);
|
|
159
|
+
api.setSetting("warnings", false);
|
|
160
|
+
expect(events).toEqual([{ name: "warnings", value: false }, "settings"]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("{{live}} shows a setting as it changes, and only fades when its own value changes", async () => {
|
|
164
|
+
const { api, element } = await start();
|
|
165
|
+
const live = element.querySelector<HTMLElement>(".squiffy-live")!;
|
|
166
|
+
expect(live.textContent).toBe("3");
|
|
167
|
+
api.setSetting("warnings", false);
|
|
168
|
+
expect(live.classList.contains("fade-out")).toBe(false);
|
|
169
|
+
api.setSetting("speed", 4);
|
|
170
|
+
expect(live.classList.contains("fade-out")).toBe(true);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const settingsDialog = () => document.querySelector<HTMLDialogElement>("dialog.squiffy-settings");
|
|
174
|
+
|
|
175
|
+
test("{{settings}} is a link that opens the runtime's own Settings dialog, if the page doesn't handle it", async () => {
|
|
176
|
+
const { api, element } = await start({ strings: { settings: "Réglages" } });
|
|
177
|
+
expect(link(element, "Réglages").classList.contains("link-settings")).toBe(true);
|
|
178
|
+
expect(await api.clickLink(link(element, "Options"))).toBe(true);
|
|
179
|
+
const dialog = settingsDialog()!;
|
|
180
|
+
expect(dialog.open).toBe(true);
|
|
181
|
+
expect(dialog.querySelector("h2")!.textContent).toBe("Réglages");
|
|
182
|
+
// The link can be clicked again
|
|
183
|
+
expect(link(element, "Options").classList.contains("disabled")).toBe(false);
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
test("{{settings}} raises settingsRequested if the page handles it", async () => {
|
|
187
|
+
const { api, element } = await start();
|
|
188
|
+
let requested = 0;
|
|
189
|
+
api.on("settingsRequested", () => requested++);
|
|
190
|
+
await api.clickLink(link(element, "Options"));
|
|
191
|
+
expect(requested).toBe(1);
|
|
192
|
+
expect(settingsDialog()).toBeNull();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const dialogFor = async () => {
|
|
196
|
+
const { api, element } = await start();
|
|
197
|
+
const dialog = createSettingsDialog(api);
|
|
198
|
+
dialog.open();
|
|
199
|
+
return { api, element, dialog: dialog.element };
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
test("The Settings dialog shows each setting with its value", async () => {
|
|
203
|
+
const { dialog } = await dialogFor();
|
|
204
|
+
const rows = [...dialog.querySelectorAll(".squiffy-setting")];
|
|
205
|
+
expect(rows.map(r => r.querySelector("label")!.textContent)).toEqual(["Content warnings", "Text size", "Speed"]);
|
|
206
|
+
expect(rows[0].querySelector("p")!.textContent).toBe("Before scenes some may find upsetting");
|
|
207
|
+
expect(rows[0].querySelector<HTMLInputElement>("input[type=checkbox]")!.checked).toBe(true);
|
|
208
|
+
const select = rows[1].querySelector<HTMLSelectElement>("select")!;
|
|
209
|
+
expect([...select.options].map(o => o.textContent)).toEqual(["Small", "Medium", "Large"]);
|
|
210
|
+
expect(select.value).toBe("medium");
|
|
211
|
+
expect(rows[2].querySelector<HTMLInputElement>("input[type=range]")!.value).toBe("3");
|
|
212
|
+
expect(rows[2].querySelector("output")!.textContent).toBe("3");
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test("Changing a control in the Settings dialog changes the setting straight away", async () => {
|
|
216
|
+
const { api, element, dialog } = await dialogFor();
|
|
217
|
+
const toggle = dialog.querySelector<HTMLInputElement>("input[type=checkbox]")!;
|
|
218
|
+
toggle.checked = false;
|
|
219
|
+
toggle.dispatchEvent(new window.Event("change"));
|
|
220
|
+
const select = dialog.querySelector<HTMLSelectElement>("select")!;
|
|
221
|
+
select.value = "large";
|
|
222
|
+
select.dispatchEvent(new window.Event("change"));
|
|
223
|
+
const range = dialog.querySelector<HTMLInputElement>("input[type=range]")!;
|
|
224
|
+
range.value = "5";
|
|
225
|
+
range.dispatchEvent(new window.Event("input"));
|
|
226
|
+
expect(api.get("settings")).toEqual({ warnings: false, textSize: "large", speed: 5 });
|
|
227
|
+
expect(dialog.querySelector("output")!.textContent).toBe("5");
|
|
228
|
+
expect(element.classList.contains("setting-textSize-large")).toBe(true);
|
|
229
|
+
|
|
230
|
+
dialog.querySelector<HTMLButtonElement>("[data-reset]")!.click();
|
|
231
|
+
expect(api.get("settings")).toEqual({ warnings: true, textSize: "medium", speed: 3 });
|
|
232
|
+
expect(dialog.querySelector<HTMLSelectElement>("select")!.value).toBe("medium");
|
|
233
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// The Settings dialog (#452): the story's player settings, from squiffy.json's "settings", which
|
|
2
|
+
// the player can change at any time. A toggle is a switch, a list a drop-down and a range a
|
|
3
|
+
// slider. Changes take effect straight away, and Reset to defaults puts them all back.
|
|
4
|
+
|
|
5
|
+
import type { PlayerSetting, SquiffyApi } from "./types.js";
|
|
6
|
+
|
|
7
|
+
// The dialog's text in English. The published page passes in squiffy.json's "strings", which
|
|
8
|
+
// use the same names (see PAGE_STRINGS in the compiler).
|
|
9
|
+
export const SETTINGS_STRINGS = {
|
|
10
|
+
settings: "Settings",
|
|
11
|
+
resetSettings: "Reset to defaults",
|
|
12
|
+
close: "Close",
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
type SettingsStringName = keyof typeof SETTINGS_STRINGS;
|
|
16
|
+
|
|
17
|
+
export interface SettingsDialogOptions {
|
|
18
|
+
// The dialog's text, for a story in another language. Anything left out is in English.
|
|
19
|
+
strings?: Partial<Record<string, string>>;
|
|
20
|
+
// Where the dialog goes in the page: the end of the body by default
|
|
21
|
+
container?: HTMLElement;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface SettingsDialog {
|
|
25
|
+
element: HTMLDialogElement;
|
|
26
|
+
open: () => void;
|
|
27
|
+
close: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Lucide's settings icon (ISC licence), as on the published page's Settings button
|
|
31
|
+
export const SETTINGS_ICON = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z\"/><circle cx=\"12\" cy=\"12\" r=\"3\"/></svg>";
|
|
32
|
+
|
|
33
|
+
const ICON_CLOSE = "<svg viewBox=\"0 0 24 24\" aria-hidden=\"true\"><path d=\"M18 6 6 18M6 6l12 12\"/></svg>";
|
|
34
|
+
|
|
35
|
+
let dialogCount = 0;
|
|
36
|
+
|
|
37
|
+
export function createSettingsDialog(api: SquiffyApi, options: SettingsDialogOptions = {}): SettingsDialog {
|
|
38
|
+
const strings: Record<SettingsStringName, string> = { ...SETTINGS_STRINGS };
|
|
39
|
+
for (const name of Object.keys(SETTINGS_STRINGS) as SettingsStringName[]) {
|
|
40
|
+
const value = options.strings?.[name];
|
|
41
|
+
if (typeof value == "string") strings[name] = value;
|
|
42
|
+
}
|
|
43
|
+
const id = `squiffy-settings-${++dialogCount}`;
|
|
44
|
+
|
|
45
|
+
const dialog = document.createElement("dialog");
|
|
46
|
+
dialog.className = "squiffy-settings";
|
|
47
|
+
dialog.setAttribute("aria-labelledby", `${id}-heading`);
|
|
48
|
+
dialog.innerHTML = `
|
|
49
|
+
<div class="squiffy-settings-header">
|
|
50
|
+
<h2 id="${id}-heading"></h2>
|
|
51
|
+
<button type="button" class="squiffy-settings-close" data-close>${ICON_CLOSE}</button>
|
|
52
|
+
</div>
|
|
53
|
+
<div class="squiffy-settings-list"></div>
|
|
54
|
+
<div class="squiffy-settings-footer">
|
|
55
|
+
<button type="button" class="squiffy-settings-reset" data-reset></button>
|
|
56
|
+
</div>`;
|
|
57
|
+
const $ = <T extends Element>(selector: string) => dialog.querySelector<T>(selector)!;
|
|
58
|
+
$("h2").textContent = strings.settings;
|
|
59
|
+
const closeButton = $<HTMLButtonElement>("[data-close]");
|
|
60
|
+
closeButton.title = strings.close;
|
|
61
|
+
closeButton.setAttribute("aria-label", strings.close);
|
|
62
|
+
closeButton.addEventListener("click", () => dialog.close());
|
|
63
|
+
const list = $<HTMLDivElement>(".squiffy-settings-list");
|
|
64
|
+
const reset = $<HTMLButtonElement>("[data-reset]");
|
|
65
|
+
reset.textContent = strings.resetSettings;
|
|
66
|
+
reset.addEventListener("click", () => {
|
|
67
|
+
api.resetSettings();
|
|
68
|
+
render();
|
|
69
|
+
});
|
|
70
|
+
// Clicking the backdrop closes it too
|
|
71
|
+
dialog.addEventListener("click", event => {
|
|
72
|
+
if (event.target === dialog) dialog.close();
|
|
73
|
+
});
|
|
74
|
+
(options.container ?? document.body).appendChild(dialog);
|
|
75
|
+
|
|
76
|
+
// A row for each setting, with the control showing its value now
|
|
77
|
+
function render() {
|
|
78
|
+
list.replaceChildren(...api.getSettings().map((setting, i) => row(setting, `${id}-${i}`)));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function row(setting: PlayerSetting, controlId: string): HTMLElement {
|
|
82
|
+
const div = document.createElement("div");
|
|
83
|
+
div.className = `squiffy-setting squiffy-setting-${setting.type}`;
|
|
84
|
+
const text = document.createElement("div");
|
|
85
|
+
text.className = "squiffy-setting-text";
|
|
86
|
+
const label = document.createElement("label");
|
|
87
|
+
label.htmlFor = controlId;
|
|
88
|
+
label.textContent = setting.label || setting.name;
|
|
89
|
+
text.appendChild(label);
|
|
90
|
+
if (setting.description) {
|
|
91
|
+
const description = document.createElement("p");
|
|
92
|
+
description.id = `${controlId}-description`;
|
|
93
|
+
description.textContent = setting.description;
|
|
94
|
+
text.appendChild(description);
|
|
95
|
+
}
|
|
96
|
+
div.appendChild(text);
|
|
97
|
+
|
|
98
|
+
const value = api.getSetting(setting.name);
|
|
99
|
+
let control: HTMLInputElement | HTMLSelectElement;
|
|
100
|
+
if (setting.type == "toggle") {
|
|
101
|
+
const input = document.createElement("input");
|
|
102
|
+
input.type = "checkbox";
|
|
103
|
+
input.setAttribute("role", "switch");
|
|
104
|
+
input.checked = value === true;
|
|
105
|
+
input.addEventListener("change", () => api.setSetting(setting.name, input.checked));
|
|
106
|
+
control = input;
|
|
107
|
+
}
|
|
108
|
+
else if (setting.type == "list") {
|
|
109
|
+
const select = document.createElement("select");
|
|
110
|
+
for (const option of setting.options) {
|
|
111
|
+
const element = document.createElement("option");
|
|
112
|
+
element.value = option.value;
|
|
113
|
+
element.textContent = option.label;
|
|
114
|
+
select.appendChild(element);
|
|
115
|
+
}
|
|
116
|
+
select.value = String(value);
|
|
117
|
+
select.addEventListener("change", () => api.setSetting(setting.name, select.value));
|
|
118
|
+
control = select;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const input = document.createElement("input");
|
|
122
|
+
input.type = "range";
|
|
123
|
+
input.min = String(setting.min);
|
|
124
|
+
input.max = String(setting.max);
|
|
125
|
+
input.step = setting.step ? String(setting.step) : "any";
|
|
126
|
+
input.value = String(value);
|
|
127
|
+
const output = document.createElement("output");
|
|
128
|
+
output.htmlFor.add(controlId);
|
|
129
|
+
output.textContent = String(value);
|
|
130
|
+
input.addEventListener("input", () => {
|
|
131
|
+
api.setSetting(setting.name, Number(input.value));
|
|
132
|
+
output.textContent = String(api.getSetting(setting.name));
|
|
133
|
+
});
|
|
134
|
+
const wrapper = document.createElement("div");
|
|
135
|
+
wrapper.className = "squiffy-setting-slider";
|
|
136
|
+
wrapper.append(input, output);
|
|
137
|
+
control = input;
|
|
138
|
+
control.id = controlId;
|
|
139
|
+
if (setting.description) control.setAttribute("aria-describedby", `${controlId}-description`);
|
|
140
|
+
div.appendChild(wrapper);
|
|
141
|
+
return div;
|
|
142
|
+
}
|
|
143
|
+
control.id = controlId;
|
|
144
|
+
if (setting.description) control.setAttribute("aria-describedby", `${controlId}-description`);
|
|
145
|
+
div.appendChild(control);
|
|
146
|
+
return div;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
element: dialog,
|
|
151
|
+
open() {
|
|
152
|
+
render();
|
|
153
|
+
if (!dialog.open) dialog.showModal();
|
|
154
|
+
list.querySelector<HTMLElement>("input, select")?.focus();
|
|
155
|
+
},
|
|
156
|
+
close() {
|
|
157
|
+
dialog.close();
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
package/src/squiffy.page.css
CHANGED
|
@@ -117,7 +117,10 @@ button#squiffy-mute:not(.squiffy-muted) .squiffy-icon-muted
|
|
|
117
117
|
grid-column: 1 / -1;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
|
|
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
|
|
121
124
|
{
|
|
122
125
|
position: sticky;
|
|
123
126
|
top: 3.5rem;
|