-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
146 lines (123 loc) · 3.95 KB
/
main.js
File metadata and controls
146 lines (123 loc) · 3.95 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
const { app, BrowserWindow, ipcMain, dialog } = require('electron');
const path = require('path');
const fs = require('fs');
// --- FORCE SYSTEM DATA PATH (Hide DB in AppData) ---
const appName = 'Note Vault';
app.setPath('userData', path.join(app.getPath('appData'), appName));
function createWindow () {
const win = new BrowserWindow({
width: 1400,
height: 1000,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
win.loadFile('index.html');
}
app.whenReady().then(createWindow);
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
// --- PDF PRINTING LOGIC ---
ipcMain.on('print-to-pdf', (event, partId) => {
const win = BrowserWindow.fromWebContents(event.sender);
if (!win) return;
const safePartId = (partId || 'note').replace(/[^a-z0-9]/gi, '_');
dialog.showSaveDialog(win, {
title: 'Save Note as PDF',
defaultPath: `${safePartId}.pdf`,
filters: [{ name: 'PDF Files', extensions: ['pdf'] }]
}).then(result => {
if (result.canceled || !result.filePath) {
return;
}
const pdfPath = result.filePath;
const options = {
marginsType: 1,
pageSize: 'Letter',
printBackground: false,
landscape: false
};
// --- TOGGLE LOADING UI (New Event-Based Logic) ---
// Tell renderer to show loader
event.sender.send('pdf-export-started');
// Delay slightly to ensure UI updates
setTimeout(() => {
win.webContents.printToPDF(options).then(data => {
fs.writeFile(pdfPath, data, (error) => {
if (error) {
console.error('Failed to write PDF:', error);
dialog.showErrorBox('Save PDF Error', 'Failed to save the PDF file.');
}
// Tell renderer to hide loader
event.sender.send('pdf-export-complete');
});
}).catch(error => {
console.error('Failed to print PDF:', error);
dialog.showErrorBox('Print PDF Error', 'Failed to generate the PDF.');
// Tell renderer to hide loader on error
event.sender.send('pdf-export-complete');
});
}, 500);
}).catch(err => {
console.error('Save dialog error:', err);
});
});
// --- EXPORT/IMPORT LOGIC ---
// 1. Handle Export Profile
ipcMain.on('export-data', (event, notesData, filename) => {
const win = BrowserWindow.fromWebContents(event.sender);
if (!win) return;
// Use the provided filename, or fallback to a generic default
const defaultName = filename || 'note-vault-profile.json';
dialog.showSaveDialog(win, {
title: 'Export Profile',
defaultPath: defaultName,
filters: [{ name: 'JSON Files', extensions: ['json'] }]
}).then(result => {
if (result.canceled || !result.filePath) {
return;
}
const jsonContent = JSON.stringify(notesData, null, 2);
fs.writeFile(result.filePath, jsonContent, (error) => {
if (error) {
dialog.showErrorBox('Export Error', 'Failed to save profile file.');
} else {
dialog.showMessageBox(win, {
title: 'Export Successful',
message: 'Your profile has been exported successfully.'
});
}
});
});
});
// 2. Handle Import Profile
ipcMain.on('import-data', (event) => {
const win = BrowserWindow.fromWebContents(event.sender);
if (!win) return;
dialog.showOpenDialog(win, {
title: 'Import Profile',
filters: [{ name: 'JSON Files', extensions: ['json'] }],
properties: ['openFile']
}).then(result => {
if (result.canceled || !result.filePaths || result.filePaths.length === 0) {
return;
}
const filePath = result.filePaths[0];
fs.readFile(filePath, 'utf-8', (error, data) => {
if (error) {
dialog.showErrorBox('Import Error', 'Failed to read profile file.');
return;
}
event.sender.send('data-loaded', data);
});
});
});