1 export class Clipboard {
3 protected data: DataTransfer;
5 constructor(clipboardData: DataTransfer) {
6 this.data = clipboardData;
10 * Check if the clipboard has any items.
13 return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0;
17 * Check if the given event has tabular-looking data in the clipboard.
19 containsTabularData(): boolean {
20 const rtfData = this.data.getData('text/rtf');
21 return !!rtfData && rtfData.includes('\\trowd');
25 * Get the images that are in the clipboard data.
28 return this.getFiles().filter(f => f.type.includes('image'));
32 * Get the files included in the clipboard data.
35 const {files} = this.data;
40 export async function copyTextToClipboard(text: string) {
41 if (window.isSecureContext && navigator.clipboard) {
42 await navigator.clipboard.writeText(text);
46 // Backup option where we can't use the navigator.clipboard API
47 const tempInput = document.createElement('textarea');
48 tempInput.setAttribute('style', 'position: absolute; left: -1000px; top: -1000px;');
49 tempInput.value = text;
50 document.body.appendChild(tempInput);
52 document.execCommand('copy');
53 document.body.removeChild(tempInput);