@peers-app/peers-ui 0.34.0 → 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/isolated-screens/frame-document.d.ts +4 -2
- package/dist/isolated-screens/frame-document.js +12 -4
- package/dist/isolated-screens/frame-protocol.d.ts +11 -0
- package/dist/isolated-screens/frame-protocol.js +11 -0
- package/dist/isolated-screens/frame-runtime.js +85 -7
- package/dist/isolated-screens/isolated-screen.d.ts +2 -0
- package/dist/isolated-screens/isolated-screen.js +13 -2
- package/dist/isolated-screens/piece-frames.js +46 -6
- package/dist/isolated-screens/wheel-relay.d.ts +48 -0
- package/dist/isolated-screens/wheel-relay.js +186 -0
- package/package.json +3 -3
|
@@ -19,8 +19,10 @@ export declare const FRAME_PORT_GLOBAL = "__peersFramePort";
|
|
|
19
19
|
/** The frame's content security policy. */
|
|
20
20
|
export declare const FRAME_CSP: string;
|
|
21
21
|
/**
|
|
22
|
-
* The frame's starting document.
|
|
22
|
+
* The frame's starting document. Its dark color scheme matches the host before Bootstrap loads
|
|
23
|
+
* and follows the theme attribute afterwards, so the browser can composite a transparent piece.
|
|
23
24
|
*
|
|
24
25
|
* @param colorMode - The theme to start in, so the frame doesn't flash the other one.
|
|
26
|
+
* @param transparent - Show the surface behind a piece. A screen paints its own page background.
|
|
25
27
|
*/
|
|
26
|
-
export declare function frameDocument(colorMode: "light" | "dark"): string;
|
|
28
|
+
export declare function frameDocument(colorMode: "light" | "dark", transparent?: boolean): string;
|
|
@@ -35,19 +35,27 @@ exports.FRAME_CSP = [
|
|
|
35
35
|
"base-uri 'none'",
|
|
36
36
|
].join("; ");
|
|
37
37
|
/**
|
|
38
|
-
* The frame's starting document.
|
|
38
|
+
* The frame's starting document. Its dark color scheme matches the host before Bootstrap loads
|
|
39
|
+
* and follows the theme attribute afterwards, so the browser can composite a transparent piece.
|
|
39
40
|
*
|
|
40
41
|
* @param colorMode - The theme to start in, so the frame doesn't flash the other one.
|
|
42
|
+
* @param transparent - Show the surface behind a piece. A screen paints its own page background.
|
|
41
43
|
*/
|
|
42
|
-
function frameDocument(colorMode) {
|
|
43
|
-
const background = colorMode === "dark" ? "#212529" : "#fff";
|
|
44
|
+
function frameDocument(colorMode, transparent = false) {
|
|
45
|
+
const background = transparent ? "transparent" : colorMode === "dark" ? "#212529" : "#fff";
|
|
46
|
+
// The settings module can write inline page colors when its theme preference arrives.
|
|
47
|
+
const surface = transparent ? "html,body{background-color:transparent!important}" : "";
|
|
44
48
|
return `<!doctype html>
|
|
45
49
|
<html data-bs-theme="${colorMode}" style="height:100%;background:${background}">
|
|
46
50
|
<head>
|
|
47
51
|
<meta charset="utf-8">
|
|
48
52
|
<meta http-equiv="Content-Security-Policy" content="${exports.FRAME_CSP}">
|
|
49
53
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
50
|
-
<style>
|
|
54
|
+
<style>
|
|
55
|
+
html,body,#peers-frame-root{height:100%;margin:0}
|
|
56
|
+
html[data-bs-theme="dark"]{color-scheme:dark}
|
|
57
|
+
${surface}
|
|
58
|
+
</style>
|
|
51
59
|
</head>
|
|
52
60
|
<body style="background:${background}">
|
|
53
61
|
<div id="peers-frame-root"></div>
|
|
@@ -134,6 +134,12 @@ export declare const FrameNotice: {
|
|
|
134
134
|
* it, so the host tells every other frame, and its own window, for their popovers.
|
|
135
135
|
*/
|
|
136
136
|
readonly pointerDown: "pointer.down";
|
|
137
|
+
/**
|
|
138
|
+
* From a piece's frame: a wheel the piece didn't scroll with, `{ deltaX, deltaY }` in pixels.
|
|
139
|
+
* Its frame is beside the screen's, so the browser never passes it on; the host hands it to
|
|
140
|
+
* the screen the piece is drawn over.
|
|
141
|
+
*/
|
|
142
|
+
readonly wheel: "frame.wheel";
|
|
137
143
|
/** The user asked to start a screen that failed to load again: the host makes a new frame. */
|
|
138
144
|
readonly restart: "frame.restart";
|
|
139
145
|
};
|
|
@@ -180,6 +186,11 @@ export declare const HostNotice: {
|
|
|
180
186
|
readonly outsidePointerDown: "pointer.outside";
|
|
181
187
|
/** A piece the screen embeds couldn't be drawn: `{ slotId, message }`. */
|
|
182
188
|
readonly pieceFailed: "piece.failed";
|
|
189
|
+
/**
|
|
190
|
+
* A wheel over a piece the screen embeds that the piece didn't scroll with: `{ slotId, deltaX,
|
|
191
|
+
* deltaY }` in pixels. The frame scrolls the boxes around the slot with it.
|
|
192
|
+
*/
|
|
193
|
+
readonly pieceWheel: "piece.wheel";
|
|
183
194
|
/** Something a piece resolution depends on changed: the screen resolves its pieces again. */
|
|
184
195
|
readonly pieceRevision: "piece.revision";
|
|
185
196
|
};
|
|
@@ -88,6 +88,12 @@ exports.FrameNotice = {
|
|
|
88
88
|
* it, so the host tells every other frame, and its own window, for their popovers.
|
|
89
89
|
*/
|
|
90
90
|
pointerDown: "pointer.down",
|
|
91
|
+
/**
|
|
92
|
+
* From a piece's frame: a wheel the piece didn't scroll with, `{ deltaX, deltaY }` in pixels.
|
|
93
|
+
* Its frame is beside the screen's, so the browser never passes it on; the host hands it to
|
|
94
|
+
* the screen the piece is drawn over.
|
|
95
|
+
*/
|
|
96
|
+
wheel: "frame.wheel",
|
|
91
97
|
/** The user asked to start a screen that failed to load again: the host makes a new frame. */
|
|
92
98
|
restart: "frame.restart",
|
|
93
99
|
};
|
|
@@ -134,6 +140,11 @@ exports.HostNotice = {
|
|
|
134
140
|
outsidePointerDown: "pointer.outside",
|
|
135
141
|
/** A piece the screen embeds couldn't be drawn: `{ slotId, message }`. */
|
|
136
142
|
pieceFailed: "piece.failed",
|
|
143
|
+
/**
|
|
144
|
+
* A wheel over a piece the screen embeds that the piece didn't scroll with: `{ slotId, deltaX,
|
|
145
|
+
* deltaY }` in pixels. The frame scrolls the boxes around the slot with it.
|
|
146
|
+
*/
|
|
147
|
+
pieceWheel: "piece.wheel",
|
|
137
148
|
/** Something a piece resolution depends on changed: the screen resolves its pieces again. */
|
|
138
149
|
pieceRevision: "piece.revision",
|
|
139
150
|
};
|
|
@@ -15,17 +15,24 @@ const popover_surface_1 = require("../ui-contracts/popover-surface");
|
|
|
15
15
|
const ui_inspection_1 = require("../ui-inspection");
|
|
16
16
|
const frame_bridge_1 = require("./frame-bridge");
|
|
17
17
|
const frame_protocol_1 = require("./frame-protocol");
|
|
18
|
+
const wheel_relay_1 = require("./wheel-relay");
|
|
18
19
|
let root;
|
|
19
20
|
let setScreen;
|
|
20
21
|
let pending = { props: {} };
|
|
21
22
|
const uis = new Map();
|
|
22
23
|
const pieces = new Map();
|
|
23
24
|
let loadedVersionId;
|
|
24
|
-
/** Whether this frame draws a piece, whose Escape
|
|
25
|
+
/** Whether this frame draws a piece, whose Escape and wheel go to its embedder. */
|
|
25
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
|
+
*/
|
|
26
31
|
function applyTheme(mode) {
|
|
27
32
|
const html = document.documentElement;
|
|
28
33
|
html.setAttribute("data-bs-theme", mode);
|
|
34
|
+
if (drawsPiece)
|
|
35
|
+
return;
|
|
29
36
|
const background = mode === "dark" ? "rgb(33, 37, 41)" : "";
|
|
30
37
|
html.style.backgroundColor = background;
|
|
31
38
|
document.body.style.backgroundColor = background;
|
|
@@ -133,7 +140,9 @@ function PieceRoot(props) {
|
|
|
133
140
|
: { width: rect.width, height: rect.height });
|
|
134
141
|
if (mode !== "content")
|
|
135
142
|
return;
|
|
136
|
-
|
|
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));
|
|
137
146
|
const width = maxWidth === undefined ? undefined : Math.ceil(rect.width);
|
|
138
147
|
const key = `${height}x${width}`;
|
|
139
148
|
if (key === last)
|
|
@@ -146,11 +155,15 @@ function PieceRoot(props) {
|
|
|
146
155
|
observer.observe(root);
|
|
147
156
|
return () => observer.disconnect();
|
|
148
157
|
}, [host, mode, maxWidth]);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
+
};
|
|
154
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 }) }));
|
|
155
168
|
}
|
|
156
169
|
function show(next) {
|
|
@@ -363,6 +376,70 @@ function installPointerRelay(host) {
|
|
|
363
376
|
}
|
|
364
377
|
});
|
|
365
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
|
+
}
|
|
366
443
|
/** The screen's warnings and errors also go to the host, whose console the Console app reads. */
|
|
367
444
|
function installConsoleForwarding(host) {
|
|
368
445
|
for (const level of ["warn", "error"]) {
|
|
@@ -405,6 +482,7 @@ function startFrameRuntime() {
|
|
|
405
482
|
installLinkHandling(host);
|
|
406
483
|
installKeyForwarding(host);
|
|
407
484
|
installPointerRelay(host);
|
|
485
|
+
installWheelRelay(host);
|
|
408
486
|
installConsoleForwarding(host);
|
|
409
487
|
window.addEventListener("error", (event) => {
|
|
410
488
|
console.error(event.error ?? event.message);
|
|
@@ -58,6 +58,8 @@ export interface IFramePieceEvents {
|
|
|
58
58
|
key: string;
|
|
59
59
|
code?: string;
|
|
60
60
|
}): void;
|
|
61
|
+
/** A wheel over it that it didn't scroll with, in pixels: for the screen it's drawn over. */
|
|
62
|
+
onWheel?(deltaX: number, deltaY: number): void;
|
|
61
63
|
}
|
|
62
64
|
/** A slot in a framed screen where one of the pieces it embeds is drawn, over the frame. */
|
|
63
65
|
interface IPieceSlot {
|
|
@@ -25,6 +25,7 @@ const frame_pvars_1 = require("./frame-pvars");
|
|
|
25
25
|
const piece_reach_1 = require("./piece-reach");
|
|
26
26
|
const port_channel_1 = require("./port-channel");
|
|
27
27
|
const screen_tab_context_1 = require("./screen-tab-context");
|
|
28
|
+
const wheel_relay_1 = require("./wheel-relay");
|
|
28
29
|
/** A rectangle as a frame reports it, refused unless it's four finite numbers. */
|
|
29
30
|
function toRect(value) {
|
|
30
31
|
const r = value;
|
|
@@ -308,6 +309,12 @@ class FrameSession {
|
|
|
308
309
|
opts.pieceEvents()?.onSize(height, boundedWidth);
|
|
309
310
|
});
|
|
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
|
+
});
|
|
311
318
|
channel.onNotify(frame_protocol_1.FrameNotice.restart, () => opts.restart());
|
|
312
319
|
// A frame that embeds pieces: the host resolves them and draws each in a frame of its own,
|
|
313
320
|
// acting as its provider, over the slot the frame reports.
|
|
@@ -541,7 +548,7 @@ function PackageFrameElement(props) {
|
|
|
541
548
|
const frameRef = (0, react_1.useRef)(null);
|
|
542
549
|
const sessionRef = (0, react_1.useRef)();
|
|
543
550
|
const [frameId] = (0, react_1.useState)(() => (0, peers_sdk_1.newid)());
|
|
544
|
-
const [srcDoc] = (0, react_1.useState)(() => (0, frame_document_1.frameDocument)((0, color_mode_dropdown_1.colorMode)() === "light" ? "light" : "dark"));
|
|
551
|
+
const [srcDoc] = (0, react_1.useState)(() => (0, frame_document_1.frameDocument)((0, color_mode_dropdown_1.colorMode)() === "light" ? "light" : "dark", content.kind === "piece"));
|
|
545
552
|
const [failure, setFailure] = (0, react_1.useState)();
|
|
546
553
|
const [slots] = (0, react_1.useState)(() => (0, peers_sdk_1.observable)([]));
|
|
547
554
|
const [shownSlots] = (0, hooks_1.useObservable)(slots);
|
|
@@ -635,7 +642,10 @@ function PackageFrameElement(props) {
|
|
|
635
642
|
height: "100%",
|
|
636
643
|
display: "block",
|
|
637
644
|
};
|
|
645
|
+
let frameClass = content.kind === "screen" ? "peers-screen-frame" : "peers-piece-frame";
|
|
638
646
|
if (content.kind === "piece" && content.width !== undefined) {
|
|
647
|
+
// PopoverSurface positions the picker over other content and supplies no background.
|
|
648
|
+
frameClass += " bg-body";
|
|
639
649
|
// Keep the viewport at the cap so vw units and media queries can't feed back into the
|
|
640
650
|
// reported width. The piece draws at (0, 0), regardless of its embedder's writing direction.
|
|
641
651
|
frameStyle.width = content.width;
|
|
@@ -643,7 +653,7 @@ function PackageFrameElement(props) {
|
|
|
643
653
|
frameStyle.top = 0;
|
|
644
654
|
frameStyle.left = 0;
|
|
645
655
|
}
|
|
646
|
-
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:
|
|
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)))] }));
|
|
647
657
|
}
|
|
648
658
|
/**
|
|
649
659
|
* A piece a framed screen embeds, drawn over the slot the screen reports: a frame of its own,
|
|
@@ -664,6 +674,7 @@ function PieceSlotFrame(props) {
|
|
|
664
674
|
onSize: (height, width) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceSize, width === undefined ? { slotId, height } : { slotId, height, width }),
|
|
665
675
|
onFailed: (message) => session.current?.slotNotify(frame_protocol_1.HostNotice.pieceFailed, { slotId, message }),
|
|
666
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 }),
|
|
667
678
|
}), [slotId, session, piece]);
|
|
668
679
|
const slotOf = (0, react_1.useMemo)(() => () => ({ session: session.current, slotId }), [session, slotId]);
|
|
669
680
|
const rect = slot.rect;
|
|
@@ -14,6 +14,7 @@ const popover_surface_1 = require("../ui-contracts/popover-surface");
|
|
|
14
14
|
const frame_component_1 = require("./frame-component");
|
|
15
15
|
const frame_protocol_1 = require("./frame-protocol");
|
|
16
16
|
const frame_realm_1 = require("./frame-realm");
|
|
17
|
+
const wheel_relay_1 = require("./wheel-relay");
|
|
17
18
|
/**
|
|
18
19
|
* Inside a framed screen: ask the host who draws a piece.
|
|
19
20
|
*
|
|
@@ -167,6 +168,12 @@ function visibleClip(element, rect) {
|
|
|
167
168
|
right: Math.max(0, rect.right - right),
|
|
168
169
|
};
|
|
169
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Events, heard on the way down to their target, after which a slot may have moved without the
|
|
173
|
+
* screen's DOM changing: an image (or other element) that loaded, a CSS transition or animation
|
|
174
|
+
* that ended.
|
|
175
|
+
*/
|
|
176
|
+
const LAYOUT_EVENTS = ["load", "transitionend", "animationend"];
|
|
170
177
|
const slotComponents = new Map();
|
|
171
178
|
/**
|
|
172
179
|
* Inside a framed screen: the component that holds a slot for a piece, which the host draws over
|
|
@@ -228,6 +235,12 @@ function slotPieceComponent(available, name) {
|
|
|
228
235
|
slotRef.current?.dispatchEvent(new Event(popover_surface_1.FRAME_POINTER_EVENT, { bubbles: true }));
|
|
229
236
|
}
|
|
230
237
|
}),
|
|
238
|
+
// A wheel over the piece that it didn't scroll with scrolls the screen around the slot.
|
|
239
|
+
host.onNotify(frame_protocol_1.HostNotice.pieceWheel, (payload) => {
|
|
240
|
+
if (mine(payload) && slotRef.current) {
|
|
241
|
+
(0, wheel_relay_1.scrollAroundSlot)(slotRef.current, payload.deltaX, payload.deltaY);
|
|
242
|
+
}
|
|
243
|
+
}),
|
|
231
244
|
];
|
|
232
245
|
host
|
|
233
246
|
.request(frame_protocol_1.FrameRequest.pieceMount, {
|
|
@@ -249,29 +262,53 @@ function slotPieceComponent(available, name) {
|
|
|
249
262
|
resize.observe(document.body);
|
|
250
263
|
window.addEventListener("resize", schedule);
|
|
251
264
|
document.addEventListener("scroll", schedule, true);
|
|
265
|
+
// The whole document: a class on <html> or a style in <head> can move it too.
|
|
266
|
+
changes.observe(document.documentElement, {
|
|
267
|
+
subtree: true,
|
|
268
|
+
childList: true,
|
|
269
|
+
attributes: true,
|
|
270
|
+
characterData: true,
|
|
271
|
+
});
|
|
272
|
+
for (const type of LAYOUT_EVENTS)
|
|
273
|
+
document.addEventListener(type, check, true);
|
|
252
274
|
if (JSON.stringify(latest.current.props) !== propsKey) {
|
|
253
275
|
host.notify(frame_protocol_1.FrameNotice.pieceProps, { slotId, props: latest.current.props });
|
|
254
276
|
}
|
|
255
277
|
}, (error) => active && setFailed(errorText(error)));
|
|
256
|
-
// Where the slot is, sent once per animation frame while anything moves it.
|
|
278
|
+
// Where the slot is, sent once per animation frame while anything moves it. A resize or a
|
|
279
|
+
// scroll always sends it. Content above the slot that grows or shrinks moves the slot
|
|
280
|
+
// without resizing it, so any other change in the screen (its document, an image that
|
|
281
|
+
// loaded, a transition that ended) measures the slot again, and sends only if it moved.
|
|
257
282
|
let scheduled = 0;
|
|
283
|
+
let always = false;
|
|
284
|
+
let sent = "";
|
|
258
285
|
const report = () => {
|
|
259
286
|
scheduled = 0;
|
|
287
|
+
const send = always;
|
|
288
|
+
always = false;
|
|
260
289
|
const element = slotRef.current;
|
|
261
290
|
if (!element)
|
|
262
291
|
return;
|
|
263
292
|
const rect = element.getBoundingClientRect();
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
293
|
+
const box = { x: rect.left, y: rect.top, width: rect.width, height: rect.height };
|
|
294
|
+
const clip = visibleClip(element, rect);
|
|
295
|
+
const key = JSON.stringify([box, clip]);
|
|
296
|
+
if (!send && key === sent)
|
|
297
|
+
return;
|
|
298
|
+
sent = key;
|
|
299
|
+
host.notify(frame_protocol_1.FrameNotice.pieceRect, { slotId, rect: box, clip });
|
|
269
300
|
};
|
|
270
301
|
const schedule = () => {
|
|
302
|
+
always = true;
|
|
303
|
+
if (!scheduled)
|
|
304
|
+
scheduled = requestAnimationFrame(report);
|
|
305
|
+
};
|
|
306
|
+
const check = () => {
|
|
271
307
|
if (!scheduled)
|
|
272
308
|
scheduled = requestAnimationFrame(report);
|
|
273
309
|
};
|
|
274
310
|
const resize = new ResizeObserver(schedule);
|
|
311
|
+
const changes = new MutationObserver(check);
|
|
275
312
|
return () => {
|
|
276
313
|
active = false;
|
|
277
314
|
if (mounted.current === slotId) {
|
|
@@ -283,8 +320,11 @@ function slotPieceComponent(available, name) {
|
|
|
283
320
|
if (scheduled)
|
|
284
321
|
cancelAnimationFrame(scheduled);
|
|
285
322
|
resize.disconnect();
|
|
323
|
+
changes.disconnect();
|
|
286
324
|
window.removeEventListener("resize", schedule);
|
|
287
325
|
document.removeEventListener("scroll", schedule, true);
|
|
326
|
+
for (const type of LAYOUT_EVENTS)
|
|
327
|
+
document.removeEventListener(type, check, true);
|
|
288
328
|
};
|
|
289
329
|
}, [host, mode, inPopover, maxWidth]);
|
|
290
330
|
// Until the host holds the slot, its mount sends the props that are current when it does.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The wheel across a piece's frame. The host draws a piece that a framed screen embeds over the
|
|
3
|
+
* screen's slot, in a frame of its own beside the screen's frame, not inside it. A wheel the piece
|
|
4
|
+
* doesn't scroll with would chain to the host's window, never to the screen under it, so the
|
|
5
|
+
* piece's frame sends it to the host, the host hands it to the screen, and the screen scrolls the
|
|
6
|
+
* boxes around the slot as the browser would have.
|
|
7
|
+
*/
|
|
8
|
+
/** One axis of a wheel. */
|
|
9
|
+
export type WheelAxis = "x" | "y";
|
|
10
|
+
/**
|
|
11
|
+
* A relayed wheel's movement along one axis, as a finite number of pixels within bounds, or 0.
|
|
12
|
+
*
|
|
13
|
+
* @param value - What the sender gave.
|
|
14
|
+
*/
|
|
15
|
+
export declare function wheelDelta(value: unknown): number;
|
|
16
|
+
/**
|
|
17
|
+
* A wheel event's movement, in pixels, along the axes the browser scrolls with it. Off Apple's
|
|
18
|
+
* systems, Chromium scrolls sideways for a vertical wheel turned with Shift held, though the event
|
|
19
|
+
* still says vertical; on Apple's systems a mouse's wheel with Shift held already arrives sideways,
|
|
20
|
+
* and a trackpad scrolls the way it moves.
|
|
21
|
+
*
|
|
22
|
+
* @param event - The wheel.
|
|
23
|
+
*/
|
|
24
|
+
export declare function wheelPixels(event: WheelEvent): {
|
|
25
|
+
x: number;
|
|
26
|
+
y: number;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* What a wheel along one axis scrolls, from `start` outward, as the browser chains it: the first
|
|
30
|
+
* box the user can scroll that can still move that way, or `"contained"` when a scrolling box on
|
|
31
|
+
* the way is at its edge and stops the wheel there (`overscroll-behavior: contain` or `none`), or
|
|
32
|
+
* nothing when no box takes it.
|
|
33
|
+
*
|
|
34
|
+
* @param start - The element under the pointer.
|
|
35
|
+
* @param axis - The axis.
|
|
36
|
+
* @param delta - Which way, and how far: down or right when positive.
|
|
37
|
+
*/
|
|
38
|
+
export declare function wheelScroller(start: Element | null, axis: WheelAxis, delta: number): Element | "contained" | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* In a framed screen: scroll as a wheel over the piece drawn on `slot` would have, had the piece
|
|
41
|
+
* been in the screen's document. Each axis moves the first box around the slot that can still
|
|
42
|
+
* move that way.
|
|
43
|
+
*
|
|
44
|
+
* @param slot - The slot the piece is drawn over.
|
|
45
|
+
* @param deltaX - Pixels to the right (negative: to the left).
|
|
46
|
+
* @param deltaY - Pixels down (negative: up).
|
|
47
|
+
*/
|
|
48
|
+
export declare function scrollAroundSlot(slot: Element, deltaX: number, deltaY: number): void;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The wheel across a piece's frame. The host draws a piece that a framed screen embeds over the
|
|
4
|
+
* screen's slot, in a frame of its own beside the screen's frame, not inside it. A wheel the piece
|
|
5
|
+
* doesn't scroll with would chain to the host's window, never to the screen under it, so the
|
|
6
|
+
* piece's frame sends it to the host, the host hands it to the screen, and the screen scrolls the
|
|
7
|
+
* boxes around the slot as the browser would have.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.wheelDelta = wheelDelta;
|
|
11
|
+
exports.wheelPixels = wheelPixels;
|
|
12
|
+
exports.wheelScroller = wheelScroller;
|
|
13
|
+
exports.scrollAroundSlot = scrollAroundSlot;
|
|
14
|
+
/** Pixels per line, for a wheel that counts lines (`deltaMode` 1), as browsers scroll them. */
|
|
15
|
+
const LINE_PIXELS = 16;
|
|
16
|
+
/** The most one relayed wheel moves along an axis, either way, in pixels. */
|
|
17
|
+
const MAX_WHEEL_PIXELS = 10_000;
|
|
18
|
+
/**
|
|
19
|
+
* A relayed wheel's movement along one axis, as a finite number of pixels within bounds, or 0.
|
|
20
|
+
*
|
|
21
|
+
* @param value - What the sender gave.
|
|
22
|
+
*/
|
|
23
|
+
function wheelDelta(value) {
|
|
24
|
+
if (typeof value !== "number" || !Number.isFinite(value))
|
|
25
|
+
return 0;
|
|
26
|
+
return Math.max(-MAX_WHEEL_PIXELS, Math.min(MAX_WHEEL_PIXELS, value));
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A wheel event's movement, in pixels, along the axes the browser scrolls with it. Off Apple's
|
|
30
|
+
* systems, Chromium scrolls sideways for a vertical wheel turned with Shift held, though the event
|
|
31
|
+
* still says vertical; on Apple's systems a mouse's wheel with Shift held already arrives sideways,
|
|
32
|
+
* and a trackpad scrolls the way it moves.
|
|
33
|
+
*
|
|
34
|
+
* @param event - The wheel.
|
|
35
|
+
*/
|
|
36
|
+
function wheelPixels(event) {
|
|
37
|
+
let x = event.deltaX;
|
|
38
|
+
let y = event.deltaY;
|
|
39
|
+
if (event.shiftKey && x === 0 && y !== 0 && !/Mac|iPod|iPhone|iPad/.test(navigator.userAgent)) {
|
|
40
|
+
x = y;
|
|
41
|
+
y = 0;
|
|
42
|
+
}
|
|
43
|
+
if (event.deltaMode === 1)
|
|
44
|
+
return { x: x * LINE_PIXELS, y: y * LINE_PIXELS };
|
|
45
|
+
if (event.deltaMode === 2)
|
|
46
|
+
return { x: x * window.innerWidth, y: y * window.innerHeight };
|
|
47
|
+
return { x, y };
|
|
48
|
+
}
|
|
49
|
+
/** The document's scrolling box: the viewport's scroller. */
|
|
50
|
+
function rootScroller() {
|
|
51
|
+
return document.scrollingElement ?? document.documentElement;
|
|
52
|
+
}
|
|
53
|
+
/** A box's overflow along an axis. */
|
|
54
|
+
function overflowAlong(style, axis) {
|
|
55
|
+
return axis === "y" ? style.overflowY : style.overflowX;
|
|
56
|
+
}
|
|
57
|
+
/** Whether containment is set explicitly or implied by the container type or content visibility. */
|
|
58
|
+
function appliesContainment(style) {
|
|
59
|
+
const contain = style.getPropertyValue("contain");
|
|
60
|
+
const visibility = style.getPropertyValue("content-visibility");
|
|
61
|
+
return ((contain !== "" && contain !== "none") ||
|
|
62
|
+
/size|anchored/.test(style.getPropertyValue("container-type")) ||
|
|
63
|
+
(visibility !== "" && visibility !== "visible"));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether the body's overflow is the viewport's, so the body itself doesn't scroll: the root's own
|
|
67
|
+
* is `visible` both ways, and neither applies containment, which would keep the body's to itself.
|
|
68
|
+
*/
|
|
69
|
+
function bodyOverflowIsViewports(body) {
|
|
70
|
+
const html = getComputedStyle(document.documentElement);
|
|
71
|
+
return (html.overflowX === "visible" &&
|
|
72
|
+
html.overflowY === "visible" &&
|
|
73
|
+
!appliesContainment(html) &&
|
|
74
|
+
!appliesContainment(body));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* How a box lets the user scroll along an axis: `auto` and `scroll` do, `visible`, `hidden` and
|
|
78
|
+
* `clip` don't, nor does a body whose overflow is the viewport's. The viewport scrolls unless the
|
|
79
|
+
* root, or a body whose overflow is the viewport's, is `hidden` or `clip`.
|
|
80
|
+
*/
|
|
81
|
+
function userScrolls(element, style, axis, isRoot) {
|
|
82
|
+
if (!isRoot) {
|
|
83
|
+
const overflow = overflowAlong(style, axis);
|
|
84
|
+
if (overflow !== "auto" && overflow !== "scroll" && overflow !== "overlay")
|
|
85
|
+
return false;
|
|
86
|
+
// The root, further out, scrolls for such a body.
|
|
87
|
+
return element !== document.body || !bodyOverflowIsViewports(style);
|
|
88
|
+
}
|
|
89
|
+
const html = document.documentElement;
|
|
90
|
+
let overflow = overflowAlong(element === html ? style : getComputedStyle(html), axis);
|
|
91
|
+
if (overflow === "visible" && document.body) {
|
|
92
|
+
// A body that keeps its overflow to itself (the root isn't `visible` both ways, or either
|
|
93
|
+
// applies containment) leaves the viewport's to the root.
|
|
94
|
+
const body = getComputedStyle(document.body);
|
|
95
|
+
if (bodyOverflowIsViewports(body))
|
|
96
|
+
overflow = overflowAlong(body, axis);
|
|
97
|
+
}
|
|
98
|
+
return overflow !== "hidden" && overflow !== "clip";
|
|
99
|
+
}
|
|
100
|
+
/** Whether a scrolling box stops a wheel at its edge from going on to the boxes around it. */
|
|
101
|
+
function containsOverscroll(style, axis) {
|
|
102
|
+
const value = style.getPropertyValue(`overscroll-behavior-${axis}`);
|
|
103
|
+
return value === "contain" || value === "none";
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Whether a box's scroll position along an axis starts at its far end and runs from 0 down to
|
|
107
|
+
* minus its room. Outside a flex box, only right to left does, along x. In a flex box, the writing
|
|
108
|
+
* mode and direction set where each axis starts, a reversed flex direction turns the main axis
|
|
109
|
+
* around, and `wrap-reverse` the cross axis. The viewport isn't a flex box, even when the root
|
|
110
|
+
* element lays out its children with flex.
|
|
111
|
+
*/
|
|
112
|
+
function countsBack(style, axis, isViewport) {
|
|
113
|
+
if (isViewport || (style.display !== "flex" && style.display !== "inline-flex")) {
|
|
114
|
+
return axis === "x" && style.direction === "rtl";
|
|
115
|
+
}
|
|
116
|
+
const mode = style.writingMode;
|
|
117
|
+
const vertical = mode.startsWith("vertical-") || mode.startsWith("sideways-");
|
|
118
|
+
const inline = axis === (vertical ? "y" : "x");
|
|
119
|
+
const backwards = inline
|
|
120
|
+
? (style.direction === "rtl") !== (mode === "sideways-lr")
|
|
121
|
+
: mode.endsWith("-rl");
|
|
122
|
+
const direction = style.flexDirection;
|
|
123
|
+
const row = !direction.startsWith("column");
|
|
124
|
+
const reversed = inline === row ? direction.endsWith("-reverse") : style.flexWrap === "wrap-reverse";
|
|
125
|
+
return backwards !== reversed;
|
|
126
|
+
}
|
|
127
|
+
/** Whether a scrolling box can still move along an axis in a direction. */
|
|
128
|
+
function hasRoom(element, style, axis, delta, isViewport) {
|
|
129
|
+
const room = axis === "y"
|
|
130
|
+
? element.scrollHeight - element.clientHeight
|
|
131
|
+
: element.scrollWidth - element.clientWidth;
|
|
132
|
+
const at = axis === "y" ? element.scrollTop : element.scrollLeft;
|
|
133
|
+
const start = countsBack(style, axis, isViewport) ? -room : 0;
|
|
134
|
+
return delta > 0 ? at < start + room - 0.5 : at > start + 0.5;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* What a wheel along one axis scrolls, from `start` outward, as the browser chains it: the first
|
|
138
|
+
* box the user can scroll that can still move that way, or `"contained"` when a scrolling box on
|
|
139
|
+
* the way is at its edge and stops the wheel there (`overscroll-behavior: contain` or `none`), or
|
|
140
|
+
* nothing when no box takes it.
|
|
141
|
+
*
|
|
142
|
+
* @param start - The element under the pointer.
|
|
143
|
+
* @param axis - The axis.
|
|
144
|
+
* @param delta - Which way, and how far: down or right when positive.
|
|
145
|
+
*/
|
|
146
|
+
function wheelScroller(start, axis, delta) {
|
|
147
|
+
if (delta === 0)
|
|
148
|
+
return undefined;
|
|
149
|
+
const root = rootScroller();
|
|
150
|
+
for (let element = start; element; element = element.parentElement) {
|
|
151
|
+
const isRoot = element === root;
|
|
152
|
+
const style = getComputedStyle(element);
|
|
153
|
+
if (userScrolls(element, style, axis, isRoot)) {
|
|
154
|
+
if (hasRoom(element, style, axis, delta, isRoot))
|
|
155
|
+
return element;
|
|
156
|
+
if (containsOverscroll(style, axis))
|
|
157
|
+
return "contained";
|
|
158
|
+
}
|
|
159
|
+
if (isRoot)
|
|
160
|
+
return undefined;
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* In a framed screen: scroll as a wheel over the piece drawn on `slot` would have, had the piece
|
|
166
|
+
* been in the screen's document. Each axis moves the first box around the slot that can still
|
|
167
|
+
* move that way.
|
|
168
|
+
*
|
|
169
|
+
* @param slot - The slot the piece is drawn over.
|
|
170
|
+
* @param deltaX - Pixels to the right (negative: to the left).
|
|
171
|
+
* @param deltaY - Pixels down (negative: up).
|
|
172
|
+
*/
|
|
173
|
+
function scrollAroundSlot(slot, deltaX, deltaY) {
|
|
174
|
+
for (const [axis, delta] of [
|
|
175
|
+
["x", deltaX],
|
|
176
|
+
["y", deltaY],
|
|
177
|
+
]) {
|
|
178
|
+
const scroller = wheelScroller(slot.parentElement, axis, delta);
|
|
179
|
+
if (!scroller || scroller === "contained")
|
|
180
|
+
continue;
|
|
181
|
+
if (axis === "y")
|
|
182
|
+
scroller.scrollTop += delta;
|
|
183
|
+
else
|
|
184
|
+
scroller.scrollLeft += delta;
|
|
185
|
+
}
|
|
186
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peers-app/peers-ui",
|
|
3
|
-
"version": "0.34.
|
|
3
|
+
"version": "0.34.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/peers-app/peers-ui.git"
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"lint:fix": "biome check --write ."
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@peers-app/peers-sdk": "^0.34.
|
|
39
|
+
"@peers-app/peers-sdk": "^0.34.1",
|
|
40
40
|
"bootstrap": "^5.3.3",
|
|
41
41
|
"react": "^18.0.0",
|
|
42
42
|
"react-dom": "^18.0.0"
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"@babel/preset-react": "^7.24.1",
|
|
48
48
|
"@babel/preset-typescript": "^7.27.1",
|
|
49
49
|
"@biomejs/biome": "2.3.11",
|
|
50
|
-
"@peers-app/peers-sdk": "0.34.
|
|
50
|
+
"@peers-app/peers-sdk": "0.34.1",
|
|
51
51
|
"@testing-library/dom": "^10.4.0",
|
|
52
52
|
"@testing-library/jest-dom": "^6.6.3",
|
|
53
53
|
"@testing-library/react": "^16.3.0",
|