-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathcodeflask.js
396 lines (330 loc) · 11.4 KB
/
codeflask.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import {
editor_css
} from './styles/editor';
import {
inject_css
} from './styles/injector';
import {
default_css_theme,
FONT_SIZE
} from './styles/theme-default';
import {
escape_html
} from './utils/html-escape';
import Prism from 'prismjs';
export default class CodeFlask {
constructor(selectorOrElement, opts) {
if (!selectorOrElement) {
// If no selector or element is passed to CodeFlask,
// stop execution and throw error.
throw Error('CodeFlask expects a parameter which is Element or a String selector');
return;
}
if (!opts) {
// If no selector or element is passed to CodeFlask,
// stop execution and throw error.
throw Error('CodeFlask expects an object containing options as second parameter');
return;
}
if (selectorOrElement.nodeType) {
// If it is an element, assign it directly
this.editorRoot = selectorOrElement;
} else {
// If it is a selector, tries to find element
const editorRoot = document.querySelector(selectorOrElement);
// If an element is found using this selector,
// assign this element as the root element
if (editorRoot) {
this.editorRoot = editorRoot;
}
}
this.opts = opts;
this.activeLineId = null;
this.startEditor();
}
startEditor() {
const isCSSInjected = inject_css(editor_css, null, this.opts.styleParent);
if (!isCSSInjected) {
throw Error('Failed to inject CodeFlask CSS.');
return;
}
// The order matters (pre > code). Don't change it
// or things are going to break.
this.createWrapper();
this.createTextarea();
this.createPre();
this.createCode();
this.createHighlighter();
this.runOptions();
this.listenTextarea();
this.populateDefault();
this.updateCode(this.code);
}
createWrapper() {
this.code = this.editorRoot.innerHTML;
this.editorRoot.innerHTML = '';
this.elWrapper = this.createElement('div', this.editorRoot);
this.elWrapper.classList.add('codeflask');
}
createTextarea() {
this.elTextarea = this.createElement('textarea', this.elWrapper);
this.elTextarea.classList.add('codeflask__textarea', 'codeflask__flatten');
}
createPre() {
this.elPre = this.createElement('pre', this.elWrapper);
this.elPre.classList.add('codeflask__pre', 'codeflask__flatten');
}
createCode() {
this.elCode = this.createElement('code', this.elPre);
this.elCode.classList.add('codeflask__code', `language-${this.opts.language || 'html'}`);
}
createLineNumbers() {
this.elLineNumbers = this.createElement('div', this.elWrapper);
this.elLineNumbers.classList.add('codeflask__lines');
this.setLineNumber();
}
createHighlighter() {
this.elHighlight = this.createElement('span', this.elWrapper);
this.elHighlight.classList.add('codeflask_active_line');
this.elHighlight.style.width = window.getComputedStyle(this.elWrapper).width;
}
createElement(elementTag, whereToAppend) {
const element = document.createElement(elementTag);
whereToAppend.appendChild(element);
return element;
}
getCurrentLineNumber() {
const caretPos = this.elTextarea.selectionStart;
const text = this.elTextarea.value;
const allText = text.split('\n');
let sum = 0;
if (caretPos !== text.length) {
for (let i = 0; i < allText.length; ++i) {
// length is incremented to one becaus newline absence dunring split
sum += (allText[i].length + 1);
// if caret position is more than the previous line lenght
if (sum >= caretPos) {
//checks if last char is newline break
if (text[caretPos - 1] === '\n') return i + 2;
else return i + 1;
}
}
} else return allText.length;
return
}
activeLineHighlight(lineNumber) {
this.activeLineId = 'codeflask_line_number_' + lineNumber;
const activeLine = document.getElementById(this.activeLineId);
const activeLineStyle = activeLine && activeLine.getBoundingClientRect().top;
const wrapperStyle = this.elWrapper.getBoundingClientRect().top;
const y = parseFloat(activeLineStyle) - parseFloat(wrapperStyle);
this.elHighlight.style.transform = `translateY(${y}px)`;
// this.elHighlight.style.top = y + 'px';
}
runOptions() {
this.opts.rtl = this.opts.rtl || false;
this.opts.tabSize = this.opts.tabSize || 2;
this.opts.enableAutocorrect = this.opts.enableAutocorrect || false;
this.opts.lineNumbers = this.opts.lineNumbers || false;
this.opts.defaultTheme = this.opts.defaultTheme !== false;
this.opts.areaId = this.opts.areaId || null;
this.opts.ariaLabelledby = this.opts.ariaLabelledby || null;
this.opts.readonly = this.opts.readonly || null;
// if handleTabs is not either true or false, make it true by default
if (typeof this.opts.handleTabs !== 'boolean') {
this.opts.handleTabs = true;
}
if (this.opts.rtl === true) {
this.elTextarea.setAttribute('dir', 'rtl');
this.elPre.setAttribute('dir', 'rtl');
}
if (this.opts.enableAutocorrect === false) {
this.elTextarea.setAttribute('spellcheck', 'false');
this.elTextarea.setAttribute('autocapitalize', 'off');
this.elTextarea.setAttribute('autocomplete', 'off');
this.elTextarea.setAttribute('autocorrect', 'off');
}
if (this.opts.lineNumbers) {
this.elWrapper.classList.add('codeflask--has-line-numbers');
this.createLineNumbers();
}
if (this.opts.defaultTheme) {
inject_css(default_css_theme, 'theme-default', this.opts.styleParent);
}
if (this.opts.areaId) {
this.elTextarea.setAttribute('id', this.opts.areaId);
}
if (this.opts.ariaLabelledby) {
this.elTextarea.setAttribute('aria-labelledby', this.opts.ariaLabelledby);
}
if (this.opts.readonly) {
this.enableReadonlyMode();
}
}
updateLineNumbersCount() {
while (this.elLineNumbers.firstChild) this.elLineNumbers.firstChild.remove();
for (let i = 1; i <= this.lineNumber; i++) {
let lineNumber = document.createElement('div');
lineNumber.className = 'codeflask__lines__line';
lineNumber.id = 'codeflask_line_number_' + i;
lineNumber.textContent = i;
this.elLineNumbers.appendChild(lineNumber);
}
}
listenTextarea() {
this.elTextarea.addEventListener('input', (e) => {
this.code = e.target.value;
this.elCode.innerHTML = escape_html(e.target.value);
this.highlight();
setTimeout(() => {
this.runUpdate();
}, 1);
this.handleSelfClosingCharacters(e);
this.handleNewLineIndentation(e);
});
this.elTextarea.addEventListener('keydown', (e) => {
this.handleTabs(e);
setTimeout(() => {
this.setLineNumber();
this.activeLineHighlight(this.getCurrentLineNumber());
}, 1);
});
this.elTextarea.addEventListener('click', (e) => {
this.activeLineHighlight(this.getCurrentLineNumber());
});
this.elTextarea.addEventListener('mousedown', (e) => {
if (e.which === 2) e.preventDefault();
});
this.elTextarea.addEventListener('scroll', (e) => {
this.elPre.style.transform = `translate3d(-${e.target.scrollLeft}px, -${e.target.scrollTop}px, 0)`;
if (this.elLineNumbers) {
this.elLineNumbers.style.transform = `translate3d(0, -${e.target.scrollTop}px, 0)`;
const activeLine = document.getElementById(this.activeLineId);
console.log(activeLine.getBoundingClientRect().top);
this.elHighlight.style.transform = `translateY(${activeLine.getBoundingClientRect().top}px)`;
}
});
}
handleTabs(e) {
if (this.opts.handleTabs) {
if (e.keyCode !== 9) {
return;
}
e.preventDefault();
const tabCode = 9;
const pressedCode = e.keyCode;
const selectionStart = this.elTextarea.selectionStart;
const selectionEnd = this.elTextarea.selectionEnd;
const newCode = `${this.code.substring(0, selectionStart)}${' '.repeat(this.opts.tabSize)}${this.code.substring(selectionEnd)}`;
this.updateCode(newCode);
this.elTextarea.selectionEnd = selectionEnd + this.opts.tabSize;
}
}
handleSelfClosingCharacters(e) {
const openChars = ['(', '[', '{', '<'];
const key = e.data;
if (!openChars.includes(key)) {
return;
}
switch (key) {
case '(':
this.closeCharacter(')');
break;
case '[':
this.closeCharacter(']');
break;
case '{':
this.closeCharacter('}');
break;
case '<':
this.closeCharacter('>');
break;
}
}
setLineNumber() {
this.lineNumber = this.code.split('\n').length;
if (this.opts.lineNumbers) {
this.updateLineNumbersCount();
}
}
handleNewLineIndentation(e) {
if (e.type !== 'input' || e.type !== 'input' && e.inputType !== 'insertLineBreak') {
return;
};
// TODO: Make this shit work right
// const selectionStart = this.elTextarea.selectionStart;
// const selectionEnd = this.elTextarea.selectionEnd;
// const allLines = this.code.split('\n').length;
// const lines = this.code.substring(0, selectionStart).split('\n');
// const currentLine = lines.length;
// const lastLine = lines[currentLine - 1];
// const selection = this.textSelection;
// const charIndex = selection.focusOffset;
// console.log(selection);
// if (lastLine !== undefined && currentLine < allLines) {
// e.preventDefault();
// const spaces = lastLine.match(/^ {1,}/);
// if (spaces) {
// console.log(spaces[0].length);
// const newCode = `${this.code.substring(0, selectionStart)}\n${' '.repeat(spaces[0].length)}${this.code.substring(selectionEnd)}`;
// this.updateCode(newCode);
// console.log(newCode);
// setTimeout(() => {
// this.elTextarea.selectionEnd = selectionEnd + spaces[0].length + 1;
// }, 0);
// }
// }
}
closeCharacter(closeChar) {
const selectionStart = this.elTextarea.selectionStart;
const selectionEnd = this.elTextarea.selectionEnd;
const newCode = `${this.code.substring(0, selectionStart)}${closeChar}${this.code.substring(selectionEnd)}`;
this.updateCode(newCode);
this.elTextarea.selectionEnd = selectionEnd;
}
updateCode(newCode) {
this.code = newCode;
this.elTextarea.value = newCode;
this.elCode.innerHTML = escape_html(newCode);
this.highlight();
setTimeout(this.runUpdate.bind(this), 1);
this.activeLineHighlight(1);
}
updateLanguage(newLanguage) {
const oldLanguage = this.opts.language;
this.elCode.classList.remove(`language-${oldLanguage}`);
this.elCode.classList.add(`language-${newLanguage}`);
this.opts.language = newLanguage;
this.highlight();
}
addLanguage(name, options) {
Prism.languages[name] = options;
}
populateDefault() {
this.updateCode(this.code);
}
highlight() {
Prism.highlightElement(this.elCode, false);
}
onUpdate(callback) {
if (callback && {}.toString.call(callback) !== '[object Function]') {
throw Error('CodeFlask expects callback of type Function');
return;
}
this.updateCallBack = callback;
}
getCode() {
return this.code;
}
runUpdate() {
if (this.updateCallBack) {
this.updateCallBack(this.code);
}
}
enableReadonlyMode() {
this.elTextarea.setAttribute('readonly', true);
}
disableReadonlyMode() {
this.elTextarea.removeAttribute('readonly');
}
}