Skip to content

Commit

Permalink
feat: add electron context bridge (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgz5689 authored Jun 12, 2024
1 parent c441a60 commit 689169f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion electron/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
console.log("---- electron/preload.ts ----");
import { contextBridge, shell } from "electron";
import fs from "node:fs";

import { IElectronAPI } from "../../src/types/global";

const Api: IElectronAPI = {
fileExists: fs.existsSync,
getPlatform: () => process.platform,
openFile: shell.openPath,
showFile: shell.showItemInFolder,
};

contextBridge.exposeInMainWorld("electronAPI", Api);
12 changes: 12 additions & 0 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface IElectronAPI {
fileExists: (path: string) => boolean;
getPlatform: () => string;
openFile: (path: string) => Promise<string>;
showFile: (path: string) => void;
}

declare global {
interface Window {
electronAPI?: IElectronAPI;
}
}

0 comments on commit 689169f

Please sign in to comment.