All files / hooks / useCopy.ts

100.00% Branches 0/0
2.70% Lines 2/74
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1












































































































import { useState } from "preact/hooks";

interface UseCopyOptions {
  timeout?: number;
  onSuccess?: () => void;
  onError?: (error: Error) => void;
}

interface UseCopyReturn {
  copy: (text: string) => Promise<void>;
  copied: boolean;
  error: string | null;
  isSupported: boolean;
}

// 传统的复制方法(降级方案)
function fallbackCopyTextToClipboard(text: string): Promise<void> {
  return new Promise((resolve, reject) => {
    const textArea = document.createElement("textarea");
    textArea.value = text;

    // 避免滚动到底部
    textArea.style.top = "0";
    textArea.style.left = "0";
    textArea.style.position = "fixed";
    textArea.style.opacity = "0";
    textArea.style.pointerEvents = "none";

    document.body.appendChild(textArea);
    textArea.focus();
    textArea.select();

    try {
      const successful = document.execCommand("copy");
      document.body.removeChild(textArea);

      if (successful) {
        resolve();
      } else {
        reject(new Error("execCommand('copy') 失败"));
      }
    } catch (err) {
      document.body.removeChild(textArea);
      reject(err);
    }
  });
}

export function useCopy(options: UseCopyOptions = {}): UseCopyReturn {
  const { timeout = 2000, onSuccess, onError } = options;

  const [copied, setCopied] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // 检查是否支持现代 Clipboard API
  const isClipboardAPISupported = typeof navigator !== "undefined" &&
    "clipboard" in navigator &&
    "writeText" in navigator.clipboard;

  // 检查是否支持传统的 execCommand
  const isExecCommandSupported = typeof document !== "undefined" &&
    "execCommand" in document;

  // 至少有一种方法可用
  const isSupported = isClipboardAPISupported || isExecCommandSupported;

  const copy = async (text: string): Promise<void> => {
    if (!text) {
      const error = new Error("复制内容不能为空");
      setError(error.message);
      onError?.(error);
      return;
    }

    try {
      // 优先使用现代 Clipboard API
      if (isClipboardAPISupported) {
        await navigator.clipboard.writeText(text);
      } else if (isExecCommandSupported) {
        // 降级到传统方法
        await fallbackCopyTextToClipboard(text);
      } else {
        throw new Error("浏览器不支持复制功能,请手动选择并复制文本");
      }

      setCopied(true);
      setError(null);
      onSuccess?.();

      // 自动重置状态
      setTimeout(() => {
        setCopied(false);
      }, timeout);
    } catch (err) {
      const error = err instanceof Error ? err : new Error("复制失败");
      setError(error.message);
      setCopied(false);
      onError?.(error);
    }
  };

  return {
    copy,
    copied,
    error,
    isSupported,
  };
}

export default useCopy;