~glacambre/firenvim

ref: d6f6fe7b3ad47676e2a27c42d28de73078535e77 firenvim/src/page/functions.ts -rw-r--r-- 2.5 KiB
d6f6fe7bglacambre Correctly set browserAction icon 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as browser from "webextension-polyfill";

function _getElementContent(e: any) {
    if (e.value !== undefined) {
        return e.value;
    }
    if (e.textContent !== undefined) {
        return e.textContent;
    }
    return e.innerText;
}

export function getFunctions(global: {
    lastEditorLocation: [string, string, number],
    nvimify: (evt: FocusEvent) => void,
    selectorToElems: Map<string, PageElements>,
    disabled: boolean | Promise<boolean>,
}) {
    return {
        getEditorLocation: () => global.lastEditorLocation,
        getElementContent: (selector: string) => _getElementContent(global.selectorToElems.get(selector).input),
        killEditor: (selector: string) => {
            const { span, input } = global.selectorToElems.get(selector);
            span.parentNode.removeChild(span);
            global.selectorToElems.delete(selector);
            input.removeEventListener("focus", global.nvimify);
            input.focus();
            input.addEventListener("focus", global.nvimify);
        },
        resizeEditor: (selector: string, width: number, height: number) => {
            const { iframe } = global.selectorToElems.get(selector);
            iframe.style.width = `${width}px`;
            iframe.style.height = `${height}px`;
        },
        setDisabled: (disabled: boolean) => {
            global.disabled = disabled;
        },
        setElementContent: (selector: string, text: string) => {
            const { input: e } = global.selectorToElems.get(selector) as 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 }));
        },
        setElementCursor: (selector: string, line: number, column: number) => {
            const { input } = global.selectorToElems.get(selector) as any;
            const pos = _getElementContent(input)
                .split("\n")
                .reduce((acc: number, l: string, index: number) => acc + (index < (line - 1)
                    ? (l.length + 1)
                    : 0), column + 1);
            input.setSelectionRange(pos, pos);
        },
    };
}