Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

preview.ts

414 lines14.7 KB

1import {
2 DesktopPreviewAnnotationThemeInputSchema,
3 DesktopPreviewArtifactInputSchema,
4 DesktopPreviewAutomationClickInputSchema,
5 DesktopPreviewAutomationEvaluateInputSchema,
6 DesktopPreviewAutomationPressInputSchema,
7 DesktopPreviewAutomationScrollInputSchema,
8 DesktopPreviewAutomationStatusSchema,
9 DesktopPreviewAutomationTypeInputSchema,
10 DesktopPreviewAutomationWaitForInputSchema,
11 DesktopPreviewConfigInputSchema,
12 DesktopPreviewNavigateInputSchema,
13 DesktopPreviewRecordingArtifactSchema,
14 DesktopPreviewRecordingSourceSchema,
15 DesktopPreviewRecordingSaveInputSchema,
16 DesktopPreviewRegisterWebviewInputSchema,
17 DesktopPreviewScreenshotArtifactSchema,
18 DesktopPreviewSetAudioMutedInputSchema,
19 DesktopPreviewSetColorSchemeInputSchema,
20 DesktopPreviewCreateTabInputSchema,
21 DesktopPreviewTabInputSchema,
22 DesktopPreviewWebviewConfigSchema,
23 PreviewAnnotationSubmissionResultSchema,
24 PreviewAutomationSnapshot,
25} from "@t3tools/contracts";
26import * as Effect from "effect/Effect";
27import * as Schema from "effect/Schema";
28import * as NodeURL from "node:url";
29​
30import * as ElectronWindow from "../../electron/ElectronWindow.ts";
31import * as PreviewManager from "../../preview/Manager.ts";
32import { PREVIEW_WEBVIEW_PREFERENCES } from "../../preview/WebviewPreferences.ts";
33import * as IpcChannels from "../channels.ts";
34import * as DesktopIpc from "../DesktopIpc.ts";
35​
36export const installPreviewEventForwarding = Effect.fn(
37 "desktop.ipc.preview.installEventForwarding",
38)(function* () {
39 const electronWindow = yield* ElectronWindow.ElectronWindow;
40 const manager = yield* PreviewManager.PreviewManager;
41 yield* manager.subscribeStateChanges((tabId, state) =>
42 electronWindow.sendAll(IpcChannels.PREVIEW_STATE_CHANGE_CHANNEL, tabId, state),
43 );
44 yield* manager.subscribeRecordingFrames((frame) =>
45 electronWindow.sendAll(IpcChannels.PREVIEW_RECORDING_FRAME_CHANNEL, frame),
46 );
47 yield* manager.subscribePointerEvents((event) =>
48 electronWindow.sendAll(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, event),
49 );
50});
51​
52export const createTab = DesktopIpc.makeIpcMethod({
53 channel: IpcChannels.PREVIEW_CREATE_TAB_CHANNEL,
54 payload: DesktopPreviewCreateTabInputSchema,
55 result: Schema.Void,
56 handler: Effect.fn("desktop.ipc.preview.createTab")(function* ({
57 tabId,
58 zoomFactor,
59 colorScheme,
60 }) {
61 const manager = yield* PreviewManager.PreviewManager;
62 yield* manager.createTab(tabId, { zoomFactor, colorScheme });
63 }),
64});
65​
66export const closeTab = DesktopIpc.makeIpcMethod({
67 channel: IpcChannels.PREVIEW_CLOSE_TAB_CHANNEL,
68 payload: DesktopPreviewTabInputSchema,
69 result: Schema.Void,
70 handler: Effect.fn("desktop.ipc.preview.closeTab")(function* ({ tabId }) {
71 const manager = yield* PreviewManager.PreviewManager;
72 yield* manager.closeTab(tabId);
73 }),
74});
75​
76export const registerWebview = DesktopIpc.makeIpcMethod({
77 channel: IpcChannels.PREVIEW_REGISTER_WEBVIEW_CHANNEL,
78 payload: DesktopPreviewRegisterWebviewInputSchema,
79 result: Schema.Void,
80 handler: Effect.fn("desktop.ipc.preview.registerWebview")(function* ({ tabId, webContentsId }) {
81 const manager = yield* PreviewManager.PreviewManager;
82 yield* manager.registerWebview(tabId, webContentsId);
83 }),
84});
85​
86export const navigate = DesktopIpc.makeIpcMethod({
87 channel: IpcChannels.PREVIEW_NAVIGATE_CHANNEL,
88 payload: DesktopPreviewNavigateInputSchema,
89 result: Schema.Void,
90 handler: Effect.fn("desktop.ipc.preview.navigate")(function* ({ tabId, url }) {
91 const manager = yield* PreviewManager.PreviewManager;
92 yield* manager.navigate(tabId, url);
93 }),
94});
95​
96const tabMethod = (
97 channel: string,
98 name: string,
99 invoke: (
100 manager: PreviewManager.PreviewManager["Service"],
101 tabId: string,
102 ) => Effect.Effect<void, PreviewManager.PreviewManagerError>,
103) =>
104 DesktopIpc.makeIpcMethod({
105 channel,
106 payload: DesktopPreviewTabInputSchema,
107 result: Schema.Void,
108 handler: Effect.fn(name)(function* ({ tabId }) {
109 const manager = yield* PreviewManager.PreviewManager;
110 yield* invoke(manager, tabId);
111 }),
112 });
113​
114export const goBack = tabMethod(
115 IpcChannels.PREVIEW_GO_BACK_CHANNEL,
116 "desktop.ipc.preview.goBack",
117 (manager, tabId) => manager.goBack(tabId),
118);
119export const goForward = tabMethod(
120 IpcChannels.PREVIEW_GO_FORWARD_CHANNEL,
121 "desktop.ipc.preview.goForward",
122 (manager, tabId) => manager.goForward(tabId),
123);
124export const refresh = tabMethod(
125 IpcChannels.PREVIEW_REFRESH_CHANNEL,
126 "desktop.ipc.preview.refresh",
127 (manager, tabId) => manager.refresh(tabId),
128);
129export const zoomIn = tabMethod(
130 IpcChannels.PREVIEW_ZOOM_IN_CHANNEL,
131 "desktop.ipc.preview.zoomIn",
132 (manager, tabId) => manager.zoomIn(tabId),
133);
134export const zoomOut = tabMethod(
135 IpcChannels.PREVIEW_ZOOM_OUT_CHANNEL,
136 "desktop.ipc.preview.zoomOut",
137 (manager, tabId) => manager.zoomOut(tabId),
138);
139export const resetZoom = tabMethod(
140 IpcChannels.PREVIEW_RESET_ZOOM_CHANNEL,
141 "desktop.ipc.preview.resetZoom",
142 (manager, tabId) => manager.resetZoom(tabId),
143);
144export const hardReload = tabMethod(
145 IpcChannels.PREVIEW_HARD_RELOAD_CHANNEL,
146 "desktop.ipc.preview.hardReload",
147 (manager, tabId) => manager.hardReload(tabId),
148);
149export const setColorScheme = DesktopIpc.makeIpcMethod({
150 channel: IpcChannels.PREVIEW_SET_COLOR_SCHEME_CHANNEL,
151 payload: DesktopPreviewSetColorSchemeInputSchema,
152 result: Schema.Void,
153 handler: Effect.fn("desktop.ipc.preview.setColorScheme")(function* ({ tabId, colorScheme }) {
154 const manager = yield* PreviewManager.PreviewManager;
155 yield* manager.setColorScheme(tabId, colorScheme);
156 }),
157});
158export const setAudioMuted = DesktopIpc.makeIpcMethod({
159 channel: IpcChannels.PREVIEW_SET_AUDIO_MUTED_CHANNEL,
160 payload: DesktopPreviewSetAudioMutedInputSchema,
161 result: Schema.Void,
162 handler: Effect.fn("desktop.ipc.preview.setAudioMuted")(function* ({ tabId, audioMuted }) {
163 const manager = yield* PreviewManager.PreviewManager;
164 yield* manager.setAudioMuted(tabId, audioMuted);
165 }),
166});
167export const openDevTools = tabMethod(
168 IpcChannels.PREVIEW_OPEN_DEVTOOLS_CHANNEL,
169 "desktop.ipc.preview.openDevTools",
170 (manager, tabId) => manager.openDevTools(tabId),
171);
172export const cancelPickElement = tabMethod(
173 IpcChannels.PREVIEW_CANCEL_PICK_ELEMENT_CHANNEL,
174 "desktop.ipc.preview.cancelPickElement",
175 (manager, tabId) => manager.cancelPickElement(tabId),
176);
177export const startRecording = DesktopIpc.makeIpcMethod({
178 channel: IpcChannels.PREVIEW_RECORDING_START_CHANNEL,
179 payload: DesktopPreviewTabInputSchema,
180 result: DesktopPreviewRecordingSourceSchema,
181 handler: Effect.fn("desktop.ipc.preview.startRecording")(function* ({ tabId }) {
182 const manager = yield* PreviewManager.PreviewManager;
183 return yield* manager.startRecording(tabId);
184 }),
185});
186export const stopRecording = tabMethod(
187 IpcChannels.PREVIEW_RECORDING_STOP_CHANNEL,
188 "desktop.ipc.preview.stopRecording",
189 (manager, tabId) => manager.stopRecording(tabId),
190);
191export const openPictureInPicture = tabMethod(
192 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_OPEN_CHANNEL,
193 "desktop.ipc.preview.openPictureInPicture",
194 (manager, tabId) => manager.openPictureInPicture(tabId),
195);
196export const closePictureInPicture = tabMethod(
197 IpcChannels.PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL,
198 "desktop.ipc.preview.closePictureInPicture",
199 (manager, tabId) => manager.closePictureInPicture(tabId),
200);
201​
202export const clearCookies = DesktopIpc.makeIpcMethod({
203 channel: IpcChannels.PREVIEW_CLEAR_COOKIES_CHANNEL,
204 payload: Schema.Void,
205 result: Schema.Void,
206 handler: Effect.fn("desktop.ipc.preview.clearCookies")(function* () {
207 const manager = yield* PreviewManager.PreviewManager;
208 yield* manager.clearCookies();
209 }),
210});
211​
212export const clearCache = DesktopIpc.makeIpcMethod({
213 channel: IpcChannels.PREVIEW_CLEAR_CACHE_CHANNEL,
214 payload: Schema.Void,
215 result: Schema.Void,
216 handler: Effect.fn("desktop.ipc.preview.clearCache")(function* () {
217 const manager = yield* PreviewManager.PreviewManager;
218 yield* manager.clearCache();
219 }),
220});
221​
222export const getPreviewConfig = DesktopIpc.makeIpcMethod({
223 channel: IpcChannels.PREVIEW_GET_CONFIG_CHANNEL,
224 payload: DesktopPreviewConfigInputSchema,
225 result: DesktopPreviewWebviewConfigSchema,
226 handler: Effect.fn("desktop.ipc.preview.getConfig")(function* ({ environmentId }) {
227 const manager = yield* PreviewManager.PreviewManager;
228 yield* manager.getBrowserSession(environmentId);
229 return {
230 partition: yield* manager.getBrowserPartition(environmentId),
231 webPreferences: PREVIEW_WEBVIEW_PREFERENCES,
232 preloadUrl: NodeURL.pathToFileURL(`${__dirname}/preview-pick-preload.cjs`).href,
233 };
234 }),
235});
236​
237export const setAnnotationTheme = DesktopIpc.makeIpcMethod({
238 channel: IpcChannels.PREVIEW_SET_ANNOTATION_THEME_CHANNEL,
239 payload: DesktopPreviewAnnotationThemeInputSchema,
240 result: Schema.Void,
241 handler: Effect.fn("desktop.ipc.preview.setAnnotationTheme")(function* ({ theme }) {
242 const manager = yield* PreviewManager.PreviewManager;
243 yield* manager.setAnnotationTheme(theme);
244 }),
245});
246​
247export const pickElement = DesktopIpc.makeIpcMethod({
248 channel: IpcChannels.PREVIEW_PICK_ELEMENT_CHANNEL,
249 payload: DesktopPreviewTabInputSchema,
250 result: Schema.NullOr(PreviewAnnotationSubmissionResultSchema),
251 handler: Effect.fn("desktop.ipc.preview.pickElement")(function* ({ tabId }) {
252 const manager = yield* PreviewManager.PreviewManager;
253 return yield* manager.pickElement(tabId);
254 }),
255});
256​
257export const captureScreenshot = DesktopIpc.makeIpcMethod({
258 channel: IpcChannels.PREVIEW_CAPTURE_SCREENSHOT_CHANNEL,
259 payload: DesktopPreviewTabInputSchema,
260 result: DesktopPreviewScreenshotArtifactSchema,
261 handler: Effect.fn("desktop.ipc.preview.captureScreenshot")(function* ({ tabId }) {
262 const manager = yield* PreviewManager.PreviewManager;
263 return yield* manager.captureScreenshot(tabId);
264 }),
265});
266​
267export const revealArtifact = DesktopIpc.makeIpcMethod({
268 channel: IpcChannels.PREVIEW_REVEAL_ARTIFACT_CHANNEL,
269 payload: DesktopPreviewArtifactInputSchema,
270 result: Schema.Void,
271 handler: Effect.fn("desktop.ipc.preview.revealArtifact")(function* ({ path }) {
272 const manager = yield* PreviewManager.PreviewManager;
273 yield* manager.revealArtifact(path);
274 }),
275});
276​
277export const copyArtifactToClipboard = DesktopIpc.makeIpcMethod({
278 channel: IpcChannels.PREVIEW_COPY_ARTIFACT_CHANNEL,
279 payload: DesktopPreviewArtifactInputSchema,
280 result: Schema.Void,
281 handler: Effect.fn("desktop.ipc.preview.copyArtifactToClipboard")(function* ({ path }) {
282 const manager = yield* PreviewManager.PreviewManager;
283 yield* manager.copyArtifactToClipboard(path);
284 }),
285});
286​
287export const automationStatus = DesktopIpc.makeIpcMethod({
288 channel: IpcChannels.PREVIEW_AUTOMATION_STATUS_CHANNEL,
289 payload: DesktopPreviewTabInputSchema,
290 result: DesktopPreviewAutomationStatusSchema,
291 handler: Effect.fn("desktop.ipc.preview.automationStatus")(function* ({ tabId }) {
292 const manager = yield* PreviewManager.PreviewManager;
293 return yield* manager.automationStatus(tabId);
294 }),
295});
296​
297export const automationSnapshot = DesktopIpc.makeIpcMethod({
298 channel: IpcChannels.PREVIEW_AUTOMATION_SNAPSHOT_CHANNEL,
299 payload: DesktopPreviewTabInputSchema,
300 result: PreviewAutomationSnapshot,
301 handler: Effect.fn("desktop.ipc.preview.automationSnapshot")(function* ({ tabId }) {
302 const manager = yield* PreviewManager.PreviewManager;
303 return yield* manager.automationSnapshot(tabId);
304 }),
305});
306​
307export const automationClick = DesktopIpc.makeIpcMethod({
308 channel: IpcChannels.PREVIEW_AUTOMATION_CLICK_CHANNEL,
309 payload: DesktopPreviewAutomationClickInputSchema,
310 result: Schema.Void,
311 handler: Effect.fn("desktop.ipc.preview.automationClick")(function* ({ tabId, input }) {
312 const manager = yield* PreviewManager.PreviewManager;
313 yield* manager.automationClick(tabId, input);
314 }),
315});
316​
317export const automationType = DesktopIpc.makeIpcMethod({
318 channel: IpcChannels.PREVIEW_AUTOMATION_TYPE_CHANNEL,
319 payload: DesktopPreviewAutomationTypeInputSchema,
320 result: Schema.Void,
321 handler: Effect.fn("desktop.ipc.preview.automationType")(function* ({ tabId, input }) {
322 const manager = yield* PreviewManager.PreviewManager;
323 yield* manager.automationType(tabId, input);
324 }),
325});
326​
327export const automationPress = DesktopIpc.makeIpcMethod({
328 channel: IpcChannels.PREVIEW_AUTOMATION_PRESS_CHANNEL,
329 payload: DesktopPreviewAutomationPressInputSchema,
330 result: Schema.Void,
331 handler: Effect.fn("desktop.ipc.preview.automationPress")(function* ({ tabId, input }) {
332 const manager = yield* PreviewManager.PreviewManager;
333 yield* manager.automationPress(tabId, input);
334 }),
335});
336​
337export const automationScroll = DesktopIpc.makeIpcMethod({
338 channel: IpcChannels.PREVIEW_AUTOMATION_SCROLL_CHANNEL,
339 payload: DesktopPreviewAutomationScrollInputSchema,
340 result: Schema.Void,
341 handler: Effect.fn("desktop.ipc.preview.automationScroll")(function* ({ tabId, input }) {
342 const manager = yield* PreviewManager.PreviewManager;
343 yield* manager.automationScroll(tabId, input);
344 }),
345});
346​
347export const automationEvaluate = DesktopIpc.makeIpcMethod({
348 channel: IpcChannels.PREVIEW_AUTOMATION_EVALUATE_CHANNEL,
349 payload: DesktopPreviewAutomationEvaluateInputSchema,
350 result: Schema.Unknown,
351 handler: Effect.fn("desktop.ipc.preview.automationEvaluate")(function* ({ tabId, input }) {
352 const manager = yield* PreviewManager.PreviewManager;
353 return yield* manager.automationEvaluate(tabId, input);
354 }),
355});
356​
357export const automationWaitFor = DesktopIpc.makeIpcMethod({
358 channel: IpcChannels.PREVIEW_AUTOMATION_WAIT_FOR_CHANNEL,
359 payload: DesktopPreviewAutomationWaitForInputSchema,
360 result: Schema.Void,
361 handler: Effect.fn("desktop.ipc.preview.automationWaitFor")(function* ({ tabId, input }) {
362 const manager = yield* PreviewManager.PreviewManager;
363 yield* manager.automationWaitFor(tabId, input);
364 }),
365});
366​
367export const saveRecording = DesktopIpc.makeIpcMethod({
368 channel: IpcChannels.PREVIEW_RECORDING_SAVE_CHANNEL,
369 payload: DesktopPreviewRecordingSaveInputSchema,
370 result: DesktopPreviewRecordingArtifactSchema,
371 handler: Effect.fn("desktop.ipc.preview.saveRecording")(function* ({ tabId, mimeType, data }) {
372 const manager = yield* PreviewManager.PreviewManager;
373 return yield* manager.saveRecording(tabId, mimeType, data);
374 }),
375});
376​
377export const methods = [
378 createTab,
379 closeTab,
380 registerWebview,
381 navigate,
382 goBack,
383 goForward,
384 refresh,
385 zoomIn,
386 zoomOut,
387 resetZoom,
388 hardReload,
389 setColorScheme,
390 setAudioMuted,
391 openDevTools,
392 clearCookies,
393 clearCache,
394 getPreviewConfig,
395 setAnnotationTheme,
396 pickElement,
397 cancelPickElement,
398 captureScreenshot,
399 revealArtifact,
400 copyArtifactToClipboard,
401 openPictureInPicture,
402 closePictureInPicture,
403 automationStatus,
404 automationSnapshot,
405 automationClick,
406 automationType,
407 automationPress,
408 automationScroll,
409 automationEvaluate,
410 automationWaitFor,
411 startRecording,
412 stopRecording,
413 saveRecording,
414] as const;