@peers-app/peers-ui 0.33.24 → 0.34.0
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 +26 -0
- package/dist/isolated-screens/frame-document.js +69 -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 +185 -0
- package/dist/isolated-screens/frame-protocol.js +139 -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 +416 -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 +191 -0
- package/dist/isolated-screens/isolated-screen.js +684 -0
- package/dist/isolated-screens/piece-frames.d.ts +27 -0
- package/dist/isolated-screens/piece-frames.js +311 -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/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,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createFramePvarService = createFramePvarService;
|
|
4
|
+
const peers_sdk_1 = require("@peers-app/peers-sdk");
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
/**
|
|
7
|
+
* The host side of an isolated screen's persistent variables.
|
|
8
|
+
*
|
|
9
|
+
* A screen's frame asks the host for every variable its code (and peers-ui's, inside the frame)
|
|
10
|
+
* declares. The host answers in one of three ways:
|
|
11
|
+
*
|
|
12
|
+
* - `mainContentPath` is the screen's tab path; writing it navigates the tab.
|
|
13
|
+
* - A few variables Peers keeps for every screen (this device, the active group, the theme, the
|
|
14
|
+
* open tabs) are the host's own, read only. A write is ignored, and the frame is told the
|
|
15
|
+
* host's value again.
|
|
16
|
+
* - Every other variable is the package's own, stored under the package's id
|
|
17
|
+
* (`<packageId>_<name>`), as a variable its worker declares is. A name that already starts
|
|
18
|
+
* with the package's id is used as it is.
|
|
19
|
+
*
|
|
20
|
+
* Secret variables aren't served to screens.
|
|
21
|
+
*/
|
|
22
|
+
/** Host variables every screen reads, and none writes. */
|
|
23
|
+
const HOST_READ_ONLY_PVARS = new Map([
|
|
24
|
+
["thisDeviceId", "device"],
|
|
25
|
+
["currentlyActiveGroupId", "device"],
|
|
26
|
+
["colorMode", "device"],
|
|
27
|
+
["colorModePreference", "device"],
|
|
28
|
+
["activeTabs", "groupDevice"],
|
|
29
|
+
["activeTabId", "groupDevice"],
|
|
30
|
+
["tabHistory", "groupDevice"],
|
|
31
|
+
]);
|
|
32
|
+
/** The variable that is the screen's tab path. */
|
|
33
|
+
const TAB_PATH_PVAR = "mainContentPath";
|
|
34
|
+
function makeVar(scope, name, defaultValue, dataContext) {
|
|
35
|
+
const opts = {
|
|
36
|
+
...(defaultValue !== undefined ? { defaultValue } : {}),
|
|
37
|
+
...(dataContext ? { dataContext } : {}),
|
|
38
|
+
};
|
|
39
|
+
switch (scope) {
|
|
40
|
+
case "device":
|
|
41
|
+
return (0, peers_sdk_1.deviceVar)(name, opts);
|
|
42
|
+
case "user":
|
|
43
|
+
return (0, peers_sdk_1.userVar)(name, opts);
|
|
44
|
+
case "group":
|
|
45
|
+
return (0, peers_sdk_1.groupVar)(name, opts);
|
|
46
|
+
case "groupDevice":
|
|
47
|
+
return (0, peers_sdk_1.groupDeviceVar)(name, opts);
|
|
48
|
+
case "groupUser":
|
|
49
|
+
return (0, peers_sdk_1.groupUserVar)(name, opts);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async function pinnedDataContext(dataContextId) {
|
|
53
|
+
if (!dataContextId)
|
|
54
|
+
return undefined;
|
|
55
|
+
const userContext = await (0, peers_sdk_1.getUserContext)();
|
|
56
|
+
if (dataContextId !== userContext.userId && !userContext.groupIds().includes(dataContextId)) {
|
|
57
|
+
throw new Error(`Permission denied: data context '${dataContextId}' is not available here`);
|
|
58
|
+
}
|
|
59
|
+
return userContext.getDataContext(dataContextId);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Serve one isolated screen's persistent variables.
|
|
63
|
+
*
|
|
64
|
+
* @param host - The screen's package, tab path and channel.
|
|
65
|
+
*/
|
|
66
|
+
function createFramePvarService(host) {
|
|
67
|
+
const bindings = new Map();
|
|
68
|
+
// A bind still loading when the screen closes must not start following its variable after.
|
|
69
|
+
let disposed = false;
|
|
70
|
+
const bindTabPath = (bindingId) => {
|
|
71
|
+
if (disposed)
|
|
72
|
+
return undefined;
|
|
73
|
+
const subscription = host.tabPath.subscribe((path) => host.changed(bindingId, path));
|
|
74
|
+
bindings.set(bindingId, {
|
|
75
|
+
write: async (value) => {
|
|
76
|
+
if (typeof value === "string" && value !== host.tabPath())
|
|
77
|
+
host.navigate(value);
|
|
78
|
+
},
|
|
79
|
+
delete: async () => undefined,
|
|
80
|
+
stop: () => subscription.dispose(),
|
|
81
|
+
});
|
|
82
|
+
return host.tabPath();
|
|
83
|
+
};
|
|
84
|
+
const bindVar = async (bindingId, variable, readOnly) => {
|
|
85
|
+
await variable.loadingPromise;
|
|
86
|
+
if (disposed)
|
|
87
|
+
return undefined;
|
|
88
|
+
const subscription = variable.subscribe((value) => host.changed(bindingId, value));
|
|
89
|
+
bindings.set(bindingId, {
|
|
90
|
+
write: async (value) => {
|
|
91
|
+
if (readOnly) {
|
|
92
|
+
// The host's own: the frame goes back to the host's value.
|
|
93
|
+
if (!(0, lodash_1.isEqual)(value, variable()))
|
|
94
|
+
host.changed(bindingId, variable());
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
variable(value);
|
|
98
|
+
await variable.loadingPromise;
|
|
99
|
+
},
|
|
100
|
+
delete: async () => {
|
|
101
|
+
if (!readOnly)
|
|
102
|
+
await variable.delete();
|
|
103
|
+
},
|
|
104
|
+
stop: () => subscription.dispose(),
|
|
105
|
+
});
|
|
106
|
+
return variable();
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
async bind({ bindingId, request }) {
|
|
110
|
+
bindings.get(bindingId)?.stop();
|
|
111
|
+
const { name, scope } = request;
|
|
112
|
+
if (request.isSecret) {
|
|
113
|
+
throw new Error(`Permission denied: screens can't use secret variables (${name})`);
|
|
114
|
+
}
|
|
115
|
+
if (name === TAB_PATH_PVAR)
|
|
116
|
+
return bindTabPath(bindingId);
|
|
117
|
+
const dataContext = await pinnedDataContext(request.dataContextId);
|
|
118
|
+
if (disposed)
|
|
119
|
+
return undefined;
|
|
120
|
+
const hostScope = HOST_READ_ONLY_PVARS.get(name);
|
|
121
|
+
if (hostScope) {
|
|
122
|
+
// The SDK migrates stored rows on a scope mismatch, even for a caller that only reads.
|
|
123
|
+
if (scope !== hostScope) {
|
|
124
|
+
throw new Error(`Permission denied: ${name} is a ${hostScope} variable`);
|
|
125
|
+
}
|
|
126
|
+
return bindVar(bindingId, makeVar(hostScope, name, undefined, dataContext), true);
|
|
127
|
+
}
|
|
128
|
+
const stored = name.startsWith(`${host.packageId}_`)
|
|
129
|
+
? name
|
|
130
|
+
: (0, peers_sdk_1.packageScopedPersistentVarName)(host.packageId, name);
|
|
131
|
+
return bindVar(bindingId, makeVar(scope, stored, request.defaultValue, dataContext), false);
|
|
132
|
+
},
|
|
133
|
+
async write({ bindingId, value }) {
|
|
134
|
+
const binding = bindings.get(bindingId);
|
|
135
|
+
if (!binding)
|
|
136
|
+
throw new Error("That variable isn't bound");
|
|
137
|
+
await binding.write(value);
|
|
138
|
+
},
|
|
139
|
+
async delete({ bindingId }) {
|
|
140
|
+
const binding = bindings.get(bindingId);
|
|
141
|
+
if (!binding)
|
|
142
|
+
throw new Error("That variable isn't bound");
|
|
143
|
+
await binding.delete();
|
|
144
|
+
},
|
|
145
|
+
dispose() {
|
|
146
|
+
disposed = true;
|
|
147
|
+
for (const binding of bindings.values())
|
|
148
|
+
binding.stop();
|
|
149
|
+
bindings.clear();
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { IPortChannel } from "./port-channel";
|
|
2
|
+
/** The channel to the host, inside a package screen's frame; `undefined` in the host's window. */
|
|
3
|
+
export declare function frameHost(): IPortChannel | undefined;
|
|
4
|
+
/** Whether this realm is a package screen's frame. */
|
|
5
|
+
export declare function isInFrame(): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* Mark this realm as a package screen's frame. Only the frame bridge calls it, once, before any
|
|
8
|
+
* package code runs.
|
|
9
|
+
*
|
|
10
|
+
* @param hostChannel - The channel to the host, or `undefined` to clear it (tests).
|
|
11
|
+
*/
|
|
12
|
+
export declare function setFrameHost(hostChannel: IPortChannel | undefined): void;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.frameHost = frameHost;
|
|
4
|
+
exports.isInFrame = isInFrame;
|
|
5
|
+
exports.setFrameHost = setFrameHost;
|
|
6
|
+
/**
|
|
7
|
+
* Whether this realm is an isolated package screen's frame, and its channel to the host.
|
|
8
|
+
*
|
|
9
|
+
* peers-ui runs in two kinds of realm: the host's window, which draws the shell and owns the tabs,
|
|
10
|
+
* and each package screen's frame. A few of its functions do something different in a frame:
|
|
11
|
+
* navigation, tab titles, opening links and mentions ask the host instead of changing host state
|
|
12
|
+
* the frame can't reach. They call {@link frameHost} to tell which realm they're in.
|
|
13
|
+
*/
|
|
14
|
+
let channel;
|
|
15
|
+
/** The channel to the host, inside a package screen's frame; `undefined` in the host's window. */
|
|
16
|
+
function frameHost() {
|
|
17
|
+
return channel;
|
|
18
|
+
}
|
|
19
|
+
/** Whether this realm is a package screen's frame. */
|
|
20
|
+
function isInFrame() {
|
|
21
|
+
return channel !== undefined;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Mark this realm as a package screen's frame. Only the frame bridge calls it, once, before any
|
|
25
|
+
* package code runs.
|
|
26
|
+
*
|
|
27
|
+
* @param hostChannel - The channel to the host, or `undefined` to clear it (tests).
|
|
28
|
+
*/
|
|
29
|
+
function setFrameHost(hostChannel) {
|
|
30
|
+
channel = hostChannel;
|
|
31
|
+
}
|
|
@@ -0,0 +1,416 @@
|
|
|
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
|
+
let root;
|
|
19
|
+
let setScreen;
|
|
20
|
+
let pending = { props: {} };
|
|
21
|
+
const uis = new Map();
|
|
22
|
+
const pieces = new Map();
|
|
23
|
+
let loadedVersionId;
|
|
24
|
+
/** Whether this frame draws a piece, whose Escape goes to its embedder. */
|
|
25
|
+
let drawsPiece = false;
|
|
26
|
+
function applyTheme(mode) {
|
|
27
|
+
const html = document.documentElement;
|
|
28
|
+
html.setAttribute("data-bs-theme", mode);
|
|
29
|
+
const background = mode === "dark" ? "rgb(33, 37, 41)" : "";
|
|
30
|
+
html.style.backgroundColor = background;
|
|
31
|
+
document.body.style.backgroundColor = background;
|
|
32
|
+
}
|
|
33
|
+
class ScreenErrorBoundary extends react_1.Component {
|
|
34
|
+
state = {};
|
|
35
|
+
static getDerivedStateFromError(error) {
|
|
36
|
+
return { error: error instanceof Error ? error : new Error(String(error)) };
|
|
37
|
+
}
|
|
38
|
+
componentDidCatch(error, info) {
|
|
39
|
+
console.error(`${this.props.packageName}'s screen threw`, error, info.componentStack);
|
|
40
|
+
this.props.onError(this.state.error?.message || String(error));
|
|
41
|
+
}
|
|
42
|
+
render() {
|
|
43
|
+
if (this.state.error) {
|
|
44
|
+
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" })] }) }));
|
|
45
|
+
}
|
|
46
|
+
return this.props.children;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Overlays peers-ui registers for itself when it loads (the priority alert, the delegate request
|
|
51
|
+
* dialog). They're the host's, drawn by the host's window, so a frame leaves them out.
|
|
52
|
+
*/
|
|
53
|
+
let peersUIOverlayIds = new Set();
|
|
54
|
+
/** The package's global overlays, drawn inside its own screen until global widgets exist. */
|
|
55
|
+
function FrameOverlays() {
|
|
56
|
+
const [overlays] = (0, hooks_1.useObservable)(global_overlays_1.globalOverlays);
|
|
57
|
+
return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: overlays
|
|
58
|
+
.filter(({ id }) => !peersUIOverlayIds.has(id))
|
|
59
|
+
.map(({ id, Component: Overlay }) => ((0, jsx_runtime_1.jsx)(Overlay, {}, id))) }));
|
|
60
|
+
}
|
|
61
|
+
function FrameScreen(props) {
|
|
62
|
+
const [state, setState] = (0, react_1.useState)(pending);
|
|
63
|
+
const [desktop] = (0, hooks_1.useObservable)(globals_1.isDesktop);
|
|
64
|
+
(0, react_1.useEffect)(() => {
|
|
65
|
+
setScreen = setState;
|
|
66
|
+
setState(pending);
|
|
67
|
+
return () => {
|
|
68
|
+
setScreen = undefined;
|
|
69
|
+
};
|
|
70
|
+
}, []);
|
|
71
|
+
(0, react_1.useEffect)(() => {
|
|
72
|
+
if (state.ui)
|
|
73
|
+
props.host.notify(frame_protocol_1.FrameNotice.rendered);
|
|
74
|
+
}, [state.ui, props.host]);
|
|
75
|
+
// A bundle can throw an error with no message: it still failed.
|
|
76
|
+
if (state.error !== undefined) {
|
|
77
|
+
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" }))] }) }));
|
|
78
|
+
}
|
|
79
|
+
if (state.piece) {
|
|
80
|
+
return ((0, jsx_runtime_1.jsx)(PieceInFrame, { host: props.host, packageName: props.packageName, piece: state.piece, props: state.props }));
|
|
81
|
+
}
|
|
82
|
+
if (!state.ui)
|
|
83
|
+
return null;
|
|
84
|
+
const Screen = state.ui.content;
|
|
85
|
+
// The same box a tab draws around a screen in the host, so a screen's CSS that cancels the
|
|
86
|
+
// tab's padding (`.p-3 > .x-app`) still lines up.
|
|
87
|
+
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, {})] }) }));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The emit functions a piece drawn in a frame gets: each emit the embedder's version declares,
|
|
91
|
+
* and `open`, sent to the host, which checks them and hands them to the embedder; any other name
|
|
92
|
+
* does nothing, as in the window. They're the object's own properties, so a piece can spread
|
|
93
|
+
* them into a control.
|
|
94
|
+
*/
|
|
95
|
+
function frameEmitters(host, names) {
|
|
96
|
+
const declared = Object.create(null);
|
|
97
|
+
for (const name of new Set([...names, "open"])) {
|
|
98
|
+
declared[name] = (payload) => host.notify(frame_protocol_1.FrameNotice.pieceEmit, { name, payload });
|
|
99
|
+
}
|
|
100
|
+
return new Proxy(declared, {
|
|
101
|
+
get: (target, name) => typeof name === "string" ? (target[name] ?? (() => undefined)) : undefined,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* A piece drawn in its own frame, acting as its provider. Its emits and `close` go to the host,
|
|
106
|
+
* which hands them to the embedder; a piece that takes its own height reports it as it changes,
|
|
107
|
+
* and one in a popover its width too. `usePieceSize` reads the box it's drawn in.
|
|
108
|
+
*/
|
|
109
|
+
function PieceInFrame(props) {
|
|
110
|
+
const { host, piece } = props;
|
|
111
|
+
const emit = (0, react_1.useMemo)(() => frameEmitters(host, piece.emits), [host, piece.emits.join("\n")]);
|
|
112
|
+
const Component = piece.component;
|
|
113
|
+
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 }) }) }));
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The box a framed piece draws in: measured for `usePieceSize`, and, for a piece that takes its
|
|
117
|
+
* own height (and in a popover its width), reported to the host. The size lives here, apart from
|
|
118
|
+
* the piece, so a resize re-renders only what reads it, as `<Piece>`'s box does in the window.
|
|
119
|
+
*/
|
|
120
|
+
function PieceRoot(props) {
|
|
121
|
+
const { host, height: mode, width: maxWidth } = props;
|
|
122
|
+
const rootRef = (0, react_1.useRef)(null);
|
|
123
|
+
const [size, setSize] = (0, react_1.useState)({ width: 0, height: 0 });
|
|
124
|
+
(0, react_1.useEffect)(() => {
|
|
125
|
+
const root = rootRef.current;
|
|
126
|
+
if (!root)
|
|
127
|
+
return;
|
|
128
|
+
let last = "";
|
|
129
|
+
const report = () => {
|
|
130
|
+
const rect = root.getBoundingClientRect();
|
|
131
|
+
setSize((previous) => previous.width === rect.width && previous.height === rect.height
|
|
132
|
+
? previous
|
|
133
|
+
: { width: rect.width, height: rect.height });
|
|
134
|
+
if (mode !== "content")
|
|
135
|
+
return;
|
|
136
|
+
const height = Math.ceil(root.scrollHeight);
|
|
137
|
+
const width = maxWidth === undefined ? undefined : Math.ceil(rect.width);
|
|
138
|
+
const key = `${height}x${width}`;
|
|
139
|
+
if (key === last)
|
|
140
|
+
return;
|
|
141
|
+
last = key;
|
|
142
|
+
host.notify(frame_protocol_1.FrameNotice.size, width === undefined ? { height } : { height, width });
|
|
143
|
+
};
|
|
144
|
+
report();
|
|
145
|
+
const observer = new ResizeObserver(report);
|
|
146
|
+
observer.observe(root);
|
|
147
|
+
return () => observer.disconnect();
|
|
148
|
+
}, [host, mode, maxWidth]);
|
|
149
|
+
// In a popover the piece is as wide as its content, up to the width the embedder allows. The
|
|
150
|
+
// frame is that wide too, and the host shows only the width reported here.
|
|
151
|
+
const style = maxWidth === undefined
|
|
152
|
+
? undefined
|
|
153
|
+
: { position: "absolute", top: 0, left: 0, width: "max-content", maxWidth };
|
|
154
|
+
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 }) }));
|
|
155
|
+
}
|
|
156
|
+
function show(next) {
|
|
157
|
+
pending = next;
|
|
158
|
+
setScreen?.(next);
|
|
159
|
+
}
|
|
160
|
+
/** The bundle run under way, so a second init while it runs waits for it instead of running it again. */
|
|
161
|
+
let loading;
|
|
162
|
+
/** Run the package's UI bundle in this frame, once per version, keeping the UIs it exports. */
|
|
163
|
+
function loadBundle(init) {
|
|
164
|
+
if (loadedVersionId === init.packageVersionId)
|
|
165
|
+
return Promise.resolve();
|
|
166
|
+
if (loading?.versionId === init.packageVersionId)
|
|
167
|
+
return loading.promise;
|
|
168
|
+
const promise = runBundle(init).finally(() => {
|
|
169
|
+
if (loading?.promise === promise)
|
|
170
|
+
loading = undefined;
|
|
171
|
+
});
|
|
172
|
+
loading = { versionId: init.packageVersionId, promise };
|
|
173
|
+
return promise;
|
|
174
|
+
}
|
|
175
|
+
async function runBundle(init) {
|
|
176
|
+
const exportUIs = (exported) => {
|
|
177
|
+
for (const ui of exported?.uis ?? [])
|
|
178
|
+
uis.set(ui.peersUIId, ui);
|
|
179
|
+
for (const piece of exported?.pieces ?? []) {
|
|
180
|
+
pieces.set((0, implement_piece_1.pieceKey)(piece.contractId, piece.version, piece.name), piece.component);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
const run = new Function("exportUIs", init.bundleCode);
|
|
184
|
+
await run(exportUIs);
|
|
185
|
+
loadedVersionId = init.packageVersionId;
|
|
186
|
+
}
|
|
187
|
+
/** Numbers each init, so an older one that finishes later doesn't replace a newer one's screen. */
|
|
188
|
+
let latestInit = 0;
|
|
189
|
+
async function handleInit(host, init) {
|
|
190
|
+
const thisInit = ++latestInit;
|
|
191
|
+
drawsPiece = init.piece !== undefined;
|
|
192
|
+
const firstInit = !root;
|
|
193
|
+
if (firstInit) {
|
|
194
|
+
const bridge = (0, frame_bridge_1.frameBridge)();
|
|
195
|
+
bridge?.storage.load(init.storage, init.legacyStorage);
|
|
196
|
+
(0, peers_sdk_1.setDefaultClientUserContext)(init.userId);
|
|
197
|
+
applyTheme(init.colorMode);
|
|
198
|
+
const container = document.getElementById("peers-frame-root");
|
|
199
|
+
if (!container)
|
|
200
|
+
throw new Error("The frame has no #peers-frame-root");
|
|
201
|
+
root = (0, client_1.createRoot)(container);
|
|
202
|
+
root.render((0, jsx_runtime_1.jsx)(FrameScreen, { packageName: init.packageName, host: host }));
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
// The user context loads the active group and this device: code that reads them while it
|
|
206
|
+
// renders gets the right ones, not the personal context it starts with.
|
|
207
|
+
await (0, peers_sdk_1.getUserContext)();
|
|
208
|
+
await loadBundle(init);
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
212
|
+
console.error(`${init.packageName}'s UI bundle failed to run`, error);
|
|
213
|
+
show({ props: {}, error: message });
|
|
214
|
+
host.notify(frame_protocol_1.FrameNotice.failed, { message });
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
// A newer init arrived while this one waited: it shows instead.
|
|
218
|
+
if (thisInit !== latestInit)
|
|
219
|
+
return;
|
|
220
|
+
if (init.piece) {
|
|
221
|
+
const component = pieces.get(init.piece.key);
|
|
222
|
+
if (!component) {
|
|
223
|
+
const message = `Its UI bundle registered no component for the piece ${init.piece.key}`;
|
|
224
|
+
show({ props: {}, error: message });
|
|
225
|
+
host.notify(frame_protocol_1.FrameNotice.failed, { message });
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const width = init.piece.width;
|
|
229
|
+
show({
|
|
230
|
+
props: init.props,
|
|
231
|
+
piece: {
|
|
232
|
+
component,
|
|
233
|
+
groupId: init.piece.groupId,
|
|
234
|
+
height: init.piece.height,
|
|
235
|
+
...(typeof width === "number" && Number.isFinite(width) && width > 0 ? { width } : {}),
|
|
236
|
+
emits: Array.isArray(init.piece.emits)
|
|
237
|
+
? init.piece.emits.filter((name) => typeof name === "string")
|
|
238
|
+
: [],
|
|
239
|
+
},
|
|
240
|
+
});
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const ui = init.peersUIId ? uis.get(init.peersUIId) : undefined;
|
|
244
|
+
if (!ui) {
|
|
245
|
+
const message = `Its UI bundle has no UI ${init.peersUIId}`;
|
|
246
|
+
show({ props: {}, error: message });
|
|
247
|
+
host.notify(frame_protocol_1.FrameNotice.failed, { message });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
ui.propsSchema?.parse?.(init.props);
|
|
252
|
+
}
|
|
253
|
+
catch (error) {
|
|
254
|
+
const message = `Its props didn't match: ${error instanceof Error ? error.message : String(error)}`;
|
|
255
|
+
show({ props: {}, error: message });
|
|
256
|
+
host.notify(frame_protocol_1.FrameNotice.failed, { message });
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
show({ ui, props: init.props });
|
|
260
|
+
}
|
|
261
|
+
/** Links to the web open outside Peers, and `#` links navigate the tab, through the host. */
|
|
262
|
+
function installLinkHandling(host) {
|
|
263
|
+
document.addEventListener("click", (event) => {
|
|
264
|
+
if (event.defaultPrevented || event.button !== 0)
|
|
265
|
+
return;
|
|
266
|
+
const anchor = event.target?.closest?.("a[href]");
|
|
267
|
+
if (!anchor || anchor.hasAttribute("download"))
|
|
268
|
+
return;
|
|
269
|
+
const href = anchor.getAttribute("href") ?? "";
|
|
270
|
+
if (href.startsWith("#")) {
|
|
271
|
+
event.preventDefault();
|
|
272
|
+
void host.request(frame_protocol_1.FrameRequest.navigate, { path: href.slice(1) }).catch(() => undefined);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
let url;
|
|
276
|
+
try {
|
|
277
|
+
url = new URL(href, "https://invalid.example/");
|
|
278
|
+
}
|
|
279
|
+
catch {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
// Others (`tel:`, `sms:`) are left to the browser, as the host only opens these.
|
|
283
|
+
if (["http:", "https:", "mailto:"].includes(url.protocol)) {
|
|
284
|
+
event.preventDefault();
|
|
285
|
+
void host.request(frame_protocol_1.FrameRequest.openLink, { url: anchor.href }).catch(() => undefined);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
// A screen that navigates by setting `location.hash` navigates its tab.
|
|
289
|
+
window.addEventListener("hashchange", () => {
|
|
290
|
+
const path = window.location.hash.slice(1);
|
|
291
|
+
void host.request(frame_protocol_1.FrameRequest.navigate, { path }).catch(() => undefined);
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Shortcuts the host always takes, so the frame cancels the browser's own action for them as it
|
|
296
|
+
* forwards them (in a browser tab, Ctrl+K would also focus the browser's search box): the
|
|
297
|
+
* command palette, the console and tab switching.
|
|
298
|
+
*/
|
|
299
|
+
function isHostShortcut(event) {
|
|
300
|
+
if (event.type !== "keydown" || event.altKey)
|
|
301
|
+
return false;
|
|
302
|
+
const key = event.key.toLowerCase();
|
|
303
|
+
if ((event.metaKey || event.ctrlKey) && key === "k")
|
|
304
|
+
return true;
|
|
305
|
+
if (event.ctrlKey && !event.metaKey && event.code === "Backquote")
|
|
306
|
+
return true;
|
|
307
|
+
return event.ctrlKey && key === "tab";
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* ⌘ and Ctrl keys the screen didn't use go to the host, for its shortcuts. In a piece's frame,
|
|
311
|
+
* so does Escape the piece didn't use, for a popover around it: the host hands it to the
|
|
312
|
+
* embedder. A popover inside the frame stops its own Escape before it gets here; a listener the
|
|
313
|
+
* piece added after this one can still take it, so it's sent once the event has reached them all.
|
|
314
|
+
* With ⌘ or Ctrl held, Escape is a shortcut like the others (Ctrl+Escape cancels the host's tab
|
|
315
|
+
* switcher).
|
|
316
|
+
*/
|
|
317
|
+
function installKeyForwarding(host) {
|
|
318
|
+
const forward = (event) => {
|
|
319
|
+
if (event.defaultPrevented)
|
|
320
|
+
return;
|
|
321
|
+
if (event.key === "Escape" &&
|
|
322
|
+
drawsPiece &&
|
|
323
|
+
event.type === "keydown" &&
|
|
324
|
+
!event.metaKey &&
|
|
325
|
+
!event.ctrlKey) {
|
|
326
|
+
setTimeout(() => {
|
|
327
|
+
if (event.defaultPrevented)
|
|
328
|
+
return;
|
|
329
|
+
host.notify(frame_protocol_1.FrameNotice.key, { type: event.type, key: event.key, code: event.code });
|
|
330
|
+
}, 0);
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const modifierKey = event.key === "Control" || event.key === "Meta";
|
|
334
|
+
if (!(event.metaKey || event.ctrlKey || modifierKey))
|
|
335
|
+
return;
|
|
336
|
+
if (isHostShortcut(event))
|
|
337
|
+
event.preventDefault();
|
|
338
|
+
host.notify(frame_protocol_1.FrameNotice.key, {
|
|
339
|
+
type: event.type,
|
|
340
|
+
key: event.key,
|
|
341
|
+
code: event.code,
|
|
342
|
+
metaKey: event.metaKey,
|
|
343
|
+
ctrlKey: event.ctrlKey,
|
|
344
|
+
shiftKey: event.shiftKey,
|
|
345
|
+
altKey: event.altKey,
|
|
346
|
+
repeat: event.repeat,
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
window.addEventListener("keydown", forward);
|
|
350
|
+
window.addEventListener("keyup", forward);
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* A pointer down in this frame goes to the host, which tells the other frames and its own window
|
|
354
|
+
* (a click in a frame reaches no document around it); one elsewhere comes back as
|
|
355
|
+
* `FRAME_POINTER_EVENT` on the body, or on a piece's slot when it was in that piece.
|
|
356
|
+
*/
|
|
357
|
+
function installPointerRelay(host) {
|
|
358
|
+
window.addEventListener("mousedown", () => host.notify(frame_protocol_1.FrameNotice.pointerDown), true);
|
|
359
|
+
host.onNotify(frame_protocol_1.HostNotice.outsidePointerDown, (payload) => {
|
|
360
|
+
// A slot's own component dispatches the ones inside its piece.
|
|
361
|
+
if (payload?.slotId === undefined) {
|
|
362
|
+
document.body.dispatchEvent(new Event(popover_surface_1.FRAME_POINTER_EVENT, { bubbles: true }));
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
/** The screen's warnings and errors also go to the host, whose console the Console app reads. */
|
|
367
|
+
function installConsoleForwarding(host) {
|
|
368
|
+
for (const level of ["warn", "error"]) {
|
|
369
|
+
const original = console[level].bind(console);
|
|
370
|
+
console[level] = (...args) => {
|
|
371
|
+
original(...args);
|
|
372
|
+
try {
|
|
373
|
+
const message = args
|
|
374
|
+
.map((arg) => arg instanceof Error
|
|
375
|
+
? arg.stack || arg.message
|
|
376
|
+
: typeof arg === "string"
|
|
377
|
+
? arg
|
|
378
|
+
: JSON.stringify(arg))
|
|
379
|
+
.join(" ")
|
|
380
|
+
.slice(0, 4000);
|
|
381
|
+
host.notify(frame_protocol_1.FrameNotice.log, { level, message });
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
// A value that won't stringify isn't worth losing the screen over.
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Start the frame runtime. The frame's entry calls it once, after the bridge is installed and
|
|
391
|
+
* the window's `React`, `PeersSDK`, `PeersUI` and `zod` globals are set.
|
|
392
|
+
*/
|
|
393
|
+
function startFrameRuntime() {
|
|
394
|
+
const bridge = (0, frame_bridge_1.frameBridge)();
|
|
395
|
+
if (!bridge)
|
|
396
|
+
throw new Error("startFrameRuntime needs the frame bridge installed first");
|
|
397
|
+
const { host } = bridge;
|
|
398
|
+
peersUIOverlayIds = new Set((0, global_overlays_1.globalOverlays)().map(({ id }) => id));
|
|
399
|
+
host.onRequest(frame_protocol_1.HostRequest.init, (init) => handleInit(host, init));
|
|
400
|
+
host.onRequest(frame_protocol_1.HostRequest.inspect, (input) => (0, ui_inspection_1.getDocumentUIState)({ includeRects: input?.includeRects === true }));
|
|
401
|
+
host.onRequest(frame_protocol_1.HostRequest.act, (input) => (0, ui_inspection_1.actOnDocument)(input.operation, input.input));
|
|
402
|
+
host.onNotify(frame_protocol_1.HostNotice.theme, ({ colorMode }) => applyTheme(colorMode));
|
|
403
|
+
// The host resolves the screen's pieces; when what they depend on changes, they resolve again.
|
|
404
|
+
host.onNotify(frame_protocol_1.HostNotice.pieceRevision, () => (0, piece_resolution_1.pieceRevision)((0, piece_resolution_1.pieceRevision)() + 1));
|
|
405
|
+
installLinkHandling(host);
|
|
406
|
+
installKeyForwarding(host);
|
|
407
|
+
installPointerRelay(host);
|
|
408
|
+
installConsoleForwarding(host);
|
|
409
|
+
window.addEventListener("error", (event) => {
|
|
410
|
+
console.error(event.error ?? event.message);
|
|
411
|
+
});
|
|
412
|
+
window.addEventListener("unhandledrejection", (event) => {
|
|
413
|
+
console.error("Unhandled rejection in a screen:", event.reason);
|
|
414
|
+
});
|
|
415
|
+
host.notify(frame_protocol_1.FrameNotice.ready);
|
|
416
|
+
}
|
|
@@ -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;
|