Open History to see the last commit
1import type { DeviceHubAccess } from "@t3tools/client-runtime/device/hub-access";
2import type { PreviewStreamHostSetup } from "@t3tools/contracts";
3import type {
4 PreviewStreamControl,
5 PreviewStreamDownload,
6 PreviewStreamFileChooser,
7 PreviewStreamSelectMenu,
8} from "@t3tools/client-runtime/preview/server-browser-stream";
9
10export interface PreviewStreamConfiguration {
11 readonly access: DeviceHubAccess;
12 readonly threadId: string;
13 readonly tabId: string;
14 readonly interactive: boolean;
15 readonly background: string;
16 /** Translated in React Native: the WebView cannot load the catalogs. */
17 readonly labels: {
18 readonly page: string;
19 readonly pageInput: string;
20 readonly pictureInPictureNotReady: string;
21 readonly pictureInPictureUnavailable: string;
22 };
23}
24
25export type PreviewStreamMessage =
26 | ({ readonly type: "control" } & PreviewStreamControl)
27 | {
28 readonly type: "status";
29 readonly status: "connecting" | "streaming" | "error";
30 readonly detail?: string;
31 }
32 | { readonly type: "unauthorized" }
33 | { readonly type: "gone" }
34 | ({ readonly type: "hostSetup" } & PreviewStreamHostSetup)
35 | { readonly type: "viewport"; readonly width: number; readonly height: number }
36 /** The direction a drag scrolls the page, sent every few dozen pixels of it. */
37 | { readonly type: "scroll"; readonly direction: "up" | "down" }
38 | { readonly type: "clipboard"; readonly text: string }
39 | ({ readonly type: "download" } & PreviewStreamDownload)
40 | { readonly type: "fileChooser"; readonly chooser: PreviewStreamFileChooser | null }
41 | { readonly type: "selectMenu"; readonly menu: PreviewStreamSelectMenu | null }
42 | {
43 readonly type: "pictureInPicture";
44 readonly supported: boolean;
45 readonly active: boolean;
46 readonly detail?: string;
47 };
48
49export function previewStreamDocument(
50 configuration: string,
51 script: string,
52 stoppedDetail: string,
53) {
54 const safeConfiguration = configuration.replace(/</g, "\\u003c");
55 const safeScript = script.replace(/<\/script/gi, "<\\/script");
56 const safeStoppedDetail = JSON.stringify(stoppedDetail).replace(/</g, "\\u003c");
57 const failure = `window.ReactNativeWebView.postMessage(JSON.stringify({type:"status",status:"error",detail:${safeStoppedDetail}}));`;
58 // oxlint-disable-next-line t3code/no-untranslated-copy -- WebView document markup
59 return `<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
60<style>
61 html, body { height: 100%; overflow: hidden; }
62 body { margin: 0; }
63 body > div { position: fixed; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; }
64 canvas, video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
65 canvas { touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
66 video { pointer-events: none; }
67 /* The agent's pointer, positioned over the letterboxed frame by the viewer script. */
68 .agent-cursor {
69 position: absolute; left: 0; top: 0; width: 20px; height: 20px; pointer-events: none;
70 opacity: 0; transition: opacity 150ms ease-out, transform 150ms ease-out;
71 }
72 .agent-cursor svg { position: relative; display: block; filter: drop-shadow(0 1px 1px rgba(0,0,0,.35)); }
73 .agent-cursor .ping {
74 position: absolute; left: 2px; top: 2px; width: 16px; height: 16px; border-radius: 50%;
75 background: rgba(59,130,246,.3); animation: agent-cursor-ping 600ms ease-out forwards;
76 }
77 @keyframes agent-cursor-ping { from { transform: scale(.6); opacity: 1; } to { transform: scale(2.2); opacity: 0; } }
78 @media (prefers-reduced-motion: reduce) {
79 .agent-cursor { transition: none; }
80 .agent-cursor .ping { animation: none; opacity: 0; }
81 }
82 /* Pinned so focus never scrolls; 16px keeps iOS from zooming on focus. */
83 textarea {
84 position: fixed; left: 0; top: 0; width: 1px; height: 1px;
85 padding: 0; margin: -1px; border: 0; overflow: hidden;
86 clip: rect(0, 0, 0, 0); white-space: nowrap; font-size: 16px;
87 }
88</style></head><body><script>window.addEventListener("error",function(){${failure}});window.addEventListener("unhandledrejection",function(){${failure}});\n${safeScript}\ntry{T3PreviewStream.start(${safeConfiguration});}catch{${failure}}</script></body></html>`;
89}
90
91export function previewStreamMessage(data: string): PreviewStreamMessage | null {
92 try {
93 return JSON.parse(data) as PreviewStreamMessage;
94 } catch {
95 return null;
96 }
97}