| 1 | import type { DesktopBridge } from "@t3tools/contracts"; |
| 2 | |
| 3 | const SNAP_SHOT_FOCUS_EVENT = "t3code:focus-composer"; |
| 4 | |
| 5 | export function dispatchSnapShotComposerFocus(): void { |
| 6 | if (typeof window === "undefined") return; |
| 7 | window.dispatchEvent(new Event(SNAP_SHOT_FOCUS_EVENT)); |
| 8 | } |
| 9 | |
| 10 | export function subscribeSnapShotComposerFocus(listener: () => void): () => void { |
| 11 | if (typeof window === "undefined") return () => {}; |
| 12 | window.addEventListener(SNAP_SHOT_FOCUS_EVENT, listener); |
| 13 | return () => window.removeEventListener(SNAP_SHOT_FOCUS_EVENT, listener); |
| 14 | } |
| 15 | |
| 16 | type SnapShotMethods = |
| 17 | | "requestSnapShotPermissions" |
| 18 | | "getSnapShotState" |
| 19 | | "checkSnapShotShortcut" |
| 20 | | "setSnapShotShortcutSuppressed" |
| 21 | | "listPendingSnapShots" |
| 22 | | "readSnapShot" |
| 23 | | "acknowledgeSnapShot" |
| 24 | | "onSnapShotEvent"; |
| 25 | |
| 26 | export type DesktopSnapShotBridge = DesktopBridge & Required<Pick<DesktopBridge, SnapShotMethods>>; |
| 27 | |
| 28 | export function getDesktopSnapShotBridge(): DesktopSnapShotBridge | undefined { |
| 29 | const bridge = typeof window === "undefined" ? undefined : window.desktopBridge; |
| 30 | if ( |
| 31 | typeof bridge?.requestSnapShotPermissions !== "function" || |
| 32 | typeof bridge?.getSnapShotState !== "function" || |
| 33 | typeof bridge.checkSnapShotShortcut !== "function" || |
| 34 | typeof bridge.setSnapShotShortcutSuppressed !== "function" || |
| 35 | typeof bridge.listPendingSnapShots !== "function" || |
| 36 | typeof bridge.readSnapShot !== "function" || |
| 37 | typeof bridge.acknowledgeSnapShot !== "function" || |
| 38 | typeof bridge.onSnapShotEvent !== "function" |
| 39 | ) { |
| 40 | return undefined; |
| 41 | } |
| 42 | |
| 43 | return bridge as DesktopSnapShotBridge; |
| 44 | } |