preload.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { contextBridge, ipcRenderer } from 'electron';
  2. // ─── Types ───────────────────────────────────────────────────────────────────
  3. interface AppConfig {
  4. recentProjects: RecentProject[];
  5. lastActiveProjectPath?: string;
  6. }
  7. interface RecentProject {
  8. name: string;
  9. path: string;
  10. lastOpenedAt: string;
  11. novelType: string;
  12. }
  13. interface ElectronAPI {
  14. /**
  15. * Opens the native OS folder selection dialog.
  16. * Returns the selected folder path, or null if the user cancelled.
  17. */
  18. selectFolder: () => Promise<string | null>;
  19. /**
  20. * Reads the global app config from the Electron userData directory.
  21. */
  22. getAppConfig: () => Promise<AppConfig>;
  23. /**
  24. * Writes the global app config to the Electron userData directory.
  25. */
  26. saveAppConfig: (config: AppConfig) => Promise<{ success: boolean }>;
  27. }
  28. // ─── Expose API via contextBridge ─────────────────────────────────────────────
  29. const electronAPI: ElectronAPI = {
  30. selectFolder: () => ipcRenderer.invoke('select-folder'),
  31. getAppConfig: () => ipcRenderer.invoke('get-app-config'),
  32. saveAppConfig: (config: AppConfig) => ipcRenderer.invoke('save-app-config', config),
  33. };
  34. contextBridge.exposeInMainWorld('electronAPI', electronAPI);