summaryrefslogtreecommitdiff
path: root/electron/src/main/index.js
blob: 546580eaaa1a75cbeb05746edfeb053a367b630e (plain)
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
/*
 * Authors: see git history
 *
 * Copyright (c) 2010 Authors
 * Licensed under the GNU GPL version 3.0 or later.  See the file LICENSE for details.
 *
 */

'use strict'

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

var port = process.env.FLASKPORT
const printPdfUrl = `http://127.0.0.1:${port}/`

const isDev = process.env.BABEL_ENV === 'development'

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

// Eventually this will be migrated to Vue.
if (targetURL.protocol) {
    winURL = target
} else {
    winURL = `file://${__dirname}/index.html?${targetURL.query || ""}#${targetURL.pathname || ""}`
}

function createWindow() {
    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)
        })
      })
    })
    // 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, pageSize, 'utf-8');
                shell.openPath(path);
            })
        })
    })
}

app.whenReady().then(() => {
    createWindow()
    app.on('activate', () => {
        if(BrowserWindow.getAllWindows().length === 0)  {
            createWindow()
        }
    })
})

app.on('window-all-closed', () => {
    app.quit()
})