]> BookStack Code Mirror - bookstack/blob - resources/js/services/clipboard.js
Merge branch 'master' of git://github.com/albergoniSivaf/BookStack into albergoniSiva...
[bookstack] / resources / js / services / clipboard.js
1
2 class Clipboard {
3
4     /**
5      * Constructor
6      * @param {DataTransfer} clipboardData
7      */
8     constructor(clipboardData) {
9         this.data = clipboardData;
10     }
11
12     /**
13      * Check if the clipboard has any items.
14      */
15     hasItems() {
16         return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0;
17     }
18
19     /**
20      * Check if the given event has tabular-looking data in the clipboard.
21      * @return {boolean}
22      */
23     containsTabularData() {
24         const rtfData = this.data.getData( 'text/rtf');
25         return rtfData && rtfData.includes('\\trowd');
26     }
27
28     /**
29      * Get the images that are in the clipboard data.
30      * @return {Array<File>}
31      */
32     getImages() {
33         const types = this.data.types;
34         const files = this.data.files;
35         const images = [];
36
37         for (const type of types) {
38             if (type.includes('image')) {
39                 const item = this.data.getData(type);
40                 images.push(item.getAsFile());
41             }
42         }
43
44         for (const file of files) {
45             if (file.type.includes('image')) {
46                 images.push(file);
47             }
48         }
49
50         return images;
51     }
52 }
53
54 export default Clipboard;