import { actions , ContentRef , createKernelRef , KernelspecInfo , selectors } from "@nteract/core" ; import { setConfigFile } from "@nteract/mythic-configuration/src/backends/filesystem" ; import { Event , ipcRenderer as ipc , remote } from "electron" ; import * as path from "path" ; import { NewNotebook } from "../common/commands" ; import { dispatchCommandInRenderer } from "../common/commands/dispatch" ; import { ReqContent , ReqKernelSpec } from "../common/commands/types" ; import { closeNotebook } from "./actions" ; import { DESKTOP_NOTEBOOK_CLOSING_NOT_STARTED , DESKTOP_NOTEBOOK_CLOSING_READY_TO_CLOSE } from "./state" ; import { DesktopStore } from "./store" ; import { clipboard } from "electron" ; import * as plist from "plist" ; import { PlistValue } from "plist" ; import { insertImages } from "./insert-images" ; export function onBeforeUnloadOrReload ( contentRef , store , reloading ) { const state = store . getState ( ) ; const model = selectors . model ( state , { contentRef } ) ; if ( ! model || model . type !== "notebook" ) { return ; } const closingState = state . desktopNotebook . closingState ; if ( closingState === DESKTOP_NOTEBOOK_CLOSING_NOT_STARTED ) { setTimeout ( ( ) => store . dispatch ( closeNotebook ( { contentRef , reloading } ) ) , 0 ) ; } if ( closingState !== DESKTOP_NOTEBOOK_CLOSING_READY_TO_CLOSE ) { return false ; } } export function onDrop ( event , contentRef , store ) { let imagePaths = ( ( event . dataTransfer ) ? Array . from ( event . dataTransfer . files ) : [ ] ) . filter ( file => file . type . match ( /image.*/ ) ) . map ( file => file . path ) ; let copyImagesToNotebookDirectory = ( event . dataTransfer ?. effectAllowed == "copy" ) ; let linkImagesAndKeepAtOriginalPath = ( event . dataTransfer ?. effectAllowed == "link" ) ; let embedImagesInNotebook = ! ( copyImagesToNotebookDirectory || linkImagesAndKeepAtOriginalPath ) ; insertImages ( { imagePaths , copyImagesToNotebookDirectory , linkImagesAndKeepAtOriginalPath , embedImagesInNotebook , contentRef , store } ) ; } export function onPaste ( event , contentRef , store ) { if ( clipboard . has ( 'NSFilenamesPboardType' ) ) { event . preventDefault ( ) ; let filePathsFromClipboard = < Array < string > > < PlistValue > ( plist . parse ( clipboard . read ( 'NSFilenamesPboardType' ) ) ) ; let imagePaths = filePathsFromClipboard . filter ( filePath => /[\w-]+\.(png|jpg|jpeg|heic|gif|tiff)/ . test ( filePath ) ) ; insertImages ( { imagePaths , embedImagesInNotebook : true , contentRef , store } ) ; } else if ( clipboard . has ( 'public.tiff' ) ) { event . preventDefault ( ) ; let base64ImageSource = clipboard . readImage ( ) . toDataURL ( ) ; insertImages ( { base64ImageSource , embedImagesInNotebook : true , contentRef , store } ) ; } } export function initGlobalHandlers ( contentRef , store ) { window . onbeforeunload = onBeforeUnloadOrReload . bind ( null , contentRef , store , false ) ; ipc . on ( "reload" , ( ) => onBeforeUnloadOrReload ( contentRef , store , true ) ) ; ipc . on ( "main:load" , ( _event , filepath ) => store . dispatch ( actions . fetchContent ( { filepath : filepath . replace ( "file://" , "" ) , params : { } , kernelRef : createKernelRef ( ) , contentRef , } ) ) ) ; ipc . on ( "main:new" , ( _event , filepath , kernelSpec ) => dispatchCommandInRenderer ( store , NewNotebook , { contentRef , kernelSpec , filepath : filepath ?? undefined , } as ReqContent & ReqKernelSpec ) ) ; ipc . on ( "main:load-config" , ( _event ) => store . dispatch ( setConfigFile ( path . join ( remote . app . getPath ( "home" ) , ".jupyter" , "nteract.json" ) ) ) ) ; window . addEventListener ( 'drop' , ( event ) => onDrop ( event , contentRef , store ) ) ; window . addEventListener ( 'paste' , ( event ) => onPaste ( < ClipboardEvent > event , contentRef , store ) ) ; }