scinote-web/app/javascript/vue/shared/content/attachments/mixins/paste.js
2023-10-26 14:58:27 +02:00

32 lines
887 B
JavaScript

export default {
data() {
return {
showClipboardPasteModal: false,
pasteImages: null,
};
},
mounted() {
document.addEventListener('paste', this.handlePaste);
},
unmounted() {
document.removeEventListener('paste', this.handlePaste);
},
methods: {
handlePaste(e) {
if ( e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' ) return;
this.pasteImages = this.getImagesFromClipboard(e);
if (this.pasteImages && this.firstObjectInViewport()) this.showClipboardPasteModal = true;
},
getImagesFromClipboard(e) {
let image = null;
if (e.clipboardData) {
for (let i = 0; i < e.clipboardData.items.length; i++) {
if (e.clipboardData.items[i].type.indexOf('image') !== -1) {
image = e.clipboardData.items[i];
}
}
}
return image;
},
},
};