Files
timeline-desktop/src/main/preload.ts
jianghao 9c9997ae64 feat: 初始化 Timeline 桌面客户端基础框架
添加 Electron 应用基础结构,包括:
- package.json 配置文件
- 主进程和预加载脚本
- README 文档
- 自动更新功能
- 系统托盘和菜单
- IPC 通信机制
2026-02-24 10:41:39 +08:00

66 lines
1.9 KiB
TypeScript

/**
* Electron Preload 脚本
*
* 功能描述:
* 在渲染进程中安全地暴露主进程 API。
* 使用 contextBridge 确保安全性。
*
* @author Timeline Team
* @date 2024
*/
import { contextBridge, ipcRenderer } from 'electron';
/**
* 暴露给渲染进程的 API
*/
contextBridge.exposeInMainWorld('electronAPI', {
// 应用信息
getVersion: () => ipcRenderer.invoke('get-version'),
getPlatform: () => ipcRenderer.invoke('get-platform'),
// 更新相关
checkUpdate: () => ipcRenderer.invoke('check-update'),
downloadUpdate: () => ipcRenderer.invoke('download-update'),
installUpdate: () => ipcRenderer.invoke('install-update'),
// 更新事件监听
onUpdateAvailable: (callback: (info: any) => void) => {
ipcRenderer.on('update-available', (_, info) => callback(info));
},
onUpdateDownloaded: (callback: (info: any) => void) => {
ipcRenderer.on('update-downloaded', (_, info) => callback(info));
},
// 外部链接
openExternal: (url: string) => ipcRenderer.invoke('open-external', url),
// 导航事件
onNavigate: (callback: (path: string) => void) => {
ipcRenderer.on('navigate', (_, path) => callback(path));
},
// 移除监听器
removeAllListeners: (channel: string) => {
ipcRenderer.removeAllListeners(channel);
}
});
// 类型声明
declare global {
interface Window {
electronAPI: {
getVersion: () => Promise<string>;
getPlatform: () => Promise<string>;
checkUpdate: () => Promise<any>;
downloadUpdate: () => Promise<void>;
installUpdate: () => Promise<void>;
onUpdateAvailable: (callback: (info: any) => void) => void;
onUpdateDownloaded: (callback: (info: any) => void) => void;
openExternal: (url: string) => Promise<void>;
onNavigate: (callback: (path: string) => void) => void;
removeAllListeners: (channel: string) => void;
};
}
}