diff options
| author | rejbasket <39080670+rejbasket@users.noreply.github.com> | 2023-05-22 22:33:19 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-22 22:33:19 +0200 |
| commit | ef6f6580df6e8fbce913eecc1fe7e0f8caf1315b (patch) | |
| tree | c1119a5d1affd44ad27e60cc6981ac98534c518d /electron/src/main | |
| parent | da54f104e6bf5d0e98f7479cf1d060c76e0b01f2 (diff) | |
Update electron version to v14.2.9 (#2214)
Authored-by: rejbasket
Co-authored-by: Kaalleen
Co-authored-by: Lex Neva
Diffstat (limited to 'electron/src/main')
| -rw-r--r-- | electron/src/main/index.dev.js | 32 | ||||
| -rw-r--r-- | electron/src/main/index.js | 169 | ||||
| -rw-r--r-- | electron/src/main/preload.js | 7 |
3 files changed, 87 insertions, 121 deletions
diff --git a/electron/src/main/index.dev.js b/electron/src/main/index.dev.js deleted file mode 100644 index 7fc8c159..00000000 --- a/electron/src/main/index.dev.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Authors: see git history - * - * Copyright (c) 2010 Authors - * Licensed under the GNU GPL version 3.0 or later. See the file LICENSE for details. - * - */ - -/** - * This file is used specifically and only for development. It installs - * `electron-debug` & `vue-devtools`. There shouldn't be any need to - * modify this file, but it can be used to extend your development - * environment. - */ - -/* eslint-disable */ - -// Install `electron-debug` with `devtron` -require('electron-debug')({ showDevTools: true }) - -// Install `vue-devtools` -require('electron').app.on('ready', () => { - let installExtension = require('electron-devtools-installer') - installExtension.default(installExtension.VUEJS_DEVTOOLS) - .then(() => {}) - .catch(err => { - console.log('Unable to install `vue-devtools`: \n', err) - }) -}) - -// Require `main` process to boot app -require('./index') diff --git a/electron/src/main/index.js b/electron/src/main/index.js index d6f259fe..546580ea 100644 --- a/electron/src/main/index.js +++ b/electron/src/main/index.js @@ -8,113 +8,104 @@ 'use strict' -import {app, BrowserWindow, ipcMain, dialog, shell} from 'electron' -var fs = require('fs'); -var path = require('path'); -var tmp = require('tmp'); - +const path = require('path') +const fs = require('fs') +const tmp = require('tmp') const url = require('url') +const { app, BrowserWindow, ipcMain, dialog, shell, Menu} = require('electron') +// url for printPDF flask server which is used in development and production mode -/** - * Set `__static` path to static files in production - * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html - */ -if (process.env.NODE_ENV === 'development') { - // we were run as electron --inspect=5858 path/to/main.js <args> - // so get rid of the first two args - console.log("args " + process.argv) - process.argv.shift() - process.argv.shift() -} else { - global.__static = path.join(__dirname, '/static').replace(/\\/g, '\\\\') -} +var port = process.env.FLASKPORT +const printPdfUrl = `http://127.0.0.1:${port}/` -let mainWindow +const isDev = process.env.BABEL_ENV === 'development' -var target = process.argv[1] || ""; +var target = null +// Finds this url in the argv array and sets to target value +if (process.argv.includes(printPdfUrl)) { + target = printPdfUrl +} else { + target = process.argv[1] || ""; +} var targetURL = url.parse(target) -var winURL = null; +var winURL = null -// Print PDF will give us a full URL to a flask server, bypassing Vue entirely. // Eventually this will be migrated to Vue. if (targetURL.protocol) { - winURL = target + winURL = target } else { - if (process.env.NODE_ENV === 'development') { - winURL = `http://localhost:9080/?${targetURL.query || ""}#${targetURL.pathname || ""}` - } else { - winURL = `file://${__dirname}/index.html?${targetURL.query || ""}#${targetURL.pathname || ""}`; - } + winURL = `file://${__dirname}/index.html?${targetURL.query || ""}#${targetURL.pathname || ""}` } function createWindow() { - /** - * Initial window options - */ - mainWindow = new BrowserWindow({ - height: 563, - useContentSize: true, - width: 1000, - webPreferences: {nodeIntegration: true} - }) - - mainWindow.loadURL(winURL) - mainWindow.maximize() - - mainWindow.on('closed', () => { - mainWindow = null - }) -} - -app.on('ready', createWindow) - -app.on('window-all-closed', () => { - app.quit() -}) - -app.on('activate', () => { - if (mainWindow === null) { - createWindow() - } -}) - -ipcMain.on('save-pdf', function (event, pageSize) { - mainWindow.webContents.printToPDF({"pageSize": pageSize}, function(error, data) { - dialog.showSaveDialog(mainWindow, {"defaultPath": "inkstitch.pdf", - "filters": [{ name: 'PDF', extensions: ['pdf'] }] - }, function(filename, bookmark) { - if (typeof filename !== 'undefined') - fs.writeFileSync(filename, data, 'utf-8'); + const mainWindow = new BrowserWindow({ + useContentSize: true, + webPreferences: { + preload: path.join(__dirname, 'preload.js'), + nodeIntegration: false, + contextIsolation: true, + }, + }) + if (isDev) { + // printPDF in development mode will have dev tools activated + // Vuejs parts of Ink/Stich will not and dev tools must be accessed though the menu of electron window + mainWindow.loadURL(winURL) + mainWindow.webContents.openDevTools() + } else { + mainWindow.loadURL(winURL) + } + // This will remove the menu from the release, in dev mode the menu is available. + if(process.platform === "darwin" && !isDev) { + Menu.setApplicationMenu(Menu.buildFromTemplate([])); + } if(process.platform === "win32" || process.platform === "linux" && !isDev) { + mainWindow.removeMenu(); + } + mainWindow.maximize() + // save to PDF + ipcMain.on('save-pdf', (event, pageSize) => { + const webContents = event.sender + const win = BrowserWindow.fromWebContents(webContents) + const saveOpt = { + title: "Save PDF", + defaultPath: "Inkstitch.pdf", + bookmark: "true", + } + win.webContents.printToPDF({}).then(pageSize => { + dialog.showSaveDialog(saveOpt).then(filename => { + const { filePath } = filename; + fs.writeFileSync(filePath, pageSize, (error) => { + if (error) { + throw error + } + console.log(`Wrote PDF successfully to ${pdfPath}`) + }) + }).catch(error => { + console.log(`Failed to write PDF to ${pdfPath}: `, error) }) + }) }) -}) - -ipcMain.on('open-pdf', function (event, pageSize) { - mainWindow.webContents.printToPDF({"pageSize": pageSize}, function(error, data) { + // openPDF + ipcMain.on('open-pdf', (event, pageSize) => { + const webContents = event.sender + const win = BrowserWindow.fromWebContents(webContents) + win.webContents.printToPDF({}).then(pageSize => { tmp.file({keep: true, discardDescriptor: true}, function(err, path, fd, cleanupCallback) { - fs.writeFileSync(path, data, 'utf-8'); - shell.openItem(path); + fs.writeFileSync(path, pageSize, 'utf-8'); + shell.openPath(path); + }) }) }) -}) - - -/** - * Auto Updater - * - * Uncomment the following code below and install `electron-updater` to - * support auto updating. Code Signing with a valid certificate is required. - * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating - */ - -/* -import { autoUpdater } from 'electron-updater' +} -autoUpdater.on('update-downloaded', () => { - autoUpdater.quitAndInstall() +app.whenReady().then(() => { + createWindow() + app.on('activate', () => { + if(BrowserWindow.getAllWindows().length === 0) { + createWindow() + } + }) }) -app.on('ready', () => { - if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates() +app.on('window-all-closed', () => { + app.quit() }) - */ diff --git a/electron/src/main/preload.js b/electron/src/main/preload.js new file mode 100644 index 00000000..b7d1c02a --- /dev/null +++ b/electron/src/main/preload.js @@ -0,0 +1,7 @@ +const { contextBridge, ipcRenderer } = require ('electron') + +contextBridge.exposeInMainWorld('inkstitchAPI', { + savepdf: (pageSize) => { ipcRenderer.send('save-pdf', pageSize) }, + openpdf: (pageSize) => { ipcRenderer.send('open-pdf', pageSize) }, + flaskport: () => process.env.FLASKPORT, +}) |
