From 06ba453a412c774eb05dd8de8119f8aaf964ab3c Mon Sep 17 00:00:00 2001 From: sitaras Date: Sun, 24 Dec 2023 19:09:36 +0200 Subject: [PATCH] feat: save as all qrs button implementation --- public/electron.js | 33 ++++++++++++++++++++++++++++++++- public/preload.js | 8 ++++++-- src/views/QRGenerator.jsx | 9 ++++++++- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/public/electron.js b/public/electron.js index 0d97c8f..f6eecb2 100644 --- a/public/electron.js +++ b/public/electron.js @@ -31,7 +31,6 @@ function createWindow() { const historyDirName = `${app.getPath("appData")}/QRsHistory`; app.whenReady().then(() => { - createWindow(); if (!fs.existsSync(historyDirName)) { @@ -110,3 +109,35 @@ ipcMain.on("saveQRfile", (event, fileData) => { console.log(err); }); }); + +ipcMain.on("saveQRfilesFolder", (event, files) => { + const options = { + title: "Save QR", + defaultPath: app.getPath("documents"), + }; + + dialog + .showSaveDialog(mainWindow, options) + .then(({ filePath: dir }) => { + if (!dir) return; + + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir); + } + + files?.forEach((file) => { + const fileName = `${dir}/${file?.fileName}.${file?.extension}`; + imageConverter( + fs.readFileSync(file?.file), + file?.extension, + file?.width, + file?.height + ).then((data) => { + fs.writeFileSync(fileName, data); + }); + }); + }) + .catch((err) => { + console.log(err); + }); +}); diff --git a/public/preload.js b/public/preload.js index d5619e9..5a3bd73 100644 --- a/public/preload.js +++ b/public/preload.js @@ -4,13 +4,17 @@ const { contextBridge, ipcRenderer } = require("electron"); contextBridge.exposeInMainWorld("api", { send: (channel, data) => { // whitelist channels - const validChannels = ["convertUrlsToQRs", "saveQRfile"]; + const validChannels = [ + "convertUrlsToQRs", + "saveQRfile", + "saveQRfilesFolder", + ]; if (validChannels.includes(channel)) { ipcRenderer.send(channel, data); } }, receive: (channel, func) => { - const validChannels = ["qrData", "saveQRfile"]; + const validChannels = ["qrData", "saveQRfile", "saveQRfilesFolder"]; if (validChannels.includes(channel)) { // Deliberately strip event as it includes `sender` ipcRenderer.on(channel, (event, ...args) => func(...args)); diff --git a/src/views/QRGenerator.jsx b/src/views/QRGenerator.jsx index 530f18b..93b343e 100644 --- a/src/views/QRGenerator.jsx +++ b/src/views/QRGenerator.jsx @@ -29,6 +29,10 @@ const QRGenerator = () => { window.api.send("convertUrlsToQRs", urlsArray); }; + const handleSaveAsAll = () => { + window.api.send("saveQRfilesFolder", qrData); + }; + return ( { tableStyles={styles.tableInnerContainer} /> {displaySaveAsAllButton && ( - {}}> + Save as all )}