# 复制文本

import { Toast } from "antd-mobile";

// 复制文本
export function CopyTxt(str: string, isShowToast = true) {
  if (typeof document.execCommand === "function") {
    const input = document.createElement("input");
    document.body.appendChild(input);
    input.setAttribute("readonly", "readonly");
    input.setAttribute("value", str);
    // safari 这个方法有兼容问题造成无法执行下面代码
    input.select();
    // 兼容上诉代码
    // HTMLInputElement.setSelectionRange 方法用于设定<input> 或 <textarea> 元素中当前选中文本的起始和结束位置。
    input.setSelectionRange(0, str.length);
    try {
      document.execCommand("copy");
    } catch (err) {
      console.log(err);
    }
    document.body.removeChild(input);

    isShowToast && Toast.info("复制成功", undefined, undefined, false);
  } else {
    Toast.info("当前环境不支持复制");
  }
}

export function newCopy(str: string, isShowToast = true) {
  if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
    const el = document.createElement("input");
    el.value = str; // 要复制的内容
    el.style.opacity = "0";
    document.body.appendChild(el);
    const editable = el.contentEditable;
    const readOnly = el.readOnly;
    el.contentEditable = "true";
    el.readOnly = false;
    const range = document.createRange();
    range.selectNodeContents(el);
    const sel = window.getSelection();
    sel?.removeAllRanges();
    sel?.addRange(range);
    el.setSelectionRange(0, 999999);
    el.contentEditable = editable;
    el.readOnly = readOnly;
    const ret = document.execCommand("copy");
    el.blur();
    if (ret) {
      isShowToast && Toast.info("复制成功", undefined, undefined, false);
    } else {
      Toast.info("复制失败");
    }
  } else {
    CopyTxt(str, isShowToast);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Last Updated: 12/5/2022, 3:32:15 PM