import * as React from "react" ; import { Callback } from "polar-shared/src/util/Functions" ; import { useComponentDidMount , useComponentWillUnmount } from "../../hooks/ReactLifecycleHooks" ; import { deepMemo } from "../../react/ReactUtils" ; export function isInputCompleteEvent ( event ) { return ( event . ctrlKey || event . metaKey ) && event . key === 'Enter' ; } interface InputCompleteListenerOpts { readonly onComplete : ( ) => void ; readonly onCancel ? : Callback ; } function useInputCompleteWindowListener ( opts ) { const onKeyDown = React . useCallback ( ( event ) => { if ( isInputCompleteEvent ( event ) ) { opts . onComplete ( ) ; } if ( event . key === 'Escape' && opts . onCancel ) { opts . onCancel ( ) ; } console . log ( "FIXME1: blocking key binding" ) ; event . preventDefault ( ) ; event . stopPropagation ( ) ; } , [ ] ) ; const stopPropagationHandler = React . useCallback ( ( event ) => { event . preventDefault ( ) ; event . stopPropagation ( ) ; } , [ ] ) ; useComponentDidMount ( ( ) => { document . addEventListener ( 'keydown' , onKeyDown , { capture : true } ) ; document . addEventListener ( 'keyup' , stopPropagationHandler , { capture : true } ) ; document . addEventListener ( 'keypress' , stopPropagationHandler , { capture : true } ) ; } ) ; useComponentWillUnmount ( ( ) => { document . removeEventListener ( 'keydown' , onKeyDown , { capture : true } ) ; document . removeEventListener ( 'keypress' , stopPropagationHandler , { capture : true } ) ; document . removeEventListener ( 'keyup' , stopPropagationHandler , { capture : true } ) ; window . blur ( ) ; window . focus ( ) ; } ) ; } interface IProps { readonly onComplete : Callback ; readonly onCancel ? : Callback ; readonly children : React . ReactElement ; } export const InputCompleteWindowListener = deepMemo ( function InputCompleteWindowListener ( props ) { useInputCompleteWindowListener ( { onComplete : props . onComplete , onCancel : props . onCancel } ) ; return props . children ; } ) ;