summaryrefslogtreecommitdiff
path: root/static/edit.js
blob: 8e754d8dec7a5f439a2fecca4720a8f499841989 (plain)
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
function tab_add(t, ti, ss, se) {
	t.value = t.value.substr(0, ti) + "\t" + t.value.substr(ti);
	t.selectionStart = ss + 1;
	t.selectionEnd = se + 1;
}

function tab_remove(t, ti, ss, se) {
	t.value = t.value.substr(0, ti-1) + t.value.substr(ti);
	t.selectionStart = ss - 1;
	t.selectionEnd = se - 1;
}

function textarea_handle_tab(t, e) {
	if(navigator.userAgent.match("Gecko")) {
		keyCode = e.which;
	} else {
		keyCode = e.keyCode;
	}
	if(keyCode != 9)
		return;
	e.preventDefault();
	var ss = t.selectionStart;
	var se = t.selectionEnd;
	if(ss == se) {
		if(e.shiftKey) {
			if(t.value[ss-1] == "\t") {
				tab_remove(t, ss, ss, se);
			}
		} else {
			tab_add(t, ss, ss, se);
		}
	} else {
		var s;
		if(ss < se)
			s = t.value.substr(ss, se - ss);
		else
			s = t.value.substr(se, ss - se);
		if(s.indexOf("\n") > -1) {
			var nl = s.lastIndexOf("\n");
			if(e.shiftKey) {
				while(nl > -1) {
					if(s[nl+1] == "\t") {
						tab_remove(t, ss + nl + 2, ss, se);
						se--;
					}
					nl = s.lastIndexOf("\n", nl - 2);
				}
				if(t.value[(ss < se ? ss : se)-1] == "\n" && e.shiftKey)
					tab_remove(t, ss+1, ss, se);
				if((ss == 0 || se == 0) && t.value[0] == "\t")
					tab_remove(t, 1, ss, se);
			} else {
				while(nl > -1) {
					tab_add(t, ss + nl + 1, ss, se);
					se++;
					if(nl == 0)
						break;
					nl = s.lastIndexOf("\n", nl - 2);
				}
				if(ss == 0 || se == 0 || t.value[(ss < se ? ss : se)-1] == "\n")
					tab_add(t, ss, ss-1, se);
			}

		} else {
			var nl = t.value.lastIndexOf("\n", (ss < se ? se : ss) - 1) + 1;
			if(e.shiftKey) {
				if(t.value[nl] == "\t") {
					tab_remove(t, nl + 1, ss, se);
				}
			} else {
				tab_add(t, nl, ss, se);
			}
		}
	}
	setTimeout("document.getElementById('" + t.id + "').focus();", 0);
}

window.onload = function() {
	text = document.getElementById("text");
	text.onkeydown = function(event) {
		return textarea_handle_tab(text, event);
	}
}