-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtextareatab.js
105 lines (103 loc) · 3.13 KB
/
textareatab.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
// Code taken from https://css-tricks.com/snippets/javascript/support-tabs-in-textareas
HTMLTextAreaElement.prototype.getCaretPosition = function () {
//return the caret position of the textarea
return this.selectionStart;
};
HTMLTextAreaElement.prototype.setCaretPosition = function (position) {
//change the caret position of the textarea
this.selectionStart = position;
this.selectionEnd = position;
this.focus();
};
HTMLTextAreaElement.prototype.hasSelection = function () {
//if the textarea has selection then return true
if (this.selectionStart == this.selectionEnd) {
return false;
} else {
return true;
}
};
HTMLTextAreaElement.prototype.getSelectedText = function () {
//return the selection text
return this.value.substring(this.selectionStart, this.selectionEnd);
};
HTMLTextAreaElement.prototype.setSelection = function (start, end) {
//change the selection area of the textarea
this.selectionStart = start;
this.selectionEnd = end;
this.focus();
};
function enableTextareaTabbing(textarea) {
textarea.dataset.captureTab = "false";
textarea.onkeydown = function (event) {
if (event.ctrlKey && event.keyCode === 77) {
textarea.dataset.captureTab =
textarea.dataset.captureTab === "false" ? "true" : "false";
}
if (textarea.dataset.captureTab === "false") {
return;
}
if (event.keyCode == 9) {
//tab was pressed
var newCaretPosition;
newCaretPosition = textarea.getCaretPosition() + " ".length;
textarea.value =
textarea.value.substring(0, textarea.getCaretPosition()) +
" " +
textarea.value.substring(
textarea.getCaretPosition(),
textarea.value.length
);
textarea.setCaretPosition(newCaretPosition);
return false;
}
if (event.keyCode == 8) {
//backspace
if (
textarea.value.substring(
textarea.getCaretPosition() - 4,
textarea.getCaretPosition()
) == " "
) {
//it's a tab space
var newCaretPosition;
newCaretPosition = textarea.getCaretPosition() - 3;
textarea.value =
textarea.value.substring(0, textarea.getCaretPosition() - 3) +
textarea.value.substring(
textarea.getCaretPosition(),
textarea.value.length
);
textarea.setCaretPosition(newCaretPosition);
}
}
if (event.keyCode == 37) {
//left arrow
var newCaretPosition;
if (
textarea.value.substring(
textarea.getCaretPosition() - 4,
textarea.getCaretPosition()
) == " "
) {
//it's a tab space
newCaretPosition = textarea.getCaretPosition() - 3;
textarea.setCaretPosition(newCaretPosition);
}
}
if (event.keyCode == 39) {
//right arrow
var newCaretPosition;
if (
textarea.value.substring(
textarea.getCaretPosition() + 4,
textarea.getCaretPosition()
) == " "
) {
//it's a tab space
newCaretPosition = textarea.getCaretPosition() + 3;
textarea.setCaretPosition(newCaretPosition);
}
}
};
}