-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
70 lines (60 loc) · 1.92 KB
/
main.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
const copy = (text) => {
navigator.clipboard.writeText(text)
toast.classList.add('show');
setTimeout(() => toast.classList.remove('show'), 3000);
};
const codeblock = document.getElementById('code-preview');
const toast = document.querySelector('.toast');
const createModal = document.getElementById('create-dialog');
const create = document.getElementById('save');
const codeInput = document.getElementById('code-input');
const urlParams = new URLSearchParams(window.location.search);
let text = urlParams.get('text')
let url
text = text.replace("%20", ' ');
text = text.replace("%2", '/');
text = text.replace("\\n", `
`);
let lang = urlParams.get('lang');
if (lang) {
codeblock.innerHTML = hljs.highlight(
text,
{ language: lang }
).value
} else {
codeblock.innerText = text || 'Add ?text=your-text to copy some text!';
}
codeblock.addEventListener('click', () => copy(text));
codeblock.addEventListener('contextmenu', (e) => {
e.preventDefault();
createModal.showModal()
codeInput.value = text;
codeInput.focus();
});
createModal.addEventListener('close', () => {
text = url;
lang = urlParams.get('lang');
if (lang) {
codeblock.innerHTML = hljs.highlight(
text,
{ language: lang }
).value
location.href = location.href.split('?')[0] + `?text=${url}&lang=${lang}`;
} else {
codeblock.innerText = text || 'Add ?text=your-text to copy some text!';
}
});
codeInput.addEventListener('input', (e) => {
url = e.target.value;
url = url.replace(' ', '%20');
url = url.replace('+', '%2B');
url = url.replace('/', '%2F');
url = url.replace('\\n', '%0A');
url = url.replace('\\r', '%0D');
url = url.replace('\\t', '%09');
url = url.replace('#', '%23');
url = url.replace('&', '%26');
url = url.replace('=', '%3D');
url = url.replace('?', '%3F');
url = url.replace('@', '%40');
});