squiffy-runtime 6.0.1 → 7.0.0-beta.1
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 +2 -0
- package/dist/assets.test.d.ts +1 -0
- package/dist/events.d.ts +4 -0
- package/dist/pluginManager.d.ts +2 -1
- package/dist/preview-host.d.ts +29 -0
- package/dist/preview-host.js +83 -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 +53 -0
- package/dist/preview.d.ts +14 -0
- package/dist/preview.main.d.ts +1 -0
- package/dist/preview.test.d.ts +1 -0
- package/dist/squiffy.preview.html +298 -0
- package/dist/squiffy.runtime.global.js +62 -57
- package/dist/squiffy.runtime.global.js.map +1 -1
- package/dist/squiffy.runtime.js +3146 -3069
- package/dist/squiffy.runtime.js.map +1 -1
- package/dist/types.d.ts +12 -1
- package/dist/types.plugins.d.ts +1 -0
- package/package.json +20 -11
- package/src/assets.test.ts +227 -0
- package/src/assets.ts +92 -0
- package/src/events.ts +1 -0
- package/src/pluginManager.ts +5 -1
- package/src/plugins/animate.ts +6 -5
- package/src/preview-host.test.ts +75 -0
- package/src/preview-host.ts +129 -0
- package/src/preview-protocol.ts +57 -0
- package/src/preview.main.ts +33 -0
- package/src/preview.test.ts +168 -0
- package/src/preview.ts +165 -0
- package/src/squiffy.runtime.ts +25 -6
- package/src/types.plugins.ts +4 -0
- package/src/types.ts +17 -1
- package/vite.preview.config.ts +72 -0
package/dist/assets.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/events.d.ts
CHANGED
|
@@ -9,6 +9,10 @@ export type SquiffyEventMap = {
|
|
|
9
9
|
canGoBackChanged: {
|
|
10
10
|
canGoBack: boolean;
|
|
11
11
|
};
|
|
12
|
+
navigated: {
|
|
13
|
+
section: string;
|
|
14
|
+
passage?: string;
|
|
15
|
+
};
|
|
12
16
|
};
|
|
13
17
|
export type SquiffyEventHandler<E extends keyof SquiffyEventMap> = (payload: SquiffyEventMap[E]) => void;
|
|
14
18
|
export declare class Emitter<Events extends Record<string, any>> {
|
package/dist/pluginManager.d.ts
CHANGED
|
@@ -12,11 +12,12 @@ export declare class PluginManager {
|
|
|
12
12
|
getSectionText: (name: string) => string | null;
|
|
13
13
|
getPassageText: (name: string) => string | null;
|
|
14
14
|
processText: (text: string, inline: boolean) => string;
|
|
15
|
+
resolveAssetsInHtml: (html: string) => string;
|
|
15
16
|
addTransition: (fn: () => Promise<void>) => void;
|
|
16
17
|
emitter: Emitter<SquiffyEventMap>;
|
|
17
18
|
plugins: SquiffyPlugin[];
|
|
18
19
|
animation: Animation;
|
|
19
|
-
constructor(outputElement: HTMLElement, textProcessor: TextProcessor, state: State, linkHandler: LinkHandler, getSectionText: (name: string) => string | null, getPassageText: (name: string) => string | null, processText: (text: string, inline: boolean) => string, addTransition: (fn: () => Promise<void>) => void, animation: Animation, emitter: Emitter<SquiffyEventMap
|
|
20
|
+
constructor(outputElement: HTMLElement, textProcessor: TextProcessor, state: State, linkHandler: LinkHandler, getSectionText: (name: string) => string | null, getPassageText: (name: string) => string | null, processText: (text: string, inline: boolean) => string, addTransition: (fn: () => Promise<void>) => void, animation: Animation, emitter: Emitter<SquiffyEventMap>, resolveAssetsInHtml: (html: string) => string);
|
|
20
21
|
add(plugin: SquiffyPlugin): void | Promise<void>;
|
|
21
22
|
onWrite(el: HTMLElement): void;
|
|
22
23
|
onLoad(): void;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Envelope, HostMessage, PreviewAssets, PreviewStory, StartAt } from './preview-protocol.js';
|
|
2
|
+
export type { HostMessage, PreviewAssets, PreviewMessage, PreviewStory, StartAt } from './preview-protocol.js';
|
|
3
|
+
export { PREVIEW_CHANNEL } from './preview-protocol.js';
|
|
4
|
+
export interface PreviewChannel {
|
|
5
|
+
send(message: Envelope<HostMessage>): void;
|
|
6
|
+
onMessage(handler: (data: unknown) => void): () => void;
|
|
7
|
+
}
|
|
8
|
+
export interface PreviewHandlers {
|
|
9
|
+
onReady?(): void;
|
|
10
|
+
onLoaded?(): void;
|
|
11
|
+
onAttributeSet?(name: string, value: unknown): void;
|
|
12
|
+
onCanGoBackChanged?(canGoBack: boolean): void;
|
|
13
|
+
onNavigated?(section: string, passage?: string): void;
|
|
14
|
+
onRuntimeError?(message: string, stack?: string): void;
|
|
15
|
+
}
|
|
16
|
+
export interface PreviewHost {
|
|
17
|
+
readonly ready: Promise<void>;
|
|
18
|
+
load(story: PreviewStory, options?: {
|
|
19
|
+
startAt?: StartAt;
|
|
20
|
+
}): void;
|
|
21
|
+
update(js: string): void;
|
|
22
|
+
restart(): void;
|
|
23
|
+
back(): void;
|
|
24
|
+
setAssets(assets: PreviewAssets): void;
|
|
25
|
+
dispose(): void;
|
|
26
|
+
}
|
|
27
|
+
export declare function setUpPreviewFrame(iframe: HTMLIFrameElement, previewHtml: string): void;
|
|
28
|
+
export declare function iframeChannel(iframe: HTMLIFrameElement): PreviewChannel;
|
|
29
|
+
export declare function createPreviewHost(channel: PreviewChannel, handlers?: PreviewHandlers): PreviewHost;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// src/preview-protocol.ts
|
|
2
|
+
var PREVIEW_CHANNEL = "squiffy-preview";
|
|
3
|
+
var isEnvelope = (data) => typeof data == "object" && data !== null && data.channel === PREVIEW_CHANNEL && typeof data.type == "string";
|
|
4
|
+
|
|
5
|
+
// src/preview-host.ts
|
|
6
|
+
function setUpPreviewFrame(iframe, previewHtml) {
|
|
7
|
+
iframe.setAttribute("sandbox", "allow-scripts");
|
|
8
|
+
iframe.srcdoc = previewHtml;
|
|
9
|
+
}
|
|
10
|
+
function iframeChannel(iframe) {
|
|
11
|
+
return {
|
|
12
|
+
// The page has an opaque origin, so there's no origin to target
|
|
13
|
+
send: (message) => iframe.contentWindow?.postMessage(message, "*"),
|
|
14
|
+
onMessage(handler) {
|
|
15
|
+
const listener = (event) => {
|
|
16
|
+
if (event.source === iframe.contentWindow) handler(event.data);
|
|
17
|
+
};
|
|
18
|
+
window.addEventListener("message", listener);
|
|
19
|
+
return () => window.removeEventListener("message", listener);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function createPreviewHost(channel, handlers = {}) {
|
|
24
|
+
let isReady = false;
|
|
25
|
+
let queue = [];
|
|
26
|
+
let resolveReady;
|
|
27
|
+
const ready = new Promise((resolve) => {
|
|
28
|
+
resolveReady = resolve;
|
|
29
|
+
});
|
|
30
|
+
const send = (message) => {
|
|
31
|
+
if (isReady) channel.send({ channel: PREVIEW_CHANNEL, ...message });
|
|
32
|
+
else queue.push(message);
|
|
33
|
+
};
|
|
34
|
+
const stop = channel.onMessage((data) => {
|
|
35
|
+
if (!isEnvelope(data)) return;
|
|
36
|
+
const message = data;
|
|
37
|
+
switch (message.type) {
|
|
38
|
+
case "ready": {
|
|
39
|
+
isReady = true;
|
|
40
|
+
const queued = queue;
|
|
41
|
+
queue = [];
|
|
42
|
+
for (const m of queued) send(m);
|
|
43
|
+
resolveReady();
|
|
44
|
+
handlers.onReady?.();
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case "loaded":
|
|
48
|
+
handlers.onLoaded?.();
|
|
49
|
+
break;
|
|
50
|
+
case "attributeSet":
|
|
51
|
+
handlers.onAttributeSet?.(message.name, message.value);
|
|
52
|
+
break;
|
|
53
|
+
case "canGoBackChanged":
|
|
54
|
+
handlers.onCanGoBackChanged?.(message.canGoBack);
|
|
55
|
+
break;
|
|
56
|
+
case "navigated":
|
|
57
|
+
handlers.onNavigated?.(message.section, message.passage);
|
|
58
|
+
break;
|
|
59
|
+
case "runtimeError":
|
|
60
|
+
handlers.onRuntimeError?.(message.message, message.stack);
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
return {
|
|
65
|
+
ready,
|
|
66
|
+
load: (story, options = {}) => send({ type: "load", story, ...options.startAt ? { startAt: options.startAt } : {} }),
|
|
67
|
+
update: (js) => send({ type: "update", js }),
|
|
68
|
+
restart: () => send({ type: "restart" }),
|
|
69
|
+
back: () => send({ type: "back" }),
|
|
70
|
+
setAssets: (assets) => send({ type: "assets", assets }),
|
|
71
|
+
dispose: () => {
|
|
72
|
+
stop();
|
|
73
|
+
queue = [];
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export {
|
|
78
|
+
PREVIEW_CHANNEL,
|
|
79
|
+
createPreviewHost,
|
|
80
|
+
iframeChannel,
|
|
81
|
+
setUpPreviewFrame
|
|
82
|
+
};
|
|
83
|
+
//# sourceMappingURL=preview-host.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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;AA6CxB,IAAM,aAAa,CAA6B,SACnD,OAAO,QAAQ,YAAY,SAAS,QAAS,KAA+B,YAAY,mBACrF,OAAQ,KAA4B,QAAQ;;;ACJ5C,SAAS,kBAAkB,QAA2B,aAAqB;AAC9E,SAAO,aAAa,WAAW,eAAe;AAC9C,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,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