1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253open!CoreopenImportopenDom_htmltypet=keyboardEventJs.tmoduleKeyboard_code=Keyboard_codeletkeye=Keyboard_code.of_evente(* This function looks silly (and it is): why not just use [Js.to_bool]? [robust_to_bool]
handles [bool Js.t] that _might_ actually be [bool Js.t Js.Optdef.t].
This is an important distinction to make if someone lied when submitting a keyboard
event and it doesn't have the properties that we expect. *)letrobust_to_boolb=phys_equalbJs._trueletmodifiere~states~fallback=(* we don't trust that [fallback] is actually a [bool Js.t] because it was pulled out of
an event that could be missing the fields that the DOM promises exists, so we use
[robust_to_bool] instead.
To my knowledge this can only happen if a user manually submits an event that is
missing these fields, but it's better to be safe. *)matchrobust_to_boolfallbackwith|true->true|false->(* Some [KeyboardEvent]s don't have a getModifierState function for some reason. This
issue was first noticed in a typeahead component, but it may be an issue for other
input elements too. If we can't find this property, we bail. *)lethas_get_modifier_state_function=Js.Unsafe.gete(Js.string"getModifierState")|>Js.Optdef.testin(matchhas_get_modifier_state_functionwith|false->false|true->List.existsstates~f:(funstate_name->robust_to_bool(e##getModifierState(Js.stringstate_name))));;letctrle=modifiere~states:["Control";"AltGraph"]~fallback:e##.ctrlKeyletalte=modifiere~states:["Alt";"AltGraph"]~fallback:e##.altKeyletshifte=modifiere~states:["Shift"]~fallback:e##.shiftKeyletmetae=modifiere~states:["Meta"]~fallback:e##.metaKeyletmatch_modifiers?ctrl:ctrl'?alt:alt'?shift:shift'?meta:meta'e=List.for_all[ctrl',ctrle;alt',alte;shift',shifte;meta',metae]~f:(fun(cond,env)->Option.value_mapcond~default:true~f:(Bool.equalenv));;letno_modifierse=match_modifiers~ctrl:false~alt:false~shift:false~meta:falseeletmape~f=f(`Ctrl(ctrle),`Alt(alte),`Shift(shifte),`Meta(metae),keye)