You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
725 B
23 lines
725 B
import { NsMessage } from '/nerv-lib/component';
|
|
const flag = navigator.clipboard && window.isSecureContext;
|
|
|
|
export const copyText = (text: any) => {
|
|
if (flag) {
|
|
// navigator clipboard 向剪贴板写文本
|
|
navigator.clipboard.writeText(text).then(() => {
|
|
NsMessage.success('复制成功');
|
|
});
|
|
} else {
|
|
// 创建text area
|
|
const textArea = document.createElement('textarea');
|
|
textArea.value = text;
|
|
// 使text area不在viewport,同时设置不可见
|
|
document.body.appendChild(textArea);
|
|
textArea.focus();
|
|
textArea.select();
|
|
NsMessage.success('复制成功');
|
|
// 执行复制命令并移除文本框
|
|
document.execCommand('copy');
|
|
textArea.remove();
|
|
}
|
|
};
|
|
|