-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
334 lines (290 loc) · 8.41 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
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
const {app, BrowserWindow, ipcMain} = require("electron");
const path = require("path");
const sqlite3 = require("sqlite3").verbose();
let mainWindow;
let loaderWindow;
// icon for the app
const iconPath = path.join(__dirname, "src/public/favicon2.ico");
// function to create loader window
function createLoaderWindow() {
loaderWindow = new BrowserWindow({
width: 1180,
height: 700,
frame: false,
transparent: true,
alwaysOnTop: true,
webPreferences: {
nodeIntegration: false,
enableRemoteModule: false,
contextIsolation: true,
},
});
loaderWindow.loadFile(path.join(__dirname, "src/loader.html"));
// Remove the default menu
loaderWindow.removeMenu();
}
function createMainWindow() {
// Creating the window
mainWindow = new BrowserWindow({
width: 1180,
height: 700,
icon: iconPath,
backgroundColor: "#232323",
webPreferences: {
preload: path.join(__dirname, "preload.js"), // path to preload.js
contextIsolation: true, // Important for security
enableRemoteModule: false,
nodeIntegration: false, // Disable node integration in renderer process
},
});
// main window entry files
mainWindow.loadFile("src/index.html");
// Remove the default menu
mainWindow.removeMenu();
// Show main window after 2 seconds
setTimeout(() => {
loaderWindow.close();
mainWindow.show();
}, 8000);
}
app.on("ready", () => {
createLoaderWindow();
createMainWindow();
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createLoaderWindow();
createMainWindow();
}
});
// Handle IPC communication - test communication
ipcMain.on("message-from-renderer", (event, arg) => {
console.log(arg); // Print message from renderer process
event.reply("reply-from-main", "Hello from the main process");
});
// Database initialization carried below
let db;
// Open database connection
function openDatabase() {
if (!db) {
const dbPath = app.isPackaged
? path.join(process.resourcesPath, "mindscriber.sqlite")
: path.join(__dirname, "mindscriber.sqlite");
db = new sqlite3.Database(dbPath, (err) => {
if (err) {
console.error("Failed to open database:", err);
} else {
console.log("Database opened successfully at", dbPath);
initializeDatabase();
}
});
}
}
// initialize and check if table exists or else create one and insert a welcoming note
function initializeDatabase() {
db.serialize(() => {
db.run(
`
CREATE TABLE IF NOT EXISTS note (
NOTE_ID INTEGER PRIMARY KEY AUTOINCREMENT,
NOTE_CATEGORY TEXT,
NOTE_TITLE TEXT,
NOTE_CONTENT TEXT,
NOTE_DATE TEXT
)
`,
(err) => {
if (err) {
console.error("Error creating note table:", err);
} else {
console.log("Table 'note' ensured to exist.");
// Check if the table is empty
db.get("SELECT COUNT(*) AS count FROM note", (err, row) => {
if (err) {
console.error("Error checking table contents:", err);
} else {
if (row.count === 0) {
// function to format date
function formatDate(date) {
// Array of months in short
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
// get date
const dayOfMonth = String(date.getDate()).padStart(2, "0");
const month = months[date.getMonth()];
const year = date.getFullYear();
// return date
return `${month} ${dayOfMonth}, ${year}`;
}
// function to format time
function formatTime(date) {
// get time
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
// return time
return `${hours}:${minutes}`;
}
//format date to post to database
const currentDate = new Date();
const formattedDate = formatDate(currentDate);
const currentTime = new Date();
const formattedTime = formatTime(currentTime);
const fullDate = `${formattedDate} ${formattedTime}`;
console.log(fullDate);
// Table is empty, insert welcome note
const welcomeNote = {
category: "Getting Started",
title: "Welcome",
content:
"Hello Mindscriber, I'm thrilled to have you on board. This application is designed to help you manage your notes efficiently and effectively. Create new notes, and track your thoughts with ease. If you have any questions, feel free to reach out to THE BLACKGEEK. Happy note-taking!",
date: fullDate,
};
const sql = `
INSERT INTO note (NOTE_CATEGORY, NOTE_TITLE, NOTE_CONTENT, NOTE_DATE)
VALUES (?, ?, ?, ?)
`;
const params = [
welcomeNote.category,
welcomeNote.title,
welcomeNote.content,
welcomeNote.date,
];
db.run(sql, params, function (err) {
if (err) {
console.error("Error inserting welcome note:", err);
} else {
console.log("Welcome note added with ID:", this.lastID);
}
});
} else {
console.log("Table 'note' is not empty, no welcome note added.");
}
}
});
}
}
);
});
}
// new promise to perform db operations whilst open
function withDatabase(fn) {
return new Promise((resolve, reject) => {
openDatabase();
fn(resolve, reject);
});
}
// function to open the database connections
openDatabase();
// handler for feching notes (notes)
ipcMain.handle("fetch-note", async () => {
return withDatabase((resolve, reject) => {
db.all("SELECT * FROM note ORDER BY NOTE_ID DESC", [], (err, notes) => {
if (err) {
console.error("Error fetching notes:", err);
reject(err);
} else {
resolve(notes);
}
});
});
});
// handle for counting notes (notes)
ipcMain.handle("count-notes", async () => {
return withDatabase((resolve, reject) => {
db.all("SELECT COUNT(*) FROM note", [], (err, notes) => {
if (err) {
console.error("Error counting notes:", err);
reject(err);
} else {
resolve(notes);
}
});
});
});
// handle for fetching note by id
ipcMain.handle("fetch-note-by-id", async (event, id) => {
return withDatabase((resolve, reject) => {
db.all("SELECT * FROM note WHERE NOTE_ID = ?", [id], (err, note) => {
if (err) {
console.error("Error fetching note by ID:", err);
reject(err);
} else {
resolve(note);
}
});
});
});
// handle for adding a new note
ipcMain.handle("add-note", async (event, note) => {
return withDatabase((resolve, reject) => {
const {note_category, note_title, note_content, note_date} = note;
const sql =
"INSERT INTO note (NOTE_CATEGORY, NOTE_TITLE, NOTE_CONTENT, NOTE_DATE) VALUES (?, ?, ?, ?)";
const params = [note_category, note_title, note_content, note_date];
db.run(sql, params, function (err) {
if (err) {
console.error("Error adding note:", err);
reject(err);
} else {
resolve({id: this.lastID});
}
});
});
});
// handle for deleting a note
ipcMain.handle("delete-note", async (event, noteId) => {
return withDatabase((resolve, reject) => {
const sql = "DELETE FROM note WHERE NOTE_ID = ?";
db.run(sql, [noteId], function (err) {
if (err) {
console.error("Error deleting note:", err);
reject(err);
} else {
resolve({success: true});
}
});
});
});
// handle for updating a note
ipcMain.handle("update-note", async (event, note) => {
return withDatabase((resolve, reject) => {
const {note_id, note_category, note_title, note_content} = note;
const sql =
"UPDATE note SET NOTE_CATEGORY = ?, NOTE_TITLE = ?, NOTE_CONTENT = ? WHERE NOTE_ID = ?";
const params = [note_category, note_title, note_content, note_id];
db.run(sql, params, function (err) {
if (err) {
console.error("Error updating note:", err);
reject(err);
} else {
resolve();
}
});
});
});
/*
::Developed from scratch with the so purpose of;
1. Learning Desktop App Development with Electron JS
2. Sharpen my JavaScript Coding Skills
3. Writing a Efficient and Clean Code.
Deloveped BY THE BLACKGEEK :)
Happy Coding Dev :)
Happy Note Taking User :)
*/