-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
184 lines (159 loc) · 5.15 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
// main.js
// Modules to control application life and create native browser window
const {app, BrowserWindow, ipcMain, dialog, Menu, globalShortcut} = require('electron')
const path = require('path')
const fs = require('fs')
const request = require('request');
const renderTree = (filePath, data) => {
const stats = fs.statSync(filePath)
if (stats.isFile()) {
if (path.extname(filePath).toLowerCase() === '.md') {
data.push({
title: path.basename(filePath),
key: filePath,
isLeaf: true
})
}
} else if (stats.isDirectory()) {
const children = [];
data.push({
title: path.basename(filePath),
key: filePath,
children,
})
const files = fs.readdirSync(filePath)
files.forEach(filename => {
const fileDir = path.join(filePath, filename);
renderTree(fileDir, children)
})
}
}
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 1200,
height: 800,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
// contextIsolation: false,
enableRemoteModule: true,
preload: path.join(__dirname, 'preload.js'),
}
})
/*隐藏electron创听的菜单栏*/
Menu.setApplicationMenu(null)
// 加载 index.html
// mainWindow.loadFile(path.join(__dirname, '../build/index.html'))
// mainWindow.loadURL(url.format({
// pathname: path.join(__dirname, './build/index.html'), // 注意这里修改
// protocol: 'file:',
// slashes: true
// }))
// mainWindow.loadURL("http://localhost:8000")
// mainWindow.webContents.openDevTools()
mainWindow.loadFile(path.join(__dirname, './poraBuild/index.html'))
return mainWindow;
// 打开开发工具
// mainWindow.webContents.openDevTools()
}
// 这段程序将会在 Electron 结束初始化
// 和创建浏览器窗口的时候调用
// 部分 API 在 ready 事件触发后才能使用。
app.whenReady().then(() => {
const win = createWindow()
var currentPath;
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
ipcMain.on('open-folder', (event) => {
dialog.showOpenDialog({properties: ['openFile', 'openDirectory']}).then(filePaths => {
if (!filePaths.canceled) {
const treeData = [];
currentPath = filePaths.filePaths[0];
renderTree(filePaths.filePaths[0], treeData)
event.sender.send("read-file", treeData)
}
})
});
ipcMain.on('create-file', (event, data) => {
dialog.showSaveDialog({title: "保存文件", properties: ['createDirectory']}).then(filePaths => {
if (!filePaths.canceled) {
try {
const filePath = filePaths.filePath;
fs.writeFileSync(filePath, data, 'utf8');
event.sender.send("create-file-callback", null);
} catch (e) {
event.sender.send("create-file-callback", e);
}
}
})
});
ipcMain.on("rename", (event, data) => {
try {
const arr = data.path.split(path.sep)
arr[arr.length - 1] = data.title
fs.renameSync(data.path, arr.join(path.sep))
event.sender.send("refresh", true)
} catch (e) {
event.sender.send("refresh", false)
}
})
ipcMain.on("add-file", (event, data) => {
try {
const filename = data.path + path.sep + data.title
fs.writeFileSync(filename, "", 'utf8');
event.sender.send("refresh", true)
} catch (e) {
event.sender.send("refresh", false)
}
})
ipcMain.on("fetch-file", (event, data) => {
const buffer = fs.readFileSync(data, 'utf8');
event.sender.send("write-file", buffer)
})
ipcMain.on('update-file', (event, data) => {
fs.writeFileSync(data.file, data.value, 'utf8');
});
ipcMain.on("refresh-file", (event, data) => {
if (currentPath) {
const treeData = [];
renderTree(currentPath, treeData)
event.sender.send("read-file", treeData)
}
})
ipcMain.on("upload-file", (event, data) => {
uploadFile(event.sender, data)
})
globalShortcut.register('Ctrl+S', function () {
// win.webContents.openDevTools();
win.webContents.send("save-file-command", null)
})
})
const uploadFile = (sender, data) => {
const {line, ch} = data;
var options = {
url: 'http://localhost:36677/upload',
method: 'POST',
};
request(options, (error, response, body) => {
if (!error && response.statusCode === 200) {
const result = JSON.parse(body)
if (result.success) {
sender.send("update-image", {line, ch, data: result.result[0]})
return
}
}
sender.send("update-image", null)
});
}
// 除了 macOS 外,当所有窗口都被关闭的时候退出程序。 There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. 也可以拆分成几个文件,然后用 require 导入。