Skip to content
Source
Sign inSign up

desktopPasteAsText.ts

20 lines742 Bytes

1import type { DesktopBridge } from "@t3tools/contracts";
2​
3export const DESKTOP_PASTE_AS_TEXT_EVENT = "t3:paste-as-text";
4​
5/**
6 * Arm composer paste handling before Electron delivers the native clipboard event. The event
7 * bubbles from the focused element, so a composer can listen on its own form or on `target`.
8 */
9export function installDesktopPasteAsText(
10 bridge: Pick<DesktopBridge, "onMenuAction" | "pasteAsText"> | undefined,
11 target: EventTarget,
12): (() => void) | undefined {
13 return bridge?.onMenuAction((action) => {
14 if (action !== "paste-as-text") return;
15 (document.activeElement ?? target).dispatchEvent(
16 new Event(DESKTOP_PASTE_AS_TEXT_EVENT, { bubbles: true }),
17 );
18 void bridge.pasteAsText();
19 });