|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import insertDataBlock from 'draft-js-buttons-plugin/lib/utils/insertDataBlock.js'; |
| 4 | +import axios from 'axios'; |
| 5 | + |
| 6 | +const token = '?token=token'; |
| 7 | +const serverURL = 'http://localhost:25478'; |
| 8 | + |
| 9 | +export default class imageButton extends Component { |
| 10 | + onClick = e => { |
| 11 | + e.preventDefault(); |
| 12 | + ReactDOM.findDOMNode(this.refs.fileInput).click(); |
| 13 | + }; |
| 14 | + |
| 15 | + inputChange = async e => { |
| 16 | + const file = e.target.files[0]; |
| 17 | + const params = new FormData(); |
| 18 | + params.append('file', file); |
| 19 | + const response = await axios.post(`${serverURL}/upload${token}`, params, { |
| 20 | + headers: { 'Content-Type': `multipart/form-data; boundary=${params._boundary}` } |
| 21 | + }); |
| 22 | + const imageData = { src: `${serverURL}/${response.data.path}${token}`, type: 'placeholder' }; |
| 23 | + this.props.setEditorState(insertDataBlock(this.props.getEditorState(), imageData, 'image')); |
| 24 | + }; |
| 25 | + |
| 26 | + preventBubblingUp = event => { |
| 27 | + event.preventDefault(); |
| 28 | + }; |
| 29 | + |
| 30 | + render() { |
| 31 | + const { theme } = this.props; |
| 32 | + return ( |
| 33 | + <div className={theme.buttonWrapper} onMouseDown={this.preventBubblingUp} style={{ color: 'inherit' }}> |
| 34 | + <button className={theme.button} onClick={this.onClick} type="button"> |
| 35 | + <svg width="24" height="24" viewBox="0 0 24 24"> |
| 36 | + <path |
| 37 | + d="M18.222 6H5.778C4.8 6 4 6.6 4 7.333v9.334C4 17.4 4.8 18 5.778 18h12.444C19.2 18 20 17.4 20 16.667V7.333C20 6.6 19.2 6 18.222 6zm-4.084 4l-3 4.51L9 11.503 6 16h12l-3.862-6z" |
| 38 | + fill="currentColor" |
| 39 | + fillRule="evenodd" |
| 40 | + /> |
| 41 | + </svg> |
| 42 | + </button> |
| 43 | + |
| 44 | + <div className={theme.addImage}> |
| 45 | + <input type="file" ref="fileInput" onChange={this.inputChange} style={{ display: 'none' }} /> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
0 commit comments