-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
154 lines (143 loc) · 4.14 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
'use strict';
const {app, BrowserWindow, dialog, Menu, ipcMain} = require('electron');
const path = require("path");
const fs = require("fs");
let eventsTimeOut = 0;
require('electron-reload')(__dirname, {
electron: require(`${__dirname}\\node_modules\\electron`)
});
require("path");
app.on('ready', () => {
createWindow();
});
let windows = new Set();
let openFiles = new Map();
const createWindow = exports.createWindow = () => {
let x, y;
let currentWindow = BrowserWindow.getFocusedWindow();
if (currentWindow) {
const [currentX, currentY] = currentWindow.getPosition();
x = currentX + 110;
y = currentY + 10;
}
let preload = path.resolve(__dirname + '/app/preload.js');
let newWindow = new BrowserWindow({
width: 900,
icon: __dirname + "\\icons\\icon.ico",
height: 500,
show: false,
x: x,
y: y,
webPreferences: {
nodeIntegration: false,
preload: preload
},
});
newWindow.webContents.loadURL('file://' + __dirname + '/app/index.html');
newWindow.on("closed", () => {
windows.delete(newWindow);
stopWatchingFiles(newWindow);
newWindow = null;
})
windows.add(newWindow);
Menu.setApplicationMenu(Menu.buildFromTemplate([]))
//newWindow.webContents.openDevTools();
newWindow.setMenuBarVisibility(false)
newWindow.on('ready-to-show', () => {
newWindow.show();
})
return newWindow;
};
const selectDefaultDirectory = exports.selectDefaultDirectory = (targetWindow) => {
const directories = dialog.showOpenDialog(targetWindow, {
defaultPath: app.getPath('desktop'),
properties: ['openDirectory'],
"message": "Select Defaults Directory"
});
directories.then(response => {
if (!response) {
return;
}
const directory = response['filePaths'][0];
targetWindow.webContents.send("directory-selected", directory);
})
};
app.on("activate", (e, hasVisibleWindow) => {
if (!hasVisibleWindow) {
createWindow();
}
})
app.on('will-finish-launching', () => {
app.on('open-file', (event, file) => {
const win = createWindow();
win.once('ready-to-show', () => {
openFile(win, file)
})
})
})
const saveText = exports.saveText = async (window, filePath, directoryPath, text) => {
window.webContents.send("log", [__dirname + "\\icons\\icon.ico"]);
return fs.mkdir(directoryPath, {recursive: true}, (err) => {
if (err) {
//s
} else {
fs.writeFileSync(filePath, text);
}
});
};
const openFile = exports.openFile = (window, file,{newFile=false}={}) => {
if (fs.existsSync(file)) {
startWatchingFiles(window, file);
const content = fs.readFileSync((file)).toString();
let baseName = path.basename(file);
let directoryPath = path.dirname(file);
let paths = {filePath: file, directoryPath, baseName};
return window.webContents.send("file-opened", paths, content);
}
if(!newFile){
window.webContents.send("file-not-found", false);
}
};
const startWatchingFiles = exports.startWatchingFile = (targetWindow, file) => {
const watcher = fs.watchFile(file, (event) => {
if (eventsTimeOut < 1) {
eventsTimeOut++;
if (event['birthtime']) {
if (!targetWindow.isFocused()) {
const content = fs.readFileSync(file).toString();
targetWindow.webContents.send("file-changed", file, content);
}
}
}
setTimeout(() => eventsTimeOut = 0, 2000);
});
openFiles.set(targetWindow, watcher);
watcher.on('error', () => {
targetWindow.webContents.send("log", "folder deleted!");
if (!fs.existsSync(file)) {
targetWindow.webContents.send("log", "folder deleted!");
}
})
};
const stopWatchingFiles = (window) => {
if (openFiles.has(window)) {
openFiles.get(window).stop();
openFiles.delete(window)
}
};
const getFileFromUser = exports.getFileFromUser = (targetWindow) => {
const files = dialog.showOpenDialog(targetWindow, {
properties: ['openFile'],
filters: [
{name: 'Text Files', extensions: ['txt']},
]
});
files.then(response => {
if (!response) {
return;
}
const file = response['filePaths'][0];
startWatchingFiles(targetWindow, file);
openFile(targetWindow, file)
})
};