-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (126 loc) · 3.57 KB
/
index.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const vim = {
mode: "insert", // Keep track of current mode
num: 1, // Keep track of number keys pressed by the user
currentSequence: [], // Keep track of key sequences
clipboard: "",
keys: {
move: "hjkl", // QWERTY: hjkl
escapeSequence: "jk", // QWERTY: jk or jl
},
addKeyMappings: function (baseMap) {
baseMap[vim.keys.move[0]] = "ArrowLeft";
baseMap[vim.keys.move[1]] = "ArrowDown";
baseMap[vim.keys.move[2]] = "ArrowUp";
baseMap[vim.keys.move[3]] = "ArrowRight";
},
normal: {},
visual: {},
insert: {},
universal: {},
switchToNormalMode: () => {
vim.currentSequence = [];
vim.mode = "normal";
vim.num = 1;
docs.setCursorWidth("7px");
},
switchToVisualMode: () => {
vim.currentSequence = [];
vim.mode = "visual";
vim.num = 1;
docs.setCursorWidth("7px");
},
};
// vim.switchToNormalMode = switchToNormalMode;
// vim.switchToInsertMode = switchToInsertMode;
// vim.switchToVisualMode = switchToVisualMode;
// vim.insert_keydown = insert_keydown;
// vim.normal_keydown = normal_keydown;
// vim.visual_keydown = visual_keydown;
const formatMsg = (vim) => `vim
mode:${vim.mode}
current sequence: ${vim.currentSequence}
number: ${vim.num}`;
function resetVim() {
vim.currentSequence = [];
vim.num = 1;
vim.display.innerText = formatMsg(vim);
if (vim.mode === "normal") {
docs.deselect();
}
}
async function execute(sequence, e) {
// console.log("execute called");
sequence = sequence.reverse();
let valFunc = vim[vim.mode][sequence[0]] || vim.universal[sequence[0]];
for (let i = 0; i < vim.num; i++) {
let val = valFunc ? await valFunc() : valFunc;
sequence = sequence.slice(1);
for (const char of sequence) {
let charFunc = vim[vim.mode][char] || vim.universal[char];
// console.log("char is", char, "charFunc is ", charFunc);
await charFunc(val);
}
}
e.preventDefault();
e.stopPropagation();
resetVim();
}
vim.display = docs.createDisplay(formatMsg(vim));
const ignoreChars = /(Shift|F\d+|Control)/g;
const keydown_handler = async (e) => {
if (ignoreChars.test(e.key)) {
return true;
}
vim.display.innerText = formatMsg(vim);
try {
// console.log(vim.currentSequence[vim.currentSequence.length - 1], e.key);
if (e.key === vim.currentSequence[vim.currentSequence.length - 1]) {
//@ts-ignore
e.key = "selectLine";
}
if (e.key === "i" && vim.currentSequence.length > 0) {
//@ts-ignore
e.key = "selectInside";
}
const func = vim[vim.mode][e.key] || vim.universal[e.key];
if (vim.mode !== "normal" && vim.mode !== "visual" && e.key !== "Escape") {
vim.display.innerText = formatMsg(vim);
return true;
} else {
e.preventDefault();
e.stopPropagation();
if (/\d/.test(e.key)) {
if (!(e.key == "0" && vim.num == 1)) {
vim.num = Number(vim.num.toString() + e.key);
return false;
}
}
vim.currentSequence.push(e.key);
if (!func) {
console.log("c");
await execute(vim.currentSequence, e);
return true;
}
if (func.length === 1) {
return false;
}
if (func.length === 0) {
console.log("b");
await execute(vim.currentSequence, e);
return true;
}
// console.log("a");
// execute(vim.currentSequence, e);
// return true;
}
} catch (e) {
console.log("error", e);
return false;
// vim.display("invalid squenece");
}
return true;
};
docs.keydown = async function (e) {
await keydown_handler(e);
vim.display.innerText = formatMsg(vim);
};