From 7da5f65c85a0a5cd18957382c0a01dd6be730e76 Mon Sep 17 00:00:00 2001
From: dragonwocky <thedragonring.bod@gmail.com>
Date: Wed, 20 Oct 2021 23:02:26 +1100
Subject: [PATCH] clipboard helpers

---
 api/web.mjs | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/api/web.mjs b/api/web.mjs
index 0de9929..e5f6f5f 100644
--- a/api/web.mjs
+++ b/api/web.mjs
@@ -143,6 +143,35 @@ export const loadStylesheet = (path) => {
   return true;
 };
 
+/**
+ * copy text to the clipboard
+ * @param {string} str - the string to copy
+ * @returns {Promise<void>}
+ */
+export const copyToClipboard = async (str) => {
+  try {
+    await navigator.clipboard.writeText(str);
+  } catch {
+    const $el = document.createElement('textarea');
+    $el.value = str;
+    $el.setAttribute('readonly', '');
+    $el.style.position = 'absolute';
+    $el.style.left = '-9999px';
+    document.body.appendChild($el);
+    $el.select();
+    document.execCommand('copy');
+    document.body.removeChild($el);
+  }
+};
+
+/**
+ * read text from the clipboard
+ * @returns {Promise<string>}
+ */
+export const readFromClipboard = () => {
+  return navigator.clipboard.readText();
+};
+
 document.addEventListener('keyup', (event) => {
   if (document.activeElement.nodeName === 'INPUT') return;
   for (const hotkey of _hotkeyEventListeners) {