mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-05-31 01:36:11 +00:00
[fix] Clipboard browser support
Safari 10.3 supports copying (but apparently not from inputs)
This commit is contained in:
@@ -1,19 +1,43 @@
|
|||||||
|
/* global window */
|
||||||
|
|
||||||
class Clipboard {
|
class Clipboard {
|
||||||
|
static isSupported() {
|
||||||
|
return (
|
||||||
|
typeof document.queryCommandSupported === 'function' && document.queryCommandSupported('copy')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
static getString() {
|
static getString() {
|
||||||
return Promise.resolve('');
|
return Promise.resolve('');
|
||||||
}
|
}
|
||||||
|
|
||||||
static setString(text) {
|
static setString(text) {
|
||||||
let success = false;
|
let success = false;
|
||||||
const textField = document.createElement('textarea');
|
|
||||||
textField.innerText = text;
|
// add the text to a hidden node
|
||||||
document.body.appendChild(textField);
|
const node = document.createElement('span');
|
||||||
textField.select();
|
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 {
|
try {
|
||||||
document.execCommand('copy');
|
document.execCommand('copy');
|
||||||
success = true;
|
success = true;
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
document.body.removeChild(textField);
|
|
||||||
|
// remove selection and node
|
||||||
|
selection.removeAllRanges();
|
||||||
|
document.body.removeChild(node);
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user