常用工具函数

/* 复制功能 */


export const copyText = async (
  text: string,
  successCb?: () => void,
  errorCb?: (err: any) => void,
) => {
  try {
    if (navigator.clipboard && navigator.permissions) {
      Notification?.requestPermission()?.then();
      await navigator.clipboard.writeText(text);
      successCb?.();
    } else {
      const textArea = document.createElement('textArea') as any;
      textArea.value = text;

      textArea.setAttribute('display', 'none');
      document.body.appendChild(textArea);
      textArea.select();
      document?.execCommand('copy');

      document.body.removeChild(textArea);
      successCb?.();
      Toast.show('复制成功');
    }
  } catch (err) {
    console.log('err', err);
    Toast.show('当前环境不支持复制功能。');
    errorCb?.(err);
  }
};

你可能感兴趣的:(工作沉淀,javascript,前端,开发语言)