(text: string)
| 1 | function copyWithSelection(text: string): boolean { |
| 2 | if (typeof document === "undefined" || !document.body || !document.execCommand) { |
| 3 | return false; |
| 4 | } |
| 5 | |
| 6 | const textarea = document.createElement("textarea"); |
| 7 | textarea.value = text; |
| 8 | textarea.setAttribute("readonly", "true"); |
| 9 | textarea.style.position = "fixed"; |
| 10 | textarea.style.top = "0"; |
| 11 | textarea.style.left = "0"; |
| 12 | textarea.style.width = "1px"; |
| 13 | textarea.style.height = "1px"; |
| 14 | textarea.style.padding = "0"; |
| 15 | textarea.style.border = "0"; |
| 16 | textarea.style.opacity = "0"; |
| 17 | textarea.style.pointerEvents = "none"; |
| 18 | |
| 19 | document.body.appendChild(textarea); |
| 20 | textarea.focus({ preventScroll: true }); |
| 21 | textarea.select(); |
| 22 | textarea.setSelectionRange(0, text.length); |
| 23 | |
| 24 | try { |
| 25 | return document.execCommand("copy"); |
| 26 | } catch { |
| 27 | return false; |
| 28 | } finally { |
| 29 | document.body.removeChild(textarea); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | function shouldCopyWithSelectionFirst(): boolean { |
| 34 | if (typeof navigator === "undefined") return false; |
no test coverage detected