import { Type } from '@ephox/katamari'; import { type SugarElement, Value } from '@ephox/sugar'; import * as TypeInInput from '../keyboard/TypeInInput'; import { Chain } from './Chain'; import { Step } from './Step'; import * as UiFinder from './UiFinder'; type TogglableElement = HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | HTMLOptionElement | HTMLButtonElement; const fireEvent = (elem: SugarElement, event: string) => { const evt = new Event(event, { bubbles: true, cancelable: true }); elem.dom.dispatchEvent(evt); }; const setValue = (element: SugarElement, newValue: string, eventName?: string): void => { Value.set(element, newValue); if (Type.isNonNullable(eventName)) { fireEvent(element, eventName); } }; const setValueOn = (container: SugarElement, selector: string, newValue: string, eventName?: string): void => { const element = UiFinder.findIn(container, selector).getOrDie(); setValue(element, newValue, eventName); }; const getValue = (element: SugarElement): string => Value.get(element); const cSetValue = (newValue: string): Chain, SugarElement> => Chain.op((element) => { setValue(element, newValue); }); const cGetValue: Chain, string> = Chain.mapper(getValue); const sSetValue = (element: SugarElement, newValue: string): Step => Step.sync(() => setValue(element, newValue)); const sSetValueOn = (container: SugarElement, selector: string, newValue: string): Step => Step.sync(() => setValueOn(container, selector, newValue)); const pTypeOn = async (container: SugarElement, selector: string, text: string, speed: number = 0): Promise => { const input = UiFinder.findIn(container, selector).getOrDie(); await TypeInInput.pTypeTextInInput(input, text, speed); }; export { setValue, setValueOn, getValue, sSetValueOn, sSetValue, cSetValue, cGetValue, pTypeOn };