@malloy-publisher/sdk 0.18.0 → 0.18.2
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/{RenderedResult-QdYnecnr.es.js → RenderedResult--fYcsY1h.es.js} +2 -2
- package/dist/{ServerProvider-DAVb5QK1.es.js → ServerProvider-nC__R_WW.es.js} +8 -8
- package/dist/builder/index.es.js +3 -3
- package/dist/client/api.d.ts +9 -9
- package/dist/client/index.es.js +1 -1
- package/dist/index.cjs.js +28 -28
- package/dist/index.d.ts +1 -1
- package/dist/index.es.js +2687 -2651
- package/dist/{useCompiledDocument-BbdwlcRj.es.js → useCompiledDocument-X00FD-D6.es.js} +2 -2
- package/dist/utils/dataAppEmbed.d.ts +29 -0
- package/package.json +1 -1
- package/src/components/DataAppViewer/DataAppViewer.spec.tsx +202 -0
- package/src/components/DataAppViewer/DataAppViewer.tsx +68 -5
- package/src/index.ts +7 -1
- package/src/utils/dataAppEmbed.spec.ts +44 -0
- package/src/utils/dataAppEmbed.ts +60 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as S, jsxs as re, Fragment as Bt } from "react/jsx-runtime";
|
|
2
2
|
import { Box as be, Dialog as Ed, DialogTitle as Od, IconButton as Qn, DialogContent as Ad, Typography as Ge, Stack as wt, DialogActions as Fd, Button as st, Tooltip as wn, TextField as Vn, InputAdornment as Wl, Alert as Ld, Autocomplete as Nd, CircularProgress as Hl, FormControl as Ki, Slider as Bd, FormHelperText as Xi, FormControlLabel as Vd, Checkbox as zd, Paper as ni, MenuItem as An, ButtonBase as Ul, Popover as _d, LinearProgress as Wd } from "@mui/material";
|
|
3
|
-
import { bq as Ke, bv as Zn, bw as Hd, br as Jn, bp as Yl, bF as Ud } from "./ServerProvider-
|
|
3
|
+
import { bq as Ke, bv as Zn, bw as Hd, br as Jn, bp as Yl, bF as Ud } from "./ServerProvider-nC__R_WW.es.js";
|
|
4
4
|
import * as x from "react";
|
|
5
5
|
import xt, { useState as Oe, useEffect as He, useRef as Ye, useId as jl, useMemo as Ve, useCallback as et, useContext as er, createContext as Fo, isValidElement as co, cloneElement as uo, Children as Yd, Suspense as jd, lazy as Gd } from "react";
|
|
6
6
|
import { useQueries as Gl, QueryClientContext as qd, useQuery as Kd, useMutation as Xd, keepPreviousData as Qd } from "@tanstack/react-query";
|
|
@@ -23487,7 +23487,7 @@ function mk(e) {
|
|
|
23487
23487
|
`)
|
|
23488
23488
|
};
|
|
23489
23489
|
}
|
|
23490
|
-
const gk = Gd(() => import("./RenderedResult
|
|
23490
|
+
const gk = Gd(() => import("./RenderedResult--fYcsY1h.es.js").then((e) => e.c));
|
|
23491
23491
|
function yk({
|
|
23492
23492
|
result: e,
|
|
23493
23493
|
maxHeight: t,
|
|
@@ -12,6 +12,35 @@ export interface PublisherResizeMessage {
|
|
|
12
12
|
}
|
|
13
13
|
/** Type guard for a {@link PublisherResizeMessage} arriving via postMessage. */
|
|
14
14
|
export declare function isPublisherResizeMessage(data: unknown): data is PublisherResizeMessage;
|
|
15
|
+
/**
|
|
16
|
+
* postMessage `type` a host sends an embedded data app with the reader's light or dark mode: when the frame loads,
|
|
17
|
+
* whenever the reader switches, and whenever the app asks with {@link PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE}.
|
|
18
|
+
* Payload shape is {@link PublisherColorSchemeMessage}. A "system" choice is resolved to light or dark by the host.
|
|
19
|
+
*/
|
|
20
|
+
export declare const PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE = "publisher:color-scheme";
|
|
21
|
+
/**
|
|
22
|
+
* postMessage `type` a data app's runtime sends its host on start, asking for the reader's mode, in case the host
|
|
23
|
+
* spoke before the page was listening. It carries nothing else.
|
|
24
|
+
*/
|
|
25
|
+
export declare const PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE = "publisher:color-scheme-request";
|
|
26
|
+
/**
|
|
27
|
+
* postMessage `type` a data app sends its host once it has drawn the mode it was told. A host may hold a frame
|
|
28
|
+
* light until its page answers, since a page written only for a light background would be unreadable on a dark one.
|
|
29
|
+
* Payload shape is {@link PublisherColorSchemeMessage}.
|
|
30
|
+
*/
|
|
31
|
+
export declare const PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE = "publisher:color-scheme-applied";
|
|
32
|
+
export type PublisherColorScheme = "light" | "dark";
|
|
33
|
+
/** The reader's mode, from the host, or the mode a page answers it drew. */
|
|
34
|
+
export interface PublisherColorSchemeMessage {
|
|
35
|
+
type: typeof PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE | typeof PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE;
|
|
36
|
+
mode: PublisherColorScheme;
|
|
37
|
+
}
|
|
38
|
+
/** Type guard for a page's {@link PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE}. */
|
|
39
|
+
export declare function isPublisherColorSchemeRequest(data: unknown): data is {
|
|
40
|
+
type: typeof PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE;
|
|
41
|
+
};
|
|
42
|
+
/** Type guard for a page's {@link PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE} answer. */
|
|
43
|
+
export declare function isPublisherColorSchemeApplied(data: unknown): data is PublisherColorSchemeMessage;
|
|
15
44
|
/**
|
|
16
45
|
* Derive the Publisher data origin (where static package files are served)
|
|
17
46
|
* from the configured API base URL by stripping the trailing `/api/v0`.
|
package/package.json
CHANGED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { createTheme, ThemeProvider } from "@mui/material";
|
|
5
|
+
import { act, fireEvent, render } from "@testing-library/react";
|
|
6
|
+
import { beforeEach, describe, expect, it, mock } from "bun:test";
|
|
7
|
+
import { type ReactNode, useState } from "react";
|
|
8
|
+
import {
|
|
9
|
+
clearCache,
|
|
10
|
+
mockServerProvider,
|
|
11
|
+
serverWrapper,
|
|
12
|
+
} from "../../../test/serverProvider";
|
|
13
|
+
import {
|
|
14
|
+
PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE,
|
|
15
|
+
PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE,
|
|
16
|
+
PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE,
|
|
17
|
+
} from "../../utils/dataAppEmbed";
|
|
18
|
+
|
|
19
|
+
mockServerProvider({
|
|
20
|
+
dataApps: { listDataApps: async () => ({ data: [] }) },
|
|
21
|
+
});
|
|
22
|
+
const { default: DataAppViewer } = await import("./DataAppViewer");
|
|
23
|
+
|
|
24
|
+
const RESOURCE = "publisher://environments/env/packages/pkg/models/index.html";
|
|
25
|
+
const FRAME_ORIGIN = "http://localhost";
|
|
26
|
+
|
|
27
|
+
function mount(initial: "light" | "dark") {
|
|
28
|
+
// The console's theme, switchable from a test as the console's own toggle switches it.
|
|
29
|
+
let setMode: (m: "light" | "dark") => void = () => {};
|
|
30
|
+
function Themed({ children }: { children: ReactNode }) {
|
|
31
|
+
const [mode, set] = useState(initial);
|
|
32
|
+
setMode = set;
|
|
33
|
+
return (
|
|
34
|
+
<ThemeProvider theme={createTheme({ palette: { mode } })}>
|
|
35
|
+
{children}
|
|
36
|
+
</ThemeProvider>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const wrapper = ({ children }: { children: ReactNode }) =>
|
|
40
|
+
serverWrapper({ children: <Themed>{children}</Themed> });
|
|
41
|
+
const view = render(<DataAppViewer resourceUri={RESOURCE} />, { wrapper });
|
|
42
|
+
const iframe = view.container.querySelector("iframe") as HTMLIFrameElement;
|
|
43
|
+
// happy-dom gives the frame no window of its own: a stand-in that records
|
|
44
|
+
// what the viewer posts, and is the `source` of what the page posts back.
|
|
45
|
+
const posted: unknown[] = [];
|
|
46
|
+
const frameWindow = {
|
|
47
|
+
postMessage: mock((data: unknown, origin: string) =>
|
|
48
|
+
posted.push({ data, origin }),
|
|
49
|
+
),
|
|
50
|
+
};
|
|
51
|
+
Object.defineProperty(iframe, "contentWindow", {
|
|
52
|
+
configurable: true,
|
|
53
|
+
get: () => frameWindow,
|
|
54
|
+
});
|
|
55
|
+
const fromPage = (
|
|
56
|
+
data: unknown,
|
|
57
|
+
origin = FRAME_ORIGIN,
|
|
58
|
+
source: unknown = frameWindow,
|
|
59
|
+
) =>
|
|
60
|
+
act(() => {
|
|
61
|
+
window.dispatchEvent(
|
|
62
|
+
new MessageEvent("message", {
|
|
63
|
+
data,
|
|
64
|
+
origin,
|
|
65
|
+
source: source as Window,
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
const switchTo = (m: "light" | "dark") => act(() => setMode(m));
|
|
70
|
+
return { iframe, posted, fromPage, switchTo };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
describe("DataAppViewer color scheme", () => {
|
|
74
|
+
beforeEach(clearCache);
|
|
75
|
+
|
|
76
|
+
it("sends the console's mode to the page's own origin when the frame loads", () => {
|
|
77
|
+
const { iframe, posted } = mount("dark");
|
|
78
|
+
fireEvent.load(iframe);
|
|
79
|
+
expect(posted).toContainEqual({
|
|
80
|
+
data: { type: PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE, mode: "dark" },
|
|
81
|
+
origin: FRAME_ORIGIN,
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("answers the page's request with the mode", () => {
|
|
86
|
+
const { posted, fromPage } = mount("light");
|
|
87
|
+
fromPage({ type: PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE });
|
|
88
|
+
expect(posted).toContainEqual({
|
|
89
|
+
data: { type: PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE, mode: "light" },
|
|
90
|
+
origin: FRAME_ORIGIN,
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("sends the new mode when the console switches", () => {
|
|
95
|
+
const { posted, switchTo } = mount("light");
|
|
96
|
+
posted.length = 0;
|
|
97
|
+
switchTo("dark");
|
|
98
|
+
expect(posted).toContainEqual({
|
|
99
|
+
data: { type: PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE, mode: "dark" },
|
|
100
|
+
origin: FRAME_ORIGIN,
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("answers no window but the frame's own, even from the frame's origin", () => {
|
|
105
|
+
const { iframe, posted, fromPage } = mount("dark");
|
|
106
|
+
posted.length = 0;
|
|
107
|
+
const other = { postMessage: () => {} };
|
|
108
|
+
fromPage(
|
|
109
|
+
{ type: PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE },
|
|
110
|
+
FRAME_ORIGIN,
|
|
111
|
+
other,
|
|
112
|
+
);
|
|
113
|
+
fromPage(
|
|
114
|
+
{ type: PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE, mode: "dark" },
|
|
115
|
+
FRAME_ORIGIN,
|
|
116
|
+
other,
|
|
117
|
+
);
|
|
118
|
+
expect(posted).toEqual([]);
|
|
119
|
+
expect(iframe.style.colorScheme).toBe("");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("gives the frame the page's colour scheme once the page says it drew it", () => {
|
|
123
|
+
const { iframe, fromPage } = mount("dark");
|
|
124
|
+
expect(iframe.style.colorScheme).toBe("");
|
|
125
|
+
fromPage({
|
|
126
|
+
type: PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE,
|
|
127
|
+
mode: "dark",
|
|
128
|
+
});
|
|
129
|
+
expect(iframe.style.colorScheme).toBe("dark");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("ignores an answer from another origin, and forgets the answer when a new page loads", () => {
|
|
133
|
+
const { iframe, fromPage } = mount("dark");
|
|
134
|
+
fromPage(
|
|
135
|
+
{ type: PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE, mode: "dark" },
|
|
136
|
+
"https://elsewhere.example",
|
|
137
|
+
);
|
|
138
|
+
expect(iframe.style.colorScheme).toBe("");
|
|
139
|
+
fromPage({
|
|
140
|
+
type: PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE,
|
|
141
|
+
mode: "dark",
|
|
142
|
+
});
|
|
143
|
+
expect(iframe.style.colorScheme).toBe("dark");
|
|
144
|
+
act(() => {
|
|
145
|
+
fireEvent.load(iframe);
|
|
146
|
+
});
|
|
147
|
+
expect(iframe.style.colorScheme).toBe("");
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* What a data app's frame may do in the Console: downloads, new tabs, the
|
|
153
|
+
* clipboard and full screen, and never anything over the Console itself.
|
|
154
|
+
*/
|
|
155
|
+
describe("DataAppViewer frame permissions", () => {
|
|
156
|
+
function frame() {
|
|
157
|
+
const { container } = render(
|
|
158
|
+
<DataAppViewer resourceUri="publisher://environments/env/packages/pkg/models/index.html" />,
|
|
159
|
+
{ wrapper: serverWrapper },
|
|
160
|
+
);
|
|
161
|
+
const iframe = container.querySelector("iframe");
|
|
162
|
+
expect(iframe).not.toBeNull();
|
|
163
|
+
return {
|
|
164
|
+
sandbox: (iframe!.getAttribute("sandbox") ?? "").split(/\s+/),
|
|
165
|
+
allow: (iframe!.getAttribute("allow") ?? "")
|
|
166
|
+
.split(";")
|
|
167
|
+
.map((feature) => feature.trim()),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
it("lets the page save a file, copy text, show an element full screen and open a link in a new tab", () => {
|
|
172
|
+
const { sandbox, allow } = frame();
|
|
173
|
+
expect(sandbox).toEqual(
|
|
174
|
+
expect.arrayContaining(["allow-downloads", "allow-popups"]),
|
|
175
|
+
);
|
|
176
|
+
expect(allow).toEqual(
|
|
177
|
+
expect.arrayContaining(["clipboard-write", "fullscreen"]),
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("keeps what the page needs to run, query as the reader and report its size", () => {
|
|
182
|
+
expect(frame().sandbox).toEqual(
|
|
183
|
+
expect.arrayContaining([
|
|
184
|
+
"allow-scripts",
|
|
185
|
+
"allow-same-origin",
|
|
186
|
+
"allow-forms",
|
|
187
|
+
]),
|
|
188
|
+
);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it("grants nothing else: no top navigation, dialogs, clipboard reads, tabs that leave the sandbox or other features", () => {
|
|
192
|
+
const { sandbox, allow } = frame();
|
|
193
|
+
expect([...sandbox].sort()).toEqual([
|
|
194
|
+
"allow-downloads",
|
|
195
|
+
"allow-forms",
|
|
196
|
+
"allow-popups",
|
|
197
|
+
"allow-same-origin",
|
|
198
|
+
"allow-scripts",
|
|
199
|
+
]);
|
|
200
|
+
expect([...allow].sort()).toEqual(["clipboard-write", "fullscreen"]);
|
|
201
|
+
});
|
|
202
|
+
});
|
|
@@ -2,14 +2,25 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
|
|
4
4
|
import OpenInNewIcon from "@mui/icons-material/OpenInNew";
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
Box,
|
|
7
|
+
IconButton,
|
|
8
|
+
Stack,
|
|
9
|
+
Tooltip,
|
|
10
|
+
Typography,
|
|
11
|
+
useTheme,
|
|
12
|
+
} from "@mui/material";
|
|
13
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
7
14
|
import { useQueryWithApiError } from "../../hooks/useQueryWithApiError";
|
|
8
15
|
import { useServer } from "../ServerProvider";
|
|
9
16
|
import { MONO_FONT_FAMILY } from "../styles";
|
|
10
17
|
import { parseResourceUri } from "../../utils/formatting";
|
|
11
18
|
import {
|
|
19
|
+
isPublisherColorSchemeApplied,
|
|
20
|
+
isPublisherColorSchemeRequest,
|
|
12
21
|
isPublisherResizeMessage,
|
|
22
|
+
PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE,
|
|
23
|
+
type PublisherColorScheme,
|
|
13
24
|
packageFileUrl,
|
|
14
25
|
} from "../../utils/dataAppEmbed";
|
|
15
26
|
|
|
@@ -112,6 +123,46 @@ export default function DataAppViewer({ resourceUri }: DataAppViewerProps) {
|
|
|
112
123
|
// within a frame of load.
|
|
113
124
|
const [iframeHeight, setIframeHeight] = useState<number>(120);
|
|
114
125
|
|
|
126
|
+
// The reader's light or dark mode goes to the page on load, on every switch,
|
|
127
|
+
// and whenever its runtime asks. A page that answers it drew that mode gets
|
|
128
|
+
// a frame in the same colour scheme; one that never answers is left as it
|
|
129
|
+
// was, since a page written for a light background is unreadable on a dark
|
|
130
|
+
// one.
|
|
131
|
+
const mode: PublisherColorScheme =
|
|
132
|
+
useTheme().palette.mode === "dark" ? "dark" : "light";
|
|
133
|
+
const modeRef = useRef(mode);
|
|
134
|
+
modeRef.current = mode;
|
|
135
|
+
const [appliedMode, setAppliedMode] = useState<PublisherColorScheme | null>(
|
|
136
|
+
null,
|
|
137
|
+
);
|
|
138
|
+
const frameOrigin = useMemo(() => {
|
|
139
|
+
try {
|
|
140
|
+
return new URL(standaloneUrl, window.location.href).origin;
|
|
141
|
+
} catch {
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
}, [standaloneUrl]);
|
|
145
|
+
const sendMode = useCallback(() => {
|
|
146
|
+
const target = iframeRef.current?.contentWindow;
|
|
147
|
+
if (!target || !frameOrigin) return;
|
|
148
|
+
target.postMessage(
|
|
149
|
+
{ type: PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE, mode: modeRef.current },
|
|
150
|
+
frameOrigin,
|
|
151
|
+
);
|
|
152
|
+
}, [frameOrigin]);
|
|
153
|
+
useEffect(sendMode, [mode, sendMode]);
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
function onMessage(e: MessageEvent) {
|
|
156
|
+
if (e.source !== iframeRef.current?.contentWindow) return;
|
|
157
|
+
if (frameOrigin && e.origin !== frameOrigin) return;
|
|
158
|
+
if (isPublisherColorSchemeRequest(e.data)) sendMode();
|
|
159
|
+
else if (isPublisherColorSchemeApplied(e.data))
|
|
160
|
+
setAppliedMode(e.data.mode);
|
|
161
|
+
}
|
|
162
|
+
window.addEventListener("message", onMessage);
|
|
163
|
+
return () => window.removeEventListener("message", onMessage);
|
|
164
|
+
}, [frameOrigin, sendMode]);
|
|
165
|
+
|
|
115
166
|
useEffect(() => {
|
|
116
167
|
// Fill mode pins the iframe to 100%, so content-height resize messages are
|
|
117
168
|
// irrelevant; skip subscribing (and the per-message re-renders) until the
|
|
@@ -214,12 +265,23 @@ export default function DataAppViewer({ resourceUri }: DataAppViewerProps) {
|
|
|
214
265
|
<iframe
|
|
215
266
|
ref={iframeRef}
|
|
216
267
|
src={standaloneUrl}
|
|
268
|
+
onLoad={() => {
|
|
269
|
+
// A new page has not answered yet.
|
|
270
|
+
setAppliedMode(null);
|
|
271
|
+
sendMode();
|
|
272
|
+
}}
|
|
217
273
|
title={title}
|
|
218
274
|
// allow-same-origin so the iframe can use cookies for the
|
|
219
275
|
// Publisher API. allow-scripts so the dashboard's JS runs.
|
|
220
|
-
// allow-forms so dropdown/filter forms work.
|
|
221
|
-
//
|
|
222
|
-
|
|
276
|
+
// allow-forms so dropdown/filter forms work. Beyond those: saving
|
|
277
|
+
// a file, a link in a new tab, and through `allow` the clipboard
|
|
278
|
+
// and full screen. Intentionally NOT allowing top navigation or
|
|
279
|
+
// modal dialogs, which from another origin is what keeps the
|
|
280
|
+
// Console in charge of its own tab, and NOT letting an opened tab
|
|
281
|
+
// escape the sandbox: in Firefox such a tab can redirect its
|
|
282
|
+
// opener's top window.
|
|
283
|
+
sandbox="allow-scripts allow-same-origin allow-forms allow-downloads allow-popups"
|
|
284
|
+
allow="clipboard-write; fullscreen"
|
|
223
285
|
style={{
|
|
224
286
|
display: "block",
|
|
225
287
|
width: "100%",
|
|
@@ -228,6 +290,7 @@ export default function DataAppViewer({ resourceUri }: DataAppViewerProps) {
|
|
|
228
290
|
// track the content height reported via publisher:resize.
|
|
229
291
|
height: fillViewport ? "100%" : iframeHeight,
|
|
230
292
|
border: 0,
|
|
293
|
+
...(appliedMode ? { colorScheme: appliedMode } : {}),
|
|
231
294
|
}}
|
|
232
295
|
/>
|
|
233
296
|
</Box>
|
package/src/index.ts
CHANGED
|
@@ -20,5 +20,11 @@ export * from "./utils/formatting";
|
|
|
20
20
|
// How a package's own files are addressed. Exported because a host that renders
|
|
21
21
|
// its own "this path is not a model" state has to point at the URL that does
|
|
22
22
|
// serve the file, and this is the one place that string is built.
|
|
23
|
-
export {
|
|
23
|
+
export {
|
|
24
|
+
packageFileUrl,
|
|
25
|
+
PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE,
|
|
26
|
+
PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE,
|
|
27
|
+
PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE,
|
|
28
|
+
type PublisherColorScheme,
|
|
29
|
+
} from "./utils/dataAppEmbed";
|
|
24
30
|
export * from "./constants/docLinks";
|
|
@@ -3,8 +3,13 @@
|
|
|
3
3
|
|
|
4
4
|
import { describe, expect, it } from "bun:test";
|
|
5
5
|
import {
|
|
6
|
+
isPublisherColorSchemeApplied,
|
|
7
|
+
isPublisherColorSchemeRequest,
|
|
6
8
|
isPublisherResizeMessage,
|
|
7
9
|
packageFileUrl,
|
|
10
|
+
PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE,
|
|
11
|
+
PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE,
|
|
12
|
+
PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE,
|
|
8
13
|
PUBLISHER_RESIZE_MESSAGE_TYPE,
|
|
9
14
|
serverBaseUrl,
|
|
10
15
|
} from "./dataAppEmbed";
|
|
@@ -88,3 +93,42 @@ describe("isPublisherResizeMessage", () => {
|
|
|
88
93
|
expect(isPublisherResizeMessage("publisher:resize")).toBe(false);
|
|
89
94
|
});
|
|
90
95
|
});
|
|
96
|
+
|
|
97
|
+
describe("color scheme messages", () => {
|
|
98
|
+
it("accepts a page's request and its light or dark answer", () => {
|
|
99
|
+
expect(
|
|
100
|
+
isPublisherColorSchemeRequest({
|
|
101
|
+
type: PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE,
|
|
102
|
+
}),
|
|
103
|
+
).toBe(true);
|
|
104
|
+
for (const mode of ["light", "dark"]) {
|
|
105
|
+
expect(
|
|
106
|
+
isPublisherColorSchemeApplied({
|
|
107
|
+
type: PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE,
|
|
108
|
+
mode,
|
|
109
|
+
}),
|
|
110
|
+
).toBe(true);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
it("refuses another type, an unknown mode and a non-object", () => {
|
|
114
|
+
expect(
|
|
115
|
+
isPublisherColorSchemeApplied({
|
|
116
|
+
type: PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE,
|
|
117
|
+
mode: "sepia",
|
|
118
|
+
}),
|
|
119
|
+
).toBe(false);
|
|
120
|
+
expect(
|
|
121
|
+
isPublisherColorSchemeApplied({
|
|
122
|
+
type: PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE,
|
|
123
|
+
mode: "dark",
|
|
124
|
+
}),
|
|
125
|
+
).toBe(false);
|
|
126
|
+
expect(isPublisherColorSchemeRequest({ type: "publisher:resize" })).toBe(
|
|
127
|
+
false,
|
|
128
|
+
);
|
|
129
|
+
expect(
|
|
130
|
+
isPublisherColorSchemeRequest("publisher:color-scheme-request"),
|
|
131
|
+
).toBe(false);
|
|
132
|
+
expect(isPublisherColorSchemeApplied(null)).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
@@ -35,6 +35,66 @@ export function isPublisherResizeMessage(
|
|
|
35
35
|
);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* postMessage `type` a host sends an embedded data app with the reader's light or dark mode: when the frame loads,
|
|
40
|
+
* whenever the reader switches, and whenever the app asks with {@link PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE}.
|
|
41
|
+
* Payload shape is {@link PublisherColorSchemeMessage}. A "system" choice is resolved to light or dark by the host.
|
|
42
|
+
*/
|
|
43
|
+
export const PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE = "publisher:color-scheme";
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* postMessage `type` a data app's runtime sends its host on start, asking for the reader's mode, in case the host
|
|
47
|
+
* spoke before the page was listening. It carries nothing else.
|
|
48
|
+
*/
|
|
49
|
+
export const PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE =
|
|
50
|
+
"publisher:color-scheme-request";
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* postMessage `type` a data app sends its host once it has drawn the mode it was told. A host may hold a frame
|
|
54
|
+
* light until its page answers, since a page written only for a light background would be unreadable on a dark one.
|
|
55
|
+
* Payload shape is {@link PublisherColorSchemeMessage}.
|
|
56
|
+
*/
|
|
57
|
+
export const PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE =
|
|
58
|
+
"publisher:color-scheme-applied";
|
|
59
|
+
|
|
60
|
+
export type PublisherColorScheme = "light" | "dark";
|
|
61
|
+
|
|
62
|
+
/** The reader's mode, from the host, or the mode a page answers it drew. */
|
|
63
|
+
export interface PublisherColorSchemeMessage {
|
|
64
|
+
type:
|
|
65
|
+
| typeof PUBLISHER_COLOR_SCHEME_MESSAGE_TYPE
|
|
66
|
+
| typeof PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE;
|
|
67
|
+
mode: PublisherColorScheme;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const isMode = (m: unknown): m is PublisherColorScheme =>
|
|
71
|
+
m === "light" || m === "dark";
|
|
72
|
+
|
|
73
|
+
/** Type guard for a page's {@link PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE}. */
|
|
74
|
+
export function isPublisherColorSchemeRequest(
|
|
75
|
+
data: unknown,
|
|
76
|
+
): data is { type: typeof PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE } {
|
|
77
|
+
return (
|
|
78
|
+
typeof data === "object" &&
|
|
79
|
+
data !== null &&
|
|
80
|
+
(data as { type?: unknown }).type ===
|
|
81
|
+
PUBLISHER_COLOR_SCHEME_REQUEST_MESSAGE_TYPE
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Type guard for a page's {@link PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE} answer. */
|
|
86
|
+
export function isPublisherColorSchemeApplied(
|
|
87
|
+
data: unknown,
|
|
88
|
+
): data is PublisherColorSchemeMessage {
|
|
89
|
+
return (
|
|
90
|
+
typeof data === "object" &&
|
|
91
|
+
data !== null &&
|
|
92
|
+
(data as { type?: unknown }).type ===
|
|
93
|
+
PUBLISHER_COLOR_SCHEME_APPLIED_MESSAGE_TYPE &&
|
|
94
|
+
isMode((data as { mode?: unknown }).mode)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
38
98
|
/**
|
|
39
99
|
* Derive the Publisher data origin (where static package files are served)
|
|
40
100
|
* from the configured API base URL by stripping the trailing `/api/v0`.
|