-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.js
213 lines (205 loc) · 7.64 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
const {app, BrowserWindow, Menu, ipcMain, dialog, screen, remote, Tray} = require('electron')
const fs = require('fs');
const path = require('path')
const exec = require('child_process').exec
function writeRegedit(cmdstr){
console.log("cmd " + cmdstr)
exec(cmdstr, {encoding: "gbk"},function (err, stdout, stderr) {
if (err) {
console.log("cmd errror" + stderr)
}
console.log("cmd success" )
})
}
v='1.3.0'
canQuit = false;
if(process.platform == 'win32'){
try{
let exePath = path.resolve(__dirname,'../..')+'\\NewPad.exe';//获取exe路径 如 this.remote.process.argv[0]
let eName = "newpad";
writeRegedit('REG ADD HKEY_CURRENT_USER\\SOFTWARE\\Classes\\'+eName+' /t REG_SZ /d Markdown文件 /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\SOFTWARE\\Classes\\'+eName+' /v "URL Protocol" /t REG_SZ /d "" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\SOFTWARE\\Classes\\'+eName+'\\shell\\open\\command /t REG_SZ /d "\\"'+exePath+'\\" \\"%1\\"" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\SOFTWARE\\Classes\\'+eName+'\\DefaultIcon /t REG_SZ /d "\\"'+exePath+'\\",0" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\Software\\Classes\\.md /t REG_SZ /d "'+eName +'" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\Software\\Classes\\.md\\Shellnew /v "NullFile" /t REG_SZ /d "" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\Software\\Classes\\.markdown /t REG_SZ /d "'+eName +'" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\Software\\Classes\\.mdtext /t REG_SZ /d "'+eName +'" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\Software\\Classes\\.mdtxt /t REG_SZ /d "'+eName +'" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\Software\\Classes\\.mdown /t REG_SZ /d "'+eName +'" /f');
writeRegedit('REG ADD HKEY_CURRENT_USER\\Software\\Classes\\.mdwn /t REG_SZ /d "'+eName +'" /f');
}
catch(e){
console.log("HKEY_CURRENT_USER Applications error")
}
}
function createWindow() {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
title: "NewPad",
icon: "./logo.ico",
frame: false,
transparent: true,
hasShadow: false,
maximizable: false,
minimizable: false,
backgroundColor: "rgba(0,0,0,0.001)",
webPreferences: {
nodeIntegration: true, contextIsolation: false,
}
});
var Fpath = 'new'
mainWindow.loadURL('file://' + __dirname + '/index.html');
Menu.setApplicationMenu(null);
//mainWindow.webContents.openDevTools()
//接收最小化命令
ipcMain.on('window-min', function () {
mainWindow.minimize();
})
/*mainWindow.on('close', (event) => {
mainWindow.hide();
if (!canQuit) {
event.preventDefault();
}
});*/
//接收最大化命令
ipcMain.on('window-max', function () {
mainWindow.restore();
console.log(mainWindow.isMaximized());
if (mainWindow.isMaximized()) {
mainWindow.unmaximize();
} else {
mainWindow.maximize();
}
})
//接收关闭命令
ipcMain.on('window-close', function () {
//mainWindow.hide();
mainWindow.close()
})
//打开文件
ipcMain.on('file-open', function (event, arg) {
console.log(Fpath)
dialog.showOpenDialog({
multiSelections: false,
openDirectory: false,
title: '选择文件',
filters: [{name: 'Markdown文件', extensions: ['md', 'markdown']}, {
name: 'Markdown格式的文本', extensions: ['txt']
}]
}).then(result => {
if(result.filePaths.length>0) Fpath = result.filePaths[0];
fs.readFile(result.filePaths[0], 'utf8', function (err, data) {
if (err) {
event.reply('error', '打开失败');
}
event.reply('file-return', data);
})
})
})
ipcMain.on('main-win-info', function (event, arg) {
event.reply('return-main-win-info', {
max: mainWindow.isMaximized(),
min: mainWindow.isMinimized(),
});
})
//保存文件
ipcMain.on('file-save', function (event, arg) {
if (Fpath != 'new') {
fs.writeFile(Fpath, arg, 'utf8', function (err) {
if (err) {
event.reply('error', '保存失败');
}
})
} else {
dialog.showSaveDialog({
multiSelections: false,
openDirectory: false,
title: '保存文件',
filters: [{name: 'Markdown文件', extensions: ['md', 'markdown']}, {
name: 'Markdown格式的文本', extensions: ['txt']
}]
}).then(r => {
if (r.filePath) Fpath = r.filePath;
console.log(Fpath);
fs.writeFile(Fpath, arg, 'utf8', function (err) {
if (err) {
event.reply('error', '保存失败');
}
})
})
}
})
//保存文件
ipcMain.on('init-file-save', function (event, arg) {
if (Fpath != 'new') {
fs.writeFile(Fpath, arg, 'utf8', function (err) {
if (err) {
event.reply('error', '保存失败');
}
})
}
})
if (process.argv[0].indexOf('electron') > -1) {
if (process.argv.length >= 3) {
Fpath = process.argv[process.argv.length - 1];
}
} else {
if (process.argv.length >= 2) {
Fpath = process.argv[process.argv.length - 1];
}
}
ipcMain.on('init-file-open', function (event, arg) {
if (Fpath !== 'new') {
fs.readFile(Fpath, 'utf8', function (err, data) {
if (err) {
event.reply('error', '打开失败');
}
event.reply('file-return', data);
})
}
})
}
function tray() {
// 当托盘最小化时,右击有一个菜单显示,这里进设置一个退出的菜单
let trayMenuTemplate = [{ // 系统托盘图标目录
label: '刷新', click: function () {
//mainWindow.loadFile('inex.html')
mainWindow.loadFile('index.html')
//updataFWindows.loadFile('updaFWindow.html')
}
},{ // 系统托盘图标目录
label: '调试', click: function () {
mainWindow.webContents.openDevTools({mode:'detach'});
}
},{ // 系统托盘图标目录
label: '重启', click: function () {
app.relaunch()
app.exit()
}
}, { // 系统托盘图标目录
label: '退出', click: function () {
canQuit = true;
app.quit(); // 点击之后退出应用
}
}];
appTray = new Tray(path.join(__dirname, 'logo.ico'));
// 图标的上下文菜单
contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
// 设置托盘悬浮提示
appTray.setToolTip('NewPad');
// 设置托盘菜单
appTray.setContextMenu(contextMenu);
// 单机托盘小图标显示应用
appTray.on('click', function () {
mainWindow.show();
});
}
app.whenReady().then(() => {
createWindow()
//tray()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})