-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
202 lines (189 loc) · 6.09 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
define(function (require, exports, module) {
"use strict";
var spaceDown = false, keyDown = false, inserted = false,
moved = false, sameKey = false, shouldRepeat = false,
repeated = false, lastKey = '', keyCount = 0,
C = require('C');
C.Commands.setC(C);
C.Cursor.setC(C);
C.Select.setC(C);
C.ExtensionUtils.loadStyleSheet(module, './style.css');
var reg = C.CommandManager.register,
bind = C.KeyBindingManager.addBinding;
reg("Toggle Line Comment", "vii.lineComment", C.Commands.toggleComment);
reg("Duplicate Lines", "vii.duplicateLines", C.Commands.duplicateLines);
reg("Join Lines", "vii.joinLines", C.Commands.joinLines);
reg("Delete Lines and Go Up", "vii.deleteLines", C.Commands.deleteLines);
reg("Delete To Head", "vii.deleteToHead", C.Commands.deleteToHead);
reg("Delete To Tail", "vii.deleteToTail", function(){
CodeMirror.commands["killLine"](C.cm);});
["Cmd-J", "Shift-Cmd-D", "Ctrl-Cmd-Up","Ctrl-Cmd-Down",
"Ctrl-Tab", "Ctrl-L", "Ctrl-Shift-Tab","Cmd-D", "Cmd-T",
"Shift-Cmd-Z", 'Cmd-/', 'Cmd-L'].forEach(function(i){
C.KeyBindingManager.removeBinding(i);});
bind('edit.deletelines','Shift-Delete');
bind('navigate.jumptoDefinition','Cmd-T');
bind('navigate.gotoDefinition','Cmd-2');
bind('navigate.gotoLine','Cmd-3');
bind('edit.redo','Cmd-Y');
var menu = C.Menus.getMenu(C.Menus.AppMenuBar.EDIT_MENU);
menu.addMenuDivider();
menu.addMenuItem('vii.lineComment', 'Cmd-/')
menu.addMenuItem('vii.duplicateLines', 'Cmd-D');
menu.addMenuItem('vii.joinLines', 'Cmd-J');
menu.addMenuItem('vii.deleteLines', 'Shift-Backspace');
menu.addMenuItem('vii.deleteToHead', 'Cmd-Backspace');
menu.addMenuItem('vii.deleteToTail', 'Cmd-Delete');
['edit.lineUp', 'edit.lineDown', 'edit.selectLine',
'edit.duplicate'].forEach(function(i){
menu.removeMenuItem(i);});
function isKeycodeInRange(keyCode) {
if((keyCode >= 65 && keyCode <= 90) ||
(keyCode >= 48 && keyCode <= 57) ||
(keyCode >= 186 && keyCode <= 192) ||
(keyCode >= 219 && keyCode <= 222))
return true;
return false;
}
function command(key) {
function cmd(s) {
CodeMirror.commands[s](C.cm);
}
function exe(s) {
C.CommandManager.execute(s);
}
switch(key){
case C.LEFT:
case C.RIGHT: C.Cursor.move(key); break;
case C.UP: C.cm.moveV(-1, "line"); break;
case C.DOWN: C.cm.moveV(1, "line"); break;
case C.HOME: cmd("goLineStartSmart"); break;
case C.END: cmd("goLineEnd"); break;
case C.SCROLLUP: C.Commands.scroll(true); break;
case C.SCROLLDN: C.Commands.scroll(false); break;
case C.DOCHOME: cmd("goDocStart"); break;
case C.DOCEND: cmd("goDocEnd"); break;
case C.UP10: C.Cursor.move10(true); break;
case C.DOWN10: C.Cursor.move10(false); break;
case C.CENTER: C.Commands.centerCursor(); break;
case C.FOCUS: C.Cursor.focusAtCenter(); break;
case C.LINEUP: exe('edit.lineUp'); break;
case C.LINEDOWN: exe('edit.lineDown'); break;
case C.SMARTSELECT: C.Select.smartSelect(); break;
case C.SELECTLINE: C.Select.line(); break;
case C.TOGGLESELECT:C.Select.selectToggle(); break;
case C.SWAPANCHOR: C.Select.swapAnchor(); break;
case C.NEXTDOC: exe('navigate.nextDoc'); break;
case C.PREVDOC: exe('navigate.prevDoc'); break;
case C.FIND: exe('edit.find'); break;
case C.FINDNEXT: exe('edit.findNext'); break;
case C.FINDPREV: exe('edit.findPrevious'); break;
case C.REPLACE: exe('edit.replace'); break;
case C.MULTICURSOR: console.log(Test.nn++); break;
}
}
C.KeyBindingManager.addGlobalKeydownHook(function (e) {
if (spaceDown) {
switch(e.keyCode) {
case C.BACKSPACE: C.Commands.deleteWord(C.LEFT); break;
case C.DEL: C.Commands.deleteWord(C.RIGHT); break;
case C.ENTER: C.Commands.insertLineBefore(); break;//Todo: c.enter
default: return false;
}
moved = true;
return true;
}
return false;
});
document.onkeydown = function (e) {
if(!C.editorReady()) return true;
e = e || window.event;
if (e.keyCode === C.SPACE) { // space down
spaceDown = true;
C.originalLine = C.doc.getLine(C.doc.getCursor().line);
return false;
}
else if (isKeycodeInRange(e.keyCode)) { // key down
var k;
switch(e.keyCode){
case 187: k = "="; break;
case 189: k = "-"; break;
case 219: k = "["; break;
case 221: k = "]"; break;
case 220: k = "\\"; break;
case 186: k = ";"; break;
case 222: k = "\'"; break;
case 188: k = ","; break;
case 190: k = "."; break;
case 191: k = "/"; break;
default: k = String.fromCharCode(e.keyCode).toLowerCase();
}
if (k != lastKey) {
keyCount += 1;
lastKey = k;
} else shouldRepeat = true;
if (spaceDown) {
keyDown = true;
inserted = false;
if (moved) shouldRepeat = true;
if (shouldRepeat) {
repeated = true;
command(e.keyCode);
}
return false;
} else {
inserted = true;
return true;
}
}
return true;
};
document.onkeyup = function (e) {
if (!C.editorReady()) return true;
e = e || window.event;
C.currentCursor = C.editor.getCursorPos();
if (e.keyCode != C.SPACE && isKeycodeInRange(e.keyCode))
keyCount -= 1;
if (e.keyCode === C.SPACE) { // space up
if (spaceDown) {
spaceDown = false;
shouldRepeat = false;
repeated = false;
C.selectExtending = false;
C.doc.setExtending(false);
if (moved) moved = false;
else if (keyDown) {
if (C.doc.somethingSelected())
C.doc.replaceSelection(" " + lastKey);
else C.Commands.insert(" " + lastKey);
inserted = true;
} else {
if (C.doc.somethingSelected())
C.doc.replaceSelection(" ");
else C.Commands.insert(" ");
inserted = false;
}
return false;
} else {
console.error("vii: space is already up");
return true;
}
return true;
}
else if (isKeycodeInRange(e.keyCode)) { // key up
if (spaceDown && keyDown && !inserted) {
if (!repeated) command(e.keyCode);
moved = true;
}
if (keyCount < 1) {
keyDown = false;
lastKey = '';
keyCount = 0; // prevent problem with hot key
}
return false;
}
return true;
}
C.setQwerty();
console.log("vii is running");
});