import { KeyboardEventData } from "coral-stream/common/KeyboardShortcuts/KeyboardShortcuts" ; import onIntersectChange from "../onIntersectChange" ; import { Decorator } from "./types" ; const withKeypressEvent = ( pym ) => { const handleKeypress = ( e ) => { if ( e . target ) { const el = e . target as HTMLElement ; if ( el . tagName === "INPUT" || el . tagName === "TEXTAREA" ) { return ; } if ( el . isContentEditable ) { return ; } } const payload = { key : e . key , shiftKey : e . shiftKey , altKey : e . altKey , ctrlKey : e . ctrlKey , metaKey : e . metaKey , } ; pym . sendMessage ( "keypress" , JSON . stringify ( payload ) ) ; } ; const disconnectObserver = onIntersectChange ( pym . el , ( isIntersecting ) => { if ( isIntersecting ) { document . addEventListener ( "keypress" , handleKeypress ) ; } else { document . removeEventListener ( "keypress" , handleKeypress ) ; } } , { rootMargin : "-100px -20px" } ) ; return ( ) => { disconnectObserver ( ) ; document . removeEventListener ( "keypress" , handleKeypress ) ; } ; } ; export default withKeypressEvent ;