-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (37 loc) · 880 Bytes
/
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
module.exports = indent;
function indent(tx){
tx.addEventListener("keydown",function(event){
if(event.keyCode === 13){
event.preventDefault();
insert("\r\n");
}
if(event.keyCode === 9){
event.preventDefault();
insert(" ");
}
});
function insert(v){
var txt = tx.value;
var point = tx.selectionEnd;
var s = "",e = "",c = "",hh=false;
var sn = txt.lastIndexOf('\n',point-1);
if(sn === -1){
hh = true;
}
var x1 = txt.substring(sn,txt.length);
var rxx = /^\s*/gi
c = x1.match(rxx)[0];
s = txt.substring(0,point);
e = txt.substring(point,txt.length);
if(hh){
txt = s + c + v + e;
}else
txt = s +c+ e;
tx.value = txt;
if(hh){
tx.setSelectionRange(point+c.length+1,point+c.length+1)
}else{
tx.setSelectionRange(point+c.length,point+c.length)
}
}
}