import { Chain, FocusTools, Keyboard, NamedChain } from '@ephox/agar'; import { Fun } from '@ephox/katamari'; import { SugarElement } from '@ephox/sugar'; import { Editor } from '../../alien/EditorTypes'; export interface ActionChains { cContentKeypress: (code: number, modifiers?: Record) => Chain; cContentKeydown: (code: number, modifiers?: Record) => Chain; cContentKeystroke: (code: number, modifiers?: Record) => Chain; cUiKeydown: (code: number, modifiers?: Record) => Chain; } const cIDoc = Chain.mapper((editor: Editor) => { return SugarElement.fromDom(editor.getDoc()); }); const cUiDoc = Chain.injectThunked(() => { return SugarElement.fromDom(document); }); const cTriggerKeyEvent = (cTarget: Chain>, evtType: 'keydown' | 'keyup' | 'keypress' | 'keystroke', code: number, modifiers = {}) => { return NamedChain.asChain([ NamedChain.direct(NamedChain.inputName(), cTarget, 'doc'), NamedChain.direct('doc', FocusTools.cGetFocused, 'activeElement'), NamedChain.direct('activeElement', Chain.op((dispatcher) => { Keyboard[evtType](code, modifiers, dispatcher); }), '_'), NamedChain.output(NamedChain.inputName()) ]); }; export const ActionChains: ActionChains = { cContentKeypress: Fun.curry(cTriggerKeyEvent, cIDoc, 'keypress'), cContentKeydown: Fun.curry(cTriggerKeyEvent, cIDoc, 'keydown'), cContentKeystroke: Fun.curry(cTriggerKeyEvent, cIDoc, 'keystroke'), cUiKeydown: Fun.curry(cTriggerKeyEvent, cUiDoc, 'keydown') };