| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- // preload.js
- const { contextBridge, ipcRenderer } = require('electron');
- // 暴露安全的 API 给渲染进程
- contextBridge.exposeInMainWorld('electronAPI', {
- // 系统退出相关操作
- exitApp: () => ipcRenderer.invoke('exit-close'),
- shutdownSystem: () => ipcRenderer.invoke('exit-shutdown'),
-
- // 日志功能(保留原有功能)
- writeLog: (level, msg) => ipcRenderer.invoke('write-log', level, msg),
- // 文件保存功能
- /**
- * 保存 Blob 数据到文件
- * @param {Object} options - 保存选项
- * @param {ArrayBuffer} options.data - 文件数据(ArrayBuffer)
- * @param {string} options.defaultFilename - 默认文件名
- * @param {Array} options.filters - 文件类型过滤器
- * @param {string} options.title - 对话框标题
- * @returns {Promise<{success: boolean, filePath?: string, canceled?: boolean, error?: string}>}
- */
- saveFile: (options) => ipcRenderer.invoke('save-blob', options)
- });
- // 暴露存储API
- contextBridge.exposeInMainWorld('electronStorage', {
- /**
- * 获取存储项
- * @param {string} key - 存储键
- * @returns {Promise<string|null>} 存储的值
- */
- getItem: (key) => ipcRenderer.invoke('storage-get', key),
- /**
- * 设置存储项
- * @param {string} key - 存储键
- * @param {string} value - 要存储的值
- * @returns {Promise<void>}
- */
- setItem: (key, value) => ipcRenderer.invoke('storage-set', key, value),
- /**
- * 删除存储项
- * @param {string} key - 存储键
- * @returns {Promise<void>}
- */
- removeItem: (key) => ipcRenderer.invoke('storage-remove', key),
- });
- // 暴露打印API
- contextBridge.exposeInMainWorld('electronPrint', {
- /**
- * 打印HTML内容(传统方式)
- * @param {Object} options - 打印选项
- * @param {string} options.html - 要打印的HTML内容
- * @param {string} options.orientation - 打印方向 ('portrait' | 'landscape')
- * @param {string} options.paperSize - 纸张尺寸 ('A3' | 'A4')
- * @param {boolean} options.silent - 是否静默打印
- * @param {Object} options.margins - 页边距
- */
- print: (options) => ipcRenderer.invoke('print-film', options),
- /**
- * 打印HTML内容(使用File System API)
- * @param {Object} options - 打印选项
- * @param {string} options.html - 要打印的HTML内容
- * @param {Array} options.imageDataList - 图片数据数组
- * @param {string} options.orientation - 打印方向 ('portrait' | 'landscape')
- * @param {string} options.paperSize - 纸张尺寸 ('A3' | 'A4')
- * @param {boolean} options.silent - 是否静默打印
- * @param {Object} options.margins - 页边距
- */
- printToFile: (options) => ipcRenderer.invoke('print-film-to-file', options),
- /**
- * 导出为PDF
- * @param {Object} options - 导出选项
- */
- printToPDF: (options) => ipcRenderer.invoke('print-to-pdf', options),
- });
|