]> BookStack Code Mirror - bookstack/blob - resources/js/services/clipboard.js
Fix timestamp in API docs example response
[bookstack] / resources / js / services / clipboard.js
1 export class Clipboard {
2
3     /**
4      * Constructor
5      * @param {DataTransfer} clipboardData
6      */
7     constructor(clipboardData) {
8         this.data = clipboardData;
9     }
10
11     /**
12      * Check if the clipboard has any items.
13      */
14     hasItems() {
15         return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0;
16     }
17
18     /**
19      * Check if the given event has tabular-looking data in the clipboard.
20      * @return {boolean}
21      */
22     containsTabularData() {
23         const rtfData = this.data.getData('text/rtf');
24         return rtfData && rtfData.includes('\\trowd');
25     }
26
27     /**
28      * Get the images that are in the clipboard data.
29      * @return {Array<File>}
30      */
31     getImages() {
32         const {types} = this.data;
33         const {files} = this.data;
34         const images = [];
35
36         for (const type of types) {
37             if (type.includes('image')) {
38                 const item = this.data.getData(type);
39                 images.push(item.getAsFile());
40             }
41         }
42
43         for (const file of files) {
44             if (file.type.includes('image')) {
45                 images.push(file);
46             }
47         }
48
49         return images;
50     }
51
52 }
53
54 export async function copyTextToClipboard(text) {
55     if (window.isSecureContext && navigator.clipboard) {
56         await navigator.clipboard.writeText(text);
57         return;
58     }
59
60     // Backup option where we can't use the navigator.clipboard API
61     const tempInput = document.createElement('textarea');
62     tempInput.style = 'position: absolute; left: -1000px; top: -1000px;';
63     tempInput.value = text;
64     document.body.appendChild(tempInput);
65     tempInput.select();
66     document.execCommand('copy');
67     document.body.removeChild(tempInput);
68 }