1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/dashboard/client/utils/copyToClipboard.ts
Sebastian Malton b1ff34879a cleanup Lens repo with tighter linting
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
2020-07-09 17:00:23 -04:00

23 lines
702 B
TypeScript

// Helper for selecting element's text content and copy in clipboard
function isSelectable(elem: HTMLElement): elem is HTMLInputElement {
return !!(elem as HTMLInputElement).select;
}
export function copyToClipboard(elem: HTMLElement, resetSelection = true): boolean {
let clearSelection: () => void;
if (isSelectable(elem)) {
elem.select();
clearSelection = (): void => elem.setSelectionRange(0, 0);
} else {
const selection = window.getSelection();
selection.selectAllChildren(elem);
clearSelection = (): void => selection.removeAllRanges();
}
const copyResult = document.execCommand("copy");
if (resetSelection) {
clearSelection();
}
return copyResult;
}