mirror of
https://github.com/youzan/vant.git
synced 2025-10-18 17:51:54 +00:00
chore(cli): extract copyToClipboard to common (#10263)
* chore(cli): extract copyToClipboard to common * style: update copied
This commit is contained in:
@@ -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 };
|
||||
|
Reference in New Issue
Block a user