]> BookStack Code Mirror - bookstack/blobdiff - resources/js/services/clipboard.js
Search: Prevented negated terms filling in UI inputs
[bookstack] / resources / js / services / clipboard.js
index da921e51590d0c67e63b994e4cf1d4a25ae5e772..5f73c3020cf932685c1369cbe40070405079b19e 100644 (file)
@@ -1,5 +1,4 @@
-
-class Clipboard {
+export class Clipboard {
 
     /**
      * Constructor
@@ -21,7 +20,7 @@ class Clipboard {
      * @return {boolean}
      */
     containsTabularData() {
-        const rtfData = this.data.getData( 'text/rtf');
+        const rtfData = this.data.getData('text/rtf');
         return rtfData && rtfData.includes('\\trowd');
     }
 
@@ -30,8 +29,7 @@ class Clipboard {
      * @return {Array<File>}
      */
     getImages() {
-        const types = this.data.types;
-        const files = this.data.files;
+        const {types} = this.data;
         const images = [];
 
         for (const type of types) {
@@ -41,14 +39,35 @@ class Clipboard {
             }
         }
 
-        for (const file of files) {
-            if (file.type.includes('image')) {
-                images.push(file);
-            }
-        }
+        const imageFiles = this.getFiles().filter(f => f.type.includes('image'));
+        images.push(...imageFiles);
 
         return images;
     }
+
+    /**
+     * Get the files included in the clipboard data.
+     * @return {File[]}
+     */
+    getFiles() {
+        const {files} = this.data;
+        return [...files];
+    }
+
 }
 
-export default Clipboard;
\ No newline at end of file
+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);
+}