-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
260 lines (216 loc) · 7.18 KB
/
app.js
File metadata and controls
260 lines (216 loc) · 7.18 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
const electron = require('electron');
const {dialog} = require("electron").remote;
let jQuery = require('jquery');
const hljs = require('highlight.js');
let Quill = require('quill');
const fs = require('fs');
const path = require('path');
const moment = require('moment');
const pdf = require('html-pdf');
let gui = require('./gui.js');
let config = {};
const notesFolder = '/notes/';
const app = (process.type === 'renderer') ? electron.remote.app : electron.app;
// Get the file contents
function readFile(filePath){
try{
filePath = path.join( app.getPath('userData') + filePath );
let data = fs.readFileSync(filePath, 'utf8');
return data;
} catch(err) {
throw(err);
}
}
// Create/save a new file
function saveFile(filePath, content){
try{
filePath = path.join( app.getPath('userData') + filePath );
fs.writeFileSync(filePath, content, 'utf8');
} catch(err) {
throw(err);
}
}
// Delete a file
function deleteFile(filePath){
try{
filePath = path.join( app.getPath('userData') + filePath );
fs.unlinkSync(filePath);
} catch(err) {
throw(err);
}
}
// Make a new directory
function makedir(folderPath){
try{
folderPath = path.join( app.getPath('userData') + folderPath );
fs.mkdirSync(folderPath);
} catch(err) {
throw(err);
}
}
// Check if a path exists
function pathExists(filePath){
try{
filePath = path.join( app.getPath('userData') + filePath );
return fs.existsSync(filePath);
} catch(err) {
throw(err);
}
}
// Add a new note
function addNotebook(title){
// Generate a random string to append to the file name
let id = Math.random().toString(36).substr(2, 9);
let notebookID = (title + "-" + id).toLowerCase().replace(" ", "");
let filePath = notesFolder + notebookID + '.json';
let cfg = JSON.parse(readFile('/config.json'));
cfg.notebooks[notebookID] = { 'title': title, 'path': notebookID+'.json' };
saveFile('/config.json', JSON.stringify(cfg));
saveFile(filePath, JSON.stringify([]));
let defaultContent = {"ops":[{"attributes":{"size":"large","bold":true},"insert":"Untitled"},{"insert":"\n\n\n"}]};
addNote(notebookID, 'Untitled', defaultContent);
}
// Retrieve a list of the notebooks
function getNotebooks(){
return JSON.parse(readFile('/config.json')).notebooks;
}
// Delete a notebook and its file
function deleteNotebook(notebookID){
let cfg = JSON.parse(readFile('/config.json'));
let file = cfg.notebooks[notebookID].path;
delete cfg.notebooks[notebookID];
saveFile('/config.json', JSON.stringify(cfg));
deleteFile(path.join(notesFolder, file));
}
// Get the file path for a notebook
function getNotebookPath(notebookID){
let cfg = JSON.parse(readFile('/config.json'));
return path.join(notesFolder, cfg.notebooks[notebookID].path);
}
// Retrieve a json array of the notes in the notebook
function getNotes(notebookID){
let cfg = JSON.parse(readFile('/config.json'));
let notePath = cfg.notebooks[notebookID].path;
return JSON.parse(readFile( getNotebookPath(notebookID) ));
}
// Add a new note to the notebook
function addNote(notebook, title, content){
let newNote = {};
let id = Math.random().toString(36).substr(2, 9);
newNote.id = id;
newNote.title = title;
newNote.created = moment().format('MM-DD-YYYY, HH:mm');
newNote.favourite = false;
newNote.content = content;
let notesFile = getNotes(notebook);
notesFile.push(newNote);
saveFile(getNotebookPath(notebook), JSON.stringify(notesFile));
}
// Modify the note content
function editNoteContent(notebook, noteID, content){
let notes = getNotes(notebook);
notes.forEach(function(note){
if(note.id == noteID){
title = content.ops[0].insert;
note.title = (title.length > 18) ? title.substr(0, 18) + "..." : title;
note.content = content;
}
});
saveFile(getNotebookPath(notebook), JSON.stringify(notes));
}
// Delete a note
function deleteNote(notebook, noteID){
let notes = getNotes(notebook).filter((note) => note.id != noteID);
saveFile(getNotebookPath(notebook), JSON.stringify(notes));
}
// Get text contents of a note
function getContent(notebook, noteID){
let notes = getNotes(notebook);
let noteContent;
notes.forEach(function(note){
if(note.id == noteID){
noteContent = note.content;
}
});
return noteContent;
}
// Calculate how many days ago the note was created
function getNoteTime(date){
let days = moment().diff(moment(date), 'days');
if(days==0){
return 'Today';
}else if(days==1){
return 'Yesterday';
}else{
return days + " days ago";
}
}
// Add a note to the favourites
function toggleFavourites(notebook, noteID){
let notes = getNotes(notebook);
notes.forEach(function(note){
if(note.id == noteID){
note.favourite = !note.favourite;
}
});
saveFile(getNotebookPath(notebook), JSON.stringify(notes));
}
// Get a list of the favourite notes
function getFavourites(){
let favourites = [];
let notebooks = getNotebooks();
for(var id in notebooks){
let notes = getNotes(id);
notes.forEach(function(note){
if(note.favourite){
favourites.push([id, note]);
}
});
}
return favourites;
}
// Export note contents into PDF file
function exportPdf(content){
let savePath = dialog.showSaveDialog({
filters: [{
name: 'PDF',
extensions: ['pdf']
}]
});
if(savePath){
let css = fs.readFileSync(path.join(__dirname, '/css/quill.snow.css'), 'utf8');
let html = `<!doctype html><html lang="en"><head><meta charset="utf-8"><style>${css}</style></head><body style="margin: 25px 35px">${content}</body></html>`;
var options = { };
pdf.create(html, options).toFile(savePath, function(err, res) {
console.log(err,res);
});
}
}
// Save user settings
function saveSettings(theme, codeTheme){
let cfg = JSON.parse(readFile('/config.json'));
cfg.settings.theme = theme;
cfg.settings.codeTheme = codeTheme;
saveFile('/config.json', JSON.stringify(cfg));
}
function getSettings(){
return JSON.parse(readFile('/config.json')).settings;
}
// Create /notes/ folder
if( !pathExists(notesFolder) ){
makedir(notesFolder);
}
// Load or Initialize the config file
if( !pathExists('/config.json') ){
config = {
'notebooks': {},
'favourites': {},
'settings': { 'theme': 'light', 'codeTheme': 'monokai'}
};
saveFile('/config.json', JSON.stringify(config));
addNotebook('Getting Started');
let startContent = fs.readFileSync(path.join(__dirname, '/start.json'), 'utf8');
addNote(Object.keys(getNotebooks())[0], 'Untitled', JSON.parse(startContent));
}
config = JSON.parse(readFile('/config.json'));
gui.renderGUI();