squiffy-runtime 7.0.0-beta.1 → 7.0.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets.d.ts +1 -0
- package/dist/classes.test.d.ts +1 -0
- package/dist/confirmDialog.d.ts +8 -0
- package/dist/events.d.ts +12 -0
- package/dist/every.d.ts +12 -0
- package/dist/every.test.d.ts +1 -0
- package/dist/helpers.test.d.ts +1 -0
- package/dist/numbers.d.ts +3 -0
- package/dist/panels.d.ts +14 -0
- package/dist/panels.test.d.ts +1 -0
- package/dist/paths.d.ts +15 -0
- package/dist/paths.test.d.ts +1 -0
- package/dist/persistent.test.d.ts +1 -0
- package/dist/playerSettings.d.ts +17 -0
- package/dist/preview-host.d.ts +7 -3
- package/dist/preview-host.js +47 -5
- package/dist/preview-host.js.map +2 -2
- package/dist/preview-html.js +1 -1
- package/dist/preview-protocol.d.ts +16 -0
- package/dist/preview.d.ts +1 -0
- package/dist/saveSlots.d.ts +16 -0
- package/dist/saves.test.d.ts +1 -0
- package/dist/savesDialog.d.ts +43 -0
- package/dist/savesDialog.test.d.ts +1 -0
- package/dist/scripts.test.d.ts +1 -0
- package/dist/settings.test.d.ts +1 -0
- package/dist/settingsDialog.d.ts +17 -0
- package/dist/skill.test.d.ts +1 -0
- package/dist/sound.d.ts +35 -0
- package/dist/sound.test.d.ts +1 -0
- package/dist/squiffy.page.css +155 -0
- package/dist/squiffy.preview.html +1469 -136
- package/dist/squiffy.runtime.css +1123 -20
- package/dist/squiffy.runtime.d.ts +13 -1
- package/dist/squiffy.runtime.global.js +144 -79
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +6771 -4424
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/state.d.ts +17 -0
- package/dist/storyLists.d.ts +4 -0
- package/dist/storyLists.test.d.ts +1 -0
- package/dist/style.test.d.ts +1 -0
- package/dist/textProcessor.d.ts +27 -0
- package/dist/transitions.test.d.ts +1 -0
- package/dist/types.d.ts +125 -0
- package/dist/types.plugins.d.ts +1 -0
- package/dist/uiLinks.test.d.ts +1 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.test.d.ts +1 -0
- package/package.json +11 -6
- package/src/__snapshots__/squiffy.runtime.test.ts.snap +22 -22
- package/src/animation.test.ts +44 -0
- package/src/animation.ts +9 -5
- package/src/assets.test.ts +1 -3
- package/src/assets.ts +1 -1
- package/src/classes.test.ts +201 -0
- package/src/confirmDialog.ts +66 -0
- package/src/events.ts +19 -0
- package/src/every.test.ts +25 -0
- package/src/every.ts +94 -0
- package/src/helpers.test.ts +34 -0
- package/src/numbers.ts +21 -0
- package/src/panels.test.ts +225 -0
- package/src/panels.ts +187 -0
- package/src/paths.test.ts +66 -0
- package/src/paths.ts +155 -0
- package/src/persistent.test.ts +155 -0
- package/src/playerSettings.ts +128 -0
- package/src/pluginManager.ts +1 -0
- package/src/plugins/animate.ts +28 -4
- package/src/plugins/live.ts +18 -5
- package/src/plugins/plugins.test.ts +93 -6
- package/src/plugins/replaceLabel.ts +7 -3
- package/src/preview-host.test.ts +97 -1
- package/src/preview-host.ts +71 -10
- package/src/preview-protocol.ts +25 -3
- package/src/preview.main.ts +1 -0
- package/src/preview.test.ts +203 -4
- package/src/preview.ts +178 -12
- package/src/saveSlots.ts +159 -0
- package/src/saves.test.ts +199 -0
- package/src/savesDialog.test.ts +290 -0
- package/src/savesDialog.ts +499 -0
- package/src/scripts.test.ts +130 -0
- package/src/settings.test.ts +233 -0
- package/src/settingsDialog.ts +160 -0
- package/src/skill.test.ts +67 -0
- package/src/sound.test.ts +273 -0
- package/src/sound.ts +207 -0
- package/src/squiffy.page.css +155 -0
- package/src/squiffy.runtime.css +1123 -20
- package/src/squiffy.runtime.test.ts +2806 -44
- package/src/squiffy.runtime.ts +1111 -64
- package/src/state.ts +172 -11
- package/src/storyLists.test.ts +134 -0
- package/src/storyLists.ts +57 -0
- package/src/style.test.ts +89 -0
- package/src/textProcessor.ts +739 -58
- package/src/transitions.test.ts +343 -0
- package/src/types.plugins.ts +3 -0
- package/src/types.ts +209 -1
- package/src/uiLinks.test.ts +156 -0
- package/src/updater.ts +26 -8
- package/src/utils.test.ts +60 -0
- package/src/utils.ts +46 -14
- package/vite.config.ts +3 -4
- package/vite.preview.config.ts +16 -5
- package/dist/infixExpressions.d.ts +0 -2
- package/src/infixExpressions.test.ts +0 -187
- package/src/infixExpressions.ts +0 -323
- /package/dist/{infixExpressions.test.d.ts → animation.test.d.ts} +0 -0
package/src/preview-host.test.ts
CHANGED
|
@@ -31,20 +31,114 @@ describe("preview host", () => {
|
|
|
31
31
|
]);
|
|
32
32
|
|
|
33
33
|
host.update("x");
|
|
34
|
+
host.update("y", ["body { color: red; }"]);
|
|
34
35
|
host.restart();
|
|
35
36
|
host.back();
|
|
36
37
|
expect(sent.slice(2)).toEqual([
|
|
37
38
|
{ channel: PREVIEW_CHANNEL, type: "update", js: "x" },
|
|
39
|
+
{ channel: PREVIEW_CHANNEL, type: "update", js: "y", css: ["body { color: red; }"] },
|
|
38
40
|
{ channel: PREVIEW_CHANNEL, type: "restart" },
|
|
39
41
|
{ channel: PREVIEW_CHANNEL, type: "back" },
|
|
40
42
|
]);
|
|
41
43
|
});
|
|
42
44
|
|
|
45
|
+
test("sends the theme when the page is ready, and again if it reloads", () => {
|
|
46
|
+
const { channel, sent, receive } = fakeChannel();
|
|
47
|
+
const host = createPreviewHost(channel);
|
|
48
|
+
host.setTheme("light");
|
|
49
|
+
host.setTheme("dark");
|
|
50
|
+
expect(sent).toEqual([]);
|
|
51
|
+
|
|
52
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
53
|
+
expect(sent).toEqual([{ channel: PREVIEW_CHANNEL, type: "theme", theme: "dark" }]);
|
|
54
|
+
|
|
55
|
+
host.setTheme("system");
|
|
56
|
+
expect(sent.slice(1)).toEqual([{ channel: PREVIEW_CHANNEL, type: "theme", theme: "system" }]);
|
|
57
|
+
|
|
58
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
59
|
+
expect(sent.slice(2)).toEqual([{ channel: PREVIEW_CHANNEL, type: "theme", theme: "system" }]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("sends mute when the page is ready, and again if it reloads (#444)", () => {
|
|
63
|
+
const { channel, sent, receive } = fakeChannel();
|
|
64
|
+
const host = createPreviewHost(channel, { onUsesSound: vi.fn() });
|
|
65
|
+
host.setMuted(true);
|
|
66
|
+
expect(sent).toEqual([]);
|
|
67
|
+
|
|
68
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
69
|
+
expect(sent).toEqual([{ channel: PREVIEW_CHANNEL, type: "muted", muted: true }]);
|
|
70
|
+
|
|
71
|
+
host.setMuted(false);
|
|
72
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
73
|
+
expect(sent.slice(1)).toEqual([{ channel: PREVIEW_CHANNEL, type: "muted", muted: false }]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("tells the host whether the story uses sound (#444)", () => {
|
|
77
|
+
const { channel, receive } = fakeChannel();
|
|
78
|
+
const onUsesSound = vi.fn();
|
|
79
|
+
createPreviewHost(channel, { onUsesSound });
|
|
80
|
+
receive({ channel: PREVIEW_CHANNEL, type: "usesSound", usesSound: true });
|
|
81
|
+
expect(onUsesSound).toHaveBeenCalledWith(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("reloads the page before loading a story after one with scripts, sending the assets again (#528)", () => {
|
|
85
|
+
const { channel, sent, receive } = fakeChannel();
|
|
86
|
+
const onReady = vi.fn();
|
|
87
|
+
const host = createPreviewHost(channel, { onReady });
|
|
88
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
89
|
+
expect(onReady).toHaveBeenCalledOnce();
|
|
90
|
+
const assets = { urls: { "a.png": "https://example.com/a.png" } };
|
|
91
|
+
host.setAssets(assets);
|
|
92
|
+
host.load({ js: "one", scripts: ["document.addEventListener('squiffy:ready', () => {});"] });
|
|
93
|
+
sent.length = 0;
|
|
94
|
+
|
|
95
|
+
// Its scripts could have left things behind, so the page reloads first
|
|
96
|
+
host.setAssets(assets);
|
|
97
|
+
host.load({ js: "two" });
|
|
98
|
+
expect(sent).toEqual([
|
|
99
|
+
{ channel: PREVIEW_CHANNEL, type: "assets", assets },
|
|
100
|
+
{ channel: PREVIEW_CHANNEL, type: "reload" },
|
|
101
|
+
]);
|
|
102
|
+
sent.length = 0;
|
|
103
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
104
|
+
expect(sent).toEqual([
|
|
105
|
+
{ channel: PREVIEW_CHANNEL, type: "assets", assets },
|
|
106
|
+
{ channel: PREVIEW_CHANNEL, type: "load", story: { js: "two" } },
|
|
107
|
+
]);
|
|
108
|
+
// The host asked for it, so it isn't a reload that lost the story
|
|
109
|
+
expect(onReady).toHaveBeenCalledOnce();
|
|
110
|
+
|
|
111
|
+
// That story had no scripts, so the next one loads without a reload
|
|
112
|
+
sent.length = 0;
|
|
113
|
+
host.load({ js: "three" });
|
|
114
|
+
expect(sent).toEqual([{ channel: PREVIEW_CHANNEL, type: "load", story: { js: "three" } }]);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test("a page that reloads by itself is new, so the next story doesn't reload it (#528)", () => {
|
|
118
|
+
const { channel, sent, receive } = fakeChannel();
|
|
119
|
+
const host = createPreviewHost(channel);
|
|
120
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
121
|
+
host.load({ js: "one", scripts: ["x"] });
|
|
122
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
123
|
+
sent.length = 0;
|
|
124
|
+
host.load({ js: "two" });
|
|
125
|
+
expect(sent).toEqual([{ channel: PREVIEW_CHANNEL, type: "load", story: { js: "two" } }]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("only the last of the loads sent before the page is ready is sent", () => {
|
|
129
|
+
const { channel, sent, receive } = fakeChannel();
|
|
130
|
+
const host = createPreviewHost(channel);
|
|
131
|
+
host.load({ js: "one", scripts: ["x"] });
|
|
132
|
+
host.load({ js: "two" });
|
|
133
|
+
receive({ channel: PREVIEW_CHANNEL, type: "ready" });
|
|
134
|
+
expect(sent).toEqual([{ channel: PREVIEW_CHANNEL, type: "load", story: { js: "two" } }]);
|
|
135
|
+
});
|
|
136
|
+
|
|
43
137
|
test("passes messages from the page to handlers, ignoring anything else", () => {
|
|
44
138
|
const { channel, receive } = fakeChannel();
|
|
45
139
|
const handlers = {
|
|
46
140
|
onReady: vi.fn(), onLoaded: vi.fn(), onAttributeSet: vi.fn(), onCanGoBackChanged: vi.fn(),
|
|
47
|
-
onNavigated: vi.fn(), onRuntimeError: vi.fn(),
|
|
141
|
+
onNavigated: vi.fn(), onThemeFixed: vi.fn(), onRuntimeError: vi.fn(),
|
|
48
142
|
};
|
|
49
143
|
const host = createPreviewHost(channel, handlers);
|
|
50
144
|
receive({ type: "navigated", section: "not ours" });
|
|
@@ -56,6 +150,7 @@ describe("preview host", () => {
|
|
|
56
150
|
{ type: "attributeSet", name: "score", value: 2 },
|
|
57
151
|
{ type: "canGoBackChanged", canGoBack: true },
|
|
58
152
|
{ type: "navigated", section: "s", passage: "p" },
|
|
153
|
+
{ type: "themeFixed", fixed: true },
|
|
59
154
|
{ type: "runtimeError", message: "oops", stack: "at x" },
|
|
60
155
|
]) {
|
|
61
156
|
receive({ channel: PREVIEW_CHANNEL, ...message });
|
|
@@ -66,6 +161,7 @@ describe("preview host", () => {
|
|
|
66
161
|
expect(handlers.onCanGoBackChanged).toHaveBeenCalledWith(true);
|
|
67
162
|
expect(handlers.onNavigated).toHaveBeenCalledOnce();
|
|
68
163
|
expect(handlers.onNavigated).toHaveBeenCalledWith("s", "p");
|
|
164
|
+
expect(handlers.onThemeFixed).toHaveBeenCalledWith(true);
|
|
69
165
|
expect(handlers.onRuntimeError).toHaveBeenCalledWith("oops", "at x");
|
|
70
166
|
|
|
71
167
|
host.dispose();
|
package/src/preview-host.ts
CHANGED
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
// await preview.load({ js: await result.getJs() }, { startAt: { section: "kitchen" } });
|
|
13
13
|
|
|
14
14
|
import {
|
|
15
|
-
Envelope, HostMessage, isEnvelope, PREVIEW_CHANNEL, PreviewAssets, PreviewMessage, PreviewStory, StartAt,
|
|
15
|
+
Envelope, HostMessage, isEnvelope, PREVIEW_CHANNEL, PreviewAssets, PreviewMessage, PreviewStory, PreviewTheme, StartAt,
|
|
16
16
|
} from "./preview-protocol.js";
|
|
17
17
|
|
|
18
|
-
export type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, StartAt } from "./preview-protocol.js";
|
|
18
|
+
export type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, PreviewTheme, StartAt } from "./preview-protocol.js";
|
|
19
19
|
export { PREVIEW_CHANNEL } from "./preview-protocol.js";
|
|
20
20
|
|
|
21
21
|
// How the host talks to the preview page
|
|
@@ -31,6 +31,10 @@ export interface PreviewHandlers {
|
|
|
31
31
|
onAttributeSet?(name: string, value: unknown): void;
|
|
32
32
|
onCanGoBackChanged?(canGoBack: boolean): void;
|
|
33
33
|
onNavigated?(section: string, passage?: string): void;
|
|
34
|
+
// Whether the story's own CSS sets its color-scheme, so setTheme() makes no difference
|
|
35
|
+
onThemeFixed?(fixed: boolean): void;
|
|
36
|
+
// Whether the story plays music or sounds, so a mute button is worth showing
|
|
37
|
+
onUsesSound?(usesSound: boolean): void;
|
|
34
38
|
onRuntimeError?(message: string, stack?: string): void;
|
|
35
39
|
}
|
|
36
40
|
|
|
@@ -39,12 +43,17 @@ export interface PreviewHost {
|
|
|
39
43
|
readonly ready: Promise<void>;
|
|
40
44
|
// Starts a story, replacing any that's running
|
|
41
45
|
load(story: PreviewStory, options?: { startAt?: StartAt }): void;
|
|
42
|
-
// Hot-updates the running story, keeping its state
|
|
43
|
-
|
|
46
|
+
// Hot-updates the running story, keeping its state. css, if given, replaces the story's own
|
|
47
|
+
// stylesheets; its scripts only change with load().
|
|
48
|
+
update(js: string, css?: string[]): void;
|
|
44
49
|
restart(): void;
|
|
45
50
|
back(): void;
|
|
46
51
|
// Sets the assets the story can use. Send these before load().
|
|
47
52
|
setAssets(assets: PreviewAssets): void;
|
|
53
|
+
// Shows the story in light or dark mode. It's kept if the page reloads.
|
|
54
|
+
setTheme(theme: PreviewTheme): void;
|
|
55
|
+
// Mutes the story's music and sounds. It's kept for the next story loaded, and if the page reloads.
|
|
56
|
+
setMuted(muted: boolean): void;
|
|
48
57
|
dispose(): void;
|
|
49
58
|
}
|
|
50
59
|
|
|
@@ -52,6 +61,8 @@ export interface PreviewHost {
|
|
|
52
61
|
// story runs with an opaque origin: it can't reach the host page, its storage or its cookies.
|
|
53
62
|
export function setUpPreviewFrame(iframe: HTMLIFrameElement, previewHtml: string) {
|
|
54
63
|
iframe.setAttribute("sandbox", "allow-scripts");
|
|
64
|
+
// So a story's music can start when the author starts it from the host page (#444)
|
|
65
|
+
iframe.setAttribute("allow", "autoplay");
|
|
55
66
|
iframe.srcdoc = previewHtml;
|
|
56
67
|
}
|
|
57
68
|
|
|
@@ -73,6 +84,15 @@ export function iframeChannel(iframe: HTMLIFrameElement): PreviewChannel {
|
|
|
73
84
|
export function createPreviewHost(channel: PreviewChannel, handlers: PreviewHandlers = {}): PreviewHost {
|
|
74
85
|
let isReady = false;
|
|
75
86
|
let queue: HostMessage[] = [];
|
|
87
|
+
let theme: PreviewTheme | undefined;
|
|
88
|
+
let muted = false;
|
|
89
|
+
// The assets last sent, to send again after the host has reloaded the page
|
|
90
|
+
let assets: PreviewAssets | undefined;
|
|
91
|
+
// Whether the story last loaded had scripts of its own, so the page needs reloading before
|
|
92
|
+
// the next one: what they did, such as listening for squiffy:ready, can't be undone (#528)
|
|
93
|
+
let hadScripts = false;
|
|
94
|
+
// Whether the host is waiting for the page to reload, which it asked for
|
|
95
|
+
let reloading = false;
|
|
76
96
|
let resolveReady: () => void;
|
|
77
97
|
const ready = new Promise<void>(resolve => { resolveReady = resolve; });
|
|
78
98
|
|
|
@@ -87,13 +107,24 @@ export function createPreviewHost(channel: PreviewChannel, handlers: PreviewHand
|
|
|
87
107
|
switch (message.type) {
|
|
88
108
|
case "ready": {
|
|
89
109
|
// Sent again if the page reloads, which loses its story, so onReady is
|
|
90
|
-
// where a host can load it again
|
|
110
|
+
// where a host can load it again. A reload the host asked for, to load a
|
|
111
|
+
// story, already has the story to send, with the assets that the page lost.
|
|
112
|
+
const requested = reloading;
|
|
113
|
+
reloading = false;
|
|
91
114
|
isReady = true;
|
|
92
|
-
|
|
115
|
+
if (theme) send({ type: "theme", theme });
|
|
116
|
+
if (muted) send({ type: "muted", muted });
|
|
117
|
+
let queued = queue;
|
|
93
118
|
queue = [];
|
|
119
|
+
if (requested && assets) {
|
|
120
|
+
queued = queued.filter(m => m.type != "assets");
|
|
121
|
+
send({ type: "assets", assets });
|
|
122
|
+
}
|
|
123
|
+
// The page is new, so only a story about to be loaded can have left anything behind
|
|
124
|
+
hadScripts = queued.some(m => m.type == "load" && !!m.story.scripts?.length);
|
|
94
125
|
for (const m of queued) send(m);
|
|
95
126
|
resolveReady();
|
|
96
|
-
handlers.onReady?.();
|
|
127
|
+
if (!requested) handlers.onReady?.();
|
|
97
128
|
break;
|
|
98
129
|
}
|
|
99
130
|
case "loaded":
|
|
@@ -108,6 +139,12 @@ export function createPreviewHost(channel: PreviewChannel, handlers: PreviewHand
|
|
|
108
139
|
case "navigated":
|
|
109
140
|
handlers.onNavigated?.(message.section, message.passage);
|
|
110
141
|
break;
|
|
142
|
+
case "themeFixed":
|
|
143
|
+
handlers.onThemeFixed?.(message.fixed);
|
|
144
|
+
break;
|
|
145
|
+
case "usesSound":
|
|
146
|
+
handlers.onUsesSound?.(message.usesSound);
|
|
147
|
+
break;
|
|
111
148
|
case "runtimeError":
|
|
112
149
|
handlers.onRuntimeError?.(message.message, message.stack);
|
|
113
150
|
break;
|
|
@@ -116,11 +153,35 @@ export function createPreviewHost(channel: PreviewChannel, handlers: PreviewHand
|
|
|
116
153
|
|
|
117
154
|
return {
|
|
118
155
|
ready,
|
|
119
|
-
load: (story, options = {}) =>
|
|
120
|
-
|
|
156
|
+
load: (story, options = {}) => {
|
|
157
|
+
if (!isReady) {
|
|
158
|
+
// A load that hasn't been sent yet is replaced by this one, and the page it goes to is new
|
|
159
|
+
queue = queue.filter(m => m.type != "load");
|
|
160
|
+
}
|
|
161
|
+
else if (hadScripts) {
|
|
162
|
+
channel.send({ channel: PREVIEW_CHANNEL, type: "reload" });
|
|
163
|
+
isReady = false;
|
|
164
|
+
reloading = true;
|
|
165
|
+
}
|
|
166
|
+
hadScripts = !!story.scripts?.length;
|
|
167
|
+
send({ type: "load", story, ...(options.startAt ? { startAt: options.startAt } : {}) });
|
|
168
|
+
},
|
|
169
|
+
update: (js, css) => send({ type: "update", js, ...(css ? { css } : {}) }),
|
|
121
170
|
restart: () => send({ type: "restart" }),
|
|
122
171
|
back: () => send({ type: "back" }),
|
|
123
|
-
setAssets:
|
|
172
|
+
setAssets: value => {
|
|
173
|
+
assets = value;
|
|
174
|
+
send({ type: "assets", assets });
|
|
175
|
+
},
|
|
176
|
+
setTheme: value => {
|
|
177
|
+
theme = value;
|
|
178
|
+
// Sent when the page is ready, and again if it reloads
|
|
179
|
+
if (isReady) send({ type: "theme", theme });
|
|
180
|
+
},
|
|
181
|
+
setMuted: value => {
|
|
182
|
+
muted = value;
|
|
183
|
+
if (isReady) send({ type: "muted", muted });
|
|
184
|
+
},
|
|
124
185
|
dispose: () => {
|
|
125
186
|
stop();
|
|
126
187
|
queue = [];
|
package/src/preview-protocol.ts
CHANGED
|
@@ -14,6 +14,9 @@ export interface PreviewStory {
|
|
|
14
14
|
// starts "export const story".
|
|
15
15
|
js: string;
|
|
16
16
|
// The contents of the story's own stylesheets and scripts (from @import of .css and .js files).
|
|
17
|
+
// A stylesheet's url()s that are paths, such as its fonts' files, are paths from the story's
|
|
18
|
+
// folder, like the assets', so a host should change any that are from the stylesheet's own
|
|
19
|
+
// folder. The preview swaps them for the assets' URLs.
|
|
17
20
|
css?: string[];
|
|
18
21
|
scripts?: string[];
|
|
19
22
|
}
|
|
@@ -27,24 +30,43 @@ export interface PreviewAssets {
|
|
|
27
30
|
urls?: Record<string, string>;
|
|
28
31
|
}
|
|
29
32
|
|
|
33
|
+
// "system" follows the reader's system setting, like an exported story
|
|
34
|
+
export type PreviewTheme = "light" | "dark" | "system";
|
|
35
|
+
|
|
30
36
|
export type HostMessage =
|
|
31
37
|
// Starts a story, replacing any that's running. Send assets first.
|
|
32
38
|
| { type: "load"; story: PreviewStory; startAt?: StartAt }
|
|
33
|
-
// Hot-updates the running story, keeping its state
|
|
34
|
-
|
|
39
|
+
// Hot-updates the running story, keeping its state. css, if given, replaces the story's own
|
|
40
|
+
// stylesheets. Its scripts can't be replaced in a running story: that needs a load.
|
|
41
|
+
| { type: "update"; js: string; css?: string[] }
|
|
35
42
|
| { type: "restart" }
|
|
36
43
|
| { type: "back" }
|
|
37
44
|
// Replaces the assets the story can use
|
|
38
|
-
| { type: "assets"; assets: PreviewAssets }
|
|
45
|
+
| { type: "assets"; assets: PreviewAssets }
|
|
46
|
+
// Shows the story in light or dark mode, e.g. to match the host's own theme. A story's own
|
|
47
|
+
// CSS can still override it, e.g. with ":root { color-scheme: light; }".
|
|
48
|
+
| { type: "theme"; theme: PreviewTheme }
|
|
49
|
+
// Mutes or unmutes the story's music and sounds (#444)
|
|
50
|
+
| { type: "muted"; muted: boolean }
|
|
51
|
+
// Reloads the page, which then sends "ready" again. The host sends this before loading a story
|
|
52
|
+
// after one that had scripts, which can leave things behind, such as a squiffy:ready listener
|
|
53
|
+
// that would hear about the next story too (#528).
|
|
54
|
+
| { type: "reload" };
|
|
39
55
|
|
|
40
56
|
export type PreviewMessage =
|
|
41
57
|
// The page has loaded and is listening
|
|
42
58
|
| { type: "ready" }
|
|
43
59
|
// A story from a load message has started
|
|
44
60
|
| { type: "loaded" }
|
|
61
|
+
// Whether the story's own CSS sets its color-scheme, e.g. ":root { color-scheme: light; }",
|
|
62
|
+
// so the theme message makes no difference. Sent each time a story is loaded, before "loaded".
|
|
63
|
+
| { type: "themeFixed"; fixed: boolean }
|
|
45
64
|
// An attribute was set. Internal attributes, whose names start with "_", aren't sent.
|
|
46
65
|
| { type: "attributeSet"; name: string; value: unknown }
|
|
47
66
|
| { type: "canGoBackChanged"; canGoBack: boolean }
|
|
67
|
+
// Whether the story plays music or sounds (#444), so the host can show a mute button. Sent
|
|
68
|
+
// each time a story is loaded, before "loaded", and when a sound plays.
|
|
69
|
+
| { type: "usesSound"; usesSound: boolean }
|
|
48
70
|
// A section or passage was shown, or Back went to one
|
|
49
71
|
| { type: "navigated"; section: string; passage?: string }
|
|
50
72
|
// An error from the story's JavaScript or the runtime
|
package/src/preview.main.ts
CHANGED
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 { 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) => {
|
|
35
|
-
const result = await compile({ script });
|
|
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
|
};
|
|
@@ -70,7 +70,11 @@ describe("preview", () => {
|
|
|
70
70
|
|
|
71
71
|
expect(element.textContent).toContain("Welcome.");
|
|
72
72
|
expect(messages).toEqual([
|
|
73
|
+
{ type: "themeFixed", fixed: false },
|
|
74
|
+
{ type: "usesSound", usesSound: false },
|
|
73
75
|
{ type: "canGoBackChanged", canGoBack: false },
|
|
76
|
+
// The story-wide turn count (#377), which isn't internal, so it's reported
|
|
77
|
+
{ type: "attributeSet", name: "turns", value: 0 },
|
|
74
78
|
{ type: "attributeSet", name: "score", value: 1 },
|
|
75
79
|
{ type: "navigated", section: "start" },
|
|
76
80
|
{ type: "loaded" },
|
|
@@ -80,6 +84,9 @@ describe("preview", () => {
|
|
|
80
84
|
element.querySelector<HTMLElement>("a[data-passage='look']")!.click();
|
|
81
85
|
await vi.waitFor(() => expect(messages.some(m => m.type == "navigated")).toBe(true));
|
|
82
86
|
expect(messages).toEqual([
|
|
87
|
+
{ type: "attributeSet", name: "turns", value: 1 },
|
|
88
|
+
// Where the player was before (#56), which isn't internal either
|
|
89
|
+
{ type: "attributeSet", name: "previous", value: "start" },
|
|
83
90
|
{ type: "attributeSet", name: "looked", value: true },
|
|
84
91
|
{ type: "canGoBackChanged", canGoBack: true },
|
|
85
92
|
{ type: "navigated", section: "start", passage: "look" },
|
|
@@ -92,6 +99,46 @@ describe("preview", () => {
|
|
|
92
99
|
expect(messages).toContainEqual({ type: "canGoBackChanged", canGoBack: false });
|
|
93
100
|
});
|
|
94
101
|
|
|
102
|
+
test("reports a story with sound, mutes it, and stops it when another story is loaded (#444)", async () => {
|
|
103
|
+
const played: { src: string; muted: boolean; paused: boolean }[] = [];
|
|
104
|
+
vi.stubGlobal("Audio", class {
|
|
105
|
+
muted = false;
|
|
106
|
+
paused = true;
|
|
107
|
+
volume = 1;
|
|
108
|
+
loop = false;
|
|
109
|
+
preload = "";
|
|
110
|
+
constructor(public src = "") {
|
|
111
|
+
played.push(this);
|
|
112
|
+
}
|
|
113
|
+
play() {
|
|
114
|
+
this.paused = false;
|
|
115
|
+
return Promise.resolve();
|
|
116
|
+
}
|
|
117
|
+
pause() {
|
|
118
|
+
this.paused = true;
|
|
119
|
+
}
|
|
120
|
+
addEventListener() {}
|
|
121
|
+
});
|
|
122
|
+
try {
|
|
123
|
+
const { preview, messages } = setUp();
|
|
124
|
+
await preview.receive({ type: "muted", muted: true });
|
|
125
|
+
await preview.receive({ type: "load", story: { js: await compileJs("@music theme.mp3\nHello.") } });
|
|
126
|
+
expect(messages).toContainEqual({ type: "usesSound", usesSound: true });
|
|
127
|
+
const music = played.filter(a => !a.paused);
|
|
128
|
+
expect(music.map(a => a.muted)).toEqual([true]);
|
|
129
|
+
|
|
130
|
+
await preview.receive({ type: "muted", muted: false });
|
|
131
|
+
expect(music[0].muted).toBe(false);
|
|
132
|
+
|
|
133
|
+
await preview.receive({ type: "load", story: { js: await compileJs("Hello.") } });
|
|
134
|
+
expect(played.filter(a => !a.paused)).toEqual([]);
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
vi.unstubAllGlobals();
|
|
138
|
+
vi.stubGlobal("crypto", { randomUUID: () => "00000000-0000-0000-0000-000000000000" });
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
95
142
|
test("starts at a section or a passage", async () => {
|
|
96
143
|
const { preview, messages, element } = setUp();
|
|
97
144
|
const js = await compileJs(script);
|
|
@@ -139,12 +186,157 @@ describe("preview", () => {
|
|
|
139
186
|
expect(element.textContent).toContain("Welcome.");
|
|
140
187
|
});
|
|
141
188
|
|
|
189
|
+
test("hot updates replace the story's stylesheets, keeping its state", async () => {
|
|
190
|
+
const { preview, element, messages } = setUp();
|
|
191
|
+
const js = await compileJs(script);
|
|
192
|
+
const styles = () => [...document.head.querySelectorAll("style")].map(s => s.textContent);
|
|
193
|
+
const fixed = () => messages.filter(m => m.type == "themeFixed");
|
|
194
|
+
await preview.receive({ type: "load", story: { js, css: ["body { color: red; }", "p { margin: 0; }"] } });
|
|
195
|
+
element.querySelector<HTMLElement>("a[data-section='cellar']")!.click();
|
|
196
|
+
await vi.waitFor(() => expect(element.textContent).toContain("It's dark"));
|
|
197
|
+
|
|
198
|
+
messages.length = 0;
|
|
199
|
+
await preview.receive({ type: "update", js, css: ["body { color: blue; }"] });
|
|
200
|
+
// After the preview's own (empty) theme style
|
|
201
|
+
expect(styles()).toEqual(["", "body { color: blue; }"]);
|
|
202
|
+
expect(element.textContent).toContain("It's dark");
|
|
203
|
+
expect(fixed()).toEqual([{ type: "themeFixed", fixed: false }]);
|
|
204
|
+
|
|
205
|
+
// A stylesheet that sets color-scheme
|
|
206
|
+
messages.length = 0;
|
|
207
|
+
await preview.receive({ type: "update", js, css: [":root { color-scheme: light; }"] });
|
|
208
|
+
expect(fixed()).toEqual([{ type: "themeFixed", fixed: true }]);
|
|
209
|
+
|
|
210
|
+
// The same stylesheets again, or none given, change nothing
|
|
211
|
+
messages.length = 0;
|
|
212
|
+
await preview.receive({ type: "update", js, css: [":root { color-scheme: light; }"] });
|
|
213
|
+
await preview.receive({ type: "update", js });
|
|
214
|
+
expect(styles()).toEqual(["", ":root { color-scheme: light; }"]);
|
|
215
|
+
expect(fixed()).toEqual([]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test("swaps the url()s in the story's stylesheets for its assets' URLs", async () => {
|
|
219
|
+
const { preview } = setUp();
|
|
220
|
+
const js = await compileJs(script);
|
|
221
|
+
const css = `@font-face { src: url("fonts/a.woff2") format("woff2"), url(missing.woff); }
|
|
222
|
+
body { background: url('./bg.png'), url(https://example.com/x.png); }`;
|
|
223
|
+
const style = () => [...document.head.querySelectorAll("style")][1].textContent;
|
|
224
|
+
await preview.receive({ type: "assets", assets: { files: { "fonts/a.woff2": new Uint8Array([1]), "bg.png": new Uint8Array([2]) } } });
|
|
225
|
+
await preview.receive({ type: "load", story: { js, css: [css] } });
|
|
226
|
+
expect(style()).toBe(`@font-face { src: url("blob:preview/0") format("woff2"), url(missing.woff); }
|
|
227
|
+
body { background: url("blob:preview/1"), url(https://example.com/x.png); }`);
|
|
228
|
+
|
|
229
|
+
// New assets go to the stylesheets that are already there
|
|
230
|
+
await preview.receive({ type: "assets", assets: { files: { "fonts/a.woff2": new Uint8Array([3]) } } });
|
|
231
|
+
expect(style()).toContain("src: url(\"blob:preview/2\")");
|
|
232
|
+
expect(style()).toContain("url('./bg.png')");
|
|
233
|
+
// The same stylesheet again changes nothing
|
|
234
|
+
await preview.receive({ type: "update", js, css: [css] });
|
|
235
|
+
expect(style()).toContain("src: url(\"blob:preview/2\")");
|
|
236
|
+
});
|
|
237
|
+
|
|
142
238
|
test("adds the story's stylesheets, replacing the previous story's", async () => {
|
|
143
239
|
const { preview } = setUp();
|
|
144
240
|
const js = await compileJs(script);
|
|
145
241
|
await preview.receive({ type: "load", story: { js, css: ["body { color: red; }"] } });
|
|
146
242
|
await preview.receive({ type: "load", story: { js, css: ["body { color: blue; }"] } });
|
|
147
|
-
|
|
243
|
+
// After the preview's own (empty) theme style
|
|
244
|
+
expect([...document.head.querySelectorAll("style")].map(s => s.textContent)).toEqual(["", "body { color: blue; }"]);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("sets the theme before the story's stylesheets, so they can override it", async () => {
|
|
248
|
+
const { preview } = setUp();
|
|
249
|
+
const js = await compileJs(script);
|
|
250
|
+
await preview.receive({ type: "load", story: { js, css: [":root { color-scheme: light; }"] } });
|
|
251
|
+
await preview.receive({ type: "theme", theme: "dark" });
|
|
252
|
+
const styles = () => [...document.head.querySelectorAll("style")].map(s => s.textContent);
|
|
253
|
+
expect(styles()).toEqual([":root { color-scheme: dark; }", ":root { color-scheme: light; }"]);
|
|
254
|
+
|
|
255
|
+
await preview.receive({ type: "theme", theme: "system" });
|
|
256
|
+
expect(styles()[0]).toBe("");
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
test("says whether the story's stylesheets set its color-scheme", async () => {
|
|
260
|
+
const { preview, messages } = setUp();
|
|
261
|
+
const js = await compileJs(script);
|
|
262
|
+
const fixed = () => messages.filter(m => m.type == "themeFixed");
|
|
263
|
+
|
|
264
|
+
await preview.receive({ type: "theme", theme: "dark" });
|
|
265
|
+
await preview.receive({ type: "load", story: { js, css: ["body { color: red; }"] } });
|
|
266
|
+
expect(fixed()).toEqual([{ type: "themeFixed", fixed: false }]);
|
|
267
|
+
|
|
268
|
+
messages.length = 0;
|
|
269
|
+
await preview.receive({ type: "load", story: { js, css: [":root { color-scheme: light; }"] } });
|
|
270
|
+
expect(fixed()).toEqual([{ type: "themeFixed", fixed: true }]);
|
|
271
|
+
// Finding out doesn't change the theme the host set
|
|
272
|
+
expect(document.head.querySelector("style")!.textContent).toBe(":root { color-scheme: dark; }");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
test("shows the story's panels, and shows them again when they change", async () => {
|
|
276
|
+
const { preview, element } = setUp();
|
|
277
|
+
const panelScript = "[[]]:\n\n[status]:\nScore: {{score}}\n\n" + script;
|
|
278
|
+
await preview.receive({ type: "load", story: { js: await compileJs(panelScript, [{ passage: "status", label: "Status", position: "side" }]) } });
|
|
279
|
+
expect(element.querySelector(".squiffy-preview-header button.squiffy-panel-button")!.textContent).toBe("Status");
|
|
280
|
+
expect(element.querySelector(".squiffy-preview-body > aside section")!.innerHTML).toBe("<p>Score: 1</p>");
|
|
281
|
+
|
|
282
|
+
await preview.receive({ type: "update", js: await compileJs(panelScript, [{ passage: "status", label: "Score" }]) });
|
|
283
|
+
expect(element.querySelector("aside")).toBeNull();
|
|
284
|
+
expect([...element.querySelectorAll("button.squiffy-panel-button")].map(b => b.textContent)).toEqual(["Score"]);
|
|
285
|
+
|
|
286
|
+
// A new story has no panels left over
|
|
287
|
+
await preview.receive({ type: "load", story: { js: await compileJs(script) } });
|
|
288
|
+
expect(element.querySelector("button.squiffy-panel-button")).toBeNull();
|
|
289
|
+
expect(document.querySelectorAll("dialog.squiffy-panel-dialog")).toHaveLength(0);
|
|
290
|
+
});
|
|
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([]);
|
|
148
340
|
});
|
|
149
341
|
|
|
150
342
|
test("reports errors", async () => {
|
|
@@ -158,6 +350,13 @@ describe("preview", () => {
|
|
|
158
350
|
expect(messages).toMatchObject([{ type: "runtimeError", message: "Bad story" }]);
|
|
159
351
|
});
|
|
160
352
|
|
|
353
|
+
test("reloads the page when the host asks, as it does before the next story after one with scripts (#528)", async () => {
|
|
354
|
+
const reload = vi.fn();
|
|
355
|
+
const preview = createPreview({ element: document.getElementById("squiffy")!, send: () => {}, loadModule, reload });
|
|
356
|
+
await preview.receive({ type: "reload" });
|
|
357
|
+
expect(reload).toHaveBeenCalledOnce();
|
|
358
|
+
});
|
|
359
|
+
|
|
161
360
|
test("ignores messages for a story that hasn't loaded", async () => {
|
|
162
361
|
const { preview, messages } = setUp();
|
|
163
362
|
await preview.receive({ type: "restart" });
|