-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrail.js
219 lines (193 loc) · 6.45 KB
/
contrail.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var justBefore;
var intelli = [ "{", "[", "(", "'", '"', "<"]
class Contrail {
constructor() {
this.cm = false;
this.cp = 1;
this.color_mode = false;
}
get codeMode() {
return this.cm;
}
get colorMode() {
return this.color_mode;
}
set cursorPos(pos) {
this.cp = pos;
}
get cursorPos() {
return this.cp;
}
switchCodeMode() {
this.cm = !this.cm;
}
}
const contrail = new Contrail();
function CountLine( str ) {
num = str.match(/\r\n|\n/g);
if (num!=null){
line = num.length +1;
} else {
line = 1;
}
var linenum = document.getElementById('line-number');
cnt = linenum.childElementCount;
if (line < cnt) {
for (i = 0; i < cnt - line; i++) {
linenum.lastElementChild.remove()
}
} else if (line > cnt) {
for (i = cnt; i < line; i++) {
linenum.insertAdjacentHTML('beforeend', "<span class=line-item>" + (i + 1) + "</span>");
}
}
document.getElementById('code').innerText = str;
}
function onTextAreaKeyDown(event, object) {
var keyCode = event.keyCode;
var keyVal = event.key;
var cursorPosition = object.selectionStart;
var leftString = object.value.substr(0, cursorPosition);
var rightString = object.value.substr(cursorPosition, object.value.length);
if(keyCode === 9) {
event.preventDefault(); // 元の挙動を止める
// textareaの値をカーソル左の文字列 + タブスペース + カーソル右の文字列にする
object.value = leftString + "\t" + rightString;
// カーソル位置をタブスペースの後ろにする
object.selectionEnd = cursorPosition + 1;
}
if (contrail.codeMode) {
//自動補完
if (keyVal === "{") { //括弧
event.preventDefault();
object.value = leftString + "{}" + rightString;
object.selectionEnd = cursorPosition + 1;
justBefore = "{"
}
else if (keyVal === "[") {
event.preventDefault();
object.value = leftString + "[]" + rightString;
object.selectionEnd = cursorPosition + 1;
justBefore = "["
} else if (keyVal === '"') { //ダブルクォート
event.preventDefault();
object.value = leftString + '""' + rightString;
object.selectionEnd = cursorPosition + 1;
justBefore = '"'
} else if (keyVal === "'") { //シングルクォート
event.preventDefault();
object.value = leftString + "''" + rightString;
object.selectionEnd = cursorPosition + 1;
justBefore = "'"
} else if (keyVal === "(") { // 括弧
event.preventDefault();
object.value = leftString + "()" + rightString;
object.selectionEnd = cursorPosition + 1;
justBefore = "("
} else if (keyVal === "<") {
event.preventDefault();
object.value = leftString + "<>" + rightString;
object.selectionEnd = cursorPosition + 1;
justBefore = "<";
} else if (keyCode === 8) { //backspace
if (intelli.includes(justBefore)) {
object.value = leftString + rightString.slice(1);
object.selectionEnd = cursorPosition;
justBefore = "bs"
} else {
if (leftString.substr(-1) == "\n") {
const array = leftString.split("\n");
const index = array.length - 2; //連続タブ文字がある位置
if (array[index].match(/^\t+$/) != null) {
event.preventDefault();
const tabCount = array[index].match(/\t/g || []).length
console.log(tabCount)
array.splice(index, 1)
leftString_new = array.join("\n");
object.value = leftString_new + rightString;
object.selectionEnd = cursorPosition - tabCount - 1;
CountLine(object.value);
}
}
}
} else if (keyCode === 13) { //ender
event.preventDefault();
//行の初めにあるタブの数
//valueをカーソル位置までを切り取り、それを改行で区切った配列の末尾を取得することで行の初めからカーソル手前までを取得し、その中のタブ文字の数を取得する。
tabCount = object.value.substr(0, object.selectionStart).split("\n").pop().split("\t").length
if (intelli.includes(justBefore)) {
object.value = leftString + `\n${"\t".repeat(tabCount)}\n${"\t".repeat(tabCount - 1)}` + rightString;
} else {
object.value = leftString + `\n${"\t".repeat(tabCount - 1)}` + rightString;
tabCount -= 1;//下の計算の和を1減らす目的
}
justBefore = "bs"
CountLine(object.value)
object.selectionStart = cursorPosition + tabCount + 1;
object.selectionEnd = cursorPosition + tabCount + 1;
} else {
justBefore = "ot"
}
}
document.getElementById('code').innerText = object.value;
}
function escapeHTML(string){
return string.replace(/&/g, '<')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'");
}
(function() {
var editor = document.getElementById("editor"); //エディタ
var linenum = document.getElementById('line-number'); //行番号
editor.onkeydown = function(event) {onTextAreaKeyDown(event, this);}
document.getElementById('cmd-line-num').style.backgroundColor = "rgb(255, 255, 255)";
document.getElementById('cmd-line-num').style.fill = "rgb(0, 0, 0)";
const child = document.getElementById('commands').children;
for (i = 0; i < child.length; i++) {
child[i].addEventListener('click', function() {
if (window.getComputedStyle(this).backgroundColor === "rgb(255, 255, 255)") {
this.style.backgroundColor = "";
this.style.fill = "rgb(180, 180, 180)"
} else {
this.style.backgroundColor = "rgb(255, 255, 255)";
this.style.fill = "rgb(0, 0, 0)"
}
editor.focus();
editor.selectionStart = contrail.cursorPos;
})
}
//行番号
document.getElementById('cmd-line-num').addEventListener('click', function() {
if (linenum.style.display == "none") {
linenum.style.display = "flex";
} else {
linenum.style.display = "none";
}
linenum.scrollTop = editor.scrollTop;
});
//コードモード
document.getElementById('cmd-code-mode').addEventListener('click', function() {
contrail.switchCodeMode();
});
//クリップ
var isClip = false;
document.getElementById('cmd-front-clip').addEventListener('click', function() {
window.myAPI.clip_onclick(!isClip);
isClip = !isClip;
});
var translucentMode = false;
//半透明モード
document.getElementById('cmd-glass-mode').addEventListener('click', function() {
if (translucentMode) {
window.myAPI.translucent_window(1);
} else {
window.myAPI.translucent_window(0.6);
}
translucentMode = !translucentMode
});
editor.addEventListener('blur', function() {
contrail.cursorPos = this.selectionStart;
})
})();