示例工程 33-clipboard-dnd-fs

33-clipboard-dnd-fs/app/preload/index.ts

typescript · 0.9 KB

import { contextBridge, ipcRenderer, webUtils } from "electron";
import { CH, type Snapshot } from "../shared/ipc";

const api = {
  clipboard: {
    get: (): Promise<Snapshot> => ipcRenderer.invoke(CH.get),
    read: (): Promise<Snapshot> => ipcRenderer.invoke(CH.readClipboard),
    writeText: (text: string): Promise<Snapshot> =>
      ipcRenderer.invoke(CH.writeText, { text }),
    writeHtml: (html: string): Promise<Snapshot> =>
      ipcRenderer.invoke(CH.writeHtml, { html }),
  },
  fs: {
    readPath: (path: string): Promise<Snapshot> =>
      ipcRenderer.invoke(CH.readPath, { path }),
    pick: (): Promise<Snapshot> => ipcRenderer.invoke(CH.pickFile),
    refreshRecent: (): Promise<Snapshot> =>
      ipcRenderer.invoke(CH.refreshRecent),
    clearRecent: (): Promise<Snapshot> => ipcRenderer.invoke(CH.clearRecent),
    pathForFile: (file: File): string => webUtils.getPathForFile(file),
  },
};

contextBridge.exposeInMainWorld("api", api);