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

preview.ts

398 lines14.0 KB

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