-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhighlighter.js
194 lines (193 loc) · 9.02 KB
/
highlighter.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
function colorWrapper(color, content) {
return '<span style="color: rgb(' + color + ')">' + content + '</span>';
}
const OPEN_BRACKET_FORMATTED = colorWrapper("255, 94, 20", '(');
const CLOSE_BRACKET_FORMATTED = colorWrapper("255, 94, 20", ')');
const LESS_SIGN_FORMATTED = colorWrapper("193, 210, 14", "<");
const GREATER_SIGN_FORMATTED = colorWrapper("193, 210, 14", ">");
const EQUALS_SIGN_FORMATTED = colorWrapper("0, 0, 255", '=');
document.querySelector("#program_field").addEventListener("input", (event) =>{
if (event.data == null){
return;
}
let isText = false;
let text_node = document.querySelector("#program_field");
let select_node_num = 0;
let absolute_offset = document.getSelection().anchorOffset;
let text_str = [], counter = 0;
for (let elem of text_node.childNodes){
if (elem instanceof Text){
text_str.push(elem.nodeValue);
isText = true;
if (!select_node_num && Array.from(elem.childNodes).includes(document.getSelection().anchorNode.parentNode)
|| document.getSelection().anchorNode.parentNode == elem){
select_node_num = counter;
} else {
counter++;
}
} else if (elem instanceof HTMLElement){
if (!elem.textContent.length){
continue;
}
text_str.push(elem.textContent);
if (!select_node_num && Array.from(elem.childNodes).includes(document.getSelection().anchorNode.parentNode)
|| document.getSelection().anchorNode.parentNode == elem){
select_node_num = counter;
} else {
counter++;
}
}
}
for (let elem of text_node.childNodes[select_node_num].childNodes){
let a_node = document.getSelection().anchorNode;
if (elem != a_node && !Array.from(elem.childNodes).includes(a_node)){
absolute_offset += (elem instanceof Text) ? elem.nodeValue.length : elem.textContent.length;
} else {
break;
}
}
for (let i = 0; i < text_str.length; i++){
for (let j = 0; j < text_str[i].length; j++){
if (text_str[i][j] == "<") {
text_str[i] = text_str[i].slice(0, j) + '<' + text_str[i].slice(j + 1);
j += '<'.length - 1;
} else if (text_str[i][j] == ">") {
text_str[i] = text_str[i].slice(0, j) + '>' + text_str[i].slice(j + 1);
j += '>'.length - 1;
} else if (text_str[i][j] == "#" && (j == 0 || text_str[i][j - 1] != "\\")) {
text_str[i] = text_str[i].slice(0, j) + colorWrapper("128, 128, 128", '#' + text_str[i].slice(j + 1));
break;
} else if ((text_str[i][j] == "e" || text_str[i][j] == "s" || text_str[i][j] == "t") && j != text_str[i].length - 1
&& (j == 0 || text_str[i][j - 1] != "\\")) {
while (j != text_str[i].length - 1 && (text_str[i][j + 1] == " " || text_str[i][j + 1] == String.fromCharCode(160))) {
text_str[i] = text_str[i].slice(0, j + 1) + text_str[i].slice(j + 2);
}
if (j != text_str[i].length - 1 && !["=", "<", ">", "(", ")", "#", "\\"].includes(text_str[i][j + 1]) ) {
let addedString = colorWrapper("255, 0, 255", text_str[i][j]) + "<sub>" + text_str[i][j + 1] + "</sub>";
text_str[i] = text_str[i].slice(0, j) + addedString + text_str[i].slice(j + 2);
j += addedString.length - 1;
}
} else if (text_str[i][j] == "(") {
text_str[i] = text_str[i].slice(0, j) + OPEN_BRACKET_FORMATTED + text_str[i].slice(j + 1);
j += OPEN_BRACKET_FORMATTED.length - 1;
} else if (text_str[i][j] == ")"){
text_str[i] = text_str[i].slice(0, j) + CLOSE_BRACKET_FORMATTED + text_str[i].slice(j + 1);
j += CLOSE_BRACKET_FORMATTED.length - 1;
} else if (text_str[i][j] == "="){
text_str[i] = text_str[i].slice(0, j) + EQUALS_SIGN_FORMATTED + text_str[i].slice(j + 1);
j += EQUALS_SIGN_FORMATTED.length - 1;
}
}
text_str[i] = text_str[i].replaceAll("<", LESS_SIGN_FORMATTED);
text_str[i] = text_str[i].replaceAll(">", GREATER_SIGN_FORMATTED);
}
let combined_str = "";
for (let str of text_str){
combined_str += "<div>" + str + "</div>";
}
text_node.innerHTML = combined_str;
let new_node = null;
for (let node of text_node.childNodes[select_node_num].childNodes){
if (node instanceof Text){
if (absolute_offset <= node.nodeValue.length){
new_node = node;
break;
}
absolute_offset -= node.nodeValue.length;
} else if (node instanceof HTMLElement){
if (absolute_offset <= node.textContent.length){
new_node = node.firstChild;
break;
}
absolute_offset -= node.textContent.length;
}
}
document.getSelection().setBaseAndExtent(new_node, (!isText) ? absolute_offset : 1
, new_node, (!isText) ? absolute_offset : 1);
});
document.querySelector("#view_field").addEventListener("input", (event) =>{
if (event.data == null){
return;
}
let isText = false;
let text_node = document.querySelector("#view_field");
let select_node_num = 0;
let absolute_offset = document.getSelection().anchorOffset;
let text_str = [], counter = 0;
for (let elem of text_node.childNodes){
if (elem instanceof Text){
text_str.push(elem.nodeValue);
isText = true;
if (!select_node_num && Array.from(elem.childNodes).includes(document.getSelection().anchorNode.parentNode)
|| document.getSelection().anchorNode.parentNode == elem){
select_node_num = counter;
} else {
counter++;
}
} else if (elem instanceof HTMLElement){
if (!elem.textContent.length){
continue;
}
text_str.push(elem.textContent);
if (!select_node_num && Array.from(elem.childNodes).includes(document.getSelection().anchorNode.parentNode)
|| document.getSelection().anchorNode.parentNode == elem){
select_node_num = counter;
} else {
counter++;
}
}
}
for (let elem of text_node.childNodes[select_node_num].childNodes){
let a_node = document.getSelection().anchorNode;
if (elem != a_node && !Array.from(elem.childNodes).includes(a_node)){
absolute_offset += (elem instanceof Text) ? elem.nodeValue.length : elem.textContent.length;
} else {
break;
}
}
for (let i = 0; i < text_str.length; i++){
for (let j = 0; j < text_str[i].length; j++){
if (text_str[i][j] == "<") {
text_str[i] = text_str[i].slice(0, j) + '<' + text_str[i].slice(j + 1);
j += '<'.length - 1;
} else if (text_str[i][j] == ">") {
text_str[i] = text_str[i].slice(0, j) + '>' + text_str[i].slice(j + 1);
j += '>'.length - 1;
} else if (text_str[i][j] == "#" && (j == 0 || text_str[i][j - 1] != "\\")) {
text_str[i] = text_str[i].slice(0, j) + colorWrapper("128, 128, 128", '#' + text_str[i].slice(j + 1));
break;
} else if (text_str[i][j] == "(") {
text_str[i] = text_str[i].slice(0, j) + OPEN_BRACKET_FORMATTED + text_str[i].slice(j + 1);
j += OPEN_BRACKET_FORMATTED.length - 1;
} else if (text_str[i][j] == ")"){
text_str[i] = text_str[i].slice(0, j) + CLOSE_BRACKET_FORMATTED + text_str[i].slice(j + 1);
j += CLOSE_BRACKET_FORMATTED.length - 1;
}
}
text_str[i] = text_str[i].replaceAll("<", LESS_SIGN_FORMATTED);
text_str[i] = text_str[i].replaceAll(">", GREATER_SIGN_FORMATTED);
}
let combined_str = "";
for (let str of text_str){
combined_str += "<div>" + str + "</div>";
}
text_node.innerHTML = combined_str;
let new_node = null;
for (let node of text_node.childNodes[select_node_num].childNodes){
if (node instanceof Text){
if (absolute_offset <= node.nodeValue.length){
new_node = node;
break;
}
absolute_offset -= node.nodeValue.length;
} else if (node instanceof HTMLElement){
if (absolute_offset <= node.textContent.length){
new_node = node.firstChild;
break;
}
absolute_offset -= node.textContent.length;
}
}
document.getSelection().setBaseAndExtent(new_node, (!isText) ? absolute_offset : 1
, new_node, (!isText) ? absolute_offset : 1);
});