-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline_numbering.js
74 lines (56 loc) · 1.74 KB
/
line_numbering.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
textarea = document.getElementById('editor');
line_numbering = document.getElementById('line_numbering_area');
function scrollToBottom() {
var textarea = document.getElementById('line_numbering_area');
textarea.scrollTop = textarea.scrollHeight;
}
scrollToBottom();
function addText(num) {
document.getElementById("line_numbering_area").value += i;
}
function line_break() {
var txtArea ;
txtArea = document.getElementById("line_numbering_area") ;
txtArea.value += '\r\n';
}
for (var i = 1; i <=5000; i++) {
addText(i);
line_break();
}
textarea.addEventListener('keydown', function(e) {
if (e.key == 'Tab') {
e.preventDefault();
var start = this.selectionStart;
var end = this.selectionEnd;
// set textarea value to: text before caret + tab + text after caret
this.value = this.value.substring(0, start) +
"\t" + this.value.substring(end);
// put caret at right position again
this.selectionStart =
this.selectionEnd = start + 1;
}
})
// function getLineNumberAndColumnIndex(textarea){
// var textLines = textarea.value.substr(0, textarea.selectionStart).split("\n");
// var currentLineNumber = textLines.length;
// var currentColumnIndex = textLines[textLines.length-1].length;
// return currentLineNumber;
// }
function scroll_changed(textarea)
{
scrollsync(textarea, line_numbering);
}
function scrollsync(obj1, obj2)
{
// scroll text in object id1 the same as object id2
obj2.scrollTop = obj1.scrollTop;
}
textarea.addEventListener('input', () => {
scroll_changed(textarea);
})
textarea.addEventListener('click', () => {
scroll_changed(textarea);
})
textarea.addEventListener('scroll', () => {
scroll_changed(textarea);
})