Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image upload #18

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
57 changes: 0 additions & 57 deletions renderer/components/image-drop.js

This file was deleted.

54 changes: 52 additions & 2 deletions renderer/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import Open from '../components/open-button'
import New from '../components/new-button'
import Export from '../components/export-button'
import Create from '../components/window-button'
import imageDrop from '../components/image-drop'

import BoldIcon from 'react-icons/lib/fa/bold'
import ItalicIcon from 'react-icons/lib/fa/italic'
Expand Down Expand Up @@ -71,7 +70,58 @@ export default class extends Component {
)
}

handleDrop = e => imageDrop(e, this)
sendImage (file, imageData, defaultMessage) {
fetch('https://api.imgur.com/3/image', {
method: 'POST',
headers: {
'Authorization': `Client-ID e3f6a51d5c12580`
},
body: imageData})
.then(response => response.json())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Async/Await

.then(success => {
const resultMessage = Object.assign(
{},
defaultMessage,
{
target: {
value: `${this.editor.domField.value} ![${file.name}](${success.data.link})`}
})
this.editor.writeValue(resultMessage)
})
.catch(error => {
const errorMessage = Object.assign(
{},
defaultMessage,
{
target: {
value: `${this.editor.domField.value} ![A problem when sending the file, please try again.]()`}
})
this.editor.writeValue(errorMessage)
})
}

handleDrop = event => {
event.preventDefault()
// without 'preventDefault', when you drop the image, change the whole editor view
const defaultMessage = {
target: {
value: `${this.editor.domField.value} ![Problem with the format file](url)`
}
}
const file = event.dataTransfer.files[0]
const imageFormat = ['jpeg', 'png', 'gif', 'peg', 'apng', 'tiff', 'pdf', 'xcf']
const validFile = imageFormat.filter((format) => {
const regExp = new RegExp("\\b(" + format + ")\\b")
return regExp.test(file.type)
})
if (!!validFile && validFile.length !== 0) {
const imageData = new FormData()
imageData.append('image', file)
return this.sendImage(file, imageData, defaultMessage)
}
// if file format doesn't support by imgur, advice the user
return this.editor.writeValue(defaultMessage)
}

handleChange= event => {
if (event.markdown && this.state.fileName) {
Expand Down