~glacambre/firenvim

ref: 501a2485049df810baba19ead14831c3929416fd firenvim/src/page/functions.ts -rw-r--r-- 1.8 KiB
501a2485glacambre Trigger a bunch of events on setElementContent 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
export function getFunctions(global: {
    lastEditorLocation: [string, string],
    nvimify: (evt: FocusEvent) => void,
    selectorToElems: Map<string, [HTMLSpanElement, HTMLElement]>,
}) {
    return {
        getEditorLocation: () => global.lastEditorLocation,
        getElementContent: (selector: string) => {
            const [_, e] = global.selectorToElems.get(selector) as [any, any];
            if (e.value !== undefined) {
                return e.value;
            }
            if (e.textContent !== undefined) {
                return e.textContent;
            }
            return e.innerText;
        },
        killEditor: (selector: string) => {
            const tuple = global.selectorToElems.get(selector) as [any, any];
            if (tuple) {
                const [span, input] = tuple;
                span.parentNode.removeChild(span);
                global.selectorToElems.delete(selector);
                input.removeEventListener("focus", global.nvimify);
                input.focus();
                input.addEventListener("focus", global.nvimify);
            }
        },
        setElementContent: (selector: string, text: string) => {
            const [_, e] = global.selectorToElems.get(selector) as [any, any];
            if (e.value !== undefined) {
                e.value = text;
            } else {
                e.textContent = text;
            }
            e.dispatchEvent(new Event("keydown",     { bubbles: true }));
            e.dispatchEvent(new Event("keyup",       { bubbles: true }));
            e.dispatchEvent(new Event("keypress",    { bubbles: true }));
            e.dispatchEvent(new Event("beforeinput", { bubbles: true }));
            e.dispatchEvent(new Event("input",       { bubbles: true }));
            e.dispatchEvent(new Event("change",      { bubbles: true }));
        },
    };
}