-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (101 loc) · 3.44 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const { ipcRenderer, remote } = require('electron');
const { Menu, MenuItem, dialog } = remote;
let currentFile = null; //当前文档保存的路径
let isSaved = true; //当前文档是否已保存
let txtEditor = document.getElementById('txtEditor'); //获得TextArea文本框的引用
document.title = "Notepad - Untitled"; //设置文档标题,影响窗口标题栏名称
//给文本框增加右键菜单
const contextMenuTemplate=[
{ role: 'undo' }, //Undo菜单项
{ role: 'redo' }, //Redo菜单项
{ type: 'separator' }, //分隔线
{ role: 'cut' }, //Cut菜单项
{ role: 'copy' }, //Copy菜单项
{ role: 'paste' }, //Paste菜单项
{ role: 'delete' }, //Delete菜单项
{ type: 'separator' }, //分隔线
{ role: 'selectall' } //Select All菜单项
];
const contextMenu=Menu.buildFromTemplate(contextMenuTemplate);
txtEditor.addEventListener('contextmenu', (e)=>{
e.preventDefault();
contextMenu.popup(remote.getCurrentWindow());
});
//监控文本框内容是否改变
txtEditor.oninput=(e)=>{
if(isSaved) document.title += " *";
isSaved=false;
};
//监听与主进程的通信
ipcRenderer.on('action', (event, arg) => {
switch(arg){
case 'new': //新建文件
askSaveIfNeed();
currentFile=null;
txtEditor.value='';
document.title = "Notepad - Untitled";
//remote.getCurrentWindow().setTitle("Notepad - Untitled *");
isSaved=true;
break;
case 'open': //打开文件
askSaveIfNeed();
const files = remote.dialog.showOpenDialog(remote.getCurrentWindow(), {
filters: [
{ name: "Text Files", extensions: ['txt', 'js', 'html', 'md'] },
{ name: 'All Files', extensions: ['*'] } ],
properties: ['openFile']
});
if(files){
currentFile=files[0];
const txtRead=readText(currentFile);
txtEditor.value=txtRead;
document.title = "Notepad - " + currentFile;
isSaved=true;
}
break;
case 'save': //保存文件
saveCurrentDoc();
break;
case 'exiting':
askSaveIfNeed();
ipcRenderer.sendSync('reqaction', 'exit');
break;
}
});
//读取文本文件
function readText(file){
const fs = require('fs');
return fs.readFileSync(file, 'utf8');
}
//保存文本内容到文件
function saveText(text, file){
const fs = require('fs');
fs.writeFileSync(file, text);
}
//保存当前文档
function saveCurrentDoc(){
if(!currentFile){
const file = remote.dialog.showSaveDialog(remote.getCurrentWindow(), {
filters: [
{ name: "Text Files", extensions: ['txt', 'js', 'html', 'md'] },
{ name: 'All Files', extensions: ['*'] } ]
});
if(file) currentFile=file;
}
if(currentFile){
const txtSave=txtEditor.value;
saveText(txtSave, currentFile);
isSaved=true;
document.title = "Notepad - " + currentFile;
}
}
//如果需要保存,弹出保存对话框询问用户是否保存当前文档
function askSaveIfNeed(){
if(isSaved) return;
const response=dialog.showMessageBox(remote.getCurrentWindow(), {
message: 'Do you want to save the current document?',
type: 'question',
buttons: [ 'Yes', 'No' ]
});
if(response==0) saveCurrentDoc(); //点击Yes按钮后保存当前文档
}