]> BookStack Code Mirror - bookstack/blobdiff - resources/js/services/clipboard.js
CM6: Further fixes/improvements after testing
[bookstack] / resources / js / services / clipboard.js
index da921e51590d0c67e63b994e4cf1d4a25ae5e772..c0b0fbfab6260c6f84f06bbf6755e650a1e6d47e 100644 (file)
@@ -1,5 +1,5 @@
 
-class Clipboard {
+export class Clipboard {
 
     /**
      * Constructor
@@ -51,4 +51,20 @@ class Clipboard {
     }
 }
 
+export async function copyTextToClipboard(text) {
+    if (window.isSecureContext && navigator.clipboard) {
+        await navigator.clipboard.writeText(text);
+        return;
+    }
+
+    // Backup option where we can't use the navigator.clipboard API
+    const tempInput = document.createElement("textarea");
+    tempInput.style = "position: absolute; left: -1000px; top: -1000px;";
+    tempInput.value = text;
+    document.body.appendChild(tempInput);
+    tempInput.select();
+    document.execCommand("copy");
+    document.body.removeChild(tempInput);
+}
+
 export default Clipboard;
\ No newline at end of file