2 export class Clipboard {
6 * @param {DataTransfer} clipboardData
8 constructor(clipboardData) {
9 this.data = clipboardData;
13 * Check if the clipboard has any items.
16 return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0;
20 * Check if the given event has tabular-looking data in the clipboard.
23 containsTabularData() {
24 const rtfData = this.data.getData( 'text/rtf');
25 return rtfData && rtfData.includes('\\trowd');
29 * Get the images that are in the clipboard data.
30 * @return {Array<File>}
33 const types = this.data.types;
34 const files = this.data.files;
37 for (const type of types) {
38 if (type.includes('image')) {
39 const item = this.data.getData(type);
40 images.push(item.getAsFile());
44 for (const file of files) {
45 if (file.type.includes('image')) {
54 export async function copyTextToClipboard(text) {
55 if (window.isSecureContext && navigator.clipboard) {
56 await navigator.clipboard.writeText(text);
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);
66 document.execCommand("copy");
67 document.body.removeChild(tempInput);
70 export default Clipboard;