diff --git a/src/js/avatarcropper.ts b/src/js/avatarcropper.ts index 0fe80b1..c691e64 100644 --- a/src/js/avatarcropper.ts +++ b/src/js/avatarcropper.ts @@ -14,6 +14,7 @@ import { doFooterThings, showTutorial } from './footer' import { GlobalEvents } from './eventclass' import { TextDialog } from './textdialog' import { DragDrop } from './dragdrop' +import { ImagePasted } from './imagepasted' import { Point } from './point' import { KeyManager } from './keymanager' import { Border, GradientInfo } from './borders' @@ -117,6 +118,7 @@ export class AvatarCropper extends Widget { this.textOverlay.setAttribute('for', 'openInput') let dragDrop = new DragDrop(this.textOverlay) + dragDrop.on('drop', file => { this.openFile(file) }) @@ -126,6 +128,12 @@ export class AvatarCropper extends Widget { } }) + let imagePaste = new ImagePasted(this.textOverlay) + + imagePaste.on('imagepasted', file => { + this.openFile(file) + }) + document.body.appendChild(this.textOverlay) if (getIEVersion() !== false && !AvatarCropper.settings.dismissedIE) { diff --git a/src/js/imagepasted.ts b/src/js/imagepasted.ts new file mode 100644 index 0000000..1268b05 --- /dev/null +++ b/src/js/imagepasted.ts @@ -0,0 +1,34 @@ +import { hideElement } from "./util"; +import { EventClass } from "./eventclass"; + +export class ImagePasted extends EventClass { + private tempText: string = null; + + constructor(overlay: HTMLElement) { + super(); + + let imgpasted = this; // Setting "imgpasted" to "this" so that we can use it in the onpaste and other events, since "this" would mean something else there. + + imgpasted.createEvent("imagepasted"); + + document.onpaste = function (e) { + + var files: FileList = (e.clipboardData).files; // Check to see if there are files in the clipboard data + + if (files.length) { // If there are files, paste the first file. + let file: File = files[0]; + if (!file?.type.includes("image")) return; // If it's not an image, return. + imgpasted.emitEvent("imagepasted", files[0]) + } + + e.preventDefault(); + e.stopPropagation(); + + hideElement(overlay); + overlay.innerText = imgpasted.tempText; + overlay.style.zIndex = ""; + imgpasted.tempText = null; + + }; + } +}