技术栈升级: - JavaScript → TypeScript 5.6(全量类型安全) - 原生 DOM → React 18(函数组件 + Hooks) - 全局变量 → Zustand 5(轻量状态管理) - localStorage → IndexedDB / Dexie.js 4(大容量、异步、索引) - marked.js → unified / remark / rehype(插件化渲染管线) - 无打包 → electron-vite 3(HMR 热更新) - 纯 CSS → CSS Variables + CSS Modules 新增功能: - 标签页状态 IndexedDB 持久化(关闭后可恢复) - 最近打开文件列表 - 大文件虚拟化行号(>2000 行) - 搜索高亮二分查找优化 O(log N) - rehype-sanitize HTML 安全过滤 文件结构: - 7 个源文件 → 51 个模块化文件 - src/main/ 主进程(6 文件) - src/preload/ 预加载(1 文件) - src/renderer/ 渲染进程(42 文件:组件/hooks/stores/lib/db/types/styles) - src/shared/ 共享类型(2 文件) 构建验证: - TypeScript 检查零错误 - electron-vite build 成功 - 产物:main 15kB + preload 2kB + renderer 1.5MB
48 lines
2.2 KiB
TypeScript
48 lines
2.2 KiB
TypeScript
import { contextBridge, ipcRenderer, shell } from 'electron'
|
|
|
|
contextBridge.exposeInMainWorld('electronAPI', {
|
|
// File operations
|
|
openFile: () => ipcRenderer.invoke('dialog:openFile'),
|
|
readFile: (filePath: string) => ipcRenderer.invoke('file:read', filePath),
|
|
saveFile: (data: { filePath: string | null; content: string }) => ipcRenderer.invoke('file:save', data),
|
|
saveFileAs: (data: { content: string }) => ipcRenderer.invoke('file:saveAs', data),
|
|
getCurrentPath: () => ipcRenderer.invoke('file:getCurrentPath'),
|
|
getFileStats: (filePath: string) => ipcRenderer.invoke('file:stats', filePath),
|
|
reloadFile: () => ipcRenderer.invoke('file:reload'),
|
|
|
|
// Tab management
|
|
tabSwitched: (filePath: string | null) => ipcRenderer.invoke('tab:switched', filePath),
|
|
|
|
// Window control
|
|
forceClose: () => ipcRenderer.invoke('window:forceClose'),
|
|
cancelClose: () => ipcRenderer.invoke('window:cancelClose'),
|
|
|
|
// Shell
|
|
openExternal: (url: string) => shell.openExternal(url),
|
|
|
|
// File Tree (Sidebar)
|
|
readDirTree: (dirPath: string) => ipcRenderer.invoke('dir:readTree', dirPath),
|
|
openFolderDialog: () => ipcRenderer.invoke('dir:openDialog'),
|
|
watchDir: (dirPath: string) => ipcRenderer.invoke('dir:watch', dirPath),
|
|
unwatchDir: () => ipcRenderer.invoke('dir:unwatch'),
|
|
|
|
// Events from main process
|
|
onFileOpenInTab: (callback: (data: { filePath: string; content: string }) => void) =>
|
|
ipcRenderer.on('file:openInTab', (_event, data) => callback(data)),
|
|
onMenuSave: (callback: () => void) =>
|
|
ipcRenderer.on('menu:save', () => callback()),
|
|
onMenuSaveAs: (callback: () => void) =>
|
|
ipcRenderer.on('menu:saveAs', () => callback()),
|
|
onViewModeChange: (callback: (mode: string) => void) =>
|
|
ipcRenderer.on('menu:viewMode', (_event, mode) => callback(mode)),
|
|
onExternalModification: (callback: (filePath: string) => void) =>
|
|
ipcRenderer.on('file:externallyModified', (_event, filePath) => callback(filePath)),
|
|
onDirChanged: (callback: () => void) =>
|
|
ipcRenderer.on('sidebar:dirChanged', () => callback()),
|
|
onConfirmClose: (callback: () => void) =>
|
|
ipcRenderer.on('window:confirmClose', () => callback()),
|
|
|
|
// Remove listeners
|
|
removeAllListeners: (channel: string) => ipcRenderer.removeAllListeners(channel)
|
|
})
|