Skip to content

Commit 1b4ef79

Browse files
committed
fix(web): mouse Back/Forward over a streamed preview navigate the preview
1 parent ca140c4 commit 1b4ef79

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import type { PreviewStreamInput } from "@t3tools/client-runtime/preview/server-browser-stream";
3+
4+
import "~/index.css";
5+
import { describe, expect, it, vi } from "vite-plus/test";
6+
7+
import { render } from "../../test/render";
8+
9+
const mocks = vi.hoisted(() => ({
10+
send: vi.fn<(input: PreviewStreamInput) => void>(),
11+
connect: vi.fn(),
12+
}));
13+
14+
vi.mock(import("@t3tools/client-runtime/preview/server-browser-stream"), async (original) => ({
15+
...(await original()),
16+
createPreviewFramePainter: () => ({ paint: () => undefined, stop: () => undefined }),
17+
createPreviewStreamClient: () => {
18+
mocks.connect();
19+
return { send: mocks.send, stop: () => undefined } as never;
20+
},
21+
}));
22+
23+
vi.mock(import("~/state/previewStream"), async (original) => ({
24+
...(await original()),
25+
usePreviewStreamAccess: () => ({}) as never,
26+
}));
27+
28+
import { ServerBrowserSurface } from "./ServerBrowserSurface";
29+
30+
async function renderSurface() {
31+
mocks.send.mockReset();
32+
mocks.connect.mockReset();
33+
const container = document.createElement("div");
34+
container.style.cssText = "width: 400px; height: 300px;";
35+
await using stack = new AsyncDisposableStack();
36+
stack.use(
37+
await render(
38+
<ServerBrowserSurface
39+
environmentId={EnvironmentId.make("environment-1")}
40+
threadId={ThreadId.make("thread-1")}
41+
tabId="tab-1"
42+
visible
43+
/>,
44+
{ container },
45+
),
46+
);
47+
const canvas = container.querySelector("canvas");
48+
if (!canvas) throw new Error("The surface did not render its canvas.");
49+
// The stream connects once the surface has measured its size.
50+
await vi.waitFor(() => expect(mocks.connect).toHaveBeenCalled());
51+
return Object.assign(stack.move(), { canvas });
52+
}
53+
54+
const mouseUp = (canvas: HTMLCanvasElement, button: number) => {
55+
const event = new MouseEvent("mouseup", { button, bubbles: true, cancelable: true });
56+
canvas.dispatchEvent(event);
57+
return event;
58+
};
59+
60+
describe("ServerBrowserSurface", () => {
61+
it("routes mouse thumb buttons to the preview's history instead of the app", async () => {
62+
await using surface = await renderSurface();
63+
const { canvas } = surface;
64+
65+
expect(mouseUp(canvas, 3).defaultPrevented).toBe(true);
66+
expect(mouseUp(canvas, 4).defaultPrevented).toBe(true);
67+
expect(mocks.send.mock.calls).toEqual([
68+
[{ type: "history", delta: -1 }],
69+
[{ type: "history", delta: 1 }],
70+
]);
71+
});
72+
73+
it("leaves other mouse buttons alone", async () => {
74+
await using surface = await renderSurface();
75+
const { canvas } = surface;
76+
77+
expect(mouseUp(canvas, 0).defaultPrevented).toBe(false);
78+
expect(mocks.send).not.toHaveBeenCalled();
79+
});
80+
});

‎apps/web/src/browser/ServerBrowserSurface.tsx‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import type {
2424
import {
2525
type FormEvent,
2626
type KeyboardEvent,
27+
type MouseEvent,
2728
type PointerEvent,
2829
type Ref,
2930
useEffect,
@@ -99,6 +100,10 @@ const buttonOf = (button: number): PreviewStreamMouseButton =>
99100
const pressedButtonOf = (buttons: number): PreviewStreamMouseButton =>
100101
buttons & 1 ? "left" : buttons & 2 ? "right" : buttons & 4 ? "middle" : "none";
101102

103+
// Mouse thumb buttons: `button === 3` is Back, `button === 4` is Forward.
104+
const historyDeltaOf = (button: number): -1 | 1 | null =>
105+
button === 3 ? -1 : button === 4 ? 1 : null;
106+
102107
/** Browsers may refuse a write that arrives after the key press; a toast button is a fresh gesture. */
103108
async function copyPageText(text: string) {
104109
try {
@@ -499,6 +504,8 @@ export function ServerBrowserSurface(props: {
499504

500505
const handlePointer = (event: PointerEvent<HTMLCanvasElement>) => {
501506
if (controlRef.current?.controller !== "you") return;
507+
// Thumb buttons drive this tab's history in handleMouseUp, never the page.
508+
if (historyDeltaOf(event.button) !== null) return;
502509
const { type, pointerId, clientX, clientY } = event;
503510
const cancelled = type === "pointercancel";
504511
const touch = touchRef.current;
@@ -635,6 +642,16 @@ export function ServerBrowserSurface(props: {
635642
}
636643
};
637644

645+
// Chromium runs a thumb button's back/forward on mouseup in this document, which
646+
// would move the app's router. Cancel it and send the press to the preview, as
647+
// the toolbar does.
648+
const handleMouseUp = (event: MouseEvent<HTMLDivElement>) => {
649+
const delta = historyDeltaOf(event.button);
650+
if (delta === null) return;
651+
event.preventDefault();
652+
send({ type: "history", delta });
653+
};
654+
638655
const resetInput = (textarea: HTMLTextAreaElement) => {
639656
keySentRef.current = false;
640657
textarea.value = INPUT_SENTINEL;
@@ -712,6 +729,7 @@ export function ServerBrowserSurface(props: {
712729
<div
713730
className={cn("relative flex flex-col overflow-hidden", className)}
714731
data-server-browser-surface={tabId}
732+
onMouseUp={handleMouseUp}
715733
>
716734
<div
717735
className={cn(

0 commit comments

Comments
 (0)