summaryrefslogtreecommitdiff
path: root/frontend/app/common/uploadImage.component.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2020-09-26 14:56:02 +0200
committerJules Laplace <julescarbon@gmail.com>2020-09-26 14:56:02 +0200
commita17b76ac75f506f5da6fe8adf9c36632b60d4226 (patch)
treeabb0af0c4409b830dea2ef808c146223ee973933 /frontend/app/common/uploadImage.component.js
parent2231a6e1c05b07bb7ec5906716aedec93d02429c (diff)
refactor to use app-rooted js imports
Diffstat (limited to 'frontend/app/common/uploadImage.component.js')
-rw-r--r--frontend/app/common/uploadImage.component.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/frontend/app/common/uploadImage.component.js b/frontend/app/common/uploadImage.component.js
new file mode 100644
index 0000000..3ae41c8
--- /dev/null
+++ b/frontend/app/common/uploadImage.component.js
@@ -0,0 +1,74 @@
+import React, { Component } from 'react'
+
+import { renderThumbnail } from './upload.helpers'
+
+export default class UploadImageComponent extends Component {
+ constructor(props) {
+ super(props)
+ document.body.addEventListener("dragover", this.dragOver.bind(this))
+ document.body.addEventListener("dragleave", this.dragLeave.bind(this))
+ document.body.addEventListener("drop", this.upload.bind(this))
+ }
+
+ dragOver(e) {
+ e.stopPropagation()
+ e.preventDefault()
+ document.body.className = 'dragging'
+ }
+
+ dragLeave(e) {
+ e.stopPropagation()
+ e.preventDefault()
+ document.body.className = ''
+ }
+
+ upload(e) {
+ e.preventDefault()
+ document.body.className = ''
+ const files = e.dataTransfer ? e.dataTransfer.files : e.target.files
+ let i
+ let file
+ for (i = 0; i < files.length; i++) {
+ file = files[i]
+ if (file && file.type.match('image.*')) break
+ }
+ if (!file) {
+ console.log('No file specified')
+ return
+ }
+ const fr = new FileReader()
+ fr.onload = fileReaderEvent => {
+ fr.onload = null
+ const img = new Image()
+ img.onload = () => {
+ img.onload = null
+ this.resizeAndUpload(file, img)
+ }
+ img.src = fileReaderEvent.target.result
+ }
+ fr.readAsDataURL(file)
+ }
+
+ resizeAndUpload(file, img) {
+ const canvas = renderThumbnail(img, this.props)
+ canvas.toBlob(blob => {
+ this.props.onUpload({ file, img, canvas, blob, freshen: true })
+ }, 'image/jpeg', this.props.quality || 80)
+ }
+
+ render() {
+ return (
+ <div className='uploadButton'>
+ <input
+ type="file"
+ accept="image/*"
+ onChange={this.upload.bind(this)}
+ required
+ />
+ <div className='dragCurtain'>
+ <div className='dragLabel'>Drop image here</div>
+ </div>
+ </div>
+ )
+ }
+}