@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,695 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IsolatedScreen = IsolatedScreen;
|
|
4
|
+
exports.PackageFrame = PackageFrame;
|
|
5
|
+
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
const peers_sdk_1 = require("@peers-app/peers-sdk");
|
|
7
|
+
const react_1 = require("react");
|
|
8
|
+
const hooks_1 = require("../hooks");
|
|
9
|
+
const host_shell_consumer_1 = require("../host-shell/host-shell-consumer");
|
|
10
|
+
const mentions_client_1 = require("../mentions/mentions-client");
|
|
11
|
+
const color_mode_dropdown_1 = require("../screens/settings/color-mode-dropdown");
|
|
12
|
+
const tabs_state_1 = require("../tabs-layout/tabs-state");
|
|
13
|
+
const implement_piece_1 = require("../ui-contracts/implement-piece");
|
|
14
|
+
const piece_host_1 = require("../ui-contracts/piece-host");
|
|
15
|
+
const piece_props_1 = require("../ui-contracts/piece-props");
|
|
16
|
+
const piece_resolution_1 = require("../ui-contracts/piece-resolution");
|
|
17
|
+
const popover_surface_1 = require("../ui-contracts/popover-surface");
|
|
18
|
+
const ui_inspection_1 = require("../ui-inspection");
|
|
19
|
+
const config_1 = require("./config");
|
|
20
|
+
const frame_component_1 = require("./frame-component");
|
|
21
|
+
const frame_document_1 = require("./frame-document");
|
|
22
|
+
const frame_policy_1 = require("./frame-policy");
|
|
23
|
+
const frame_protocol_1 = require("./frame-protocol");
|
|
24
|
+
const frame_pvars_1 = require("./frame-pvars");
|
|
25
|
+
const piece_reach_1 = require("./piece-reach");
|
|
26
|
+
const port_channel_1 = require("./port-channel");
|
|
27
|
+
const screen_tab_context_1 = require("./screen-tab-context");
|
|
28
|
+
const wheel_relay_1 = require("./wheel-relay");
|
|
29
|
+
/** A rectangle as a frame reports it, refused unless it's four finite numbers. */
|
|
30
|
+
function toRect(value) {
|
|
31
|
+
const r = value;
|
|
32
|
+
if (!r)
|
|
33
|
+
return undefined;
|
|
34
|
+
const nums = [r.x, r.y, r.width, r.height];
|
|
35
|
+
if (!nums.every((n) => typeof n === "number" && Number.isFinite(n)))
|
|
36
|
+
return undefined;
|
|
37
|
+
return {
|
|
38
|
+
x: r.x,
|
|
39
|
+
y: r.y,
|
|
40
|
+
width: r.width,
|
|
41
|
+
height: r.height,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function toClip(value) {
|
|
45
|
+
const r = value;
|
|
46
|
+
if (!r)
|
|
47
|
+
return undefined;
|
|
48
|
+
const nums = [r.top, r.right, r.bottom, r.left];
|
|
49
|
+
if (!nums.every((n) => typeof n === "number" && Number.isFinite(n) && n >= 0))
|
|
50
|
+
return undefined;
|
|
51
|
+
return {
|
|
52
|
+
top: r.top,
|
|
53
|
+
right: r.right,
|
|
54
|
+
bottom: r.bottom,
|
|
55
|
+
left: r.left,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** `localStorage` keys the host keeps a package's entries under. */
|
|
59
|
+
function storagePrefix(packageId) {
|
|
60
|
+
return `peers-frame:${packageId}:`;
|
|
61
|
+
}
|
|
62
|
+
function readStorage(packageId) {
|
|
63
|
+
const own = {};
|
|
64
|
+
const legacy = {};
|
|
65
|
+
try {
|
|
66
|
+
const prefix = storagePrefix(packageId);
|
|
67
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
68
|
+
const key = localStorage.key(i);
|
|
69
|
+
// The host's own entries never reach a screen: a PWA interrupted during credential
|
|
70
|
+
// migration can still have the old account key and user id under `peers-pwa:`.
|
|
71
|
+
if (!key || key.startsWith("peers-pwa:"))
|
|
72
|
+
continue;
|
|
73
|
+
const isOwn = key.startsWith(prefix);
|
|
74
|
+
if (!isOwn && key.startsWith("peers-frame:"))
|
|
75
|
+
continue;
|
|
76
|
+
const value = localStorage.getItem(key);
|
|
77
|
+
if (value === null)
|
|
78
|
+
continue;
|
|
79
|
+
if (isOwn)
|
|
80
|
+
own[key.slice(prefix.length)] = value;
|
|
81
|
+
else
|
|
82
|
+
legacy[key] = value;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
console.warn("Couldn't read localStorage for a screen", error);
|
|
87
|
+
}
|
|
88
|
+
return { storage: own, legacyStorage: legacy };
|
|
89
|
+
}
|
|
90
|
+
function writeStorage(packageId, change) {
|
|
91
|
+
const prefix = storagePrefix(packageId);
|
|
92
|
+
try {
|
|
93
|
+
if ("clear" in change) {
|
|
94
|
+
const keys = [];
|
|
95
|
+
for (let i = 0; i < localStorage.length; i++) {
|
|
96
|
+
const key = localStorage.key(i);
|
|
97
|
+
if (key?.startsWith(prefix))
|
|
98
|
+
keys.push(key);
|
|
99
|
+
}
|
|
100
|
+
for (const key of keys)
|
|
101
|
+
localStorage.removeItem(key);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (typeof change.key !== "string")
|
|
105
|
+
return;
|
|
106
|
+
if (change.value === null)
|
|
107
|
+
localStorage.removeItem(prefix + change.key);
|
|
108
|
+
else
|
|
109
|
+
localStorage.setItem(prefix + change.key, String(change.value));
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
console.warn("Couldn't save a screen's localStorage", error);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/** Every booted frame, for the clicks the host relays between them. */
|
|
116
|
+
const liveSessions = new Set();
|
|
117
|
+
/**
|
|
118
|
+
* How a frame's box cuts what lies outside it: a popover piece's frame wider than the width it
|
|
119
|
+
* reports, or a piece drawn over a slot the screen scrolled partly away. `clip`, unlike
|
|
120
|
+
* `hidden`, can't be scrolled: Chromium scrolls a `hidden` box to show a control focused in the
|
|
121
|
+
* cut-away part, which shifts the piece until it closes. `hidden` where `clip` isn't supported
|
|
122
|
+
* (Safari before 16), since an unsupported value would show the cut-away part.
|
|
123
|
+
*/
|
|
124
|
+
const FRAME_OVERFLOW = typeof CSS !== "undefined" && CSS.supports?.("overflow", "clip") ? "clip" : "hidden";
|
|
125
|
+
/** A click in the host's own window is outside every frame. */
|
|
126
|
+
function relayHostPointers() {
|
|
127
|
+
for (const session of liveSessions)
|
|
128
|
+
session.pointerOutside();
|
|
129
|
+
}
|
|
130
|
+
/** Everything one booted frame has open: torn down together. */
|
|
131
|
+
class FrameSession {
|
|
132
|
+
opts;
|
|
133
|
+
channel;
|
|
134
|
+
contractSession;
|
|
135
|
+
rpc;
|
|
136
|
+
pvars;
|
|
137
|
+
tabPath = (0, peers_sdk_1.observable)("");
|
|
138
|
+
disposers = [];
|
|
139
|
+
init;
|
|
140
|
+
constructor(opts) {
|
|
141
|
+
this.opts = opts;
|
|
142
|
+
const { pkg } = opts;
|
|
143
|
+
this.channel = (0, port_channel_1.openPortChannel)(opts.port);
|
|
144
|
+
this.rpc = (0, frame_policy_1.createFrameRpc)(pkg.packageId, (event) => this.channel.notify(frame_protocol_1.HostNotice.event, event));
|
|
145
|
+
this.pvars = (0, frame_pvars_1.createFramePvarService)({
|
|
146
|
+
packageId: pkg.packageId,
|
|
147
|
+
tabPath: this.tabPath,
|
|
148
|
+
navigate: (path) => this.navigate(path),
|
|
149
|
+
changed: (bindingId, value) => this.channel.notify(frame_protocol_1.HostNotice.pvarChanged, { bindingId, value }),
|
|
150
|
+
});
|
|
151
|
+
this.serve();
|
|
152
|
+
liveSessions.add(this);
|
|
153
|
+
if (liveSessions.size === 1)
|
|
154
|
+
document.addEventListener("mousedown", relayHostPointers, true);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* A pointer went down in this frame. The window around it hears it on the frame's element, and
|
|
158
|
+
* every other frame is told: the screens this piece is drawn over, through the pieces between,
|
|
159
|
+
* as a click in their slot; the rest as one outside.
|
|
160
|
+
*/
|
|
161
|
+
pointerDown() {
|
|
162
|
+
this.opts.element.dispatchEvent(new Event(popover_surface_1.FRAME_POINTER_EVENT, { bubbles: true }));
|
|
163
|
+
const inside = new Map();
|
|
164
|
+
let link = this.opts.slotOf?.();
|
|
165
|
+
for (let depth = 0; link?.session && depth < 16; depth++) {
|
|
166
|
+
inside.set(link.session, link.slotId);
|
|
167
|
+
link = link.session.opts.slotOf?.();
|
|
168
|
+
}
|
|
169
|
+
for (const session of liveSessions) {
|
|
170
|
+
if (session !== this)
|
|
171
|
+
session.pointerOutside(inside.get(session));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Tell this frame a pointer went down outside it: in the piece drawn over `slotId` when it's
|
|
176
|
+
* given, elsewhere when it isn't.
|
|
177
|
+
*/
|
|
178
|
+
pointerOutside(slotId) {
|
|
179
|
+
this.channel.notify(frame_protocol_1.HostNotice.outsidePointerDown, slotId ? { slotId } : {});
|
|
180
|
+
}
|
|
181
|
+
navigate(path, options) {
|
|
182
|
+
(0, tabs_state_1.navigateTo)(path, options);
|
|
183
|
+
}
|
|
184
|
+
/** Open the frame's contract session now rather than on its first call. */
|
|
185
|
+
openContractSession() {
|
|
186
|
+
try {
|
|
187
|
+
this.session();
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
console.warn(`[${this.opts.pkg.name}] couldn't open its contract session yet`, error);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
session() {
|
|
194
|
+
if (!this.contractSession) {
|
|
195
|
+
const host = (0, config_1.isolatedScreensHost)();
|
|
196
|
+
if (!host)
|
|
197
|
+
throw new Error("Isolated screens aren't configured");
|
|
198
|
+
const session = host.openSession({
|
|
199
|
+
packageId: this.opts.pkg.packageId,
|
|
200
|
+
packageVersionId: this.opts.bundle.packageVersionId,
|
|
201
|
+
packageDataContextId: this.opts.bundle.dataContextId,
|
|
202
|
+
});
|
|
203
|
+
const stop = session.transport.on(peers_sdk_1.CONTRACT_NOTIFY_CHANNEL, (...args) => {
|
|
204
|
+
this.channel.notify(frame_protocol_1.HostNotice.contract, { channel: peers_sdk_1.CONTRACT_NOTIFY_CHANNEL, args });
|
|
205
|
+
});
|
|
206
|
+
const stopReset = session.onReset?.(() => this.channel.notify(frame_protocol_1.HostNotice.contractReset));
|
|
207
|
+
this.contractSession = {
|
|
208
|
+
transport: session.transport,
|
|
209
|
+
dispose: () => {
|
|
210
|
+
stop();
|
|
211
|
+
stopReset?.();
|
|
212
|
+
session.dispose();
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
return this.contractSession;
|
|
217
|
+
}
|
|
218
|
+
serve() {
|
|
219
|
+
const { channel, opts } = this;
|
|
220
|
+
const { pkg } = opts;
|
|
221
|
+
channel.onRequest(frame_protocol_1.FrameRequest.rpc, ({ name, args }) => this.rpc.call(name, Array.isArray(args) ? args : []));
|
|
222
|
+
channel.onRequest(frame_protocol_1.FrameRequest.contractEmit, ({ channel: name, args }) => {
|
|
223
|
+
if (name !== peers_sdk_1.CONTRACT_REQUEST_CHANNEL) {
|
|
224
|
+
throw new Error(`A screen can't send on ${name}`);
|
|
225
|
+
}
|
|
226
|
+
return this.session().transport.emit(name, ...(Array.isArray(args) ? args : []));
|
|
227
|
+
});
|
|
228
|
+
channel.onRequest(frame_protocol_1.FrameRequest.pvarBind, (input) => this.pvars.bind(input));
|
|
229
|
+
channel.onRequest(frame_protocol_1.FrameRequest.pvarWrite, (input) => this.pvars.write(input));
|
|
230
|
+
channel.onRequest(frame_protocol_1.FrameRequest.pvarDelete, (input) => this.pvars.delete(input));
|
|
231
|
+
channel.onRequest(frame_protocol_1.FrameRequest.navigate, ({ path, options }) => {
|
|
232
|
+
if (typeof path === "string")
|
|
233
|
+
this.navigate(path, options);
|
|
234
|
+
});
|
|
235
|
+
channel.onRequest(frame_protocol_1.FrameRequest.goToTabPath, ({ path }) => {
|
|
236
|
+
if (typeof path === "string")
|
|
237
|
+
this.navigate(path, { preserveSectionOnAppRoot: true });
|
|
238
|
+
});
|
|
239
|
+
channel.onRequest(frame_protocol_1.FrameRequest.setTitle, ({ title }) => {
|
|
240
|
+
const tabId = opts.tabId();
|
|
241
|
+
if (!tabId || typeof title !== "string")
|
|
242
|
+
return;
|
|
243
|
+
const tabs = (0, tabs_state_1.activeTabs)();
|
|
244
|
+
const tab = tabs.find((t) => t.tabId === tabId);
|
|
245
|
+
if (tab && tab.title !== title)
|
|
246
|
+
(0, tabs_state_1.activeTabs)(tabs.map((t) => (t === tab ? { ...t, title } : t)));
|
|
247
|
+
});
|
|
248
|
+
channel.onRequest(frame_protocol_1.FrameRequest.closeTab, () => {
|
|
249
|
+
const tabId = opts.tabId();
|
|
250
|
+
if (tabId)
|
|
251
|
+
(0, tabs_state_1.closeTab)(tabId);
|
|
252
|
+
});
|
|
253
|
+
channel.onRequest(frame_protocol_1.FrameRequest.openLink, ({ url }) => (0, host_shell_consumer_1.openLink)(String(url)));
|
|
254
|
+
channel.onRequest(frame_protocol_1.FrameRequest.openMention, ({ ref, groupId }) => (0, mentions_client_1.openMention)(ref, groupId));
|
|
255
|
+
channel.onNotify(frame_protocol_1.FrameNotice.storage, (change) => writeStorage(pkg.packageId, change));
|
|
256
|
+
channel.onNotify(frame_protocol_1.FrameNotice.key, (event) => {
|
|
257
|
+
if (event?.type !== "keydown" && event?.type !== "keyup")
|
|
258
|
+
return;
|
|
259
|
+
const active = document.activeElement;
|
|
260
|
+
// A piece may be relaying Escape from a focused descendant through its own slot. With ⌘ or
|
|
261
|
+
// Ctrl held it's a shortcut, for the host's document, as from a screen.
|
|
262
|
+
if (event.key === "Escape" &&
|
|
263
|
+
event.type === "keydown" &&
|
|
264
|
+
this.init?.piece &&
|
|
265
|
+
!event.ctrlKey &&
|
|
266
|
+
!event.metaKey) {
|
|
267
|
+
if (opts.element.parentElement?.contains(active)) {
|
|
268
|
+
opts.pieceEvents()?.onKey?.({ key: "Escape", code: "Escape" });
|
|
269
|
+
}
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (active !== opts.element)
|
|
273
|
+
return;
|
|
274
|
+
if (!(event.ctrlKey || event.metaKey || event.key === "Meta" || event.key === "Control"))
|
|
275
|
+
return;
|
|
276
|
+
document.dispatchEvent(new KeyboardEvent(event.type, {
|
|
277
|
+
key: event.key,
|
|
278
|
+
code: event.code,
|
|
279
|
+
metaKey: !!event.metaKey,
|
|
280
|
+
ctrlKey: !!event.ctrlKey,
|
|
281
|
+
shiftKey: !!event.shiftKey,
|
|
282
|
+
altKey: !!event.altKey,
|
|
283
|
+
repeat: !!event.repeat,
|
|
284
|
+
bubbles: true,
|
|
285
|
+
cancelable: true,
|
|
286
|
+
}));
|
|
287
|
+
});
|
|
288
|
+
channel.onNotify(frame_protocol_1.FrameNotice.log, ({ level, message }) => {
|
|
289
|
+
const log = level === "error" ? console.error : console.warn;
|
|
290
|
+
log(`[${pkg.name} screen] ${String(message)}`);
|
|
291
|
+
});
|
|
292
|
+
channel.onNotify(frame_protocol_1.FrameNotice.failed, ({ message }) => {
|
|
293
|
+
console.error(`[${pkg.name}] failed: ${String(message)}`);
|
|
294
|
+
opts.pieceEvents()?.onFailed(String(message));
|
|
295
|
+
});
|
|
296
|
+
// A piece's frame: its emits, close and size go to its embedder.
|
|
297
|
+
channel.onNotify(frame_protocol_1.FrameNotice.pieceEmit, ({ name, payload }) => {
|
|
298
|
+
if (typeof name === "string")
|
|
299
|
+
opts.pieceEvents()?.onEmit(name, payload);
|
|
300
|
+
});
|
|
301
|
+
channel.onNotify(frame_protocol_1.FrameNotice.pieceClose, () => opts.pieceEvents()?.onClose());
|
|
302
|
+
channel.onNotify(frame_protocol_1.FrameNotice.size, ({ height, width }) => {
|
|
303
|
+
if (typeof height !== "number" || !Number.isFinite(height) || height < 0)
|
|
304
|
+
return;
|
|
305
|
+
const cap = this.init?.piece?.width;
|
|
306
|
+
const boundedWidth = cap !== undefined && typeof width === "number" && Number.isFinite(width) && width >= 0
|
|
307
|
+
? Math.min(width, cap)
|
|
308
|
+
: undefined;
|
|
309
|
+
opts.pieceEvents()?.onSize(height, boundedWidth);
|
|
310
|
+
});
|
|
311
|
+
channel.onNotify(frame_protocol_1.FrameNotice.pointerDown, () => this.pointerDown());
|
|
312
|
+
channel.onNotify(frame_protocol_1.FrameNotice.wheel, (wheel) => {
|
|
313
|
+
const deltaX = (0, wheel_relay_1.wheelDelta)(wheel?.deltaX);
|
|
314
|
+
const deltaY = (0, wheel_relay_1.wheelDelta)(wheel?.deltaY);
|
|
315
|
+
if (deltaX || deltaY)
|
|
316
|
+
opts.pieceEvents()?.onWheel?.(deltaX, deltaY);
|
|
317
|
+
});
|
|
318
|
+
channel.onNotify(frame_protocol_1.FrameNotice.restart, () => opts.restart());
|
|
319
|
+
// A frame that embeds pieces: the host resolves them and draws each in a frame of its own,
|
|
320
|
+
// acting as its provider, over the slot the frame reports.
|
|
321
|
+
channel.onRequest(frame_protocol_1.FrameRequest.pieceResolve, async ({ handle, dataContextId }) => this.resolvePieceFor(handle, dataContextId));
|
|
322
|
+
channel.onRequest(frame_protocol_1.FrameRequest.pieceMount, async (input) => this.mountPiece(input));
|
|
323
|
+
channel.onRequest(frame_protocol_1.FrameRequest.pieceRetry, () => (0, piece_resolution_1.refreshPieceResolutions)());
|
|
324
|
+
channel.onNotify(frame_protocol_1.FrameNotice.pieceRect, ({ slotId, rect, clip }) => {
|
|
325
|
+
this.updateSlot(slotId, (slot) => ({ ...slot, rect: toRect(rect), clip: toClip(clip) }));
|
|
326
|
+
});
|
|
327
|
+
channel.onNotify(frame_protocol_1.FrameNotice.pieceProps, ({ slotId, props }) => {
|
|
328
|
+
const slot = opts.slots().find((candidate) => candidate.slotId === slotId);
|
|
329
|
+
if (!slot)
|
|
330
|
+
return;
|
|
331
|
+
// Only props that fit the declaration reach the provider, whatever the screen sends.
|
|
332
|
+
const checked = (0, piece_props_1.checkPieceProps)(props, slot.piece);
|
|
333
|
+
if ("error" in checked) {
|
|
334
|
+
channel.notify(frame_protocol_1.HostNotice.pieceFailed, { slotId, message: checked.error });
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
this.updateSlot(slotId, (current) => ({
|
|
338
|
+
...current,
|
|
339
|
+
content: { ...current.content, props: checked.value },
|
|
340
|
+
}));
|
|
341
|
+
});
|
|
342
|
+
channel.onNotify(frame_protocol_1.FrameNotice.pieceUnmount, ({ slotId }) => {
|
|
343
|
+
opts.slots(opts.slots().filter((slot) => slot.slotId !== slotId));
|
|
344
|
+
});
|
|
345
|
+
const revision = piece_resolution_1.pieceRevision.subscribe(() => channel.notify(frame_protocol_1.HostNotice.pieceRevision));
|
|
346
|
+
this.disposers.push(() => revision.dispose());
|
|
347
|
+
// The runtime answers requests only once it says it's ready: an init sent before that would
|
|
348
|
+
// reach a frame with no handler for it.
|
|
349
|
+
channel.onNotify(frame_protocol_1.FrameNotice.ready, () => {
|
|
350
|
+
this.frameReady = true;
|
|
351
|
+
void this.sendInit();
|
|
352
|
+
});
|
|
353
|
+
const theme = color_mode_dropdown_1.colorMode.subscribe((mode) => channel.notify(frame_protocol_1.HostNotice.theme, { colorMode: mode === "light" ? "light" : "dark" }));
|
|
354
|
+
this.disposers.push(() => theme.dispose());
|
|
355
|
+
this.disposers.push((0, ui_inspection_1.registerInspectableFrame)({
|
|
356
|
+
frameId: opts.frameId,
|
|
357
|
+
element: opts.element,
|
|
358
|
+
label: `${pkg.name}'s screen`,
|
|
359
|
+
inspect: (includeRects) => channel.request(frame_protocol_1.HostRequest.inspect, { includeRects }),
|
|
360
|
+
act: (operation, input) => channel.request(frame_protocol_1.HostRequest.act, { operation, input }),
|
|
361
|
+
}));
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Resolve a piece this frame embeds. The frame's package must consume the UI contract; then
|
|
365
|
+
* the host's resolution answers, as it would for a screen in the window.
|
|
366
|
+
*/
|
|
367
|
+
async resolvePieceFor(handle, dataContextId) {
|
|
368
|
+
const unavailable = (message) => ({
|
|
369
|
+
status: "unavailable",
|
|
370
|
+
dataContextId,
|
|
371
|
+
reason: "failed",
|
|
372
|
+
message,
|
|
373
|
+
});
|
|
374
|
+
if (!handle || typeof handle.contractId !== "string" || typeof handle.name !== "string") {
|
|
375
|
+
return unavailable("That isn't a piece");
|
|
376
|
+
}
|
|
377
|
+
const declared = await (0, piece_reach_1.consumesUIContract)(this.opts.bundle, handle.contractId, handle.version);
|
|
378
|
+
if (!declared) {
|
|
379
|
+
return unavailable(`${this.opts.pkg.name} doesn't consume this UI contract (${handle.contractId} v${handle.version})`);
|
|
380
|
+
}
|
|
381
|
+
// Resolving starts group watchers. Only an existing context may be passed to them.
|
|
382
|
+
const user = (0, peers_sdk_1.peekUserContext)();
|
|
383
|
+
if (!user ||
|
|
384
|
+
(dataContextId !== "" &&
|
|
385
|
+
dataContextId !== user.userId &&
|
|
386
|
+
!user.groupDataContexts.has(dataContextId))) {
|
|
387
|
+
return unavailable("That group isn't available to this screen");
|
|
388
|
+
}
|
|
389
|
+
const resolution = await (0, piece_resolution_1.resolvePiece)({
|
|
390
|
+
kind: "piece-handle",
|
|
391
|
+
contractId: handle.contractId,
|
|
392
|
+
version: handle.version,
|
|
393
|
+
name: handle.name,
|
|
394
|
+
dataContextId,
|
|
395
|
+
...(handle.providerPackageId ? { providerPackageId: handle.providerPackageId } : {}),
|
|
396
|
+
}, dataContextId);
|
|
397
|
+
// A function can't cross: the frame retries by resolving again.
|
|
398
|
+
const { retry: _retry, ...plain } = resolution;
|
|
399
|
+
return plain;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Draw a piece over one of this frame's slots: resolve it again (the frame only asks), read its
|
|
403
|
+
* provider's bundle, and add the slot; the frame's component draws a frame for the piece over it.
|
|
404
|
+
*/
|
|
405
|
+
async mountPiece(input) {
|
|
406
|
+
const { slotId, handle, dataContextId, width } = input;
|
|
407
|
+
if (typeof slotId !== "string" || !handle)
|
|
408
|
+
throw new Error("That isn't a piece slot");
|
|
409
|
+
const resolution = await this.resolvePieceFor(handle, dataContextId);
|
|
410
|
+
if (resolution.status !== "available")
|
|
411
|
+
throw new Error(resolution.message);
|
|
412
|
+
// Only props that fit the declaration reach the provider, whatever the screen sends.
|
|
413
|
+
const checked = (0, piece_props_1.checkPieceProps)(input.props, resolution.piece);
|
|
414
|
+
if ("error" in checked)
|
|
415
|
+
throw new Error(checked.error);
|
|
416
|
+
const bundle = await (0, piece_host_1.readVersionBundle)(resolution.dataContextId, resolution.providerPackageId, resolution.packageVersionId);
|
|
417
|
+
const { contractId, version } = resolution.implementation;
|
|
418
|
+
const slot = {
|
|
419
|
+
slotId,
|
|
420
|
+
bundle,
|
|
421
|
+
content: {
|
|
422
|
+
kind: "piece",
|
|
423
|
+
key: (0, implement_piece_1.pieceKey)(contractId, version, handle.name),
|
|
424
|
+
props: checked.value,
|
|
425
|
+
groupId: resolution.dataContextId,
|
|
426
|
+
height: input.height === "content" ? "content" : "fill",
|
|
427
|
+
...(typeof width === "number" && Number.isFinite(width) && width > 0
|
|
428
|
+
? { width: Math.min(width, 4096) }
|
|
429
|
+
: {}),
|
|
430
|
+
emits: resolution.piece.emits.map((emit) => emit.name),
|
|
431
|
+
},
|
|
432
|
+
piece: resolution.piece,
|
|
433
|
+
};
|
|
434
|
+
this.opts.slots([...this.opts.slots().filter((s) => s.slotId !== slotId), slot]);
|
|
435
|
+
}
|
|
436
|
+
updateSlot(slotId, change) {
|
|
437
|
+
const slots = this.opts.slots();
|
|
438
|
+
const index = slots.findIndex((slot) => slot.slotId === slotId);
|
|
439
|
+
if (index < 0)
|
|
440
|
+
return;
|
|
441
|
+
const next = [...slots];
|
|
442
|
+
next[index] = change(slots[index]);
|
|
443
|
+
this.opts.slots(next);
|
|
444
|
+
}
|
|
445
|
+
/** Tell a piece slot's frame what happened in its piece. */
|
|
446
|
+
slotNotify(type, payload) {
|
|
447
|
+
this.channel.notify(type, payload);
|
|
448
|
+
}
|
|
449
|
+
/** Show a screen or a piece: sends the init now if the frame is ready, or once it says so. */
|
|
450
|
+
async show(getContent) {
|
|
451
|
+
const userContext = await (0, peers_sdk_1.getUserContext)();
|
|
452
|
+
const content = getContent();
|
|
453
|
+
if (content.kind === "screen")
|
|
454
|
+
this.tabPath(content.path);
|
|
455
|
+
const { storage, legacyStorage } = this.init ?? readStorage(this.opts.pkg.packageId);
|
|
456
|
+
this.init = {
|
|
457
|
+
packageId: this.opts.pkg.packageId,
|
|
458
|
+
packageName: this.opts.pkg.name,
|
|
459
|
+
packageVersionId: this.opts.bundle.packageVersionId,
|
|
460
|
+
userId: userContext.userId,
|
|
461
|
+
deviceId: (0, peers_sdk_1.thisDeviceId)() ?? "",
|
|
462
|
+
bundleCode: this.opts.bundle.code,
|
|
463
|
+
...(content.kind === "screen"
|
|
464
|
+
? { peersUIId: content.peersUIId, path: content.path }
|
|
465
|
+
: {
|
|
466
|
+
path: "",
|
|
467
|
+
piece: {
|
|
468
|
+
key: content.key,
|
|
469
|
+
groupId: content.groupId,
|
|
470
|
+
height: content.height,
|
|
471
|
+
...(content.width !== undefined ? { width: content.width } : {}),
|
|
472
|
+
...(content.emits ? { emits: content.emits } : {}),
|
|
473
|
+
},
|
|
474
|
+
}),
|
|
475
|
+
props: content.props,
|
|
476
|
+
colorMode: (0, color_mode_dropdown_1.colorMode)() === "light" ? "light" : "dark",
|
|
477
|
+
storage,
|
|
478
|
+
legacyStorage,
|
|
479
|
+
};
|
|
480
|
+
await this.sendInit();
|
|
481
|
+
}
|
|
482
|
+
frameReady = false;
|
|
483
|
+
async sendInit() {
|
|
484
|
+
const init = this.init;
|
|
485
|
+
if (!init || !this.frameReady)
|
|
486
|
+
return;
|
|
487
|
+
try {
|
|
488
|
+
await this.channel.request(frame_protocol_1.HostRequest.init, init);
|
|
489
|
+
}
|
|
490
|
+
catch (error) {
|
|
491
|
+
if (!this.channel.closed && this.init === init) {
|
|
492
|
+
console.error(`[${this.opts.pkg.name} screen] didn't start`, error);
|
|
493
|
+
this.opts.pieceEvents()?.onFailed(error instanceof Error ? error.message : String(error));
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/** The tab's path changed. */
|
|
498
|
+
setPath(path) {
|
|
499
|
+
this.tabPath(path);
|
|
500
|
+
}
|
|
501
|
+
/** Whether the frame has been told what to show. */
|
|
502
|
+
get started() {
|
|
503
|
+
return this.init !== undefined;
|
|
504
|
+
}
|
|
505
|
+
dispose() {
|
|
506
|
+
liveSessions.delete(this);
|
|
507
|
+
if (!liveSessions.size)
|
|
508
|
+
document.removeEventListener("mousedown", relayHostPointers, true);
|
|
509
|
+
for (const dispose of this.disposers.splice(0))
|
|
510
|
+
dispose();
|
|
511
|
+
this.pvars.dispose();
|
|
512
|
+
this.rpc.dispose();
|
|
513
|
+
this.contractSession?.dispose();
|
|
514
|
+
this.contractSession = undefined;
|
|
515
|
+
this.channel.close();
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Show one package screen in a frame of its own.
|
|
520
|
+
*
|
|
521
|
+
* @param props.pkg - The screen's package.
|
|
522
|
+
* @param props.bundle - Its UI bundle, read and checked by the loader.
|
|
523
|
+
* @param props.peersUIId - The UI to show.
|
|
524
|
+
* @param props.props - The UI's props: data only.
|
|
525
|
+
* @param props.path - The tab's path.
|
|
526
|
+
*/
|
|
527
|
+
function IsolatedScreen(props) {
|
|
528
|
+
const tab = (0, react_1.useContext)(screen_tab_context_1.ScreenTabContext);
|
|
529
|
+
const content = (0, react_1.useMemo)(() => ({ kind: "screen", peersUIId: props.peersUIId, props: props.props, path: props.path }),
|
|
530
|
+
// The props object is new on every render; its contents decide.
|
|
531
|
+
[props.peersUIId, JSON.stringify(props.props), props.path]);
|
|
532
|
+
return ((0, jsx_runtime_1.jsx)(PackageFrame, { pkg: props.pkg, bundle: props.bundle, content: content, tabId: tab?.tabId, label: props.pkg.name }));
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* A package's frame: one of its screens, or one of its pieces for an embedder, acting as the
|
|
536
|
+
* package. A frame whose document was replaced is dropped, and the next key makes a new element.
|
|
537
|
+
*/
|
|
538
|
+
function PackageFrame(props) {
|
|
539
|
+
const [generation, setGeneration] = (0, react_1.useState)(0);
|
|
540
|
+
return ((0, jsx_runtime_1.jsx)(PackageFrameElement, { ...props, onReplaced: () => setGeneration((value) => value + 1) }, `${props.pkg.packageId}:${props.bundle.packageVersionId}:${props.bundle.uiBundleFileId}:${props.bundle.dataContextId}:${generation}`));
|
|
541
|
+
}
|
|
542
|
+
function PackageFrameElement(props) {
|
|
543
|
+
const { pkg, content } = props;
|
|
544
|
+
const tabIdRef = (0, react_1.useRef)(props.tabId);
|
|
545
|
+
tabIdRef.current = props.tabId;
|
|
546
|
+
const eventsRef = (0, react_1.useRef)(props.pieceEvents);
|
|
547
|
+
eventsRef.current = props.pieceEvents;
|
|
548
|
+
const frameRef = (0, react_1.useRef)(null);
|
|
549
|
+
const sessionRef = (0, react_1.useRef)();
|
|
550
|
+
const [frameId] = (0, react_1.useState)(() => (0, peers_sdk_1.newid)());
|
|
551
|
+
const [srcDoc] = (0, react_1.useState)(() => (0, frame_document_1.frameDocument)((0, color_mode_dropdown_1.colorMode)() === "light" ? "light" : "dark", content.kind === "piece"));
|
|
552
|
+
const [failure, setFailure] = (0, react_1.useState)();
|
|
553
|
+
const [slots] = (0, react_1.useState)(() => (0, peers_sdk_1.observable)([]));
|
|
554
|
+
const [shownSlots] = (0, hooks_1.useObservable)(slots);
|
|
555
|
+
const latest = (0, react_1.useRef)(props);
|
|
556
|
+
latest.current = props;
|
|
557
|
+
const onReplaced = (0, react_1.useRef)(props.onReplaced);
|
|
558
|
+
onReplaced.current = props.onReplaced;
|
|
559
|
+
// The srcdoc sends its only hello as soon as it loads. Listen in the mount commit.
|
|
560
|
+
(0, react_1.useLayoutEffect)(() => {
|
|
561
|
+
const element = frameRef.current;
|
|
562
|
+
if (!element)
|
|
563
|
+
return;
|
|
564
|
+
// Keep the identity and bundle this element booted with across metadata refreshes.
|
|
565
|
+
const { pkg, bundle } = latest.current;
|
|
566
|
+
let booted = false;
|
|
567
|
+
let disposed = false;
|
|
568
|
+
const onMessage = (event) => {
|
|
569
|
+
if (event.source !== element.contentWindow)
|
|
570
|
+
return;
|
|
571
|
+
if (event.data?.type !== frame_document_1.FRAME_HELLO)
|
|
572
|
+
return;
|
|
573
|
+
if (booted) {
|
|
574
|
+
// Revoke a pending transfer now, before React commits the replacement.
|
|
575
|
+
disposed = true;
|
|
576
|
+
console.warn(`[${pkg.name}] its frame's document was replaced; starting it again`);
|
|
577
|
+
onReplaced.current();
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
booted = true;
|
|
581
|
+
void (async () => {
|
|
582
|
+
let runtime;
|
|
583
|
+
try {
|
|
584
|
+
runtime = await (0, config_1.frameRuntimeSource)();
|
|
585
|
+
}
|
|
586
|
+
catch (error) {
|
|
587
|
+
if (disposed)
|
|
588
|
+
return;
|
|
589
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
590
|
+
if (eventsRef.current)
|
|
591
|
+
eventsRef.current.onFailed(message);
|
|
592
|
+
else
|
|
593
|
+
setFailure(message);
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
if (disposed || !element.contentWindow)
|
|
597
|
+
return;
|
|
598
|
+
const { port1, port2 } = new MessageChannel();
|
|
599
|
+
const session = new FrameSession({
|
|
600
|
+
port: port1,
|
|
601
|
+
frameId,
|
|
602
|
+
element,
|
|
603
|
+
pkg,
|
|
604
|
+
bundle,
|
|
605
|
+
tabId: () => tabIdRef.current,
|
|
606
|
+
pieceEvents: () => eventsRef.current,
|
|
607
|
+
slots,
|
|
608
|
+
slotOf: () => latest.current.slotOf?.(),
|
|
609
|
+
restart: () => onReplaced.current(),
|
|
610
|
+
});
|
|
611
|
+
sessionRef.current = session;
|
|
612
|
+
element.contentWindow.postMessage({ type: frame_document_1.FRAME_BOOT, runtime }, "*", [port2]);
|
|
613
|
+
// Open its contract session while the runtime starts, so the first calls find it ready.
|
|
614
|
+
session.openContractSession();
|
|
615
|
+
await session.show(() => latest.current.content);
|
|
616
|
+
})();
|
|
617
|
+
};
|
|
618
|
+
window.addEventListener("message", onMessage);
|
|
619
|
+
return () => {
|
|
620
|
+
disposed = true;
|
|
621
|
+
window.removeEventListener("message", onMessage);
|
|
622
|
+
sessionRef.current?.dispose();
|
|
623
|
+
sessionRef.current = undefined;
|
|
624
|
+
slots([]);
|
|
625
|
+
};
|
|
626
|
+
}, [frameId, slots]);
|
|
627
|
+
// Another UI of the package, new props or a new path: the same frame shows it.
|
|
628
|
+
(0, react_1.useEffect)(() => {
|
|
629
|
+
const session = sessionRef.current;
|
|
630
|
+
if (!session?.started)
|
|
631
|
+
return;
|
|
632
|
+
if (content.kind === "screen")
|
|
633
|
+
session.setPath(content.path);
|
|
634
|
+
void session.show(() => latest.current.content);
|
|
635
|
+
}, [content]);
|
|
636
|
+
if (failure !== undefined) {
|
|
637
|
+
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: [pkg.name, " couldn't start."] }), (0, jsx_runtime_1.jsx)("div", { className: "small text-break", children: failure }), (0, jsx_runtime_1.jsx)("button", { type: "button", className: "btn btn-sm btn-outline-danger mt-2", onClick: props.onReplaced, children: "Retry" })] }) }));
|
|
638
|
+
}
|
|
639
|
+
const frameStyle = {
|
|
640
|
+
border: 0,
|
|
641
|
+
width: "100%",
|
|
642
|
+
height: "100%",
|
|
643
|
+
display: "block",
|
|
644
|
+
};
|
|
645
|
+
let frameClass = content.kind === "screen" ? "peers-screen-frame" : "peers-piece-frame";
|
|
646
|
+
if (content.kind === "piece" && content.width !== undefined) {
|
|
647
|
+
// PopoverSurface positions the picker over other content and supplies no background.
|
|
648
|
+
frameClass += " bg-body";
|
|
649
|
+
// Keep the viewport at the cap so vw units and media queries can't feed back into the
|
|
650
|
+
// reported width. The piece draws at (0, 0), regardless of its embedder's writing direction.
|
|
651
|
+
frameStyle.width = content.width;
|
|
652
|
+
frameStyle.position = "absolute";
|
|
653
|
+
frameStyle.top = 0;
|
|
654
|
+
frameStyle.left = 0;
|
|
655
|
+
}
|
|
656
|
+
return ((0, jsx_runtime_1.jsxs)("div", { className: "peers-package-frame", style: { position: "relative", width: "100%", height: "100%", overflow: FRAME_OVERFLOW }, children: [(0, jsx_runtime_1.jsx)("iframe", { ref: frameRef, title: props.label, srcDoc: srcDoc, sandbox: "allow-scripts allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-downloads", allow: "clipboard-read; clipboard-write; fullscreen; autoplay", "data-peers-frame": frameId, "data-peers-package": pkg.packageId, className: frameClass, style: frameStyle }), shownSlots.map((slot) => ((0, jsx_runtime_1.jsx)(PieceSlotFrame, { slot: slot, session: sessionRef }, slot.slotId)))] }));
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* A piece a framed screen embeds, drawn over the slot the screen reports: a frame of its own,
|
|
660
|
+
* acting as its provider, placed and clipped where the slot shows. Its emits, close, size and
|
|
661
|
+
* failure go back to the screen, which hands them to its `<Piece>`.
|
|
662
|
+
*/
|
|
663
|
+
function PieceSlotFrame(props) {
|
|
664
|
+
const { slot, session } = props;
|
|
665
|
+
const { slotId, piece } = slot;
|
|
666
|
+
const events = (0, react_1.useMemo)(() => ({
|
|
667
|
+
// Only what the embedder's version declares crosses, and `open`, which every piece has.
|
|
668
|
+
onEmit: (name, payload) => {
|
|
669
|
+
if (name !== "open" && !piece.emits.some((emit) => emit.name === name))
|
|
670
|
+
return;
|
|
671
|
+
session.current?.slotNotify(frame_protocol_1.HostNotice.pieceEmit, { slotId, name, payload });
|
|
672
|
+
},
|
|
673
|
+
onClose: () => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceClose, { slotId }),
|
|
674
|
+
onSize: (height, width) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceSize, width === undefined ? { slotId, height } : { slotId, height, width }),
|
|
675
|
+
onFailed: (message) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceFailed, { slotId, message }),
|
|
676
|
+
onKey: (key) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceKey, { slotId, ...key }),
|
|
677
|
+
onWheel: (deltaX, deltaY) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceWheel, { slotId, deltaX, deltaY }),
|
|
678
|
+
}), [slotId, session, piece]);
|
|
679
|
+
const slotOf = (0, react_1.useMemo)(() => () => ({ session: session.current, slotId }), [session, slotId]);
|
|
680
|
+
const rect = slot.rect;
|
|
681
|
+
if (!rect)
|
|
682
|
+
return null;
|
|
683
|
+
const clip = slot.clip;
|
|
684
|
+
return ((0, jsx_runtime_1.jsx)("div", { "data-peers-piece-slot": slotId, style: {
|
|
685
|
+
position: "absolute",
|
|
686
|
+
left: rect.x,
|
|
687
|
+
top: rect.y,
|
|
688
|
+
width: rect.width,
|
|
689
|
+
height: rect.height,
|
|
690
|
+
...(clip
|
|
691
|
+
? { clipPath: `inset(${clip.top}px ${clip.right}px ${clip.bottom}px ${clip.left}px)` }
|
|
692
|
+
: {}),
|
|
693
|
+
}, children: (0, jsx_runtime_1.jsx)(PackageFrame, { pkg: slot.bundle.pkg, bundle: slot.bundle, content: slot.content, pieceEvents: events, slotOf: slotOf, label: slot.bundle.pkg.name }) }));
|
|
694
|
+
}
|
|
695
|
+
(0, frame_component_1.registerPackageFrame)(PackageFrame);
|