fleet/frontend/utilities/copy_text.ts
Martavis Parker 384c987389
Removed all traces of Redux from the app! (#5287)
* clean up routes and useless components

* component clean up

* removed redux from routes

* rename file

* moved useDeepEffect hook with others

* removed redux, fleet, app_constants dirs; added types to utilities

* style cleanup

* typo fix

* removed unused ts-ignore comments

* removed redux packages!!!

* formatting

* fixed typing for simple search function

* updated frontend readme
2022-04-22 09:45:35 -07:00

41 lines
946 B
TypeScript

// @ts-nocheck - may need to be reworked
import select from "select";
const removeSelectedText = () => {
return global.window.getSelection().removeAllRanges();
};
export const copyText = (elementSelector: string) => {
const { document } = global;
const element = document.querySelector(elementSelector);
const input = element.querySelector("input");
input.type = "text";
input.disabled = false;
select(input);
const canCopy = document.queryCommandEnabled("copy");
if (!canCopy) {
return false;
}
document.execCommand("copy");
input.type = "password";
input.disabled = true;
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;