squiffy-runtime 6.0.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 +3 -0
- package/dist/assets.test.d.ts +1 -0
- package/dist/classes.test.d.ts +1 -0
- package/dist/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +16 -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/pluginManager.d.ts +2 -1
- package/dist/preview-host.d.ts +33 -0
- package/dist/preview-host.js +125 -0
- package/dist/preview-host.js.map +7 -0
- package/dist/preview-host.test.d.ts +1 -0
- package/dist/preview-html.d.ts +2 -0
- package/dist/preview-html.js +2 -0
- package/dist/preview-protocol.d.ts +69 -0
- package/dist/preview.d.ts +15 -0
- package/dist/preview.main.d.ts +1 -0
- package/dist/preview.test.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 +1631 -0
- package/dist/squiffy.runtime.css +1123 -20
- package/dist/squiffy.runtime.d.ts +13 -1
- package/dist/squiffy.runtime.global.js +146 -76
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +7105 -4681
- 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 +137 -1
- package/dist/types.plugins.d.ts +2 -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 +25 -11
- 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 +225 -0
- package/src/assets.ts +92 -0
- package/src/classes.test.ts +201 -0
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +20 -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 +6 -1
- package/src/plugins/animate.ts +34 -9
- 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 +171 -0
- package/src/preview-host.ts +190 -0
- package/src/preview-protocol.ts +79 -0
- package/src/preview.main.ts +34 -0
- package/src/preview.test.ts +367 -0
- package/src/preview.ts +331 -0
- 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 +1130 -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 +7 -0
- package/src/types.ts +226 -2
- 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 +83 -0
- 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,156 @@
|
|
|
1
|
+
import { expect, test, beforeEach, afterEach, beforeAll, vi } 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 { confirmDialog } from "./confirmDialog.js";
|
|
6
|
+
import type { SquiffyApi, SquiffyInitOptions } from "./types.js";
|
|
7
|
+
|
|
8
|
+
// {{restart}} and {{saves}} links (#452), and the dialog that asks before restarting
|
|
9
|
+
|
|
10
|
+
beforeAll(() => {
|
|
11
|
+
vi.stubGlobal("crypto", { randomUUID: () => "00000000-0000-0000-0000-000000000000" });
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
let cleanup: () => void;
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
cleanup = globalJsdom("<!DOCTYPE html><html><head><title>My Story</title></head><body><div id=\"squiffy\"></div></body></html>");
|
|
18
|
+
// jsdom has no showModal()
|
|
19
|
+
window.HTMLDialogElement.prototype.showModal = function () { this.setAttribute("open", ""); };
|
|
20
|
+
window.HTMLDialogElement.prototype.close = function () {
|
|
21
|
+
this.removeAttribute("open");
|
|
22
|
+
this.dispatchEvent(new window.Event("close"));
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
localStorage.clear();
|
|
28
|
+
cleanup();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const script = `[[start]]:
|
|
32
|
+
@set score = 1
|
|
33
|
+
Hello. [[Next]](end)
|
|
34
|
+
|
|
35
|
+
[[end]]:
|
|
36
|
+
The end. {{restart text="Play again"}} {{restart confirm=false}} {{saves}} {{saves text="Save your game"}}`;
|
|
37
|
+
|
|
38
|
+
const start = async (options: Partial<SquiffyInitOptions> = {}) => {
|
|
39
|
+
const element = document.getElementById("squiffy")!;
|
|
40
|
+
const result = await squiffyCompile({ script });
|
|
41
|
+
if (!result.success) throw new Error("Compile failed");
|
|
42
|
+
const js = result.output.js.map(lines => new Function("squiffy", "get", "set", lines.join("\n")));
|
|
43
|
+
const api = await init({ element, story: { js: js as any, ...result.output.story }, persist: false, scroll: "none", ...options });
|
|
44
|
+
await api.begin();
|
|
45
|
+
return { api, element };
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const link = (element: HTMLElement, text: string) =>
|
|
49
|
+
[...element.querySelectorAll<HTMLElement>("a.squiffy-link")].filter(l => l.textContent === text).pop()!;
|
|
50
|
+
|
|
51
|
+
const click = async (api: SquiffyApi, element: HTMLElement, text: string) => {
|
|
52
|
+
expect(await api.clickLink(link(element, text))).toBe(true);
|
|
53
|
+
await settle();
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const settle = () => new Promise(resolve => setTimeout(resolve, 0));
|
|
57
|
+
|
|
58
|
+
const confirmDialogElement = () => document.querySelector<HTMLDialogElement>("dialog.squiffy-confirm");
|
|
59
|
+
|
|
60
|
+
test("{{restart}} and {{saves}} are links, with their own text or the page's", async () => {
|
|
61
|
+
const { api, element } = await start({ strings: { restart: "Recommencer", saves: "Parties" } });
|
|
62
|
+
await click(api, element, "Next");
|
|
63
|
+
const links = [...element.querySelectorAll<HTMLElement>(".link-restart, .link-saves")];
|
|
64
|
+
expect(links.map(l => l.textContent)).toEqual(["Play again", "Recommencer", "Parties", "Save your game"]);
|
|
65
|
+
expect(links[0].getAttribute("data-confirm")).toBeNull();
|
|
66
|
+
expect(links[1].getAttribute("data-confirm")).toBe("false");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test("{{restart}} asks first, in a dialog, and restarts only if the player says so", async () => {
|
|
70
|
+
const { api, element } = await start();
|
|
71
|
+
await click(api, element, "Next");
|
|
72
|
+
|
|
73
|
+
await click(api, element, "Play again");
|
|
74
|
+
let dialog = confirmDialogElement()!;
|
|
75
|
+
expect(dialog.open).toBe(true);
|
|
76
|
+
expect(dialog.querySelector("p")!.textContent).toBe("Are you sure you want to restart?");
|
|
77
|
+
dialog.querySelector<HTMLButtonElement>("[data-answer=no]")!.click();
|
|
78
|
+
await settle();
|
|
79
|
+
expect(confirmDialogElement()).toBeNull();
|
|
80
|
+
expect(api.get("_section")).toBe("end");
|
|
81
|
+
|
|
82
|
+
await click(api, element, "Play again");
|
|
83
|
+
dialog = confirmDialogElement()!;
|
|
84
|
+
dialog.querySelector<HTMLButtonElement>("[data-answer=yes]")!.click();
|
|
85
|
+
await settle();
|
|
86
|
+
expect(confirmDialogElement()).toBeNull();
|
|
87
|
+
expect(api.get("_section")).toBe("start");
|
|
88
|
+
expect(element.textContent).not.toContain("The end.");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("{{restart confirm=false}} restarts straight away", async () => {
|
|
92
|
+
const { api, element } = await start();
|
|
93
|
+
await click(api, element, "Next");
|
|
94
|
+
await click(api, element, "Restart");
|
|
95
|
+
expect(confirmDialogElement()).toBeNull();
|
|
96
|
+
expect(api.get("_section")).toBe("start");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("{{saves}} opens a Saved games dialog", async () => {
|
|
100
|
+
const { api, element } = await start();
|
|
101
|
+
await click(api, element, "Next");
|
|
102
|
+
await click(api, element, "Saved games");
|
|
103
|
+
const dialogs = document.querySelectorAll<HTMLDialogElement>("dialog.squiffy-saves");
|
|
104
|
+
expect(dialogs.length).toBe(1);
|
|
105
|
+
expect(dialogs[0].open).toBe(true);
|
|
106
|
+
dialogs[0].close();
|
|
107
|
+
// The same dialog again
|
|
108
|
+
await click(api, element, "Save your game");
|
|
109
|
+
expect(document.querySelectorAll("dialog.squiffy-saves").length).toBe(1);
|
|
110
|
+
expect(dialogs[0].open).toBe(true);
|
|
111
|
+
api.dispose();
|
|
112
|
+
expect(document.querySelectorAll("dialog.squiffy-saves").length).toBe(0);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("The page can handle {{restart}} and {{saves}} itself", async () => {
|
|
116
|
+
const { api, element } = await start();
|
|
117
|
+
const restarts: boolean[] = [];
|
|
118
|
+
let saves = 0;
|
|
119
|
+
api.on("restartRequested", e => restarts.push(e.confirm));
|
|
120
|
+
api.on("savesRequested", () => saves++);
|
|
121
|
+
await click(api, element, "Next");
|
|
122
|
+
await click(api, element, "Play again");
|
|
123
|
+
await click(api, element, "Restart");
|
|
124
|
+
await click(api, element, "Saved games");
|
|
125
|
+
expect(restarts).toEqual([true, false]);
|
|
126
|
+
expect(saves).toBe(1);
|
|
127
|
+
expect(document.querySelector("dialog")).toBeNull();
|
|
128
|
+
expect(api.get("_section")).toBe("end");
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("The links are only clickable in the current section", async () => {
|
|
132
|
+
const twoEndings = `[[a]]:
|
|
133
|
+
{{restart confirm=false}} [[b]]
|
|
134
|
+
|
|
135
|
+
[[b]]:
|
|
136
|
+
B`;
|
|
137
|
+
const element = document.getElementById("squiffy")!;
|
|
138
|
+
const result = await squiffyCompile({ script: twoEndings });
|
|
139
|
+
if (!result.success) throw new Error("Compile failed");
|
|
140
|
+
const api = await init({ element, story: { js: [], ...result.output.story }, persist: false, scroll: "none" });
|
|
141
|
+
await api.begin();
|
|
142
|
+
await click(api, element, "b");
|
|
143
|
+
expect(await api.clickLink(link(element, "Restart"))).toBe(false);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
test("confirmDialog resolves false on Escape, and uses the given text", async () => {
|
|
147
|
+
const answer = confirmDialog({ message: "Delete it?", confirm: "Delete", cancel: "Keep", danger: true });
|
|
148
|
+
const dialog = confirmDialogElement()!;
|
|
149
|
+
expect(dialog.getAttribute("role")).toBe("alertdialog");
|
|
150
|
+
expect([...dialog.querySelectorAll("button")].map(b => b.textContent)).toEqual(["Keep", "Delete"]);
|
|
151
|
+
expect(dialog.querySelector("[data-answer=yes]")!.classList.contains("squiffy-confirm-danger")).toBe(true);
|
|
152
|
+
expect(document.activeElement).toBe(dialog.querySelector("[data-answer=yes]"));
|
|
153
|
+
dialog.dispatchEvent(new window.Event("cancel", { cancelable: true }));
|
|
154
|
+
expect(await answer).toBe(false);
|
|
155
|
+
expect(confirmDialogElement()).toBeNull();
|
|
156
|
+
});
|
package/src/updater.ts
CHANGED
|
@@ -26,18 +26,36 @@ export function updateStory(oldStory: Story,
|
|
|
26
26
|
return outputElement.querySelectorAll(`[data-source='[[${safeQuerySelector(section)}]][${safeQuerySelector(passage)}]']`);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
// Links to the same passage are matched up in order, so that in an {{#each}}, the item's link
|
|
30
|
+
// that was clicked is the one that stays disabled (#448)
|
|
31
|
+
function passageLinksByName(element: Element) {
|
|
32
|
+
const byName = new Map<string, Element[]>();
|
|
33
|
+
for (const link of element.querySelectorAll("a.link-passage[data-passage]")) {
|
|
34
|
+
const name = link.getAttribute("data-passage")!;
|
|
35
|
+
if (!byName.has(name)) byName.set(name, []);
|
|
36
|
+
byName.get(name)!.push(link);
|
|
37
|
+
}
|
|
38
|
+
return byName;
|
|
39
|
+
}
|
|
40
|
+
|
|
29
41
|
function updateElementTextPreservingDisabledPassageLinks(element: Element, text: string) {
|
|
30
|
-
|
|
31
|
-
const disabledPassages = Array.from(element
|
|
32
|
-
.querySelectorAll("a.link-passage.disabled"))
|
|
33
|
-
.map((el: Element) => el.getAttribute("data-passage"));
|
|
42
|
+
const oldLinks = passageLinksByName(element);
|
|
34
43
|
|
|
35
44
|
element.innerHTML = text;
|
|
36
45
|
|
|
37
|
-
// Re-disable links that were disabled before the update
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
46
|
+
// Re-disable links that were disabled before the update, keeping which click or
|
|
47
|
+
// {{deactivate}} disabled them, so going back enables the right ones
|
|
48
|
+
const newLinks = passageLinksByName(element);
|
|
49
|
+
for (const [name, links] of oldLinks) {
|
|
50
|
+
links.forEach((oldLink, i) => {
|
|
51
|
+
const link = newLinks.get(name)?.[i];
|
|
52
|
+
if (!link || !oldLink.classList.contains("disabled")) return;
|
|
53
|
+
disableLink(link);
|
|
54
|
+
for (const attr of ["data-clicked", "data-deactivated-by"]) {
|
|
55
|
+
const value = oldLink.getAttribute(attr);
|
|
56
|
+
if (value !== null) link.setAttribute(attr, value);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
41
59
|
}
|
|
42
60
|
}
|
|
43
61
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { afterEach, beforeEach, expect, test, vi } from "vitest";
|
|
2
|
+
import globalJsdom from "global-jsdom";
|
|
3
|
+
import { fadeReplace } from "./utils.js";
|
|
4
|
+
|
|
5
|
+
let cleanup: () => void;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
cleanup = globalJsdom("<!DOCTYPE html><html><body></body></html>");
|
|
9
|
+
vi.useFakeTimers();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
vi.useRealTimers();
|
|
14
|
+
cleanup();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const label = () => {
|
|
18
|
+
const element = document.createElement("span");
|
|
19
|
+
element.innerHTML = "Old, with <a class=\"squiffy-link\">a link</a>";
|
|
20
|
+
document.body.appendChild(element);
|
|
21
|
+
return element;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const transitionEnd = (target: Element) => target.dispatchEvent(new window.Event("transitionend", { bubbles: true }));
|
|
25
|
+
|
|
26
|
+
test("fadeReplace fades the text out, replaces it, and fades it in", async () => {
|
|
27
|
+
const element = label();
|
|
28
|
+
let done = false;
|
|
29
|
+
void fadeReplace(element, "New").then(() => { done = true; });
|
|
30
|
+
expect(element.classList.contains("fade-out")).toBe(true);
|
|
31
|
+
|
|
32
|
+
transitionEnd(element);
|
|
33
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
34
|
+
expect(element.innerHTML).toBe("New");
|
|
35
|
+
expect(element.classList.contains("fade-in")).toBe(true);
|
|
36
|
+
|
|
37
|
+
transitionEnd(element);
|
|
38
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
39
|
+
expect(done).toBe(true);
|
|
40
|
+
expect(element.className).toBe("");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("fadeReplace isn't fooled by a transition ending inside the text, such as a link's colour", async () => {
|
|
44
|
+
const element = label();
|
|
45
|
+
void fadeReplace(element, "New");
|
|
46
|
+
|
|
47
|
+
transitionEnd(element.querySelector("a")!);
|
|
48
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
49
|
+
expect(element.innerHTML).toContain("Old");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("fadeReplace finishes even if the fades never run, so the section's links come back", async () => {
|
|
53
|
+
const element = label();
|
|
54
|
+
let done = false;
|
|
55
|
+
void fadeReplace(element, "New").then(() => { done = true; });
|
|
56
|
+
|
|
57
|
+
await vi.advanceTimersByTimeAsync(5000);
|
|
58
|
+
expect(element.innerHTML).toBe("New");
|
|
59
|
+
expect(done).toBe(true);
|
|
60
|
+
});
|
package/src/utils.ts
CHANGED
|
@@ -1,17 +1,49 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
// {{#replace}}: fades the label's text out, replaces it, and fades it in. The section's links wait
|
|
2
|
+
// for it, so it always finishes: see transitionEnd. written is called with the element once the
|
|
3
|
+
// new text is in it.
|
|
4
|
+
export async function fadeReplace(element: HTMLElement, text: string, written?: (el: HTMLElement) => void): Promise<void> {
|
|
5
|
+
const fadedOut = transitionEnd(element);
|
|
6
|
+
element.classList.add("fade-out");
|
|
7
|
+
await fadedOut;
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
element.innerHTML = text;
|
|
10
|
+
written?.(element);
|
|
11
|
+
const fadedIn = transitionEnd(element);
|
|
12
|
+
element.classList.remove("fade-out");
|
|
13
|
+
element.classList.add("fade-in");
|
|
14
|
+
await fadedIn;
|
|
15
|
+
element.classList.remove("fade-in");
|
|
16
|
+
}
|
|
10
17
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
// When the element's own transition ends. Not one inside it, whose transitionend bubbles up to it,
|
|
19
|
+
// such as the colour of a link that's just been clicked. And not more than a moment longer than
|
|
20
|
+
// the fades take (200ms), in case it doesn't run at all, e.g. if the element isn't shown, or its
|
|
21
|
+
// classes change back within a frame.
|
|
22
|
+
function transitionEnd(element: HTMLElement, timeout = 1000): Promise<void> {
|
|
23
|
+
return new Promise(resolve => {
|
|
24
|
+
const done = () => {
|
|
25
|
+
element.removeEventListener("transitionend", ended);
|
|
26
|
+
element.removeEventListener("transitioncancel", ended);
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
resolve();
|
|
29
|
+
};
|
|
30
|
+
const ended = (event: Event) => {
|
|
31
|
+
if (event.target === element) done();
|
|
32
|
+
};
|
|
33
|
+
element.addEventListener("transitionend", ended);
|
|
34
|
+
element.addEventListener("transitioncancel", ended);
|
|
35
|
+
const timer = setTimeout(done, timeout);
|
|
16
36
|
});
|
|
17
|
-
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// A delay in milliseconds (#451): a number, or text with a number of seconds or milliseconds in
|
|
40
|
+
// it, as in CSS and Twine, e.g. "3s", "1.5s" or "500ms" ("500" is milliseconds too). Returns null
|
|
41
|
+
// if it isn't one, or is negative.
|
|
42
|
+
export function parseDelay(value: unknown): number | null {
|
|
43
|
+
if (typeof value == "number") return Number.isFinite(value) && value >= 0 ? value : null;
|
|
44
|
+
if (typeof value != "string") return null;
|
|
45
|
+
const match = /^\s*(\d+(?:\.\d+)?|\.\d+)\s*(ms|s)?\s*$/i.exec(value);
|
|
46
|
+
if (!match) return null;
|
|
47
|
+
const n = Number(match[1]);
|
|
48
|
+
return match[2]?.toLowerCase() == "s" ? n * 1000 : n;
|
|
49
|
+
}
|
package/vite.config.ts
CHANGED
|
@@ -39,10 +39,9 @@ export default defineConfig({
|
|
|
39
39
|
closeBundle() {
|
|
40
40
|
try {
|
|
41
41
|
mkdirSync("dist", { recursive: true });
|
|
42
|
-
|
|
43
|
-
resolve("src
|
|
44
|
-
|
|
45
|
-
);
|
|
42
|
+
for (const file of ["squiffy.runtime.css", "squiffy.page.css"]) {
|
|
43
|
+
copyFileSync(resolve("src", file), resolve("dist", file));
|
|
44
|
+
}
|
|
46
45
|
} catch (err) {
|
|
47
46
|
console.error("Failed to copy CSS file:", err);
|
|
48
47
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Builds the preview page (src/preview.main.ts) as a single self-contained HTML file,
|
|
2
|
+
// dist/squiffy.preview.html, which hosts load into a sandboxed iframe with srcdoc or use as a
|
|
3
|
+
// VS Code webview's html. It's self-contained because a sandboxed frame has an opaque origin,
|
|
4
|
+
// so it can't rely on loading anything from the host's server. Also builds the host helper,
|
|
5
|
+
// dist/preview-host.js, and dist/preview-html.js, which exports the page as a string.
|
|
6
|
+
// See preview-protocol.ts, and #307 for the spike results behind this.
|
|
7
|
+
import { defineConfig } from "vite";
|
|
8
|
+
import { build } from "esbuild";
|
|
9
|
+
import { readFileSync, rmSync, writeFileSync } from "fs";
|
|
10
|
+
import { resolve } from "path";
|
|
11
|
+
|
|
12
|
+
const template = (css: string, js: string) => `<!doctype html>
|
|
13
|
+
<html lang="en">
|
|
14
|
+
<head>
|
|
15
|
+
<meta charset="utf-8">
|
|
16
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
17
|
+
<title>Squiffy preview</title>
|
|
18
|
+
<style>
|
|
19
|
+
${css}
|
|
20
|
+
/* The preview scrolls the story within the frame, with the text centred as on the exported page */
|
|
21
|
+
html, body { height: 100%; }
|
|
22
|
+
div#squiffy-container { max-width: none; height: 100%; padding: 0; }
|
|
23
|
+
#squiffy, .squiffy-preview-story, .squiffy-preview-story > div { height: 100%; box-sizing: border-box; }
|
|
24
|
+
.squiffy-preview-story > div { padding: 1em max(1.25rem, calc((100% - var(--squiffy-max-width)) / 2)); }
|
|
25
|
+
/* The story's panels: their buttons above the story, and side panels to its right */
|
|
26
|
+
#squiffy { display: flex; flex-direction: column; }
|
|
27
|
+
.squiffy-preview-header { flex: none; padding: 0.5rem 0.75rem 0; font-family: system-ui, sans-serif; font-size: 0.875rem; text-align: right; }
|
|
28
|
+
.squiffy-preview-header:empty { display: none; }
|
|
29
|
+
.squiffy-preview-body { flex: 1; min-height: 0; display: flex; }
|
|
30
|
+
#squiffy .squiffy-preview-body > .squiffy-preview-story { flex: 1; min-width: 0; height: 100%; }
|
|
31
|
+
.squiffy-preview-body > aside.squiffy-panels-side { flex: none; width: var(--squiffy-side-width); box-sizing: border-box; overflow-y: auto; padding: 1em 1.25rem 1em 0; }
|
|
32
|
+
</style>
|
|
33
|
+
</head>
|
|
34
|
+
<body>
|
|
35
|
+
<div id="squiffy-container"><div id="squiffy"></div></div>
|
|
36
|
+
<script>
|
|
37
|
+
${js.replace(/<\/(script)/gi, "<\\/$1")}
|
|
38
|
+
</script>
|
|
39
|
+
</body>
|
|
40
|
+
</html>
|
|
41
|
+
`;
|
|
42
|
+
|
|
43
|
+
export default defineConfig({
|
|
44
|
+
build: {
|
|
45
|
+
target: "es2020",
|
|
46
|
+
minify: "esbuild",
|
|
47
|
+
emptyOutDir: false,
|
|
48
|
+
outDir: "dist/preview-build",
|
|
49
|
+
lib: {
|
|
50
|
+
entry: "src/preview.main.ts",
|
|
51
|
+
name: "squiffyPreview",
|
|
52
|
+
formats: ["iife"],
|
|
53
|
+
fileName: () => "squiffy.preview.js",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
plugins: [
|
|
57
|
+
{
|
|
58
|
+
name: "inline-preview-page",
|
|
59
|
+
async closeBundle() {
|
|
60
|
+
const js = readFileSync(resolve("dist/preview-build/squiffy.preview.js"), "utf8");
|
|
61
|
+
// The same styles as an exported story's page
|
|
62
|
+
const css = readFileSync(resolve("src/squiffy.runtime.css"), "utf8") + "\n"
|
|
63
|
+
+ readFileSync(resolve("src/squiffy.page.css"), "utf8");
|
|
64
|
+
const html = template(css, js);
|
|
65
|
+
writeFileSync(resolve("dist/squiffy.preview.html"), html);
|
|
66
|
+
writeFileSync(resolve("dist/preview-html.js"),
|
|
67
|
+
`// The Squiffy preview page, built from squiffy-runtime's src/preview.main.ts\nexport const previewHtml = ${JSON.stringify(html)};\n`);
|
|
68
|
+
writeFileSync(resolve("dist/preview-html.d.ts"),
|
|
69
|
+
"/** The Squiffy preview page, as HTML to use with setUpPreviewFrame() from squiffy-runtime/preview-host. */\nexport declare const previewHtml: string;\n");
|
|
70
|
+
rmSync(resolve("dist/preview-build"), { recursive: true, force: true });
|
|
71
|
+
|
|
72
|
+
await build({
|
|
73
|
+
entryPoints: [resolve("src/preview-host.ts")],
|
|
74
|
+
bundle: true,
|
|
75
|
+
format: "esm",
|
|
76
|
+
target: "es2020",
|
|
77
|
+
outfile: resolve("dist/preview-host.js"),
|
|
78
|
+
sourcemap: true,
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
],
|
|
83
|
+
});
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import { expect, test, describe } from "vitest";
|
|
2
|
-
import { rewriteInfixCondition, rewriteInfixExpressions } from "./infixExpressions.js";
|
|
3
|
-
|
|
4
|
-
describe("rewriteInfixCondition", () => {
|
|
5
|
-
test.each([
|
|
6
|
-
["gender = \"male\"", "(eq gender \"male\")"],
|
|
7
|
-
["gender == \"male\"", "(eq gender \"male\")"],
|
|
8
|
-
["gender != \"male\"", "(ne gender \"male\")"],
|
|
9
|
-
["score > 100", "(gt score 100)"],
|
|
10
|
-
["score < 100", "(lt score 100)"],
|
|
11
|
-
["score >= 100", "(gte score 100)"],
|
|
12
|
-
["score <= 100", "(lte score 100)"],
|
|
13
|
-
["a and b", "(and a b)"],
|
|
14
|
-
["a && b", "(and a b)"],
|
|
15
|
-
["a or b", "(or a b)"],
|
|
16
|
-
["a || b", "(or a b)"],
|
|
17
|
-
["not a", "(not a)"],
|
|
18
|
-
["!a", "(not a)"],
|
|
19
|
-
["! a", "(not a)"],
|
|
20
|
-
["not not a", "(not (not a))"],
|
|
21
|
-
])("operator: %s", (input, expected) => {
|
|
22
|
-
expect(rewriteInfixCondition(input)).toBe(expected);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test.each([
|
|
26
|
-
["score>100", "(gt score 100)"],
|
|
27
|
-
["gender=\"male\"", "(eq gender \"male\")"],
|
|
28
|
-
["a&&b||c", "(or (and a b) c)"],
|
|
29
|
-
["score>=-5", "(gte score -5)"],
|
|
30
|
-
])("no spaces: %s", (input, expected) => {
|
|
31
|
-
expect(rewriteInfixCondition(input)).toBe(expected);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test.each([
|
|
35
|
-
["a and b and c", "(and a b c)"],
|
|
36
|
-
["a or b or c", "(or a b c)"],
|
|
37
|
-
["a or b and c", "(or a (and b c))"],
|
|
38
|
-
["a and b or c", "(or (and a b) c)"],
|
|
39
|
-
["score > 100 and has_key", "(and (gt score 100) has_key)"],
|
|
40
|
-
["x = 1 or y != 2 and z", "(or (eq x 1) (and (ne y 2) z))"],
|
|
41
|
-
["not a and b", "(and (not a) b)"],
|
|
42
|
-
["not a = b", "(not (eq a b))"],
|
|
43
|
-
["not gender = \"male\"", "(not (eq gender \"male\"))"],
|
|
44
|
-
["not score > 100 and has_key", "(and (not (gt score 100)) has_key)"],
|
|
45
|
-
["!a = b", "(not (eq a b))"],
|
|
46
|
-
["not a = b or not c", "(or (not (eq a b)) (not c))"],
|
|
47
|
-
["(not a) = b", "(eq (not a) b)"],
|
|
48
|
-
["a = (not b)", "(eq a (not b))"],
|
|
49
|
-
["not (seen \"intro\") or count >= 3", "(or (not (seen \"intro\")) (gte count 3))"],
|
|
50
|
-
])("precedence: %s", (input, expected) => {
|
|
51
|
-
expect(rewriteInfixCondition(input)).toBe(expected);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test.each([
|
|
55
|
-
["(a or b) and c", "(and (or a b) c)"],
|
|
56
|
-
["not (a = 1)", "(not (eq a 1))"],
|
|
57
|
-
["((a))", null],
|
|
58
|
-
["((a or b)) and c", "(and (or a b) c)"],
|
|
59
|
-
["(score > 100) = true", "(eq (gt score 100) true)"],
|
|
60
|
-
])("grouping: %s", (input, expected) => {
|
|
61
|
-
expect(rewriteInfixCondition(input)).toBe(expected);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test.each([
|
|
65
|
-
["name = 'Bob'", "(eq name 'Bob')"],
|
|
66
|
-
["name = \"a > b and c\"", "(eq name \"a > b and c\")"],
|
|
67
|
-
["done = true", "(eq done true)"],
|
|
68
|
-
["x = null", "(eq x null)"],
|
|
69
|
-
["score > 1.5", "(gt score 1.5)"],
|
|
70
|
-
["player.score > 10", "(gt player.score 10)"],
|
|
71
|
-
["[my score] > 10", "(gt [my score] 10)"],
|
|
72
|
-
["@index > 0", "(gt @index 0)"],
|
|
73
|
-
["(length \"rounds\") > 0", "(gt (length \"rounds\") 0)"],
|
|
74
|
-
["(contains \"inventory\" \"key\") and door = \"open\"", "(and (contains \"inventory\" \"key\") (eq door \"open\"))"],
|
|
75
|
-
["(and a b) or c", "(or (and a b) c)"],
|
|
76
|
-
["(at (array \"a\" \"b\")) or x", "(or (at (array \"a\" \"b\")) x)"],
|
|
77
|
-
["(get (concat a b)) = 1", "(eq (get (concat a b)) 1)"],
|
|
78
|
-
["(lookup obj key=1) = 2", "(eq (lookup obj key=1) 2)"],
|
|
79
|
-
["(seen (x)) and y", "(and (seen x) y)"],
|
|
80
|
-
])("operands: %s", (input, expected) => {
|
|
81
|
-
expect(rewriteInfixCondition(input)).toBe(expected);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test.each([
|
|
85
|
-
"has_key",
|
|
86
|
-
" has_key ",
|
|
87
|
-
"(eq gender \"male\")",
|
|
88
|
-
"(and (gt score 100) has_key)",
|
|
89
|
-
"(seen \"intro\")",
|
|
90
|
-
"(at (array \"start\" \"other\"))",
|
|
91
|
-
"x includeZero=true",
|
|
92
|
-
"(gt (length \"rounds\") 0)",
|
|
93
|
-
"\"string\"",
|
|
94
|
-
"42",
|
|
95
|
-
])("passes through prefix syntax: %s", (input) => {
|
|
96
|
-
expect(rewriteInfixCondition(input)).toBeNull();
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
test("not with prefix syntax is equivalent", () => {
|
|
100
|
-
expect(rewriteInfixCondition("(not game_over)")).toBe("(not game_over)");
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test("infix inside a helper's arguments", () => {
|
|
104
|
-
expect(rewriteInfixCondition("(foo (a > 1))")).toBe("(foo (gt a 1))");
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test.each([
|
|
108
|
-
["score >", "expected a value after '>'"],
|
|
109
|
-
["a and", "expected a value after 'and'"],
|
|
110
|
-
["a or", "expected a value after 'or'"],
|
|
111
|
-
["not", "expected a value after 'not'"],
|
|
112
|
-
["> 100", "expected a value before '>'"],
|
|
113
|
-
["and b", "expected a value before 'and'"],
|
|
114
|
-
["(a > 1", "expected ')' but found end of expression"],
|
|
115
|
-
["a > 1)", "unexpected ')'"],
|
|
116
|
-
["a > 1 b", "unexpected 'b'"],
|
|
117
|
-
["1 < x < 10", "comparisons can't be chained"],
|
|
118
|
-
["a = = b", "expected a value before '='"],
|
|
119
|
-
["a > () ", "unexpected ')'"],
|
|
120
|
-
["a = not b", "'not' must be inside parentheses here, e.g. (not value)"],
|
|
121
|
-
["a = !b", "'!' must be inside parentheses here, e.g. (!value)"],
|
|
122
|
-
])("error: %s", (input, message) => {
|
|
123
|
-
expect(() => rewriteInfixCondition(input)).toThrow(message);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test("error message includes the condition", () => {
|
|
127
|
-
expect(() => rewriteInfixCondition(" score > ")).toThrow("Invalid condition \"score >\": expected a value after '>'");
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
test("malformed prefix syntax is left for Handlebars to report", () => {
|
|
131
|
-
expect(rewriteInfixCondition("a b")).toBeNull();
|
|
132
|
-
expect(rewriteInfixCondition("(eq a")).toBeNull();
|
|
133
|
-
expect(rewriteInfixCondition("\"unterminated")).toBeNull();
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
describe("rewriteInfixExpressions", () => {
|
|
138
|
-
test("#if", () => {
|
|
139
|
-
expect(rewriteInfixExpressions("{{#if score > 100 and has_key}}yes{{/if}}"))
|
|
140
|
-
.toBe("{{#if (and (gt score 100) has_key)}}yes{{/if}}");
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
test("#unless", () => {
|
|
144
|
-
expect(rewriteInfixExpressions("{{#unless not (seen \"intro\") or count >= 3}}x{{/unless}}"))
|
|
145
|
-
.toBe("{{#unless (or (not (seen \"intro\")) (gte count 3))}}x{{/unless}}");
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test("else if / else unless", () => {
|
|
149
|
-
expect(rewriteInfixExpressions("{{#if a = 1}}one{{else if a = 2}}two{{else unless a = 3}}not three{{else}}three{{/if}}"))
|
|
150
|
-
.toBe("{{#if (eq a 1)}}one{{else if (eq a 2)}}two{{else unless (eq a 3)}}not three{{else}}three{{/if}}");
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
test("whitespace control", () => {
|
|
154
|
-
expect(rewriteInfixExpressions("{{~#if a > 1~}}x{{~else if a < 0 ~}}y{{/if}}"))
|
|
155
|
-
.toBe("{{~#if (gt a 1)~}}x{{~else if (lt a 0)~}}y{{/if}}");
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
test("!x with no space after the helper name", () => {
|
|
159
|
-
expect(rewriteInfixExpressions("{{#if !x}}y{{/if}}")).toBe("{{#if (not x)}}y{{/if}}");
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
test("}} inside a string", () => {
|
|
163
|
-
expect(rewriteInfixExpressions("{{#if x = \"}}\"}}y{{/if}}")).toBe("{{#if (eq x \"}}\")}}y{{/if}}");
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
test("leaves prefix syntax and other text untouched", () => {
|
|
167
|
-
const text = "Score: {{score}}. {{#if (gt score 100)}}High{{/if}} {{#each items}}{{this}}{{/each}} a > b";
|
|
168
|
-
expect(rewriteInfixExpressions(text)).toBe(text);
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
test("leaves escaped mustaches untouched", () => {
|
|
172
|
-
expect(rewriteInfixExpressions("\\{{#if a > 1}}")).toBe("\\{{#if a > 1}}");
|
|
173
|
-
expect(rewriteInfixExpressions("\\\\{{#if a > 1}}x{{/if}}")).toBe("\\\\{{#if (gt a 1)}}x{{/if}}");
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
test("doesn't match helpers that only start with if", () => {
|
|
177
|
-
expect(rewriteInfixExpressions("{{#iffy a > 1}}")).toBe("{{#iffy a > 1}}");
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
test("multiline", () => {
|
|
181
|
-
expect(rewriteInfixExpressions("{{#if a > 1\n and b}}\nx\n{{/if}}")).toBe("{{#if (and (gt a 1) b)}}\nx\n{{/if}}");
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
test("throws for malformed infix", () => {
|
|
185
|
-
expect(() => rewriteInfixExpressions("{{#if score >}}x{{/if}}")).toThrow("Invalid condition \"score >\"");
|
|
186
|
-
});
|
|
187
|
-
});
|