@peers-app/peers-ui 0.33.24 → 0.34.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/command-palette/command-palette.js +4 -2
- package/dist/components/router.d.ts +1 -0
- package/dist/components/router.js +4 -1
- package/dist/host-shell/host-shell-consumer.js +8 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +7 -1
- package/dist/isolated-screens/config.d.ts +51 -0
- package/dist/isolated-screens/config.js +73 -0
- package/dist/isolated-screens/frame-autostart.d.ts +1 -0
- package/dist/isolated-screens/frame-autostart.js +18 -0
- package/dist/isolated-screens/frame-bridge.d.ts +30 -0
- package/dist/isolated-screens/frame-bridge.js +113 -0
- package/dist/isolated-screens/frame-component.d.ts +6 -0
- package/dist/isolated-screens/frame-component.js +20 -0
- package/dist/isolated-screens/frame-document.d.ts +28 -0
- package/dist/isolated-screens/frame-document.js +77 -0
- package/dist/isolated-screens/frame-policy.d.ts +17 -0
- package/dist/isolated-screens/frame-policy.js +230 -0
- package/dist/isolated-screens/frame-protocol.d.ts +196 -0
- package/dist/isolated-screens/frame-protocol.js +150 -0
- package/dist/isolated-screens/frame-pvars.d.ts +33 -0
- package/dist/isolated-screens/frame-pvars.js +152 -0
- package/dist/isolated-screens/frame-realm.d.ts +12 -0
- package/dist/isolated-screens/frame-realm.js +31 -0
- package/dist/isolated-screens/frame-runtime.d.ts +5 -0
- package/dist/isolated-screens/frame-runtime.js +494 -0
- package/dist/isolated-screens/frame-storage.d.ts +43 -0
- package/dist/isolated-screens/frame-storage.js +85 -0
- package/dist/isolated-screens/index.d.ts +3 -0
- package/dist/isolated-screens/index.js +13 -0
- package/dist/isolated-screens/isolated-screen.d.ts +193 -0
- package/dist/isolated-screens/isolated-screen.js +695 -0
- package/dist/isolated-screens/piece-frames.d.ts +27 -0
- package/dist/isolated-screens/piece-frames.js +351 -0
- package/dist/isolated-screens/piece-reach.d.ts +11 -0
- package/dist/isolated-screens/piece-reach.js +56 -0
- package/dist/isolated-screens/port-channel.d.ts +47 -0
- package/dist/isolated-screens/port-channel.js +153 -0
- package/dist/isolated-screens/routes-sandbox.d.ts +8 -0
- package/dist/isolated-screens/routes-sandbox.js +130 -0
- package/dist/isolated-screens/screen-tab-context.d.ts +4 -0
- package/dist/isolated-screens/screen-tab-context.js +6 -0
- package/dist/isolated-screens/test-ports.d.ts +3 -0
- package/dist/isolated-screens/test-ports.js +21 -0
- package/dist/isolated-screens/wheel-relay.d.ts +48 -0
- package/dist/isolated-screens/wheel-relay.js +186 -0
- package/dist/mentions/mentions-client.js +9 -0
- package/dist/notifications/index.js +9 -5
- package/dist/operator-console/console-copy.d.ts +15 -0
- package/dist/operator-console/console-copy.js +97 -0
- package/dist/operator-console/console-header.d.ts +33 -0
- package/dist/operator-console/console-header.js +67 -0
- package/dist/operator-console/console-input.d.ts +20 -0
- package/dist/operator-console/console-input.js +313 -0
- package/dist/operator-console/console-repl.d.ts +7 -0
- package/dist/operator-console/console-repl.js +108 -0
- package/dist/operator-console/console-scope.d.ts +45 -0
- package/dist/operator-console/console-scope.js +174 -0
- package/dist/operator-console/console-screen.d.ts +8 -0
- package/dist/operator-console/console-screen.js +32 -0
- package/dist/operator-console/console-session.d.ts +106 -0
- package/dist/operator-console/console-session.js +567 -0
- package/dist/operator-console/console-test-host.d.ts +21 -0
- package/dist/operator-console/console-test-host.js +114 -0
- package/dist/operator-console/console-theme.d.ts +41 -0
- package/dist/operator-console/console-theme.js +55 -0
- package/dist/operator-console/console-value.d.ts +27 -0
- package/dist/operator-console/console-value.js +291 -0
- package/dist/operator-console/operator-console-state.js +3 -1
- package/dist/operator-console/operator-console.d.ts +8 -21
- package/dist/operator-console/operator-console.js +8 -61
- package/dist/root-layout/app-shell-height.d.ts +16 -3
- package/dist/root-layout/app-shell-height.js +17 -4
- package/dist/root-layout/host-app-menu.d.ts +17 -0
- package/dist/root-layout/host-app-menu.js +22 -0
- package/dist/root-layout/layouts/full-screen-layout.js +4 -2
- package/dist/system-apps/console.app.d.ts +15 -0
- package/dist/system-apps/console.app.js +25 -0
- package/dist/system-apps/index.d.ts +1 -0
- package/dist/system-apps/index.js +6 -1
- package/dist/tabs-layout/tabs-layout.js +12 -20
- package/dist/tabs-layout/tabs-state.js +39 -16
- package/dist/theme.css +32 -5
- package/dist/ui-contracts/piece-height.d.ts +18 -0
- package/dist/ui-contracts/piece-height.js +15 -0
- package/dist/ui-contracts/piece-host.d.ts +24 -1
- package/dist/ui-contracts/piece-host.js +53 -2
- package/dist/ui-contracts/piece-props.d.ts +16 -0
- package/dist/ui-contracts/piece-props.js +39 -0
- package/dist/ui-contracts/piece-resolution.d.ts +5 -0
- package/dist/ui-contracts/piece-resolution.js +38 -2
- package/dist/ui-contracts/piece.d.ts +2 -7
- package/dist/ui-contracts/piece.js +22 -37
- package/dist/ui-contracts/popover-surface.d.ts +14 -0
- package/dist/ui-contracts/popover-surface.js +32 -3
- package/dist/ui-inspection/dom-actions.d.ts +23 -0
- package/dist/ui-inspection/dom-actions.js +169 -0
- package/dist/ui-inspection/dom-inspector.d.ts +20 -0
- package/dist/ui-inspection/dom-inspector.js +184 -0
- package/dist/ui-inspection/index.d.ts +73 -0
- package/dist/ui-inspection/index.js +147 -0
- package/dist/ui-router/find-route.d.ts +6 -0
- package/dist/ui-router/find-route.js +22 -0
- package/dist/ui-router/routes-loader.js +23 -4
- package/dist/ui-router/runnable-version.js +3 -0
- package/dist/ui-router/ui-loader.d.ts +6 -1
- package/dist/ui-router/ui-loader.js +102 -26
- package/package.json +3 -3
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startFrameRuntime = startFrameRuntime;
|
|
4
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
+
const peers_sdk_1 = require("@peers-app/peers-sdk");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const client_1 = require("react-dom/client");
|
|
8
|
+
const globals_1 = require("../globals");
|
|
9
|
+
const hooks_1 = require("../hooks");
|
|
10
|
+
const global_overlays_1 = require("../root-layout/global-overlays");
|
|
11
|
+
const implement_piece_1 = require("../ui-contracts/implement-piece");
|
|
12
|
+
const piece_height_1 = require("../ui-contracts/piece-height");
|
|
13
|
+
const piece_resolution_1 = require("../ui-contracts/piece-resolution");
|
|
14
|
+
const popover_surface_1 = require("../ui-contracts/popover-surface");
|
|
15
|
+
const ui_inspection_1 = require("../ui-inspection");
|
|
16
|
+
const frame_bridge_1 = require("./frame-bridge");
|
|
17
|
+
const frame_protocol_1 = require("./frame-protocol");
|
|
18
|
+
const wheel_relay_1 = require("./wheel-relay");
|
|
19
|
+
let root;
|
|
20
|
+
let setScreen;
|
|
21
|
+
let pending = { props: {} };
|
|
22
|
+
const uis = new Map();
|
|
23
|
+
const pieces = new Map();
|
|
24
|
+
let loadedVersionId;
|
|
25
|
+
/** Whether this frame draws a piece, whose Escape and wheel go to its embedder. */
|
|
26
|
+
let drawsPiece = false;
|
|
27
|
+
/**
|
|
28
|
+
* Apply the host's theme. The starting document uses the theme attribute for its color scheme
|
|
29
|
+
* and keeps a piece's background transparent across page color updates.
|
|
30
|
+
*/
|
|
31
|
+
function applyTheme(mode) {
|
|
32
|
+
const html = document.documentElement;
|
|
33
|
+
html.setAttribute("data-bs-theme", mode);
|
|
34
|
+
if (drawsPiece)
|
|
35
|
+
return;
|
|
36
|
+
const background = mode === "dark" ? "rgb(33, 37, 41)" : "";
|
|
37
|
+
html.style.backgroundColor = background;
|
|
38
|
+
document.body.style.backgroundColor = background;
|
|
39
|
+
}
|
|
40
|
+
class ScreenErrorBoundary extends react_1.Component {
|
|
41
|
+
state = {};
|
|
42
|
+
static getDerivedStateFromError(error) {
|
|
43
|
+
return { error: error instanceof Error ? error : new Error(String(error)) };
|
|
44
|
+
}
|
|
45
|
+
componentDidCatch(error, info) {
|
|
46
|
+
console.error(`${this.props.packageName}'s screen threw`, error, info.componentStack);
|
|
47
|
+
this.props.onError(this.state.error?.message || String(error));
|
|
48
|
+
}
|
|
49
|
+
render() {
|
|
50
|
+
if (this.state.error) {
|
|
51
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "p-3", children: (0, jsx_runtime_1.jsxs)("div", { className: "alert alert-danger", children: [(0, jsx_runtime_1.jsx)("div", { className: "fw-semibold", children: "This screen stopped working." }), (0, jsx_runtime_1.jsx)("div", { className: "small text-break", children: this.state.error.message }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-sm btn-outline-danger mt-2", onClick: () => this.setState({ error: undefined }), children: "Try again" })] }) }));
|
|
52
|
+
}
|
|
53
|
+
return this.props.children;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Overlays peers-ui registers for itself when it loads (the priority alert, the delegate request
|
|
58
|
+
* dialog). They're the host's, drawn by the host's window, so a frame leaves them out.
|
|
59
|
+
*/
|
|
60
|
+
let peersUIOverlayIds = new Set();
|
|
61
|
+
/** The package's global overlays, drawn inside its own screen until global widgets exist. */
|
|
62
|
+
function FrameOverlays() {
|
|
63
|
+
const [overlays] = (0, hooks_1.useObservable)(global_overlays_1.globalOverlays);
|
|
64
|
+
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: overlays
|
|
65
|
+
.filter(({ id }) => !peersUIOverlayIds.has(id))
|
|
66
|
+
.map(({ id, Component: Overlay }) => ((0, jsx_runtime_1.jsx)(Overlay, {}, id))) }));
|
|
67
|
+
}
|
|
68
|
+
function FrameScreen(props) {
|
|
69
|
+
const [state, setState] = (0, react_1.useState)(pending);
|
|
70
|
+
const [desktop] = (0, hooks_1.useObservable)(globals_1.isDesktop);
|
|
71
|
+
(0, react_1.useEffect)(() => {
|
|
72
|
+
setScreen = setState;
|
|
73
|
+
setState(pending);
|
|
74
|
+
return () => {
|
|
75
|
+
setScreen = undefined;
|
|
76
|
+
};
|
|
77
|
+
}, []);
|
|
78
|
+
(0, react_1.useEffect)(() => {
|
|
79
|
+
if (state.ui)
|
|
80
|
+
props.host.notify(frame_protocol_1.FrameNotice.rendered);
|
|
81
|
+
}, [state.ui, props.host]);
|
|
82
|
+
// A bundle can throw an error with no message: it still failed.
|
|
83
|
+
if (state.error !== undefined) {
|
|
84
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: "p-3", children: (0, jsx_runtime_1.jsxs)("div", { className: "alert alert-danger", children: [(0, jsx_runtime_1.jsxs)("div", { className: "fw-semibold", children: [props.packageName, " couldn't load this screen."] }), (0, jsx_runtime_1.jsx)("div", { className: "small text-break", children: state.error }), state.piece || drawsPiece ? null : ((0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-sm btn-outline-danger mt-2", onClick: () => props.host.notify(frame_protocol_1.FrameNotice.restart), children: "Retry" }))] }) }));
|
|
85
|
+
}
|
|
86
|
+
if (state.piece) {
|
|
87
|
+
return ((0, jsx_runtime_1.jsx)(PieceInFrame, { host: props.host, packageName: props.packageName, piece: state.piece, props: state.props }));
|
|
88
|
+
}
|
|
89
|
+
if (!state.ui)
|
|
90
|
+
return null;
|
|
91
|
+
const Screen = state.ui.content;
|
|
92
|
+
// The same box a tab draws around a screen in the host, so a screen's CSS that cancels the
|
|
93
|
+
// tab's padding (`.p-3 > .x-app`) still lines up.
|
|
94
|
+
return ((0, jsx_runtime_1.jsx)("div", { className: `h-100 overflow-auto ${desktop ? "p-3" : "p-2"}`, children: (0, jsx_runtime_1.jsxs)(ScreenErrorBoundary, { packageName: props.packageName, onError: (message) => props.host.notify(frame_protocol_1.FrameNotice.failed, { message }), children: [(0, jsx_runtime_1.jsx)(Screen, { ...state.props }), (0, jsx_runtime_1.jsx)(FrameOverlays, {})] }) }));
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The emit functions a piece drawn in a frame gets: each emit the embedder's version declares,
|
|
98
|
+
* and `open`, sent to the host, which checks them and hands them to the embedder; any other name
|
|
99
|
+
* does nothing, as in the window. They're the object's own properties, so a piece can spread
|
|
100
|
+
* them into a control.
|
|
101
|
+
*/
|
|
102
|
+
function frameEmitters(host, names) {
|
|
103
|
+
const declared = Object.create(null);
|
|
104
|
+
for (const name of new Set([...names, "open"])) {
|
|
105
|
+
declared[name] = (payload) => host.notify(frame_protocol_1.FrameNotice.pieceEmit, { name, payload });
|
|
106
|
+
}
|
|
107
|
+
return new Proxy(declared, {
|
|
108
|
+
get: (target, name) => typeof name === "string" ? (target[name] ?? (() => undefined)) : undefined,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* A piece drawn in its own frame, acting as its provider. Its emits and `close` go to the host,
|
|
113
|
+
* which hands them to the embedder; a piece that takes its own height reports it as it changes,
|
|
114
|
+
* and one in a popover its width too. `usePieceSize` reads the box it's drawn in.
|
|
115
|
+
*/
|
|
116
|
+
function PieceInFrame(props) {
|
|
117
|
+
const { host, piece } = props;
|
|
118
|
+
const emit = (0, react_1.useMemo)(() => frameEmitters(host, piece.emits), [host, piece.emits.join("\n")]);
|
|
119
|
+
const Component = piece.component;
|
|
120
|
+
return ((0, jsx_runtime_1.jsx)(PieceRoot, { host: host, height: piece.height, width: piece.width, children: (0, jsx_runtime_1.jsx)(ScreenErrorBoundary, { packageName: props.packageName, onError: (message) => host.notify(frame_protocol_1.FrameNotice.failed, { message }), children: (0, jsx_runtime_1.jsx)(Component, { props: props.props, emit: emit, close: () => host.notify(frame_protocol_1.FrameNotice.pieceClose), groupId: piece.groupId }) }) }));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The box a framed piece draws in: measured for `usePieceSize`, and, for a piece that takes its
|
|
124
|
+
* own height (and in a popover its width), reported to the host. The size lives here, apart from
|
|
125
|
+
* the piece, so a resize re-renders only what reads it, as `<Piece>`'s box does in the window.
|
|
126
|
+
*/
|
|
127
|
+
function PieceRoot(props) {
|
|
128
|
+
const { host, height: mode, width: maxWidth } = props;
|
|
129
|
+
const rootRef = (0, react_1.useRef)(null);
|
|
130
|
+
const [size, setSize] = (0, react_1.useState)({ width: 0, height: 0 });
|
|
131
|
+
(0, react_1.useEffect)(() => {
|
|
132
|
+
const root = rootRef.current;
|
|
133
|
+
if (!root)
|
|
134
|
+
return;
|
|
135
|
+
let last = "";
|
|
136
|
+
const report = () => {
|
|
137
|
+
const rect = root.getBoundingClientRect();
|
|
138
|
+
setSize((previous) => previous.width === rect.width && previous.height === rect.height
|
|
139
|
+
? previous
|
|
140
|
+
: { width: rect.width, height: rect.height });
|
|
141
|
+
if (mode !== "content")
|
|
142
|
+
return;
|
|
143
|
+
// Its box's own height can be fractional, which `scrollHeight` rounds, sometimes down: the
|
|
144
|
+
// frame would be a pixel short, and scroll.
|
|
145
|
+
const height = Math.ceil(Math.max(root.scrollHeight, rect.height));
|
|
146
|
+
const width = maxWidth === undefined ? undefined : Math.ceil(rect.width);
|
|
147
|
+
const key = `${height}x${width}`;
|
|
148
|
+
if (key === last)
|
|
149
|
+
return;
|
|
150
|
+
last = key;
|
|
151
|
+
host.notify(frame_protocol_1.FrameNotice.size, width === undefined ? { height } : { height, width });
|
|
152
|
+
};
|
|
153
|
+
report();
|
|
154
|
+
const observer = new ResizeObserver(report);
|
|
155
|
+
observer.observe(root);
|
|
156
|
+
return () => observer.disconnect();
|
|
157
|
+
}, [host, mode, maxWidth]);
|
|
158
|
+
const style = {
|
|
159
|
+
// A piece that takes its own height holds its children's margins, so its height counts them.
|
|
160
|
+
...(mode === "content" ? { display: "flow-root" } : {}),
|
|
161
|
+
// In a popover the piece is as wide as its content, up to the width the embedder allows. The
|
|
162
|
+
// frame is that wide too, and the host shows only the width reported here.
|
|
163
|
+
...(maxWidth === undefined
|
|
164
|
+
? {}
|
|
165
|
+
: { position: "absolute", top: 0, left: 0, width: "max-content", maxWidth }),
|
|
166
|
+
};
|
|
167
|
+
return ((0, jsx_runtime_1.jsx)("div", { ref: rootRef, className: mode === "fill" ? "h-100 overflow-auto" : undefined, style: style, "data-peers-piece-root": "", children: (0, jsx_runtime_1.jsx)(piece_height_1.PieceSizeContext.Provider, { value: size, children: props.children }) }));
|
|
168
|
+
}
|
|
169
|
+
function show(next) {
|
|
170
|
+
pending = next;
|
|
171
|
+
setScreen?.(next);
|
|
172
|
+
}
|
|
173
|
+
/** The bundle run under way, so a second init while it runs waits for it instead of running it again. */
|
|
174
|
+
let loading;
|
|
175
|
+
/** Run the package's UI bundle in this frame, once per version, keeping the UIs it exports. */
|
|
176
|
+
function loadBundle(init) {
|
|
177
|
+
if (loadedVersionId === init.packageVersionId)
|
|
178
|
+
return Promise.resolve();
|
|
179
|
+
if (loading?.versionId === init.packageVersionId)
|
|
180
|
+
return loading.promise;
|
|
181
|
+
const promise = runBundle(init).finally(() => {
|
|
182
|
+
if (loading?.promise === promise)
|
|
183
|
+
loading = undefined;
|
|
184
|
+
});
|
|
185
|
+
loading = { versionId: init.packageVersionId, promise };
|
|
186
|
+
return promise;
|
|
187
|
+
}
|
|
188
|
+
async function runBundle(init) {
|
|
189
|
+
const exportUIs = (exported) => {
|
|
190
|
+
for (const ui of exported?.uis ?? [])
|
|
191
|
+
uis.set(ui.peersUIId, ui);
|
|
192
|
+
for (const piece of exported?.pieces ?? []) {
|
|
193
|
+
pieces.set((0, implement_piece_1.pieceKey)(piece.contractId, piece.version, piece.name), piece.component);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const run = new Function("exportUIs", init.bundleCode);
|
|
197
|
+
await run(exportUIs);
|
|
198
|
+
loadedVersionId = init.packageVersionId;
|
|
199
|
+
}
|
|
200
|
+
/** Numbers each init, so an older one that finishes later doesn't replace a newer one's screen. */
|
|
201
|
+
let latestInit = 0;
|
|
202
|
+
async function handleInit(host, init) {
|
|
203
|
+
const thisInit = ++latestInit;
|
|
204
|
+
drawsPiece = init.piece !== undefined;
|
|
205
|
+
const firstInit = !root;
|
|
206
|
+
if (firstInit) {
|
|
207
|
+
const bridge = (0, frame_bridge_1.frameBridge)();
|
|
208
|
+
bridge?.storage.load(init.storage, init.legacyStorage);
|
|
209
|
+
(0, peers_sdk_1.setDefaultClientUserContext)(init.userId);
|
|
210
|
+
applyTheme(init.colorMode);
|
|
211
|
+
const container = document.getElementById("peers-frame-root");
|
|
212
|
+
if (!container)
|
|
213
|
+
throw new Error("The frame has no #peers-frame-root");
|
|
214
|
+
root = (0, client_1.createRoot)(container);
|
|
215
|
+
root.render((0, jsx_runtime_1.jsx)(FrameScreen, { packageName: init.packageName, host: host }));
|
|
216
|
+
}
|
|
217
|
+
try {
|
|
218
|
+
// The user context loads the active group and this device: code that reads them while it
|
|
219
|
+
// renders gets the right ones, not the personal context it starts with.
|
|
220
|
+
await (0, peers_sdk_1.getUserContext)();
|
|
221
|
+
await loadBundle(init);
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
225
|
+
console.error(`${init.packageName}'s UI bundle failed to run`, error);
|
|
226
|
+
show({ props: {}, error: message });
|
|
227
|
+
host.notify(frame_protocol_1.FrameNotice.failed, { message });
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
// A newer init arrived while this one waited: it shows instead.
|
|
231
|
+
if (thisInit !== latestInit)
|
|
232
|
+
return;
|
|
233
|
+
if (init.piece) {
|
|
234
|
+
const component = pieces.get(init.piece.key);
|
|
235
|
+
if (!component) {
|
|
236
|
+
const message = `Its UI bundle registered no component for the piece ${init.piece.key}`;
|
|
237
|
+
show({ props: {}, error: message });
|
|
238
|
+
host.notify(frame_protocol_1.FrameNotice.failed, { message });
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
const width = init.piece.width;
|
|
242
|
+
show({
|
|
243
|
+
props: init.props,
|
|
244
|
+
piece: {
|
|
245
|
+
component,
|
|
246
|
+
groupId: init.piece.groupId,
|
|
247
|
+
height: init.piece.height,
|
|
248
|
+
...(typeof width === "number" && Number.isFinite(width) && width > 0 ? { width } : {}),
|
|
249
|
+
emits: Array.isArray(init.piece.emits)
|
|
250
|
+
? init.piece.emits.filter((name) => typeof name === "string")
|
|
251
|
+
: [],
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const ui = init.peersUIId ? uis.get(init.peersUIId) : undefined;
|
|
257
|
+
if (!ui) {
|
|
258
|
+
const message = `Its UI bundle has no UI ${init.peersUIId}`;
|
|
259
|
+
show({ props: {}, error: message });
|
|
260
|
+
host.notify(frame_protocol_1.FrameNotice.failed, { message });
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
try {
|
|
264
|
+
ui.propsSchema?.parse?.(init.props);
|
|
265
|
+
}
|
|
266
|
+
catch (error) {
|
|
267
|
+
const message = `Its props didn't match: ${error instanceof Error ? error.message : String(error)}`;
|
|
268
|
+
show({ props: {}, error: message });
|
|
269
|
+
host.notify(frame_protocol_1.FrameNotice.failed, { message });
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
show({ ui, props: init.props });
|
|
273
|
+
}
|
|
274
|
+
/** Links to the web open outside Peers, and `#` links navigate the tab, through the host. */
|
|
275
|
+
function installLinkHandling(host) {
|
|
276
|
+
document.addEventListener("click", (event) => {
|
|
277
|
+
if (event.defaultPrevented || event.button !== 0)
|
|
278
|
+
return;
|
|
279
|
+
const anchor = event.target?.closest?.("a[href]");
|
|
280
|
+
if (!anchor || anchor.hasAttribute("download"))
|
|
281
|
+
return;
|
|
282
|
+
const href = anchor.getAttribute("href") ?? "";
|
|
283
|
+
if (href.startsWith("#")) {
|
|
284
|
+
event.preventDefault();
|
|
285
|
+
void host.request(frame_protocol_1.FrameRequest.navigate, { path: href.slice(1) }).catch(() => undefined);
|
|
286
|
+
return;
|
|
287
|
+
}
|
|
288
|
+
let url;
|
|
289
|
+
try {
|
|
290
|
+
url = new URL(href, "https://invalid.example/");
|
|
291
|
+
}
|
|
292
|
+
catch {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
// Others (`tel:`, `sms:`) are left to the browser, as the host only opens these.
|
|
296
|
+
if (["http:", "https:", "mailto:"].includes(url.protocol)) {
|
|
297
|
+
event.preventDefault();
|
|
298
|
+
void host.request(frame_protocol_1.FrameRequest.openLink, { url: anchor.href }).catch(() => undefined);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
// A screen that navigates by setting `location.hash` navigates its tab.
|
|
302
|
+
window.addEventListener("hashchange", () => {
|
|
303
|
+
const path = window.location.hash.slice(1);
|
|
304
|
+
void host.request(frame_protocol_1.FrameRequest.navigate, { path }).catch(() => undefined);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Shortcuts the host always takes, so the frame cancels the browser's own action for them as it
|
|
309
|
+
* forwards them (in a browser tab, Ctrl+K would also focus the browser's search box): the
|
|
310
|
+
* command palette, the console and tab switching.
|
|
311
|
+
*/
|
|
312
|
+
function isHostShortcut(event) {
|
|
313
|
+
if (event.type !== "keydown" || event.altKey)
|
|
314
|
+
return false;
|
|
315
|
+
const key = event.key.toLowerCase();
|
|
316
|
+
if ((event.metaKey || event.ctrlKey) && key === "k")
|
|
317
|
+
return true;
|
|
318
|
+
if (event.ctrlKey && !event.metaKey && event.code === "Backquote")
|
|
319
|
+
return true;
|
|
320
|
+
return event.ctrlKey && key === "tab";
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* ⌘ and Ctrl keys the screen didn't use go to the host, for its shortcuts. In a piece's frame,
|
|
324
|
+
* so does Escape the piece didn't use, for a popover around it: the host hands it to the
|
|
325
|
+
* embedder. A popover inside the frame stops its own Escape before it gets here; a listener the
|
|
326
|
+
* piece added after this one can still take it, so it's sent once the event has reached them all.
|
|
327
|
+
* With ⌘ or Ctrl held, Escape is a shortcut like the others (Ctrl+Escape cancels the host's tab
|
|
328
|
+
* switcher).
|
|
329
|
+
*/
|
|
330
|
+
function installKeyForwarding(host) {
|
|
331
|
+
const forward = (event) => {
|
|
332
|
+
if (event.defaultPrevented)
|
|
333
|
+
return;
|
|
334
|
+
if (event.key === "Escape" &&
|
|
335
|
+
drawsPiece &&
|
|
336
|
+
event.type === "keydown" &&
|
|
337
|
+
!event.metaKey &&
|
|
338
|
+
!event.ctrlKey) {
|
|
339
|
+
setTimeout(() => {
|
|
340
|
+
if (event.defaultPrevented)
|
|
341
|
+
return;
|
|
342
|
+
host.notify(frame_protocol_1.FrameNotice.key, { type: event.type, key: event.key, code: event.code });
|
|
343
|
+
}, 0);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const modifierKey = event.key === "Control" || event.key === "Meta";
|
|
347
|
+
if (!(event.metaKey || event.ctrlKey || modifierKey))
|
|
348
|
+
return;
|
|
349
|
+
if (isHostShortcut(event))
|
|
350
|
+
event.preventDefault();
|
|
351
|
+
host.notify(frame_protocol_1.FrameNotice.key, {
|
|
352
|
+
type: event.type,
|
|
353
|
+
key: event.key,
|
|
354
|
+
code: event.code,
|
|
355
|
+
metaKey: event.metaKey,
|
|
356
|
+
ctrlKey: event.ctrlKey,
|
|
357
|
+
shiftKey: event.shiftKey,
|
|
358
|
+
altKey: event.altKey,
|
|
359
|
+
repeat: event.repeat,
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
window.addEventListener("keydown", forward);
|
|
363
|
+
window.addEventListener("keyup", forward);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* A pointer down in this frame goes to the host, which tells the other frames and its own window
|
|
367
|
+
* (a click in a frame reaches no document around it); one elsewhere comes back as
|
|
368
|
+
* `FRAME_POINTER_EVENT` on the body, or on a piece's slot when it was in that piece.
|
|
369
|
+
*/
|
|
370
|
+
function installPointerRelay(host) {
|
|
371
|
+
window.addEventListener("mousedown", () => host.notify(frame_protocol_1.FrameNotice.pointerDown), true);
|
|
372
|
+
host.onNotify(frame_protocol_1.HostNotice.outsidePointerDown, (payload) => {
|
|
373
|
+
// A slot's own component dispatches the ones inside its piece.
|
|
374
|
+
if (payload?.slotId === undefined) {
|
|
375
|
+
document.body.dispatchEvent(new Event(popover_surface_1.FRAME_POINTER_EVENT, { bubbles: true }));
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* A pause between wheel events, in milliseconds, after which the next one starts a new gesture.
|
|
381
|
+
* Trackpads and momentum send events every frame; a mouse wheel's notches further apart than this
|
|
382
|
+
* are gestures of their own.
|
|
383
|
+
*/
|
|
384
|
+
const WHEEL_GESTURE_GAP_MS = 150;
|
|
385
|
+
/**
|
|
386
|
+
* In a piece's frame: a wheel the piece doesn't scroll with goes to the host, for the screen the
|
|
387
|
+
* piece is drawn over (the piece's frame is beside the screen's, so the browser never passes it
|
|
388
|
+
* on). As in the browser, a gesture stays with what its first event scrolled: a piece scrolled to
|
|
389
|
+
* its end doesn't start scrolling the screen until the wheel pauses. What it sends is added up
|
|
390
|
+
* and sent once per animation frame; a wheel the piece prevented, and a pinch (Ctrl held), isn't
|
|
391
|
+
* sent.
|
|
392
|
+
*/
|
|
393
|
+
function installWheelRelay(host) {
|
|
394
|
+
let toScreen = false;
|
|
395
|
+
let lastAt = Number.NEGATIVE_INFINITY;
|
|
396
|
+
let pending = [];
|
|
397
|
+
let scheduled = 0;
|
|
398
|
+
const flush = () => {
|
|
399
|
+
scheduled = 0;
|
|
400
|
+
let deltaX = 0;
|
|
401
|
+
let deltaY = 0;
|
|
402
|
+
// Read once every listener has had them, so one the piece added later can still take them.
|
|
403
|
+
for (const event of pending) {
|
|
404
|
+
if (event.defaultPrevented)
|
|
405
|
+
continue;
|
|
406
|
+
const pixels = (0, wheel_relay_1.wheelPixels)(event);
|
|
407
|
+
deltaX += pixels.x;
|
|
408
|
+
deltaY += pixels.y;
|
|
409
|
+
}
|
|
410
|
+
pending = [];
|
|
411
|
+
deltaX = (0, wheel_relay_1.wheelDelta)(deltaX);
|
|
412
|
+
deltaY = (0, wheel_relay_1.wheelDelta)(deltaY);
|
|
413
|
+
if (deltaX || deltaY)
|
|
414
|
+
host.notify(frame_protocol_1.FrameNotice.wheel, { deltaX, deltaY });
|
|
415
|
+
};
|
|
416
|
+
window.addEventListener("wheel", (event) => {
|
|
417
|
+
if (!drawsPiece || event.ctrlKey)
|
|
418
|
+
return;
|
|
419
|
+
const now = event.timeStamp;
|
|
420
|
+
if (now - lastAt > WHEEL_GESTURE_GAP_MS) {
|
|
421
|
+
// A new gesture: the screen gets it if nothing in the piece takes its main direction.
|
|
422
|
+
const start = event.target instanceof Element ? event.target : document.documentElement;
|
|
423
|
+
const { x, y } = (0, wheel_relay_1.wheelPixels)(event);
|
|
424
|
+
if (Math.abs(x) > Math.abs(y)) {
|
|
425
|
+
toScreen = (0, wheel_relay_1.wheelScroller)(start, "x", x) === undefined;
|
|
426
|
+
}
|
|
427
|
+
else if (y) {
|
|
428
|
+
toScreen = (0, wheel_relay_1.wheelScroller)(start, "y", y) === undefined;
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
// A wheel can begin with zero movement. Wait for a direction before choosing a scroller.
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
lastAt = now;
|
|
436
|
+
if (!toScreen)
|
|
437
|
+
return;
|
|
438
|
+
pending.push(event);
|
|
439
|
+
if (!scheduled)
|
|
440
|
+
scheduled = requestAnimationFrame(flush);
|
|
441
|
+
}, { capture: true, passive: true });
|
|
442
|
+
}
|
|
443
|
+
/** The screen's warnings and errors also go to the host, whose console the Console app reads. */
|
|
444
|
+
function installConsoleForwarding(host) {
|
|
445
|
+
for (const level of ["warn", "error"]) {
|
|
446
|
+
const original = console[level].bind(console);
|
|
447
|
+
console[level] = (...args) => {
|
|
448
|
+
original(...args);
|
|
449
|
+
try {
|
|
450
|
+
const message = args
|
|
451
|
+
.map((arg) => arg instanceof Error
|
|
452
|
+
? arg.stack || arg.message
|
|
453
|
+
: typeof arg === "string"
|
|
454
|
+
? arg
|
|
455
|
+
: JSON.stringify(arg))
|
|
456
|
+
.join(" ")
|
|
457
|
+
.slice(0, 4000);
|
|
458
|
+
host.notify(frame_protocol_1.FrameNotice.log, { level, message });
|
|
459
|
+
}
|
|
460
|
+
catch {
|
|
461
|
+
// A value that won't stringify isn't worth losing the screen over.
|
|
462
|
+
}
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Start the frame runtime. The frame's entry calls it once, after the bridge is installed and
|
|
468
|
+
* the window's `React`, `PeersSDK`, `PeersUI` and `zod` globals are set.
|
|
469
|
+
*/
|
|
470
|
+
function startFrameRuntime() {
|
|
471
|
+
const bridge = (0, frame_bridge_1.frameBridge)();
|
|
472
|
+
if (!bridge)
|
|
473
|
+
throw new Error("startFrameRuntime needs the frame bridge installed first");
|
|
474
|
+
const { host } = bridge;
|
|
475
|
+
peersUIOverlayIds = new Set((0, global_overlays_1.globalOverlays)().map(({ id }) => id));
|
|
476
|
+
host.onRequest(frame_protocol_1.HostRequest.init, (init) => handleInit(host, init));
|
|
477
|
+
host.onRequest(frame_protocol_1.HostRequest.inspect, (input) => (0, ui_inspection_1.getDocumentUIState)({ includeRects: input?.includeRects === true }));
|
|
478
|
+
host.onRequest(frame_protocol_1.HostRequest.act, (input) => (0, ui_inspection_1.actOnDocument)(input.operation, input.input));
|
|
479
|
+
host.onNotify(frame_protocol_1.HostNotice.theme, ({ colorMode }) => applyTheme(colorMode));
|
|
480
|
+
// The host resolves the screen's pieces; when what they depend on changes, they resolve again.
|
|
481
|
+
host.onNotify(frame_protocol_1.HostNotice.pieceRevision, () => (0, piece_resolution_1.pieceRevision)((0, piece_resolution_1.pieceRevision)() + 1));
|
|
482
|
+
installLinkHandling(host);
|
|
483
|
+
installKeyForwarding(host);
|
|
484
|
+
installPointerRelay(host);
|
|
485
|
+
installWheelRelay(host);
|
|
486
|
+
installConsoleForwarding(host);
|
|
487
|
+
window.addEventListener("error", (event) => {
|
|
488
|
+
console.error(event.error ?? event.message);
|
|
489
|
+
});
|
|
490
|
+
window.addEventListener("unhandledrejection", (event) => {
|
|
491
|
+
console.error("Unhandled rejection in a screen:", event.reason);
|
|
492
|
+
});
|
|
493
|
+
host.notify(frame_protocol_1.FrameNotice.ready);
|
|
494
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `localStorage` and `sessionStorage` for an isolated package screen.
|
|
3
|
+
*
|
|
4
|
+
* A screen's frame has an opaque origin, where the browser's own storage throws. The frame gets
|
|
5
|
+
* a `Storage` of its package's instead: the host keeps the package's entries in its own
|
|
6
|
+
* `localStorage` under the package's id, hands them over when the frame starts, and is told of
|
|
7
|
+
* every change. Reads are synchronous, from the copy the frame was given. Two frames of one
|
|
8
|
+
* package (two tabs) don't see each other's changes until they start again.
|
|
9
|
+
*
|
|
10
|
+
* Entries a package wrote before its screens were isolated are in the host's storage under their
|
|
11
|
+
* own keys. Until the package writes a key of its own, a read of that key falls back to the old
|
|
12
|
+
* entry (`legacy`), so a screen keeps its settings when it moves into a frame. That fallback is
|
|
13
|
+
* temporary: it reads entries any package may have written, as every screen could before.
|
|
14
|
+
*
|
|
15
|
+
* `sessionStorage` lives only as long as the frame.
|
|
16
|
+
*/
|
|
17
|
+
/** A change to a package's stored entries, as the frame reports it to the host. */
|
|
18
|
+
export type FrameStorageChange = {
|
|
19
|
+
key: string;
|
|
20
|
+
value: string | null;
|
|
21
|
+
} | {
|
|
22
|
+
clear: true;
|
|
23
|
+
};
|
|
24
|
+
/** The frame's two storages, and how the runtime fills the persistent one. */
|
|
25
|
+
export interface IFrameStorage {
|
|
26
|
+
/** Stands in for `window.localStorage`. */
|
|
27
|
+
local: Storage;
|
|
28
|
+
/** Stands in for `window.sessionStorage`. */
|
|
29
|
+
session: Storage;
|
|
30
|
+
/**
|
|
31
|
+
* Load the package's entries and the legacy ones, when the frame starts.
|
|
32
|
+
*
|
|
33
|
+
* @param own - The package's entries.
|
|
34
|
+
* @param legacy - Entries from before isolation, read only where the package has none.
|
|
35
|
+
*/
|
|
36
|
+
load(own: Record<string, string>, legacy: Record<string, string>): void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Make a frame's storages.
|
|
40
|
+
*
|
|
41
|
+
* @param report - Told of every change to the persistent one, to pass to the host.
|
|
42
|
+
*/
|
|
43
|
+
export declare function createFrameStorage(report: (change: FrameStorageChange) => void): IFrameStorage;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `localStorage` and `sessionStorage` for an isolated package screen.
|
|
4
|
+
*
|
|
5
|
+
* A screen's frame has an opaque origin, where the browser's own storage throws. The frame gets
|
|
6
|
+
* a `Storage` of its package's instead: the host keeps the package's entries in its own
|
|
7
|
+
* `localStorage` under the package's id, hands them over when the frame starts, and is told of
|
|
8
|
+
* every change. Reads are synchronous, from the copy the frame was given. Two frames of one
|
|
9
|
+
* package (two tabs) don't see each other's changes until they start again.
|
|
10
|
+
*
|
|
11
|
+
* Entries a package wrote before its screens were isolated are in the host's storage under their
|
|
12
|
+
* own keys. Until the package writes a key of its own, a read of that key falls back to the old
|
|
13
|
+
* entry (`legacy`), so a screen keeps its settings when it moves into a frame. That fallback is
|
|
14
|
+
* temporary: it reads entries any package may have written, as every screen could before.
|
|
15
|
+
*
|
|
16
|
+
* `sessionStorage` lives only as long as the frame.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.createFrameStorage = createFrameStorage;
|
|
20
|
+
class MemoryStorage {
|
|
21
|
+
onChange;
|
|
22
|
+
entries = new Map();
|
|
23
|
+
constructor(onChange) {
|
|
24
|
+
this.onChange = onChange;
|
|
25
|
+
}
|
|
26
|
+
get length() {
|
|
27
|
+
return this.entries.size;
|
|
28
|
+
}
|
|
29
|
+
key(index) {
|
|
30
|
+
return [...this.entries.keys()][index] ?? null;
|
|
31
|
+
}
|
|
32
|
+
getItem(key) {
|
|
33
|
+
return this.entries.get(String(key)) ?? null;
|
|
34
|
+
}
|
|
35
|
+
setItem(key, value) {
|
|
36
|
+
const k = String(key);
|
|
37
|
+
const v = String(value);
|
|
38
|
+
this.entries.set(k, v);
|
|
39
|
+
this.onChange?.({ key: k, value: v });
|
|
40
|
+
}
|
|
41
|
+
removeItem(key) {
|
|
42
|
+
const k = String(key);
|
|
43
|
+
this.entries.delete(k);
|
|
44
|
+
this.onChange?.({ key: k, value: null });
|
|
45
|
+
}
|
|
46
|
+
clear() {
|
|
47
|
+
this.entries.clear();
|
|
48
|
+
this.onChange?.({ clear: true });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
class PackageStorage extends MemoryStorage {
|
|
52
|
+
legacy = new Map();
|
|
53
|
+
load(own, legacy) {
|
|
54
|
+
this.entries.clear();
|
|
55
|
+
for (const [key, value] of Object.entries(own))
|
|
56
|
+
this.entries.set(key, value);
|
|
57
|
+
this.legacy = new Map(Object.entries(legacy));
|
|
58
|
+
}
|
|
59
|
+
getItem(key) {
|
|
60
|
+
const k = String(key);
|
|
61
|
+
return this.entries.get(k) ?? this.legacy.get(k) ?? null;
|
|
62
|
+
}
|
|
63
|
+
removeItem(key) {
|
|
64
|
+
this.legacy.delete(String(key));
|
|
65
|
+
super.removeItem(key);
|
|
66
|
+
}
|
|
67
|
+
clear() {
|
|
68
|
+
this.legacy.clear();
|
|
69
|
+
super.clear();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Make a frame's storages.
|
|
74
|
+
*
|
|
75
|
+
* @param report - Told of every change to the persistent one, to pass to the host.
|
|
76
|
+
*/
|
|
77
|
+
function createFrameStorage(report) {
|
|
78
|
+
const local = new PackageStorage(report);
|
|
79
|
+
const session = new MemoryStorage();
|
|
80
|
+
return {
|
|
81
|
+
local,
|
|
82
|
+
session,
|
|
83
|
+
load: (own, legacy) => local.load(own, legacy),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.startFrameRuntime = exports.isInFrame = exports.SHARED_WINDOW_PACKAGES = exports.runsIsolated = exports.configureIsolatedScreens = void 0;
|
|
4
|
+
// Isolated package screens: each package's screen runs in a sandboxed frame of its own.
|
|
5
|
+
// Hosts turn it on with configureIsolatedScreens; docs/Packages/isolated-screens.md.
|
|
6
|
+
var config_1 = require("./config");
|
|
7
|
+
Object.defineProperty(exports, "configureIsolatedScreens", { enumerable: true, get: function () { return config_1.configureIsolatedScreens; } });
|
|
8
|
+
Object.defineProperty(exports, "runsIsolated", { enumerable: true, get: function () { return config_1.runsIsolated; } });
|
|
9
|
+
Object.defineProperty(exports, "SHARED_WINDOW_PACKAGES", { enumerable: true, get: function () { return config_1.SHARED_WINDOW_PACKAGES; } });
|
|
10
|
+
var frame_realm_1 = require("./frame-realm");
|
|
11
|
+
Object.defineProperty(exports, "isInFrame", { enumerable: true, get: function () { return frame_realm_1.isInFrame; } });
|
|
12
|
+
var frame_runtime_1 = require("./frame-runtime");
|
|
13
|
+
Object.defineProperty(exports, "startFrameRuntime", { enumerable: true, get: function () { return frame_runtime_1.startFrameRuntime; } });
|