2021-04-12 13:32:25 +00:00
|
|
|
import select from "select";
|
2017-01-20 17:52:57 +00:00
|
|
|
|
|
|
|
const removeSelectedText = () => {
|
|
|
|
return global.window.getSelection().removeAllRanges();
|
|
|
|
};
|
|
|
|
|
|
|
|
export const copyText = (elementSelector) => {
|
|
|
|
const { document } = global;
|
|
|
|
|
|
|
|
const element = document.querySelector(elementSelector);
|
2021-04-12 13:32:25 +00:00
|
|
|
const input = element.querySelector("input");
|
|
|
|
input.type = "text";
|
2017-01-25 15:58:28 +00:00
|
|
|
input.disabled = false;
|
2017-01-20 17:52:57 +00:00
|
|
|
|
2017-01-25 15:58:28 +00:00
|
|
|
select(input);
|
2017-01-20 17:52:57 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
const canCopy = document.queryCommandEnabled("copy");
|
2017-01-20 17:52:57 +00:00
|
|
|
|
|
|
|
if (!canCopy) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
document.execCommand("copy");
|
|
|
|
input.type = "password";
|
2017-01-25 15:58:28 +00:00
|
|
|
input.disabled = true;
|
2017-01-20 17:52:57 +00:00
|
|
|
removeSelectedText();
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2020-05-29 16:12:39 +00:00
|
|
|
export const stringToClipboard = (string) => {
|
|
|
|
const { navigator } = global;
|
|
|
|
|
|
|
|
return navigator.clipboard.writeText(string);
|
|
|
|
};
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
export const COPY_TEXT_SUCCESS = "Text copied to clipboard";
|
|
|
|
export const COPY_TEXT_ERROR = "Text not copied. Please copy manually.";
|
2018-07-17 18:27:30 +00:00
|
|
|
|
|
|
|
export default copyText;
|