fleet/frontend/utilities/copy_text.js
Zachary Wasserman 6bdddfacf0
Expose API Token in UI (#1868)
Useful for SAML login users who cannot log in with `fleetctl login`. Instead
they can pull their session token from the UI and configure the fleetctl client
to use it.

Closes #1865
2018-07-17 11:27:30 -07:00

34 lines
765 B
JavaScript

import select from 'select';
const removeSelectedText = () => {
return global.window.getSelection().removeAllRanges();
};
export const copyText = (elementSelector) => {
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 COPY_TEXT_SUCCESS = 'Text copied to clipboard';
export const COPY_TEXT_ERROR = 'Text not copied. Please copy manually.';
export default copyText;