-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (56 loc) · 1.13 KB
/
server.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
let filePath = null;
let fileText = null;
let fileCallback = null;
function openMarkdownFile(path) {
const fs = require('fs');
if (!path) {
const { dialog } = require('electron');
const paths = dialog.showOpenDialog({
properties: ['openFile'],
filters: [{ name: 'Markdown', extensions: ['md', 'markdown'] }],
});
if (paths && paths.length === 1) {
path = paths[0];
}
}
if (path) {
filePath = path;
fileText = fs.readFileSync(path, 'utf-8');
return true;
}
return false;
}
function saveMarkdownFile() {
if (filePath) {
const fs = require('fs');
fs.writeFileSync(filePath, fileText);
return true;
}
return false;
}
function setFileText(text) {
fileText = text;
setTimeout(() => {
if (fileCallback) {
fileCallback();
}
}, 0);
setTimeout(saveMarkdownFile, 0);
}
function getFileText() {
return fileText;
}
function getFilePath() {
return filePath;
}
function setFileTextListener(callback) {
fileCallback = callback;
}
module.exports = {
openMarkdownFile,
saveMarkdownFile,
setFileText,
setFileTextListener,
getFileText,
getFilePath,
};