-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
99 lines (78 loc) · 2.16 KB
/
Copy pathmain.js
File metadata and controls
99 lines (78 loc) · 2.16 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
const { app, BrowserWindow, ipcMain, dialog } = require("electron");
const path = require("path");
const fs = require("fs");
const Papa = require("papaparse");
// Create Main Window
function createWindow()
{
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
},
});
win.webContents.session.clearCache().then(() => {
win.loadFile("index.html");
});
}
// Open window
app.whenReady().then(createWindow);
// Handle Save As from renderer
ipcMain.on("save-file", async (event, content) =>
{
const win = BrowserWindow.getFocusedWindow();
const { canceled, filePath } = await dialog.showSaveDialog(win, {
title: "Save File",
defaultPath: "untitled",
filters: [
{ name: "Text Files", extensions: ["txt"] },
{ name: "PDF Files", extensions: ["pdf"] },
{ name: "All Files", extensions: ["*"] }
],
});
if (!canceled && filePath)
{
if (filePath.endsWith(".pdf"))
{
win.webContents.send("set-content", content);
setTimeout(async () =>
{
const pdfData = await win.webContents.printToPDF({});
fs.writeFileSync(filePath, pdfData);
console.log("PDF saved to: ", filePath);
event.reply("save-success", filePath);
}, 100);
}
else
{
fs.writeFileSync(filePath, content);
console.log("File saved to: ", filePath);
event.reply("save-success", filePath);
}
}
});
// Handle Opening file
ipcMain.on("open-file", async (event) =>
{
const win = BrowserWindow.getFocusedWindow();
const { canceled, filePaths } = await dialog.showOpenDialog(win, {
title: "Open CSV File",
filters: [
{ name: "CSV Files", extensions: ["csv"] },
{ name: "All Files", extensions: ["*"] },
],
properties: ["openFile"],
});
if (canceled || !filePaths.length)
{
return;
}
const filePath = filePaths[0];
const fileContent = fs.readFileSync(filePath, "utf-8");
// Parse CSV File data
const parsed = Papa.parse(fileContent, { header: true });
const data = parsed.data;
// Send data to renderer
event.reply("file-opened", data);
});