import { message } from 'antd'; /** * 复制文本内容到粘贴板函数 * * @category Utils * @param copyText : 需要复制的文本 * @param prev : 内容文本前缀文本,默认为空字符串 * @param after : 内容文本后缀文本,默认为空字符串 * @param type : 临时DOM节点类型,默认为input,为兼容 document.execCommand 情况加的处理 * @param showResult : 复制结果是否显示复制文本标识,默认为true,显示复制文本 */ export const textCopy = ( copyText: string | undefined = '', prev: string = '', after: string = '', type: string = 'input', showResult: boolean = true, ) => { const curClipboard = navigator.clipboard; const successMessage = showResult ? `复制成功:${prev}${copyText}${after}` : '复制成功'; if (curClipboard) { curClipboard.writeText(`${prev}${copyText}${after}`).then( () => message.success(successMessage), () => message.error('复制失败'), ); } else if (document.execCommand) { const tempDom: any = document.createElement(type); document.body.appendChild(tempDom); tempDom.value = `${prev}${copyText}${after}`; tempDom.select(); document.execCommand('copy'); document.body.removeChild(tempDom); message.success(successMessage); } else { message.warning('当前浏览器不支持复制功能!'); } };