import { app , BrowserWindow , ipcMain , Menu , shell } from 'electron' ; import log from 'electron-log' ; import * as Store from 'electron-store' ; import * as windowStateKeeper from 'electron-window-state' ; import * as fs from 'fs-extra' ; import * as os from 'os' ; import * as path from 'path' ; import * as url from 'url' ; app . commandLine . appendSwitch ( 'disable-color-correct-rendering' ) ; log . create ( 'main' ) ; log . transports . file . resolvePath = ( ) => path . join ( app . getPath ( 'userData' ) , 'logs' , 'Knowte.log' ) ; let mainWindow , workerWindow , serve ; const args = process . argv . slice ( 1 ) ; serve = args . some ( ( val ) => val === '--serve' ) ; const globalAny = global ; if ( process . env . NODE_ENV !== 'development' ) { globalAny . __static = require ( 'path' ) . join ( __dirname , '/static' ) . replace ( /\\/g , '\\\\' ) ; } const EventEmitter = require ( 'events' ) ; class GlobalEventEmitter extends EventEmitter { } globalAny . globalEmitter = new GlobalEventEmitter ( ) ; log . transports . file . level = 'info' ; function createMainWindow ( ) { const gotTheLock = app . requestSingleInstanceLock ( ) ; if ( ! gotTheLock ) { app . quit ( ) ; } else { app . on ( 'second-instance' , ( event , commandLine , workingDirectory ) => { if ( mainWindow ) { if ( mainWindow . isMinimized ( ) ) { mainWindow . restore ( ) ; } mainWindow . focus ( ) ; } } ) ; Menu . setApplicationMenu ( null ) ; const mainWindowState = windowStateKeeper ( { defaultWidth : 850 , defaultHeight : 600 , } ) ; mainWindow = new BrowserWindow ( { x : mainWindowState . x , y : mainWindowState . y , width : mainWindowState . width , height : mainWindowState . height , backgroundColor : '#fff' , frame : windowhasFrame ( ) , icon : path . join ( globalAny . __static , os . platform ( ) === 'win32' ? 'icons/icon.ico' : 'icons/64x64.png' ) , webPreferences : { webSecurity : false , nodeIntegration : true , enableRemoteModule : true , contextIsolation : false , } , show : false , } ) ; globalAny . windowHasFrame = windowhasFrame ( ) ; mainWindowState . manage ( mainWindow ) ; if ( serve ) { require ( 'electron-reload' ) ( __dirname , { electron : require ( `${ __dirname }/node_modules/electron` ) , } ) ; mainWindow . loadURL ( 'http://localhost:4200' ) ; } else { mainWindow . loadURL ( url . format ( { pathname : path . join ( __dirname , 'dist/index.html' ) , protocol : 'file:' , slashes : true , } ) ) ; } workerWindow = new BrowserWindow ( { show : false , webPreferences : { nodeIntegration : true , } , } ) ; workerWindow . loadURL ( url . format ( { pathname : path . join ( __dirname , 'dist/worker.html' ) , protocol : 'file:' , slashes : true , } ) ) ; workerWindow . on ( 'closed' , ( ) => { workerWindow = undefined ; } ) ; mainWindow . on ( 'closed' , ( ) => { mainWindow = null ; app . quit ( ) ; } ) ; mainWindow . on ( 'ready-to-show' , ( ) => { mainWindow . show ( ) ; mainWindow . focus ( ) ; } ) ; const handleRedirect = ( e , localUrl ) => { if ( localUrl !== mainWindow . webContents . getURL ( ) ) { e . preventDefault ( ) ; require ( 'electron' ) . shell . openExternal ( localUrl ) ; } } ; mainWindow . webContents . on ( 'will-navigate' , handleRedirect ) ; mainWindow . webContents . on ( 'new-window' , handleRedirect ) ; mainWindow . webContents . on ( 'before-input-event' , ( event , input ) => { if ( input . key . toLowerCase ( ) === 'f12' ) { if ( serve ) { mainWindow . webContents . toggleDevTools ( ) ; } event . preventDefault ( ) ; } } ) ; } } function windowhasFrame ( ) { const settings = new Store ( ) ; if ( ! settings . has ( 'useCustomTitleBar' ) ) { if ( os . platform ( ) === 'win32' ) { settings . set ( 'useCustomTitleBar' , true ) ; } else { settings . set ( 'useCustomTitleBar' , false ) ; } } return ! settings . get ( 'useCustomTitleBar' ) ; } function createNoteWindow ( notePath , noteId , windowHasFrame ) { const noteWindowState = windowStateKeeper ( { defaultWidth : 620 , defaultHeight : 400 , path : notePath , file : `${ noteId }.state` , } ) ; const noteWindow = new BrowserWindow ( { x : noteWindowState . x , y : noteWindowState . y , width : noteWindowState . width , height : noteWindowState . height , backgroundColor : '#fff' , frame : windowHasFrame , icon : path . join ( globalAny . __static , os . platform ( ) === 'win32' ? 'icons/icon.ico' : 'icons/64x64.png' ) , webPreferences : { webSecurity : false , nodeIntegration : true , enableRemoteModule : true , contextIsolation : false , } , show : true , } ) ; globalAny . windowHasFrame = windowHasFrame ; noteWindowState . manage ( noteWindow ) ; if ( serve ) { require ( 'electron-reload' ) ( __dirname , { electron : require ( `${ __dirname }/node_modules/electron` ) , } ) ; noteWindow . loadURL ( `http://localhost:4200#/note?id=${ noteId }` ) ; } else { noteWindow . loadURL ( `file://${ __dirname }/dist/index.html#/note?id=${ noteId }` ) ; } noteWindow . on ( 'page-title-updated' , ( e ) => { e . preventDefault ( ) ; } ) ; noteWindow . on ( 'ready-to-show' , ( ) => { noteWindow . show ( ) ; noteWindow . focus ( ) ; } ) ; const handleRedirect = ( e , localUrl ) => { if ( localUrl !== noteWindow . webContents . getURL ( ) ) { e . preventDefault ( ) ; require ( 'electron' ) . shell . openExternal ( localUrl ) ; } } ; noteWindow . webContents . on ( 'will-navigate' , handleRedirect ) ; noteWindow . webContents . on ( 'new-window' , handleRedirect ) ; noteWindow . webContents . on ( 'before-input-event' , ( event , input ) => { if ( input . key . toLowerCase ( ) === 'f12' ) { if ( serve ) { noteWindow . webContents . toggleDevTools ( ) ; } event . preventDefault ( ) ; } } ) ; } try { log . info ( '[App] [main] +++ Starting +++' ) ; ipcMain . on ( 'open-note-window' , ( event , arg ) => { createNoteWindow ( arg . notePath , arg . noteId , arg . windowHasFrame ) ; } ) ; ipcMain . on ( 'print' , ( event , content ) => { workerWindow . webContents . send ( 'print' , content ) ; } ) ; ipcMain . on ( 'readyToPrint' , ( event ) => { workerWindow . webContents . print ( { silent : false , printBackground : true } ) ; } ) ; ipcMain . on ( 'printPDF' , ( event , content ) => { workerWindow . webContents . send ( 'printPDF' , content ) ; } ) ; ipcMain . on ( 'readyToPrintPDF' , ( event , safePath ) => { workerWindow . webContents . printToPDF ( { } , ( error , data ) => { if ( error ) { throw error ; } fs . writeFile ( safePath , data , ( localError ) => { if ( localError ) { throw localError ; } shell . showItemInFolder ( safePath ) ; } ) ; } ) ; } ) ; app . on ( 'ready' , createMainWindow ) ; app . on ( 'window-all-closed' , ( ) => { log . info ( '[App] [window-all-closed] +++ Stopping +++' ) ; app . quit ( ) ; } ) ; app . on ( 'activate' , ( ) => { if ( mainWindow === null ) { createMainWindow ( ) ; } } ) ; } catch ( e ) { }