/** * 在原生 JavaScript 中,input 事件监听器是处理用户输入的常见方式。然而,由于中文输入法(如拼音输入法)会涉及候选词的选择,因此在中文输入时,input 事件可能在用户选择候选词之前触发,导致输入处理不完整。为了解决这个问题,可以利用合成事件(通过 compositionstart、compositionupdate 和 compositionend 事件)与 input 事件的结合来确保完整的输入内容被正确处理。 * * 利用合成事件追踪输入法的组合状态: * 1. compositionstart: 当用户开始使用输入法输入时触发 * 2. compositionupdate: 当输入法的组合文字更新时触发 * 3. compositionend: 当用户完成输入法输入(选择候选词)时触发 * 4. 使用 composing 标志来追踪当前是否正在进行输入法输入 */ /** * handle input event with composition events * @param inputEl input element * @param callback callback */ declare function addInputListener(inputEl: string | HTMLInputElement, callback: (value: string, event: Event) => void): () => void; type Direction = 'top' | 'right' | 'bottom' | 'left'; type Callback = (this: HTMLElement, direction: Direction, e: MouseEvent) => void; /** * 监听鼠标在元素上移入和移出的方向 * @param {HTMLElement} element 监听元素 * @param {Callback} onEnter 鼠标进入回调 * @param {Callback} onLeave 鼠标离开回调 * * 这个函数提供了以下功能: * 可以检测鼠标从元素的四个边缘(上、右、下、左)进入和离开的情况 * 支持自定义回调函数处理进入和离开事件 * 返回清理函数以便移除事件监听器 */ declare function detectMouseDirection(element: HTMLElement, onEnter: Callback, onLeave: Callback): () => void; interface TagElementMap { img: HTMLImageElement; script: HTMLScriptElement; link: HTMLLinkElement; } interface IOptions { /** * 标签类型 * @type 'img' | 'script' | 'link' */ tag: T; /** * 标签属性 */ attributes?: Record; /** * 是否异步加载 * @default false * @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-async */ async?: boolean; /** * 是否延迟加载 * @default false * @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#attr-defer */ defer?: boolean; /** * 加载超时时间 * @default 0 */ timeout?: number; /** * 加载成功回调 */ onload?: (el: TagElementMap[T], clean: () => void, event?: Event) => void; /** * 加载失败回调 */ onerror?: (el: TagElementMap[T], err?: Error) => void; /** * 是否检查资源是否存在 * @default true */ checkExist?: boolean; /** * 插入到指定元素中 */ appendTo?: HTMLElement; } /** * 加载资源文件 * 1. 支持 img, link, script 文件标签 * 2. 检测已存在情况 * @param {string} url 资源Url * @param {IOptions} options 配置项 * @returns */ declare function loadResource(url: string, options?: T | IOptions): { element: TagElementMap[T]; clean: () => void; } | undefined; export { addInputListener, detectMouseDirection, loadResource };