fleet/frontend/utilities/copy_text.ts

41 lines
946 B
TypeScript
Raw Normal View History

// @ts-nocheck - may need to be reworked
import select from "select";
2017-01-20 17:52:57 +00:00
const removeSelectedText = () => {
return global.window.getSelection().removeAllRanges();
};
export const copyText = (elementSelector: string) => {
2017-01-20 17:52:57 +00:00
const { document } = global;
const element = document.querySelector(elementSelector);
const input = element.querySelector("input");
input.type = "text";
input.disabled = false;
2017-01-20 17:52:57 +00:00
select(input);
2017-01-20 17:52:57 +00:00
const canCopy = document.queryCommandEnabled("copy");
2017-01-20 17:52:57 +00:00
if (!canCopy) {
return false;
}
document.execCommand("copy");
input.type = "password";
input.disabled = true;
2017-01-20 17:52:57 +00:00
removeSelectedText();
return true;
};
export const stringToClipboard = (string) => {
const { navigator } = global;
return navigator.clipboard.writeText(string);
};
export const COPY_TEXT_SUCCESS = "Text copied to clipboard";
export const COPY_TEXT_ERROR = "Text not copied. Please copy manually.";
export default copyText;