Skip to content

Commit a614848

Browse files
committed
Open & Save
1 parent 46433b4 commit a614848

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules/
1+
node_modules/
2+
code.txt

appWindow.html

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<title>Text Editor</title>
56
</head>
7+
<style>
8+
body {
9+
margin: 0;
10+
padding: 0;
11+
overflow: hidden;
12+
}
13+
#content {
14+
width: 100%;
15+
height: 50vh;
16+
}
17+
</style>
618
<body>
7-
19+
<textarea id="content"></textarea>
20+
<script>
21+
const electron = require('electron');
22+
const {ipcRenderer} = electron;
23+
const txt = document.getElementById("content");
24+
ipcRenderer.on('file:load', (e, data) => {
25+
txt.innerHTML = data;
26+
});
27+
ipcRenderer.on('file:save', () => {
28+
ipcRenderer.send('save', txt.value);
29+
})
30+
</script>
831
</body>
32+
933
</html>

main.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
const electron = require('electron');
22
const url = require('url');
33
const path = require('path');
4+
const fs = require("fs");
45

5-
const {app, BrowserWindow, Menu} = electron;
6+
const { app, BrowserWindow, Menu, ipcMain, dialog } = electron;
67

78
let appWindow;
9+
let file_path;
810

911
const appMenuTemplate = [
1012
{
1113
label: 'File',
1214
submenu: [
1315
{
14-
label: 'Open'
16+
label: 'Open',
17+
accelerator:process.platform == 'darwin' ? 'Command+O' : 'Ctrl+O',
18+
click() {
19+
file_path = dialog.showOpenDialog({ filters: [{ name: "Text File", extensions: 'txt' }] }, { properties: ['openFile', 'openDirectory'] });
20+
fs.readFile(file_path[0], "utf8", function (error, data) {
21+
if (error) throw error;
22+
appWindow.webContents.send('file:load', data);
23+
});
24+
}
1525
},
1626
{
17-
label: 'Save'
27+
label: 'Save',
28+
accelerator:process.platform == 'darwin' ? 'Command+S' : 'Ctrl+S',
29+
click() {
30+
appWindow.webContents.send('file:save');
31+
}
1832
},
1933
{
2034
label: 'Quit',
21-
click(){
35+
click() {
2236
app.quit();
2337
}
2438
}
@@ -39,4 +53,8 @@ app.on('ready', () => {
3953
Menu.setApplicationMenu(mainMenu);
4054

4155
appWindow.on('close', () => app.quit());
56+
})
57+
58+
ipcMain.on("save", (e, data) => {
59+
fs.writeFileSync(file_path[0], data.split("\n").join('\r\n'));
4260
})

0 commit comments

Comments
 (0)