diff --git a/src/components/ImageUpload.vue b/src/components/ImageUpload.vue index c1e37f0e5b8142dd72ca05d42283878eef9f2d5b..8f6006babb6ecd50f38f33e1d2de3343536885d5 100644 --- a/src/components/ImageUpload.vue +++ b/src/components/ImageUpload.vue @@ -9,7 +9,7 @@ @dragover.prevent="$event.currentTarget.classList.add('bg-gray-200')" @dragleave.prevent="$event.currentTarget.classList.remove('bg-gray-200')" > - <div class="relative h-full flex justify-center items-center"> + <div class="relative h-full"> <input type="file" ref="imageInput" @@ -19,25 +19,38 @@ multiple class="h-full w-full opacity-0 absolute z-10 cursor-pointer" /> - <span class="font-semibold opacity-25 relative"> + <div + v-show="!imageList.length" + class=" + h-full + font-semibold + opacity-25 + flex + justify-center + items-center + " + > Drag and drop images here or click here to browse. - </span> + </div> + <div + v-show="imageList.length" + class="h-full flex flex-row flex-wrap content-start overflow-y-auto" + > + <img + v-for="(preview, index) in previews" + :key="index" + :src="preview.url" + @click="removeImage(preview.image)" + alt="" + class="max-h-28 relative z-10" + /> + </div> </div> </div> <div class="flex flex-col items-center m-4"> <WarningBox v-if="invalidType" @click="invalidType = false"> Please upload PNG, JPEG or GIF image only. </WarningBox> - <ul v-if="imageList.length"> - <li - v-for="(image, index) in imageList" - :key="index" - @click="removeImage(imageList.indexOf(image))" - class="text-gray-600 hover:text-gray-700 font-semibold cursor-pointer" - > - {{ image.name }} - </li> - </ul> <button v-show="imageList.length" @click="submitImage" @@ -69,16 +82,24 @@ export default { data: () => { return { imageList: [], - urls: [], invalidType: false, }; }, computed: { + previews: function () { + let tempArray = []; + this.imageList.forEach((image) => + tempArray.push({ url: URL.createObjectURL(image), image }) + ); + return tempArray; + }, ...mapState(["stateUser"]), }, methods: { updateWrapper(event) { - this.checkImageType(event) ? this.updateImageList(event) : false; + this.checkImageType(event) + ? this.updateImageList(event) + : (this.$refs.imageInput.value = ""); }, updateImageList(event) { this.imageList = this.imageList.concat( @@ -99,8 +120,8 @@ export default { this.invalidType = false; return true; }, - removeImage(index) { - this.imageList.splice(index, 1); + removeImage(image) { + this.imageList.splice(this.imageList.indexOf(image), 1); }, submitImage() { let formData = new FormData(); @@ -121,7 +142,7 @@ export default { }, body: fd, }; - const response = await fetch("../api/image", options); + const response = await fetch("/api/image", options); const data = await response.json(); this.addStateHashArray(data.hash); } catch { @@ -131,10 +152,4 @@ export default { ...mapActions(["addStateHashArray"]), }, }; -</script> - -<style scoped> -[v-cloak] { - display: none; -} -</style> \ No newline at end of file +</script> \ No newline at end of file