chore(cli): extract copyToClipboard to common (#10263)

* chore(cli): extract copyToClipboard to common

* style: update copied
This commit is contained in:
neverland
2022-02-08 11:41:37 +08:00
committed by GitHub
parent 09731607d2
commit d348b36159
5 changed files with 53 additions and 70 deletions

View File

@@ -8,4 +8,31 @@ export function decamelize(str, sep = '-') {
.toLowerCase();
}
// from https://30secondsofcode.org
export function copyToClipboard(str) {
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
const selection = document.getSelection();
if (!selection) {
return;
}
const selected = selection.rangeCount > 0 ? selection.getRangeAt(0) : false;
el.select();
document.execCommand('copy');
document.body.removeChild(el);
if (selected) {
selection.removeAllRanges();
selection.addRange(selected);
}
}
export { isMobile };