[fix] Clipboard browser support

Safari 10.3 supports copying (but apparently not from inputs)
This commit is contained in:
Nicolas Gallagher
2017-04-23 13:36:38 -07:00
parent 227971d22c
commit d83cd45b6f
+29 -5
View File
@@ -1,19 +1,43 @@
/* global window */
class Clipboard {
static isSupported() {
return (
typeof document.queryCommandSupported === 'function' && document.queryCommandSupported('copy')
);
}
static getString() {
return Promise.resolve('');
}
static setString(text) {
let success = false;
const textField = document.createElement('textarea');
textField.innerText = text;
document.body.appendChild(textField);
textField.select();
// add the text to a hidden node
const node = document.createElement('span');
node.textContent = text;
node.style.position = 'absolute';
node.style.opacity = '0';
document.body.appendChild(node);
// select the text
const selection = window.getSelection();
selection.removeAllRanges();
const range = document.createRange();
range.selectNodeContents(node);
selection.addRange(range);
// attempt to copy
try {
document.execCommand('copy');
success = true;
} catch (e) {}
document.body.removeChild(textField);
// remove selection and node
selection.removeAllRanges();
document.body.removeChild(node);
return success;
}
}