|
1 | | -import { app, BrowserWindow, shell, ipcMain } from 'electron' |
| 1 | +import { app, BrowserWindow, shell, ipcMain, dialog } from 'electron' |
2 | 2 | import { createRequire } from 'node:module' |
3 | 3 | import { fileURLToPath } from 'node:url' |
4 | 4 | import path from 'node:path' |
@@ -50,11 +50,11 @@ async function createWindow() { |
50 | 50 | webPreferences: { |
51 | 51 | preload, |
52 | 52 | // Warning: Enable nodeIntegration and disable contextIsolation is not secure in production |
53 | | - // nodeIntegration: true, |
| 53 | + nodeIntegration: true, |
54 | 54 |
|
55 | 55 | // Consider using contextBridge.exposeInMainWorld |
56 | 56 | // Read more on https://www.electronjs.org/docs/latest/tutorial/context-isolation |
57 | | - // contextIsolation: false, |
| 57 | + contextIsolation: false, |
58 | 58 | }, |
59 | 59 | }) |
60 | 60 |
|
@@ -121,3 +121,54 @@ ipcMain.handle('open-win', (_, arg) => { |
121 | 121 | childWindow.loadFile(indexHtml, { hash: arg }) |
122 | 122 | } |
123 | 123 | }) |
| 124 | + |
| 125 | +ipcMain.handle('get-file-icon', async (_, filePath) => { |
| 126 | + try { |
| 127 | + const icon = await app.getFileIcon(filePath, { size: 'large' }); |
| 128 | + return icon.toDataURL(); |
| 129 | + } catch (e) { |
| 130 | + console.error('Failed to get icon', e); |
| 131 | + return null; |
| 132 | + } |
| 133 | +}); |
| 134 | + |
| 135 | +ipcMain.handle('open-path', async (_, filePath) => { |
| 136 | + await shell.openPath(filePath); |
| 137 | +}); |
| 138 | + |
| 139 | +ipcMain.handle('open-external', async (_, url) => { |
| 140 | + await shell.openExternal(url); |
| 141 | +}); |
| 142 | + |
| 143 | +ipcMain.handle('save-markdown', async (_, { content, defaultPath }) => { |
| 144 | + const { canceled, filePath } = await dialog.showSaveDialog({ |
| 145 | + defaultPath, |
| 146 | + filters: [{ name: 'Markdown', extensions: ['md'] }] |
| 147 | + }); |
| 148 | + if (!canceled && filePath) { |
| 149 | + const fs = await import('node:fs/promises'); |
| 150 | + await fs.writeFile(filePath, content); |
| 151 | + return true; |
| 152 | + } |
| 153 | + return false; |
| 154 | +}); |
| 155 | + |
| 156 | +ipcMain.handle('select-file', async () => { |
| 157 | + const { canceled, filePaths } = await dialog.showOpenDialog({ |
| 158 | + properties: ['openFile'] |
| 159 | + }); |
| 160 | + if (!canceled && filePaths.length > 0) { |
| 161 | + return filePaths[0]; |
| 162 | + } |
| 163 | + return null; |
| 164 | +}); |
| 165 | + |
| 166 | +ipcMain.handle('select-folder', async () => { |
| 167 | + const { canceled, filePaths } = await dialog.showOpenDialog({ |
| 168 | + properties: ['openDirectory'] |
| 169 | + }); |
| 170 | + if (!canceled && filePaths.length > 0) { |
| 171 | + return filePaths[0]; |
| 172 | + } |
| 173 | + return null; |
| 174 | +}); |
0 commit comments