-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
60 lines (53 loc) · 1.56 KB
/
app.js
File metadata and controls
60 lines (53 loc) · 1.56 KB
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
function formatDoc(cmd, value = null) {
if (value) {
document.execCommand(cmd, false, value);
} else {
document.execCommand(cmd);
}
}
function addLink() {
const url = prompt('Insert url');
formatDoc('createLink', url);
}
const content = document.getElementById('content');
content.addEventListener('mouseenter', function () {
const a = content.querySelectorAll('a');
a.forEach((item) => {
item.addEventListener('mouseenter', function () {
content.setAttribute('contenteditable', false);
item.target = '_blank';
});
item.addEventListener('mouseleave', function () {
content.setAttribute('contenteditable', true);
});
});
});
const showCode = document.getElementById('show-code');
let active = false;
showCode.addEventListener('click', function () {
showCode.dataset.active = !active;
active = !active;
if (active) {
content.textContent = content.innerHTML;
content.setAttribute('contenteditable', false);
} else {
content.innerHTML = content.textContent;
content.setAttribute('contenteditable', true);
}
});
const filename = document.getElementById('filename');
function fileHandle(value) {
if (value === 'new') {
content.innerHTML = '';
filename.value = 'untitled';
} else if (value === 'txt') {
const blob = new Blob([content.innerText]);
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `${filename.value}.txt`;
link.click();
} else if (value === 'pdf') {
html2pdf(content).save(filename.value);
}
}