-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCodeEditor.js
More file actions
425 lines (367 loc) · 10.2 KB
/
CodeEditor.js
File metadata and controls
425 lines (367 loc) · 10.2 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Requirements
const electron = require('electron')
const {app, dialog, BrowserWindow, Menu, ipcRenderer, MenuItem, ipcMain, clipboard} = electron
// Node.js requirements
const path = require('path')
const url = require('url')
const fs = require('fs')
const swal = require('sweetalert2')
// Import folder class
const Folder = require(__dirname + '/Classes/folder.js')
// Main window
let mainWindow
// Name and Path of selected File (Context menu)
let filepath;
let filename;
let force_quit = false;
// Template of the Menu
const mainMenuTemplate = [
{
label: 'File',
submenu: [
{label: 'Open File', click(){
openDialogFile();
}},{
label: 'Open Folders', // This part is for opening a file
click(){
openDialogFolder();
}
}
, {
label : 'New file',
accelerator : process.platform == 'darwin' ? 'Command+N' : 'Ctrl+N',
click(){
dialog.showSaveDialog((filename) => {
if (filename === undefined){
console.log('File undefined');
return;
}
fs.writeFile(filename, '', (err) => {
if (err) {
console.log('Error with creating file');
return;
}
openNewFile(filename);
})
})
}
},{
label: 'Save',
accelerator: process.platform == 'darwin' ? 'Command+S' : 'Ctrl+S',
click (){
mainWindow.webContents.send('save');
}
}, {
label: 'Save As',
accelerator: process.platform == 'darwin'? 'Command+Shift+S' : 'Ctrl+Shift+S',
click() {
var savePath = dialog.showSaveDialog((filename) => {
if (filename === undefined){
}else {
data = filename;
mainWindow.webContents.send('saveas', data);
}
})
}
}, {
label : 'Close Tab',
//accelerator : process.platform == 'darwin' ? 'Command+Shift+Q' : 'Command+Shift+Q',
click(){
mainWindow.webContents.send('closetab')
}
},{
label: 'Close Program',
accelerator: process.platform == 'darwin' ? 'Command+Q' : 'Ctrl+F5',
click (){
force_quit = true;
fs.writeFile(__dirname + '/stateOfFile.json', '[]', (err) => {
if (err) {
return
}
})
fs.readdir(__dirname + '/Files/', (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(__dirname + '/Files/', file), err => {
if (err) throw err;
});
}
});
app.quit()
}
},{
label: 'reload',
accelerator: process.platform == 'darwin' ? 'Command+R' : 'F5',
click(){
mainWindow.reload();
}
}
]
}, {
label : 'Edit',
submenu: [{
role: 'undo'
},{
role: 'redo'
},{
type: 'separator'
},
{
role: 'cut'
},
{
role: 'copy'
},
{
role: 'paste'
}]
}, {
label: 'View',
submenu :
[{role: 'togglefullscreen'},
{
label: 'Increase Font Size',
accelerator: process.platform == 'darwin' ? 'Command+ UP' : 'Ctrl+ UP',
click(){
mainWindow.webContents.send('increasefontsize')
}
}, {
label: 'Decrease Font Size',
accelerator: process.platform == 'darwin' ? 'Command+DOWN' : 'Ctrl+DOWN',
click(){
mainWindow.webContents.send('decreasefontsize')
}
}, {
label: 'Reset Font Size',
accelerator: process.platform == 'darwin' ? 'Command+O' : 'Ctrl+O',
click (){
mainWindow.webContents.send('resetfontsize')
}
}]
}, {
role: 'window',
submenu: [
{role: 'minimize'},
]}, {
role: 'help',
submenu: [
{
label: 'Learn More',
click () {
require('electron').shell.openExternal('file://' +__dirname + '/details.html');
}
}
]
}
]
// Contexts Menus for webViews and TreeView
const ctxMenuHome = new Menu();
const ctxMenuFiles = new Menu();
// The create window (Main Function)
function createWindow() {
mainWindow = new BrowserWindow({})
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
var data = fs.readFileSync(__dirname +'/folderOpen.json', 'utf8', (err) => {if (err) {return}})
if (data != ''){
data = JSON.parse(data);
// Create the Folder object if there is opened file
writeFolderOpen(data.path);
}
// Add the Menu to the app
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate)
Menu.setApplicationMenu(mainMenu)
// If Closed
mainWindow.on('closed', function() {
mainWindow = null
})
// make the size Full Screen
mainWindow.maximize();
// Context Menu Inside Pages for (WebView)
ctxMenuHome.append(new MenuItem({
role: 'selectall',
}))
ctxMenuHome.append(new MenuItem({
type: 'separator',
}))
ctxMenuHome.append(new MenuItem({
role: 'copy',
}))
ctxMenuHome.append(new MenuItem({
role: 'cut',
}))
ctxMenuHome.append(new MenuItem({
role: 'paste',
}))
// Files Context Menu for (Tree View)
ctxMenuFiles.append(new MenuItem({
label: 'Copy Path',
click(){
// Copy path of file [get name of File and then parse the path]
clipboard.writeText(filepath)
}
}))
ctxMenuFiles.append(new MenuItem({
type: 'separator',
}))
ctxMenuFiles.append(new MenuItem({
label: 'Delete',
click(){
// Delete the file [Get the selected file name and then parse the path from openFolders.json and detele the file]
deleteFile(filepath) // This function is basically deleting any path and set the new Folder (with file Deleted on it)
// On the folderOpen.json
var data = fs.readFileSync(__dirname +'/folderOpen.json', 'utf8', (err) => {if (err) {return}}) // Reading the new folderOpen.json
data = JSON.parse(data); // parse the data
mainWindow.webContents.send('delete') // Send delete event
}
}))
mainWindow.on('close', function(e){
if (!force_quit){
e.preventDefault();
mainWindow.webContents.send('closeButtonEvent')
}
});
}
// This function is reading the file from a filePath then send data and folder name as a file event
function openNewFile(filePath){
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
return
}
dataNew = [filePath, data];
mainWindow.webContents.send('file' , dataNew);
})
}
// This function deletes any File from a specific path
function deleteFile(path){
var rimraf = require('rimraf');
rimraf(path, function () {
var data = fs.readFileSync(__dirname +'/folderOpen.json', 'utf8', (err) => {if (err) {return}})
data = JSON.parse(data);
writeFolderOpen(data.path);
});
}
// This function read a folder path then return Json object as result which hold all sub files and folders
function getTreeData(filename) {
var stats = fs.lstatSync(filename),
info = {
path: filename,
name: path.basename(filename)
};
if (stats.isDirectory()) {
info.type = "folder";
info.children = fs.readdirSync(filename).map(function(child) {
return getTreeData(filename + '/' + child); // path.join(filename, child);
});
} else {
// Assuming it's a file. In real life it could be a symlink or
// something else!
info.type = "file";
info.children = [];
}
return info;
}
// This function shows the context menu of the Tree View
function rightClickFiles(e){
ctxMenuFiles.popup(mainWindow, e.x, e.y);
}
// This function shows the context menu of the WebViews
function rightClickHome(e){
console.dir(e);
ctxMenuHome.popup(mainWindow, e.x, e.y);
}
// This functions shows the Open Folder Dialog (Open Folder)
function openDialogFolder(){
dialog.showOpenDialog({
properties: ['openDirectory', 'promptToCreate'],
}, (foldername) => {
if (foldername === undefined){return}
writeFolderOpen(foldername[0]);
mainWindow.webContents.send('folder');
});
}
// This functions shows the Open File Dialog (Open File)
function openDialogFile(){
dialog.showOpenDialog((fileNames) => {
if (fileNames === undefined){
console.log('File undefined')
}else {
readFile(fileNames[0])
}
})
}
// !!!! Questionable Part -- Reason : I've never used this function !!!!
/*
function getNewFoldersWhenOpen(){
var data = fs.readFileSync('./folderOpen.json', 'utf8', (err) => {if (err) {return}})
data = JSON.parse(data);
writeFolderOpen(data.path)
}
*/
// Function that create the Folder object and launch the createFolder function that write the folderOpen.json
function writeFolderOpen(path){
var dataa = getTreeData(path);
dataa = JSON.stringify(dataa, null, 2)
let folder = new Folder(dataa.name, dataa.path, dataa);
folder.createFolder(__dirname + '/folderOpen.json');
}
// readFile function that read the file from a path and send Data as file event
function readFile(filePath){
fs.readFile(filePath, 'utf8', (err, data) => {
if (err){
return
}
dataNew = [filePath, data]
mainWindow.webContents.send('file', dataNew)
})
}
/* ********* Receiving events part ********* */
// event of right click for the webviews
ipcMain.on('home_page_right_click', (e)=>{
rightClickHome(e);
})
/*
ipcMain.on('files_right_click', (e) => {
rightClickFiles(e);
})
*/
// Event of right click for the TreeView
ipcMain.on('right_click_file', (e, elem) => {
filepath = elem.path;
filename = elem.name
rightClickFiles(e);
})
ipcMain.on('quitAppAllFileSaved', (e) => {
force_quit = true;
app.quit();
})
// The launch of our application
app.on('ready', createWindow)
// Close all Window = (quit)
app.on('window-all-closed', function() {
// set Empty to stateOfFile
fs.writeFile(__dirname +'/stateOfFile.json', '[]', (err) => {
if (err) {
return
}
})
// Delete all Sub files of Files Directory
fs.readdir(__dirname + '/Files/', (err, files) => {
if (err) throw err;
for (const file of files) {
fs.unlink(path.join(__dirname +'/Files/', file), err => {
if (err) throw err;
});
}
});
app.quit()
})
// Activate event if there is no window create it
app.on('closeSave', function() {
if (mainWindow === null) {
createWindow()
}
})