import { Observable , filter , fromEvent , map , share } from "rxjs" ; import { getActiveElement } from "../element" ; import { getToggle } from "../toggle" ; /**
 * Keyboard mode
 */ export type KeyboardMode = "global" | "search" ; /**
 * Keyboard
 */ export interface Keyboard { mode : KeyboardMode ; type : string ; claim ( ) : void ; } param el param type returns function isSusceptibleToKeyboard ( el , type ) { switch ( el . constructor ) { case HTMLInputElement : if ( el . type === "radio" ) return /^Arrow/ . test ( type ) ; else return true ; case HTMLSelectElement : case HTMLTextAreaElement : return true ; default : return el . isContentEditable ; } } returns export function watchKeyboard ( ) { return fromEvent < KeyboardEvent > ( window , "keydown" ) . pipe ( filter ( ev => ! ( ev . metaKey || ev . ctrlKey ) ) , map ( ev => ( { mode : getToggle ( "search" ) ? "search" : "global" , type : ev . key , claim ( ) { ev . preventDefault ( ) ; ev . stopPropagation ( ) ; } } as Keyboard ) ) , filter ( ( { mode , type } ) => { if ( mode === "global" ) { const active = getActiveElement ( ) ; if ( typeof active !== "undefined" ) return ! isSusceptibleToKeyboard ( active , type ) ; } return true ; } ) , share ( ) ) ; }