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) })