You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
// https://github.com/feross/clipboard-copy/blob/master/index.js
|
|
|
|
export default function clipboard(text) {
|
|
if (navigator.clipboard) {
|
|
return navigator.clipboard.writeText(text).catch(function (err) {
|
|
throw err !== undefined
|
|
? err
|
|
: new DOMException('The request is not allowed', 'NotAllowedError');
|
|
});
|
|
}
|
|
|
|
const span = document.createElement('span');
|
|
span.textContent = text;
|
|
|
|
span.style.whiteSpace = 'pre';
|
|
|
|
document.body.appendChild(span);
|
|
|
|
const selection = window.getSelection();
|
|
const range = window.document.createRange();
|
|
selection.removeAllRanges();
|
|
range.selectNode(span);
|
|
selection.addRange(range);
|
|
|
|
let success = false;
|
|
try {
|
|
success = window.document.execCommand('copy');
|
|
} catch (err) {
|
|
// eslint-disable-next-line
|
|
console.log('error', err);
|
|
}
|
|
|
|
selection.removeAllRanges();
|
|
window.document.body.removeChild(span);
|
|
|
|
return success
|
|
? Promise.resolve()
|
|
: Promise.reject(
|
|
new DOMException('The request is not allowed', 'NotAllowedError')
|
|
);
|
|
}
|