diff options
author | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-06-03 19:04:53 +0300 |
---|---|---|
committer | Alexander Kiryukhin <a.kiryukhin@mail.ru> | 2022-06-03 19:04:53 +0300 |
commit | 0e7b36c2d443306325f17bb8850f5bb6176202bf (patch) | |
tree | 86629d4fe05d73f2d77dad423cc37d5a612430f3 /src/utils/clipboard.js | |
parent | 15d75cdc37e1459f7d11d004005d4305a6377ffd (diff) |
Diffstat (limited to 'src/utils/clipboard.js')
-rw-r--r-- | src/utils/clipboard.js | 30 |
1 files changed, 30 insertions, 0 deletions
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); + }); +} |