-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy.js
More file actions
40 lines (39 loc) · 1.06 KB
/
Copy pathcopy.js
File metadata and controls
40 lines (39 loc) · 1.06 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
/**
* https://github.com/uiwjs/copy-to-clipboard/blob/master/src/main.js
*/
function copyTextToClipboard(text, cb) {
const el = document.createElement('textarea');
el.value = text;
el.setAttribute('readonly', '');
el.style = {
position: 'absolute',
left: '-9999px',
};
document.body.appendChild(el);
const selected = document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
el.select();
let isCopy = false;
try {
const successful = document.execCommand('copy');
isCopy = !!successful;
} catch (err) {
isCopy = false;
}
document.body.removeChild(el);
if (selected && document.getSelection) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
}
cb && cb(isCopy);
}
function copied(target, str) {
target.classList.add('active');
const input = target.parentElement.querySelector('input');
if (input) {
copyTextToClipboard(input.value || '', function () {
setTimeout(() => {
target.classList.remove('active');
}, 2000);
});
}
}