import * as os from "os" ; process . env [ "NODE_CONFIG_DIR" ] = os . homedir ( ) + "/.reventlou" ; import { configExists } from "./configHandler" ; configExists ( ) ; import * as path from "path" ; import { app , BrowserWindow , Menu , ipcMain , shell } from "electron" ; import { Database } from "./database" ; import { translate } from "./htmlRenderer" ; import { isValid } from "./validateConfig" ; import { copyToArchive } from "./fileHandler" ; import { log } from "./logger" ; import { runRedis } from "./redisServer" ; import * as config from "config" ; log . info ( "Version " + app . getVersion ( ) ) ; let mainWindow ; let db ; async function createWindow ( ) { runRedis ( ) ; db = new Database ( ) ; db . init ( ) ; mainWindow = new BrowserWindow ( { height : 600 , title : "reventlou " + app . getVersion ( ) , webPreferences : { nodeIntegration : true } , width : 800 } ) ; void mainWindow . loadFile ( path . join ( __dirname , "../index.html" ) ) ; mainWindow . on ( "closed" , ( ) => { mainWindow = null ; } ) ; const mainMenu = Menu . buildFromTemplate ( mainMenuTemplate ) ; Menu . setApplicationMenu ( mainMenu ) ; const handleRedirect = ( e , url ) => { if ( url != mainWindow . webContents . getURL ( ) ) { try { e . preventDefault ( ) ; if ( url . indexOf ( "http" ) != - 1 ) { void shell . openExternal ( url ) ; } else { if ( url . indexOf ( "explorer:file" ) != - 1 ) { shell . showItemInFolder ( os . homedir ( ) + "/.reventlou/" + `${ config . get ( "file.archive" ) }/${ url }` . replace ( "explorer:file://" , "" ) ) ; } else { shell . openPath ( os . homedir ( ) + "/.reventlou/" + `${ config . get ( "file.archive" ) }/${ url }` . replace ( "file://" , "" ) ) ; } } } catch ( e ) { log . error ( e ) ; } } } ; mainWindow . webContents . on ( "will-navigate" , handleRedirect ) ; mainWindow . webContents . on ( "new-window" , handleRedirect ) ; } app . on ( "ready" , createWindow ) ; app . on ( "window-all-closed" , ( ) => { if ( process . platform !== "darwin" ) { app . quit ( ) ; } } ) ; app . on ( "activate" , ( ) => { if ( mainWindow === null ) { createWindow ( ) ; } } ) ; const mainMenuTemplate = [ { label : app . getName ( ) , submenu : [ { role : "about" } , { type : "separator" } , { role : "services" , submenu : [ ] } , { type : "separator" } , { role : "hide" } , { role : "hideothers" } , { role : "unhide" } , { type : "separator" } , { role : "close" } , { role : "quit" } , ] } , { label : "Edit" , submenu : [ { label : "Save" , accelerator : "CmdOrCtrl + S" , click ( ) { handleSave ( ) ; } } , { label : "Clear" , accelerator : "CmdOrCtrl + D" , click ( ) { handleClear ( ) ; } } , { type : "separator" } , { label : "Undo" , accelerator : "CmdOrCtrl+Z" , selector : "undo:" } , { label : "Redo" , accelerator : "Shift+CmdOrCtrl+Z" , selector : "redo:" } , { type : "separator" } , { label : "Cut" , accelerator : "CmdOrCtrl+X" , selector : "cut:" } , { label : "Copy" , accelerator : "CmdOrCtrl+C" , selector : "copy:" } , { label : "Paste" , accelerator : "CmdOrCtrl+V" , selector : "paste:" } , { label : "Select All" , accelerator : "CmdOrCtrl+A" , selector : "selectAll:" } , ] } , ] ; if ( process . env . NODE_ENV !== "production" ) { mainMenuTemplate . push ( { label : "Dev Tools" , submenu : [ { label : "Toggle dev tools" , accelerator : process . platform == "darwin" ? "Command+I" : "Ctrl+I" , click ( ) { mainWindow . webContents . openDevTools ( ) ; } } , { role : "reload" } , { label : "Validate config" , click ( ) { isValid ( ) ; } } , { role : "seperator" } , { label : "Test Status Item" , click ( ) { addStatus ( undefined , "But here goes text" ) ; } } , { label : "Icon Status Item" , click ( ) { addStatus ( "assets/icons/png/icon.png" , undefined ) ; } } , { label : "Clear all Status" , click ( ) { clearStatus ( ) ; } } , { label : "Suggest" , click ( ) { suggest ( ) ; } } , ] } ) ; } function suggest ( ) { mainWindow . webContents . send ( "editor:get:SuggestValue" ) ; } param iconPath param message function addStatus ( iconPath , message ) { mainWindow . webContents . send ( "statusItem:add" , iconPath , message ) ; } /**
 * Clears whole statusBar.
 */ function clearStatus ( ) { mainWindow . webContents . send ( "statusItem:clear" ) ; } let edit = false ; let editHash = "" ; ipcMain . on ( "notify:edit" , ( e , hash ) => { log . warn ( "EDIT NOTIFY" ) ; edit = true ; editHash = hash ; log . warn ( hash ) ; } ) ; function handleSave ( ) { if ( edit ) { log . warn ( "EDIT" ) ; db . deleteNoteByHash ( editHash ) ; mainWindow . webContents . send ( "edit:done" ) ; edit = false ; } mainWindow . webContents . send ( "editor:get:SaveValue" ) ; } ipcMain . on ( "editor:save:value" , function ( _e , value ) { clearStatus ( ) ; let status ; if ( db . save ( value ) ) { status = "Saved." ; } else { status = "Failed to save note." ; } addStatus ( undefined , status ) ; } ) ; ipcMain . on ( "editor:suggest:value" , function ( _e , _value ) { log . debug ( "Needs implementation." ) ; } ) ; ipcMain . on ( "key:search" , async ( e , item ) => { keySearch ( e , item ) ; } ) ; async function keySearch ( e , item ) { let res = [ ] ; const pass = [ ] ; if ( ! item ) { clearStatus ( ) ; addStatus ( undefined , "Type to search..." ) ; } else { res = await db . search ( item ) ; for ( let i = 2 ; i < res . length ; i ++ ) { if ( i % 2 === 0 && i !== 0 ) { pass . push ( [ res [ i - 1 ] , translate ( res [ i ] [ 1 ] ) ] ) ; } } clearStatus ( ) ; addStatus ( undefined , `Results: ${ res [ 0 ] }` ) ; } e . sender . send ( "list:update" , pass ) ; } function handleClear ( ) { mainWindow . webContents . send ( "editor:clear" ) ; } ipcMain . on ( "editor:files:save" , async ( _e , files ) => { for ( const i in files ) { copyToArchive ( files [ i ] ) ; } } ) ; ipcMain . on ( "request:raw:note" , async ( e , hash ) => { e . sender . send ( "serve:raw:note" , await db . getNoteByHash ( hash ) ) ; } ) ; ipcMain . on ( "delete:note" , async ( e , hash , editorContent ) => { await db . deleteNoteByHash ( hash ) ; await keySearch ( e , editorContent ) ; } ) ;