| Total Complexity | 12 |
| Complexity/F | 4 |
| Lines of Code | 51 |
| Function Count | 3 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | export async function copyToClipboard(event, text: string): Promise<void> { |
||
| 2 | if (event.clipboardData && event.clipboardData.setData) { |
||
| 3 | // IE specific code path to prevent textarea being shown while dialog is visible. |
||
| 4 | return event.clipboardData.setData("Text", text); |
||
| 5 | } |
||
| 6 | if ( |
||
| 7 | document.queryCommandSupported && |
||
| 8 | document.queryCommandSupported("copy") |
||
| 9 | ) { |
||
| 10 | const textarea = document.createElement("textarea"); |
||
| 11 | textarea.textContent = text; |
||
| 12 | textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in MS Edge. |
||
| 13 | document.body.appendChild(textarea); |
||
| 14 | textarea.select(); |
||
| 15 | try { |
||
| 16 | document.execCommand("copy"); // Security exception may be thrown by some browsers. |
||
| 17 | return Promise.resolve(); |
||
| 18 | } catch (ex) { |
||
| 19 | console.warn("Copy to clipboard failed.", ex); |
||
| 20 | return Promise.reject(); |
||
| 21 | } finally { |
||
| 22 | document.body.removeChild(textarea); |
||
| 23 | } |
||
| 24 | } |
||
| 25 | return Promise.reject(); |
||
| 26 | } |
||
| 27 | |||
| 28 | export function copyElementContents(el: Element): void { |
||
| 29 | const documentIEBody = document.body as any; |
||
| 30 | let range; |
||
| 31 | let sel; |
||
| 32 | if (document.createRange && window.getSelection) { |
||
| 33 | range = document.createRange(); |
||
| 34 | sel = window.getSelection(); |
||
| 35 | sel.removeAllRanges(); |
||
| 36 | try { |
||
| 37 | range.selectNodeContents(el); |
||
| 38 | sel.addRange(range); |
||
| 39 | } catch (e) { |
||
| 40 | range.selectNode(el); |
||
| 41 | sel.addRange(range); |
||
| 42 | } |
||
| 43 | // createTextRange is IE only |
||
| 44 | } else if (documentIEBody.createTextRange) { |
||
| 45 | range = documentIEBody.createTextRange(); |
||
| 46 | range.moveToElementText(el); |
||
| 47 | range.select(); |
||
| 48 | } |
||
| 49 | document.execCommand("copy"); |
||
| 50 | } |
||
| 51 |