clipboard.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // NOTE: We ONLY run this on the (web) client.
  2. if (process.client) {
  3. window.Clipboard = (function(window, document, navigator) {
  4. let textArea,
  5. copy
  6. function isOS() {
  7. return navigator.userAgent.match(/ipad|iphone/i)
  8. }
  9. function createTextArea(text) {
  10. textArea = document.createElement('textArea')
  11. textArea.value = text
  12. document.body.appendChild(textArea)
  13. }
  14. function selectText() {
  15. let range,
  16. selection
  17. if (isOS()) {
  18. range = document.createRange()
  19. range.selectNodeContents(textArea)
  20. selection = window.getSelection()
  21. selection.removeAllRanges()
  22. selection.addRange(range)
  23. textArea.setSelectionRange(0, 999999)
  24. } else {
  25. textArea.select()
  26. }
  27. }
  28. function _copyToClipboard() {
  29. document.execCommand('copy')
  30. document.body.removeChild(textArea)
  31. }
  32. copy = function(text) {
  33. createTextArea(text)
  34. selectText()
  35. _copyToClipboard()
  36. }
  37. return { copy }
  38. })(window, document, navigator)
  39. }