-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (62 loc) · 2.05 KB
/
index.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
import { toPng } from 'dom-to-image';
import { changeDpiDataUrl } from 'changedpi';
import 'regenerator-runtime/runtime';
const BASE_DPI = 72;
const scale = window.devicePixelRatio;
const initialText = decodeURI(window.location.pathname.trim().substring(1));
const txt = document.getElementById('txt');
const txtWrapper = document.getElementById('txt-wrapper');
const status = document.getElementById('status');
const processText = (text) => {
return text.replaceAll("'", '’').trim();
};
const setToClipboard = async (blob) => {
const data = [new ClipboardItem({ [blob.type]: blob })];
await navigator.clipboard.write(data);
};
const setSuccess = () => {
status.innerText = 'Copied to clipboard!';
setTimeout(() => {
status.innerText = '';
}, 2000);
};
const generateImage = async () => {
let dataUrl = await toPng(txtWrapper, {
height: txtWrapper.offsetHeight * scale,
width: txtWrapper.offsetWidth * scale,
style: {
transform: `scale(${scale})`,
transformOrigin: 'top left',
width: `${txtWrapper.offsetWidth}px`,
height: `${txtWrapper.offsetHeight}px`,
},
});
dataUrl = changeDpiDataUrl(dataUrl, BASE_DPI * scale);
const data = await fetch(dataUrl);
const blob = await data.blob();
setToClipboard(blob).then(() => setSuccess());
};
if (initialText) {
txt.appendChild(document.createTextNode(initialText));
generateImage();
}
txt.addEventListener('input', (e) => {
if (txt.childNodes.length > 0) generateImage();
});
txt.addEventListener('paste', (e) => {
e.stopPropagation();
e.preventDefault();
let text = '';
const event = e.originalEvent || e;
if (event.clipboardData && event.clipboardData.getData) {
text = processText(event.clipboardData.getData('text/plain'));
} else if (window.clipboardData && window.clipboardData.getData) {
text = processText(window.clipboardData.getData('Text').trim());
}
if (document.queryCommandSupported('insertText')) {
document.execCommand('insertText', false, text);
} else {
document.execCommand('paste', false, text);
}
});
txt.focus();