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

preview.ts

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