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/dist/assets.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/events.d.ts
CHANGED
|
@@ -13,6 +13,17 @@ export type SquiffyEventMap = {
|
|
|
13
13
|
section: string;
|
|
14
14
|
passage?: string;
|
|
15
15
|
};
|
|
16
|
+
sound: Record<string, never>;
|
|
17
|
+
storyUpdated: Record<string, never>;
|
|
18
|
+
restartRequested: {
|
|
19
|
+
confirm: boolean;
|
|
20
|
+
};
|
|
21
|
+
savesRequested: Record<string, never>;
|
|
22
|
+
settingsRequested: Record<string, never>;
|
|
23
|
+
settingChanged: {
|
|
24
|
+
name: string;
|
|
25
|
+
value: unknown;
|
|
26
|
+
};
|
|
16
27
|
};
|
|
17
28
|
export type SquiffyEventHandler<E extends keyof SquiffyEventMap> = (payload: SquiffyEventMap[E]) => void;
|
|
18
29
|
export declare class Emitter<Events extends Record<string, any>> {
|
|
@@ -20,5 +31,6 @@ export declare class Emitter<Events extends Record<string, any>> {
|
|
|
20
31
|
on<E extends keyof Events>(event: E, handler: (p: Events[E]) => void): () => void;
|
|
21
32
|
off<E extends keyof Events>(event: E, handler: (p: Events[E]) => void): void;
|
|
22
33
|
once<E extends keyof Events>(event: E, handler: (p: Events[E]) => void): () => void;
|
|
34
|
+
has<E extends keyof Events>(event: E): boolean;
|
|
23
35
|
emit<E extends keyof Events>(event: E, payload: Events[E]): void;
|
|
24
36
|
}
|
package/dist/every.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare function captureEveryBodies(text: string): string;
|
|
2
|
+
export declare const everySelector = ".squiffy-every:not([data-every-stopped])";
|
|
3
|
+
export declare const EVERY_TICK = "squiffyEvery";
|
|
4
|
+
export declare class EveryTimers {
|
|
5
|
+
private getRoot;
|
|
6
|
+
private tick;
|
|
7
|
+
private timers;
|
|
8
|
+
constructor(getRoot: () => HTMLElement | undefined, tick: (el: HTMLElement) => boolean);
|
|
9
|
+
sync(): void;
|
|
10
|
+
stopAll(): void;
|
|
11
|
+
private schedule;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/panels.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Panel, SquiffyApi } from './types.js';
|
|
2
|
+
export interface PanelsOptions {
|
|
3
|
+
panels: Panel[];
|
|
4
|
+
buttons: HTMLElement;
|
|
5
|
+
side?: HTMLElement;
|
|
6
|
+
strings?: Partial<Record<string, string>>;
|
|
7
|
+
container?: HTMLElement;
|
|
8
|
+
}
|
|
9
|
+
export interface Panels {
|
|
10
|
+
refresh: () => void;
|
|
11
|
+
open: (passage: string) => void;
|
|
12
|
+
dispose: () => void;
|
|
13
|
+
}
|
|
14
|
+
export declare function createPanels(api: SquiffyApi, options: PanelsOptions): Panels;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/paths.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function checkSegment(segment: string): void;
|
|
2
|
+
export declare function parsePath(name: string): string[];
|
|
3
|
+
export declare function valueAt(value: unknown, segments: string[]): unknown;
|
|
4
|
+
export declare function withValueAt(root: unknown, segments: string[], name: string, value: unknown): unknown;
|
|
5
|
+
export type TargetSegment = {
|
|
6
|
+
name: string;
|
|
7
|
+
} | {
|
|
8
|
+
index: string;
|
|
9
|
+
};
|
|
10
|
+
export interface Target {
|
|
11
|
+
attribute: string;
|
|
12
|
+
segments: TargetSegment[];
|
|
13
|
+
rest: string;
|
|
14
|
+
}
|
|
15
|
+
export declare function readTarget(text: string): Target | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { PlayerSetting, SettingValue } from './types.js';
|
|
2
|
+
export declare class PlayerSettings {
|
|
3
|
+
private getDefinitions;
|
|
4
|
+
private storageKey?;
|
|
5
|
+
private values;
|
|
6
|
+
constructor(getDefinitions: () => PlayerSetting[], storageKey?: string | undefined);
|
|
7
|
+
definitions(): PlayerSetting[];
|
|
8
|
+
private find;
|
|
9
|
+
any(): boolean;
|
|
10
|
+
get(name: string): SettingValue | null;
|
|
11
|
+
all(): Record<string, SettingValue>;
|
|
12
|
+
set(name: string, value: SettingValue): boolean;
|
|
13
|
+
reset(): void;
|
|
14
|
+
classes(): string[];
|
|
15
|
+
private store;
|
|
16
|
+
}
|
|
17
|
+
export declare function defaultValue(setting: PlayerSetting): SettingValue;
|
package/dist/preview-host.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Envelope, HostMessage, PreviewAssets, PreviewStory, StartAt } from './preview-protocol.js';
|
|
2
|
-
export type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, StartAt } from './preview-protocol.js';
|
|
1
|
+
import { Envelope, HostMessage, PreviewAssets, PreviewStory, PreviewTheme, StartAt } from './preview-protocol.js';
|
|
2
|
+
export type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, PreviewTheme, StartAt } from './preview-protocol.js';
|
|
3
3
|
export { PREVIEW_CHANNEL } from './preview-protocol.js';
|
|
4
4
|
export interface PreviewChannel {
|
|
5
5
|
send(message: Envelope<HostMessage>): void;
|
|
@@ -11,6 +11,8 @@ export interface PreviewHandlers {
|
|
|
11
11
|
onAttributeSet?(name: string, value: unknown): void;
|
|
12
12
|
onCanGoBackChanged?(canGoBack: boolean): void;
|
|
13
13
|
onNavigated?(section: string, passage?: string): void;
|
|
14
|
+
onThemeFixed?(fixed: boolean): void;
|
|
15
|
+
onUsesSound?(usesSound: boolean): void;
|
|
14
16
|
onRuntimeError?(message: string, stack?: string): void;
|
|
15
17
|
}
|
|
16
18
|
export interface PreviewHost {
|
|
@@ -18,10 +20,12 @@ export interface PreviewHost {
|
|
|
18
20
|
load(story: PreviewStory, options?: {
|
|
19
21
|
startAt?: StartAt;
|
|
20
22
|
}): void;
|
|
21
|
-
update(js: string): void;
|
|
23
|
+
update(js: string, css?: string[]): void;
|
|
22
24
|
restart(): void;
|
|
23
25
|
back(): void;
|
|
24
26
|
setAssets(assets: PreviewAssets): void;
|
|
27
|
+
setTheme(theme: PreviewTheme): void;
|
|
28
|
+
setMuted(muted: boolean): void;
|
|
25
29
|
dispose(): void;
|
|
26
30
|
}
|
|
27
31
|
export declare function setUpPreviewFrame(iframe: HTMLIFrameElement, previewHtml: string): void;
|
package/dist/preview-host.js
CHANGED
|
@@ -5,6 +5,7 @@ var isEnvelope = (data) => typeof data == "object" && data !== null && data.chan
|
|
|
5
5
|
// src/preview-host.ts
|
|
6
6
|
function setUpPreviewFrame(iframe, previewHtml) {
|
|
7
7
|
iframe.setAttribute("sandbox", "allow-scripts");
|
|
8
|
+
iframe.setAttribute("allow", "autoplay");
|
|
8
9
|
iframe.srcdoc = previewHtml;
|
|
9
10
|
}
|
|
10
11
|
function iframeChannel(iframe) {
|
|
@@ -23,6 +24,11 @@ function iframeChannel(iframe) {
|
|
|
23
24
|
function createPreviewHost(channel, handlers = {}) {
|
|
24
25
|
let isReady = false;
|
|
25
26
|
let queue = [];
|
|
27
|
+
let theme;
|
|
28
|
+
let muted = false;
|
|
29
|
+
let assets;
|
|
30
|
+
let hadScripts = false;
|
|
31
|
+
let reloading = false;
|
|
26
32
|
let resolveReady;
|
|
27
33
|
const ready = new Promise((resolve) => {
|
|
28
34
|
resolveReady = resolve;
|
|
@@ -36,12 +42,21 @@ function createPreviewHost(channel, handlers = {}) {
|
|
|
36
42
|
const message = data;
|
|
37
43
|
switch (message.type) {
|
|
38
44
|
case "ready": {
|
|
45
|
+
const requested = reloading;
|
|
46
|
+
reloading = false;
|
|
39
47
|
isReady = true;
|
|
40
|
-
|
|
48
|
+
if (theme) send({ type: "theme", theme });
|
|
49
|
+
if (muted) send({ type: "muted", muted });
|
|
50
|
+
let queued = queue;
|
|
41
51
|
queue = [];
|
|
52
|
+
if (requested && assets) {
|
|
53
|
+
queued = queued.filter((m) => m.type != "assets");
|
|
54
|
+
send({ type: "assets", assets });
|
|
55
|
+
}
|
|
56
|
+
hadScripts = queued.some((m) => m.type == "load" && !!m.story.scripts?.length);
|
|
42
57
|
for (const m of queued) send(m);
|
|
43
58
|
resolveReady();
|
|
44
|
-
handlers.onReady?.();
|
|
59
|
+
if (!requested) handlers.onReady?.();
|
|
45
60
|
break;
|
|
46
61
|
}
|
|
47
62
|
case "loaded":
|
|
@@ -56,6 +71,12 @@ function createPreviewHost(channel, handlers = {}) {
|
|
|
56
71
|
case "navigated":
|
|
57
72
|
handlers.onNavigated?.(message.section, message.passage);
|
|
58
73
|
break;
|
|
74
|
+
case "themeFixed":
|
|
75
|
+
handlers.onThemeFixed?.(message.fixed);
|
|
76
|
+
break;
|
|
77
|
+
case "usesSound":
|
|
78
|
+
handlers.onUsesSound?.(message.usesSound);
|
|
79
|
+
break;
|
|
59
80
|
case "runtimeError":
|
|
60
81
|
handlers.onRuntimeError?.(message.message, message.stack);
|
|
61
82
|
break;
|
|
@@ -63,11 +84,32 @@ function createPreviewHost(channel, handlers = {}) {
|
|
|
63
84
|
});
|
|
64
85
|
return {
|
|
65
86
|
ready,
|
|
66
|
-
load: (story, options = {}) =>
|
|
67
|
-
|
|
87
|
+
load: (story, options = {}) => {
|
|
88
|
+
if (!isReady) {
|
|
89
|
+
queue = queue.filter((m) => m.type != "load");
|
|
90
|
+
} else if (hadScripts) {
|
|
91
|
+
channel.send({ channel: PREVIEW_CHANNEL, type: "reload" });
|
|
92
|
+
isReady = false;
|
|
93
|
+
reloading = true;
|
|
94
|
+
}
|
|
95
|
+
hadScripts = !!story.scripts?.length;
|
|
96
|
+
send({ type: "load", story, ...options.startAt ? { startAt: options.startAt } : {} });
|
|
97
|
+
},
|
|
98
|
+
update: (js, css) => send({ type: "update", js, ...css ? { css } : {} }),
|
|
68
99
|
restart: () => send({ type: "restart" }),
|
|
69
100
|
back: () => send({ type: "back" }),
|
|
70
|
-
setAssets: (
|
|
101
|
+
setAssets: (value) => {
|
|
102
|
+
assets = value;
|
|
103
|
+
send({ type: "assets", assets });
|
|
104
|
+
},
|
|
105
|
+
setTheme: (value) => {
|
|
106
|
+
theme = value;
|
|
107
|
+
if (isReady) send({ type: "theme", theme });
|
|
108
|
+
},
|
|
109
|
+
setMuted: (value) => {
|
|
110
|
+
muted = value;
|
|
111
|
+
if (isReady) send({ type: "muted", muted });
|
|
112
|
+
},
|
|
71
113
|
dispose: () => {
|
|
72
114
|
stop();
|
|
73
115
|
queue = [];
|
package/dist/preview-host.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/preview-protocol.ts", "../src/preview-host.ts"],
|
|
4
|
-
"sourcesContent": ["// The messages between a host (the editor, the desktop app or a VS Code extension) and the\n// Squiffy preview page, which runs stories in a sandboxed frame or webview. See preview-host.ts\n// for the host side and preview.ts for the page.\n\nimport type { StartAt } from \"./types.js\";\n\nexport type { StartAt } from \"./types.js\";\n\n// Every message carries this, so each side can ignore unrelated messages.\nexport const PREVIEW_CHANNEL = \"squiffy-preview\";\n\nexport interface PreviewStory {\n // The compiled story module: the output of the compiler's getJs() without globalJs, which\n // starts \"export const story\".\n js: string;\n // The contents of the story's own stylesheets and scripts (from @import of .css and .js files).\n css?: string[];\n scripts?: string[];\n}\n\nexport interface PreviewAssets {\n // Asset contents by path, as the story refers to them (relative to the main .squiffy file).\n // The preview makes its own object URLs for these: a sandboxed frame can't load the host's\n // blob URLs.\n files?: Record<string, Blob | ArrayBuffer | Uint8Array>;\n // Or URLs the preview can load as they are, e.g. from VS Code's asWebviewUri.\n urls?: Record<string, string>;\n}\n\nexport type HostMessage =\n // Starts a story, replacing any that's running. Send assets first.\n | { type: \"load\"; story: PreviewStory; startAt?: StartAt }\n // Hot-updates the running story, keeping its state\n | { type: \"update\"; js: string }\n | { type: \"restart\" }\n | { type: \"back\" }\n // Replaces the assets the story can use\n | { type: \"assets\"; assets: PreviewAssets };\n\nexport type PreviewMessage =\n // The page has loaded and is listening\n | { type: \"ready\" }\n // A story from a load message has started\n | { type: \"loaded\" }\n // An attribute was set. Internal attributes, whose names start with \"_\", aren't sent.\n | { type: \"attributeSet\"; name: string; value: unknown }\n | { type: \"canGoBackChanged\"; canGoBack: boolean }\n // A section or passage was shown, or Back went to one\n | { type: \"navigated\"; section: string; passage?: string }\n // An error from the story's JavaScript or the runtime\n | { type: \"runtimeError\"; message: string; stack?: string };\n\nexport type Envelope<T> = T & { channel: typeof PREVIEW_CHANNEL };\n\nexport const isEnvelope = <T extends { type: string }>(data: unknown): data is Envelope<T> =>\n typeof data == \"object\" && data !== null && (data as { channel?: unknown }).channel === PREVIEW_CHANNEL\n && typeof (data as { type?: unknown }).type == \"string\";\n", "// The host side of the Squiffy preview: drives a preview page in a sandboxed iframe (the editor\n// and desktop app) or a VS Code webview. It has no runtime code in it.\n//\n// import { previewHtml } from \"squiffy-runtime/preview-html\";\n// import { createPreviewHost, iframeChannel, setUpPreviewFrame } from \"squiffy-runtime/preview-host\";\n//\n// setUpPreviewFrame(iframe, previewHtml);\n// const preview = createPreviewHost(iframeChannel(iframe), {\n// onNavigated: (section, passage) => ...,\n// });\n// await preview.setAssets({ files: { \"cat.png\": blob } });\n// await preview.load({ js: await result.getJs() }, { startAt: { section: \"kitchen\" } });\n\nimport {\n Envelope, HostMessage, isEnvelope, PREVIEW_CHANNEL, PreviewAssets, PreviewMessage, PreviewStory, StartAt,\n} from \"./preview-protocol.js\";\n\nexport type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, StartAt } from \"./preview-protocol.js\";\nexport { PREVIEW_CHANNEL } from \"./preview-protocol.js\";\n\n// How the host talks to the preview page\nexport interface PreviewChannel {\n send(message: Envelope<HostMessage>): void;\n // Calls handler with each message from the page. Returns a function that stops listening.\n onMessage(handler: (data: unknown) => void): () => void;\n}\n\nexport interface PreviewHandlers {\n onReady?(): void;\n onLoaded?(): void;\n onAttributeSet?(name: string, value: unknown): void;\n onCanGoBackChanged?(canGoBack: boolean): void;\n onNavigated?(section: string, passage?: string): void;\n onRuntimeError?(message: string, stack?: string): void;\n}\n\nexport interface PreviewHost {\n // Resolves when the page has loaded. Messages sent before that are queued.\n readonly ready: Promise<void>;\n // Starts a story, replacing any that's running\n load(story: PreviewStory, options?: { startAt?: StartAt }): void;\n // Hot-updates the running story, keeping its state\n update(js: string): void;\n restart(): void;\n back(): void;\n // Sets the assets the story can use. Send these before load().\n setAssets(assets: PreviewAssets): void;\n dispose(): void;\n}\n\n// Sandboxes an iframe and loads the preview page into it. The sandbox allows scripts only, so the\n// story runs with an opaque origin: it can't reach the host page, its storage or its cookies.\nexport function setUpPreviewFrame(iframe: HTMLIFrameElement, previewHtml: string) {\n iframe.setAttribute(\"sandbox\", \"allow-scripts\");\n iframe.srcdoc = previewHtml;\n}\n\n// A channel to a preview page in an iframe\nexport function iframeChannel(iframe: HTMLIFrameElement): PreviewChannel {\n return {\n // The page has an opaque origin, so there's no origin to target\n send: message => iframe.contentWindow?.postMessage(message, \"*\"),\n onMessage(handler) {\n const listener = (event: MessageEvent) => {\n if (event.source === iframe.contentWindow) handler(event.data);\n };\n window.addEventListener(\"message\", listener);\n return () => window.removeEventListener(\"message\", listener);\n },\n };\n}\n\nexport function createPreviewHost(channel: PreviewChannel, handlers: PreviewHandlers = {}): PreviewHost {\n let isReady = false;\n let queue: HostMessage[] = [];\n let resolveReady: () => void;\n const ready = new Promise<void>(resolve => { resolveReady = resolve; });\n\n const send = (message: HostMessage) => {\n if (isReady) channel.send({ channel: PREVIEW_CHANNEL, ...message });\n else queue.push(message);\n };\n\n const stop = channel.onMessage(data => {\n if (!isEnvelope<PreviewMessage>(data)) return;\n const message = data as Envelope<PreviewMessage>;\n switch (message.type) {\n case \"ready\": {\n // Sent again if the page reloads, which loses its story, so onReady is\n // where a host can load it again\n isReady = true;\n const queued = queue;\n queue = [];\n for (const m of queued) send(m);\n resolveReady();\n handlers.onReady?.();\n break;\n }\n case \"loaded\":\n handlers.onLoaded?.();\n break;\n case \"attributeSet\":\n handlers.onAttributeSet?.(message.name, message.value);\n break;\n case \"canGoBackChanged\":\n handlers.onCanGoBackChanged?.(message.canGoBack);\n break;\n case \"navigated\":\n handlers.onNavigated?.(message.section, message.passage);\n break;\n case \"runtimeError\":\n handlers.onRuntimeError?.(message.message, message.stack);\n break;\n }\n });\n\n return {\n ready,\n load: (story, options = {}) => send({ type: \"load\", story, ...(options.startAt ? { startAt: options.startAt } : {}) }),\n update: js => send({ type: \"update\", js }),\n restart: () => send({ type: \"restart\" }),\n back: () => send({ type: \"back\" }),\n setAssets: assets => send({ type: \"assets\", assets }),\n dispose: () => {\n stop();\n queue = [];\n },\n };\n}\n"],
|
|
5
|
-
"mappings": ";AASO,IAAM,kBAAkB;
|
|
4
|
+
"sourcesContent": ["// The messages between a host (the editor, the desktop app or a VS Code extension) and the\n// Squiffy preview page, which runs stories in a sandboxed frame or webview. See preview-host.ts\n// for the host side and preview.ts for the page.\n\nimport type { StartAt } from \"./types.js\";\n\nexport type { StartAt } from \"./types.js\";\n\n// Every message carries this, so each side can ignore unrelated messages.\nexport const PREVIEW_CHANNEL = \"squiffy-preview\";\n\nexport interface PreviewStory {\n // The compiled story module: the output of the compiler's getJs() without globalJs, which\n // starts \"export const story\".\n js: string;\n // The contents of the story's own stylesheets and scripts (from @import of .css and .js files).\n // A stylesheet's url()s that are paths, such as its fonts' files, are paths from the story's\n // folder, like the assets', so a host should change any that are from the stylesheet's own\n // folder. The preview swaps them for the assets' URLs.\n css?: string[];\n scripts?: string[];\n}\n\nexport interface PreviewAssets {\n // Asset contents by path, as the story refers to them (relative to the main .squiffy file).\n // The preview makes its own object URLs for these: a sandboxed frame can't load the host's\n // blob URLs.\n files?: Record<string, Blob | ArrayBuffer | Uint8Array>;\n // Or URLs the preview can load as they are, e.g. from VS Code's asWebviewUri.\n urls?: Record<string, string>;\n}\n\n// \"system\" follows the reader's system setting, like an exported story\nexport type PreviewTheme = \"light\" | \"dark\" | \"system\";\n\nexport type HostMessage =\n // Starts a story, replacing any that's running. Send assets first.\n | { type: \"load\"; story: PreviewStory; startAt?: StartAt }\n // Hot-updates the running story, keeping its state. css, if given, replaces the story's own\n // stylesheets. Its scripts can't be replaced in a running story: that needs a load.\n | { type: \"update\"; js: string; css?: string[] }\n | { type: \"restart\" }\n | { type: \"back\" }\n // Replaces the assets the story can use\n | { type: \"assets\"; assets: PreviewAssets }\n // Shows the story in light or dark mode, e.g. to match the host's own theme. A story's own\n // CSS can still override it, e.g. with \":root { color-scheme: light; }\".\n | { type: \"theme\"; theme: PreviewTheme }\n // Mutes or unmutes the story's music and sounds (#444)\n | { type: \"muted\"; muted: boolean }\n // Reloads the page, which then sends \"ready\" again. The host sends this before loading a story\n // after one that had scripts, which can leave things behind, such as a squiffy:ready listener\n // that would hear about the next story too (#528).\n | { type: \"reload\" };\n\nexport type PreviewMessage =\n // The page has loaded and is listening\n | { type: \"ready\" }\n // A story from a load message has started\n | { type: \"loaded\" }\n // Whether the story's own CSS sets its color-scheme, e.g. \":root { color-scheme: light; }\",\n // so the theme message makes no difference. Sent each time a story is loaded, before \"loaded\".\n | { type: \"themeFixed\"; fixed: boolean }\n // An attribute was set. Internal attributes, whose names start with \"_\", aren't sent.\n | { type: \"attributeSet\"; name: string; value: unknown }\n | { type: \"canGoBackChanged\"; canGoBack: boolean }\n // Whether the story plays music or sounds (#444), so the host can show a mute button. Sent\n // each time a story is loaded, before \"loaded\", and when a sound plays.\n | { type: \"usesSound\"; usesSound: boolean }\n // A section or passage was shown, or Back went to one\n | { type: \"navigated\"; section: string; passage?: string }\n // An error from the story's JavaScript or the runtime\n | { type: \"runtimeError\"; message: string; stack?: string };\n\nexport type Envelope<T> = T & { channel: typeof PREVIEW_CHANNEL };\n\nexport const isEnvelope = <T extends { type: string }>(data: unknown): data is Envelope<T> =>\n typeof data == \"object\" && data !== null && (data as { channel?: unknown }).channel === PREVIEW_CHANNEL\n && typeof (data as { type?: unknown }).type == \"string\";\n", "// The host side of the Squiffy preview: drives a preview page in a sandboxed iframe (the editor\n// and desktop app) or a VS Code webview. It has no runtime code in it.\n//\n// import { previewHtml } from \"squiffy-runtime/preview-html\";\n// import { createPreviewHost, iframeChannel, setUpPreviewFrame } from \"squiffy-runtime/preview-host\";\n//\n// setUpPreviewFrame(iframe, previewHtml);\n// const preview = createPreviewHost(iframeChannel(iframe), {\n// onNavigated: (section, passage) => ...,\n// });\n// await preview.setAssets({ files: { \"cat.png\": blob } });\n// await preview.load({ js: await result.getJs() }, { startAt: { section: \"kitchen\" } });\n\nimport {\n Envelope, HostMessage, isEnvelope, PREVIEW_CHANNEL, PreviewAssets, PreviewMessage, PreviewStory, PreviewTheme, StartAt,\n} from \"./preview-protocol.js\";\n\nexport type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, PreviewTheme, StartAt } from \"./preview-protocol.js\";\nexport { PREVIEW_CHANNEL } from \"./preview-protocol.js\";\n\n// How the host talks to the preview page\nexport interface PreviewChannel {\n send(message: Envelope<HostMessage>): void;\n // Calls handler with each message from the page. Returns a function that stops listening.\n onMessage(handler: (data: unknown) => void): () => void;\n}\n\nexport interface PreviewHandlers {\n onReady?(): void;\n onLoaded?(): void;\n onAttributeSet?(name: string, value: unknown): void;\n onCanGoBackChanged?(canGoBack: boolean): void;\n onNavigated?(section: string, passage?: string): void;\n // Whether the story's own CSS sets its color-scheme, so setTheme() makes no difference\n onThemeFixed?(fixed: boolean): void;\n // Whether the story plays music or sounds, so a mute button is worth showing\n onUsesSound?(usesSound: boolean): void;\n onRuntimeError?(message: string, stack?: string): void;\n}\n\nexport interface PreviewHost {\n // Resolves when the page has loaded. Messages sent before that are queued.\n readonly ready: Promise<void>;\n // Starts a story, replacing any that's running\n load(story: PreviewStory, options?: { startAt?: StartAt }): void;\n // Hot-updates the running story, keeping its state. css, if given, replaces the story's own\n // stylesheets; its scripts only change with load().\n update(js: string, css?: string[]): void;\n restart(): void;\n back(): void;\n // Sets the assets the story can use. Send these before load().\n setAssets(assets: PreviewAssets): void;\n // Shows the story in light or dark mode. It's kept if the page reloads.\n setTheme(theme: PreviewTheme): void;\n // Mutes the story's music and sounds. It's kept for the next story loaded, and if the page reloads.\n setMuted(muted: boolean): void;\n dispose(): void;\n}\n\n// Sandboxes an iframe and loads the preview page into it. The sandbox allows scripts only, so the\n// story runs with an opaque origin: it can't reach the host page, its storage or its cookies.\nexport function setUpPreviewFrame(iframe: HTMLIFrameElement, previewHtml: string) {\n iframe.setAttribute(\"sandbox\", \"allow-scripts\");\n // So a story's music can start when the author starts it from the host page (#444)\n iframe.setAttribute(\"allow\", \"autoplay\");\n iframe.srcdoc = previewHtml;\n}\n\n// A channel to a preview page in an iframe\nexport function iframeChannel(iframe: HTMLIFrameElement): PreviewChannel {\n return {\n // The page has an opaque origin, so there's no origin to target\n send: message => iframe.contentWindow?.postMessage(message, \"*\"),\n onMessage(handler) {\n const listener = (event: MessageEvent) => {\n if (event.source === iframe.contentWindow) handler(event.data);\n };\n window.addEventListener(\"message\", listener);\n return () => window.removeEventListener(\"message\", listener);\n },\n };\n}\n\nexport function createPreviewHost(channel: PreviewChannel, handlers: PreviewHandlers = {}): PreviewHost {\n let isReady = false;\n let queue: HostMessage[] = [];\n let theme: PreviewTheme | undefined;\n let muted = false;\n // The assets last sent, to send again after the host has reloaded the page\n let assets: PreviewAssets | undefined;\n // Whether the story last loaded had scripts of its own, so the page needs reloading before\n // the next one: what they did, such as listening for squiffy:ready, can't be undone (#528)\n let hadScripts = false;\n // Whether the host is waiting for the page to reload, which it asked for\n let reloading = false;\n let resolveReady: () => void;\n const ready = new Promise<void>(resolve => { resolveReady = resolve; });\n\n const send = (message: HostMessage) => {\n if (isReady) channel.send({ channel: PREVIEW_CHANNEL, ...message });\n else queue.push(message);\n };\n\n const stop = channel.onMessage(data => {\n if (!isEnvelope<PreviewMessage>(data)) return;\n const message = data as Envelope<PreviewMessage>;\n switch (message.type) {\n case \"ready\": {\n // Sent again if the page reloads, which loses its story, so onReady is\n // where a host can load it again. A reload the host asked for, to load a\n // story, already has the story to send, with the assets that the page lost.\n const requested = reloading;\n reloading = false;\n isReady = true;\n if (theme) send({ type: \"theme\", theme });\n if (muted) send({ type: \"muted\", muted });\n let queued = queue;\n queue = [];\n if (requested && assets) {\n queued = queued.filter(m => m.type != \"assets\");\n send({ type: \"assets\", assets });\n }\n // The page is new, so only a story about to be loaded can have left anything behind\n hadScripts = queued.some(m => m.type == \"load\" && !!m.story.scripts?.length);\n for (const m of queued) send(m);\n resolveReady();\n if (!requested) handlers.onReady?.();\n break;\n }\n case \"loaded\":\n handlers.onLoaded?.();\n break;\n case \"attributeSet\":\n handlers.onAttributeSet?.(message.name, message.value);\n break;\n case \"canGoBackChanged\":\n handlers.onCanGoBackChanged?.(message.canGoBack);\n break;\n case \"navigated\":\n handlers.onNavigated?.(message.section, message.passage);\n break;\n case \"themeFixed\":\n handlers.onThemeFixed?.(message.fixed);\n break;\n case \"usesSound\":\n handlers.onUsesSound?.(message.usesSound);\n break;\n case \"runtimeError\":\n handlers.onRuntimeError?.(message.message, message.stack);\n break;\n }\n });\n\n return {\n ready,\n load: (story, options = {}) => {\n if (!isReady) {\n // A load that hasn't been sent yet is replaced by this one, and the page it goes to is new\n queue = queue.filter(m => m.type != \"load\");\n }\n else if (hadScripts) {\n channel.send({ channel: PREVIEW_CHANNEL, type: \"reload\" });\n isReady = false;\n reloading = true;\n }\n hadScripts = !!story.scripts?.length;\n send({ type: \"load\", story, ...(options.startAt ? { startAt: options.startAt } : {}) });\n },\n update: (js, css) => send({ type: \"update\", js, ...(css ? { css } : {}) }),\n restart: () => send({ type: \"restart\" }),\n back: () => send({ type: \"back\" }),\n setAssets: value => {\n assets = value;\n send({ type: \"assets\", assets });\n },\n setTheme: value => {\n theme = value;\n // Sent when the page is ready, and again if it reloads\n if (isReady) send({ type: \"theme\", theme });\n },\n setMuted: value => {\n muted = value;\n if (isReady) send({ type: \"muted\", muted });\n },\n dispose: () => {\n stop();\n queue = [];\n },\n };\n}\n"],
|
|
5
|
+
"mappings": ";AASO,IAAM,kBAAkB;AAmExB,IAAM,aAAa,CAA6B,SACnD,OAAO,QAAQ,YAAY,SAAS,QAAS,KAA+B,YAAY,mBACrF,OAAQ,KAA4B,QAAQ;;;ACjB5C,SAAS,kBAAkB,QAA2B,aAAqB;AAC9E,SAAO,aAAa,WAAW,eAAe;AAE9C,SAAO,aAAa,SAAS,UAAU;AACvC,SAAO,SAAS;AACpB;AAGO,SAAS,cAAc,QAA2C;AACrE,SAAO;AAAA;AAAA,IAEH,MAAM,aAAW,OAAO,eAAe,YAAY,SAAS,GAAG;AAAA,IAC/D,UAAU,SAAS;AACf,YAAM,WAAW,CAAC,UAAwB;AACtC,YAAI,MAAM,WAAW,OAAO,cAAe,SAAQ,MAAM,IAAI;AAAA,MACjE;AACA,aAAO,iBAAiB,WAAW,QAAQ;AAC3C,aAAO,MAAM,OAAO,oBAAoB,WAAW,QAAQ;AAAA,IAC/D;AAAA,EACJ;AACJ;AAEO,SAAS,kBAAkB,SAAyB,WAA4B,CAAC,GAAgB;AACpG,MAAI,UAAU;AACd,MAAI,QAAuB,CAAC;AAC5B,MAAI;AACJ,MAAI,QAAQ;AAEZ,MAAI;AAGJ,MAAI,aAAa;AAEjB,MAAI,YAAY;AAChB,MAAI;AACJ,QAAM,QAAQ,IAAI,QAAc,aAAW;AAAE,mBAAe;AAAA,EAAS,CAAC;AAEtE,Q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