feat: DiffLens v0.1.0 初始版本 - 文本对比桌面应用(TypeScript + React + Electron)
This commit is contained in:
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
import { DiffLensApi } from './index'
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
api: DiffLensApi
|
||||
}
|
||||
}
|
||||
|
||||
export {}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { contextBridge, ipcRenderer } from 'electron'
|
||||
|
||||
/** 打开文件返回的数据结构 */
|
||||
export interface FileData {
|
||||
path: string
|
||||
name: string
|
||||
text: string
|
||||
encoding: string
|
||||
}
|
||||
|
||||
/** 主进程暴露给渲染进程的安全 API */
|
||||
const api = {
|
||||
openFile: (side?: 'left' | 'right'): Promise<FileData | null> =>
|
||||
ipcRenderer.invoke('file:open', side ?? null),
|
||||
showInFolder: (filePath: string): Promise<boolean> =>
|
||||
ipcRenderer.invoke('file:show-in-folder', filePath),
|
||||
setClipboard: (text: string): Promise<boolean> =>
|
||||
ipcRenderer.invoke('clipboard:write', text),
|
||||
/** 订阅顶部菜单命令,返回取消订阅函数 */
|
||||
onMenuCommand: (callback: (cmd: string) => void): (() => void) => {
|
||||
const listener = (_e: unknown, cmd: string): void => callback(cmd)
|
||||
ipcRenderer.on('menu:command', listener)
|
||||
return () => {
|
||||
ipcRenderer.removeListener('menu:command', listener)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export type DiffLensApi = typeof api
|
||||
|
||||
if (process.contextIsolated) {
|
||||
try {
|
||||
contextBridge.exposeInMainWorld('api', api)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
} else {
|
||||
// 非隔离环境下透传到 window
|
||||
;(window as unknown as { api: DiffLensApi }).api = api
|
||||
}
|
||||
Reference in New Issue
Block a user