(text: string)
| 23 | |
| 24 | // 经典降级方案:创建隐藏 textarea + execCommand |
| 25 | function fallbackCopy(text: string): Promise<boolean> { |
| 26 | return new Promise((resolve) => { |
| 27 | const textArea = document.createElement('textarea'); |
| 28 | |
| 29 | // 防止滚动跳动 + 移动端键盘弹出 |
| 30 | textArea.value = text; |
| 31 | textArea.style.position = 'fixed'; |
| 32 | textArea.style.top = '0'; |
| 33 | textArea.style.left = '0'; |
| 34 | textArea.style.opacity = '0'; |
| 35 | textArea.style.pointerEvents = 'none'; // 防止干扰点击 |
| 36 | |
| 37 | // 兼容 iOS 需要这个 |
| 38 | textArea.setAttribute('readonly', 'true'); |
| 39 | |
| 40 | document.body.appendChild(textArea); |
| 41 | textArea.focus(); |
| 42 | textArea.select(); |
| 43 | |
| 44 | try { |
| 45 | const successful = document.execCommand('copy'); |
| 46 | resolve(successful); |
| 47 | } catch (err) { |
| 48 | console.error('复制失败:', err); |
| 49 | resolve(false); |
| 50 | } finally { |
| 51 | document.body.removeChild(textArea); |
| 52 | } |
| 53 | }); |
| 54 | } |
no test coverage detected