1 export class Clipboard {
5 * @param {DataTransfer} clipboardData
7 constructor(clipboardData) {
8 this.data = clipboardData;
12 * Check if the clipboard has any items.
15 return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0;
19 * Check if the given event has tabular-looking data in the clipboard.
22 containsTabularData() {
23 const rtfData = this.data.getData('text/rtf');
24 return rtfData && rtfData.includes('\\trowd');
28 * Get the images that are in the clipboard data.
29 * @return {Array<File>}
32 const {types} = this.data;
35 for (const type of types) {
36 if (type.includes('image')) {
37 const item = this.data.getData(type);
38 images.push(item.getAsFile());
42 const imageFiles = this.getFiles().filter(f => f.type.includes('image'));
43 images.push(...imageFiles);
49 * Get the files included in the clipboard data.
53 const {files} = this.data;
59 export async function copyTextToClipboard(text) {
60 if (window.isSecureContext && navigator.clipboard) {
61 await navigator.clipboard.writeText(text);
65 // Backup option where we can't use the navigator.clipboard API
66 const tempInput = document.createElement('textarea');
67 tempInput.style = 'position: absolute; left: -1000px; top: -1000px;';
68 tempInput.value = text;
69 document.body.appendChild(tempInput);
71 document.execCommand('copy');
72 document.body.removeChild(tempInput);