2023-04-14 13:08:40 +00:00
|
|
|
export class Clipboard {
|
2019-12-21 15:48:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
* @param {DataTransfer} clipboardData
|
|
|
|
*/
|
|
|
|
constructor(clipboardData) {
|
|
|
|
this.data = clipboardData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the clipboard has any items.
|
|
|
|
*/
|
|
|
|
hasItems() {
|
|
|
|
return Boolean(this.data) && Boolean(this.data.types) && this.data.types.length > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given event has tabular-looking data in the clipboard.
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
containsTabularData() {
|
2023-04-18 21:20:02 +00:00
|
|
|
const rtfData = this.data.getData('text/rtf');
|
2019-12-21 15:48:03 +00:00
|
|
|
return rtfData && rtfData.includes('\\trowd');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the images that are in the clipboard data.
|
|
|
|
* @return {Array<File>}
|
|
|
|
*/
|
|
|
|
getImages() {
|
2023-04-18 21:20:02 +00:00
|
|
|
const {types} = this.data;
|
2019-12-21 15:48:03 +00:00
|
|
|
const images = [];
|
|
|
|
|
|
|
|
for (const type of types) {
|
|
|
|
if (type.includes('image')) {
|
|
|
|
const item = this.data.getData(type);
|
|
|
|
images.push(item.getAsFile());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-24 15:19:20 +00:00
|
|
|
const imageFiles = this.getFiles().filter(f => f.type.includes('image'));
|
|
|
|
images.push(...imageFiles);
|
2019-12-21 15:48:03 +00:00
|
|
|
|
|
|
|
return images;
|
|
|
|
}
|
2023-04-18 21:20:02 +00:00
|
|
|
|
2023-04-24 15:19:20 +00:00
|
|
|
/**
|
|
|
|
* Get the files included in the clipboard data.
|
|
|
|
* @return {File[]}
|
|
|
|
*/
|
|
|
|
getFiles() {
|
|
|
|
const {files} = this.data;
|
|
|
|
return [...files];
|
|
|
|
}
|
|
|
|
|
2019-12-21 15:48:03 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 14:08:17 +00:00
|
|
|
export async function copyTextToClipboard(text) {
|
|
|
|
if (window.isSecureContext && navigator.clipboard) {
|
|
|
|
await navigator.clipboard.writeText(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backup option where we can't use the navigator.clipboard API
|
2023-04-18 21:20:02 +00:00
|
|
|
const tempInput = document.createElement('textarea');
|
|
|
|
tempInput.style = 'position: absolute; left: -1000px; top: -1000px;';
|
2023-04-18 14:08:17 +00:00
|
|
|
tempInput.value = text;
|
|
|
|
document.body.appendChild(tempInput);
|
|
|
|
tempInput.select();
|
2023-04-18 21:20:02 +00:00
|
|
|
document.execCommand('copy');
|
2023-04-18 14:08:17 +00:00
|
|
|
document.body.removeChild(tempInput);
|
2023-04-14 13:08:40 +00:00
|
|
|
}
|