-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
76 lines (64 loc) · 1.89 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// main.js
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
const createMenu = require('./menu');
let mainWindow;
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
},
});
mainWindow.loadFile('index.html');
// mainWindow.webContents.openDevTools(); // Open DevTools by default
createMenu(mainWindow);
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
ipcMain.on('save-file', (event, { filePath, content }) => {
fs.writeFileSync(filePath, content, 'utf8');
});
ipcMain.handle('save-file-dialog', async () => {
const result = await dialog.showSaveDialog(mainWindow, {
filters: [{ name: 'Markdown', extensions: ['md'] }]
});
return result;
});
ipcMain.handle('save-file', async (event, { filePath, content }) => {
try {
await fs.promises.writeFile(filePath, content, 'utf8');
return { success: true };
} catch (error) {
console.error('Error saving file:', error);
return { success: false, error: error.message };
}
});
ipcMain.handle('open-file-dialog', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [{ name: 'Markdown', extensions: ['md'] }]
});
if (!result.canceled && result.filePaths.length > 0) {
const filePath = result.filePaths[0];
const content = await fs.promises.readFile(filePath, 'utf8');
return { filePath, content };
}
return null;
});
ipcMain.on('toggle-text-direction', (event) => {
mainWindow.webContents.send('toggle-text-direction');
});