index.js (1282B)
1 import './style.css'; 2 import * as logging from '~/lib/logging'; 3 import {query} from '~/lib/notifications'; 4 5 function showNotification(message, mouseEvent) { 6 var el = document.createElement('div'); 7 el.innerHTML = message; 8 el.className = 'copy-clipboard-notification'; 9 document.body.appendChild(el); 10 var w = el.offsetWidth, 11 h = el.offsetHeight, 12 x = mouseEvent.clientX - w - 8, 13 y = mouseEvent.clientY - h / 2; 14 if (x < 0) { 15 x = 0; 16 } 17 if (y < 0) { 18 y = 0; 19 } 20 el.style.top = y + 'px'; 21 el.style.left = x + 'px'; 22 setTimeout(function() { 23 document.body.removeChild(el); 24 }, 1000); 25 } 26 27 function copyToClipboard(s, mouseEvent) { 28 let success = false; 29 let ta; 30 try { 31 ta = document.createElement('textarea'); 32 ta.value = s; 33 document.body.appendChild(ta); 34 ta.select(); 35 success = document.execCommand('copy'); 36 if (success) { 37 showNotification('Copied', mouseEvent); 38 } 39 } catch (e) { 40 logging.captureException(e, 'clipborad to copy failed'); 41 } finally { 42 document.body.removeChild(ta); 43 } 44 if (!success) { 45 query("Copy to clipboard: Ctrl+C, Enter", s); 46 } 47 } 48 49 export default copyToClipboard;