-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtextarea.js
69 lines (55 loc) · 1.68 KB
/
textarea.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
export default class Textarea {
constructor(options) {
this.active = false;
this.container = options.container;
this.parser = options.parser;
this.parentElement = undefined;
this.editableSlug = undefined;
this.element = this.container.querySelector('textarea#editor__textarea');
this.bindAutoGrow();
this.bindKeys();
}
bindAutoGrow() {
this.element.addEventListener('input', e => {
this.autoGrow();
});
}
autoGrow() {
const initialDocumentPosition = document.documentElement.scrollTop;
this.element.style.height = 'auto';
const height = `${this.element.scrollHeight - 28}px`;
this.element.style.height = height;
document.documentElement.scrollTop = initialDocumentPosition;
}
bindKeys() {
this.element.addEventListener('keydown', e => {
if (e.key === 'Escape') {
this.deactivate();
}
});
}
activate(parentElement) {
this.active = true;
this.parentElement = parentElement;
this.editableSlug = this.parentElement.dataset.title;
parentElement.classList.add('hide');
this.element.value = parentElement.dataset.source || '';
this.container.classList.remove('hide');
setTimeout(() => {
this.autoGrow();
this.element.selectionStart = 0;
this.element.selectionEnd = 0;
this.element.focus();
}, 50);
}
deactivate() {
const value = this.element.value;
this.parentElement.dataset.source = value;
this.parentElement.innerHTML = '';
this.container.classList.add('hide');
this.parser.parse(this.parentElement, value);
this.element.value = '';
this.active = false;
window.location = `#/page/${this.editableSlug}`;
}
}