From 0e7b36c2d443306325f17bb8850f5bb6176202bf Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Fri, 3 Jun 2022 19:04:53 +0300 Subject: initial --- src/utils/clipboard.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/utils/clipboard.js (limited to 'src/utils/clipboard.js') diff --git a/src/utils/clipboard.js b/src/utils/clipboard.js new file mode 100644 index 0000000..6d139c3 --- /dev/null +++ b/src/utils/clipboard.js @@ -0,0 +1,30 @@ +const fallbackCopyTextToClipboard = (text) => { + var textArea = document.createElement("textarea"); + textArea.value = text; + textArea.style.top = "0"; + textArea.style.left = "0"; + textArea.style.position = "fixed"; + + document.body.appendChild(textArea); + textArea.focus(); + textArea.select(); + + try { + document.execCommand('copy'); + } catch (err) { + console.error('Fallback: Oops, unable to copy', err); + } + + document.body.removeChild(textArea); +} + +export const copyTextToClipboard = (text) => { + if (!navigator.clipboard) { + fallbackCopyTextToClipboard(text); + return; + } + navigator.clipboard.writeText(text).then(function () { + }, function (err) { + console.error('Async: Could not copy text: ', err); + }); +} -- cgit v1.2.3