summaryrefslogtreecommitdiffstats
path: root/vimfx/shared.js
blob: 8dafdfa49ecd27430c603726a98465bd447ced82 (plain) (blame)
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
63
64
65
66
67
68
const EXPORTED_SYMBOLS = [
    'isEditableInput',
    'insertAtCursor',
    'killBackwardFromCursor',
    'cleanTagName',
    'lineEditingCallbacks',
    'lineEditingDataCallbacks',
];

var lineEditingCallbacks = {
    paste: (input, data) => {
        insertAtCursor(input, data);
    },
    kill_backward: (input, data) => {
        killBackwardFromCursor(input);
    },
    start_of_line: (input, data) => {
        cursorToStartOfLine(input);
    },
    end_of_line: (input, data) => {
        cursorToEndOfLine(input);
    },
};
var lineEditingDataCallbacks = {
    paste: (vim) => {
        return vim.window.readFromClipboard();
    },
};

function cleanTagName(e) {
    return e.tagName.split(':').pop().toLowerCase();
}

function isEditableInput(e) {
    let tag = cleanTagName(e);
    // XXX
    return tag == "input" || tag == "textarea";
}

function insertAtCursor(e, text) {
    text = text || "";
    var before = e.value.substring(0, e.selectionStart);
    var after = e.value.substring(e.selectionEnd, e.value.length);
    e.value = before + text + after;
    e.selectionStart = e.selectionEnd = before.length + text.length;
}

function killBackwardFromCursor(e) {
    var before = e.value.substring(0, e.selectionStart);
    var start = before.lastIndexOf('\n') + 1;
    before = before.substring(0, start);
    var after = e.value.substring(e.selectionEnd, e.value.length);
    e.value = before + after;
    e.selectionStart = e.selectionEnd = start;
}

function cursorToStartOfLine(e) {
    var start = e.value.lastIndexOf('\n', e.selectionStart) + 1;
    e.selectionStart = e.selectionEnd = start;
}

function cursorToEndOfLine(e) {
    var end = e.value.indexOf('\n', e.selectionEnd) + 1;
    if (end === 0) {
        end = e.value.length;
    }
    e.selectionStart = e.selectionEnd = end;
}