Files
SaaS-lib/hx-op/src/util/copyText.ts
2024-06-19 10:03:47 +08:00

24 lines
725 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
};