Skip to content

Commit d9b8c64

Browse files
committed
add image upload version
1 parent e9d1512 commit d9b8c64

File tree

5 files changed

+144
-777
lines changed

5 files changed

+144
-777
lines changed

AddImageButton/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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=f9403fc5f537b4ab332d`;
7+
8+
export default class imageButton extends Component {
9+
onClick = e => {
10+
e.preventDefault();
11+
ReactDOM.findDOMNode(this.refs.fileInput).click();
12+
};
13+
14+
inputChange = async e => {
15+
const file = e.target.files[0];
16+
const params = new FormData();
17+
params.append('file', file);
18+
const response = await axios.post(`http://localhost:25478/upload${token}`, params, {
19+
headers: { 'Content-Type': `multipart/form-data; boundary=${params._boundary}` }
20+
});
21+
const imageData = { src: `http://localhost:25478/${response.data.path}${token}`, type: 'placeholder' };
22+
this.props.setEditorState(insertDataBlock(this.props.getEditorState(), imageData, 'image'));
23+
};
24+
25+
preventBubblingUp = event => {
26+
event.preventDefault();
27+
};
28+
29+
render() {
30+
const { theme } = this.props;
31+
return (
32+
<div className={theme.buttonWrapper} onMouseDown={this.preventBubblingUp}>
33+
<button className={theme.button} onClick={this.onClick} type="button">
34+
<svg width="24" height="24" viewBox="0 0 24 24">
35+
<path
36+
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"
37+
fill="currentColor"
38+
fillRule="evenodd"
39+
/>
40+
</svg>
41+
</button>
42+
43+
<div className={theme.addImage}>
44+
<input type="file" ref="fileInput" onChange={::this.inputChange} style={{ display: 'none' }} />
45+
</div>
46+
</div>
47+
);
48+
}
49+
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { EditorState, ContentState } from 'draft-js';
44
import Editor, { composeDecorators } from 'draft-js-plugins-editor';
55
import createImagePlugin from 'draft-js-image-plugin';
66
import createResizeablePlugin from 'draft-js-resizeable-plugin';
7-
import { AddImageButton } from 'draft-js-buttons-plugin';
87
import { BlockquoteButton } from 'draft-js-buttons';
98
import createSideToolbarPlugin from 'draft-js-side-toolbar-plugin';
9+
import AddImageButton from './AddImageButton';
1010
import 'draft-js-side-toolbar-plugin/lib/plugin.css';
1111

1212
const resizeablePlugin = createResizeablePlugin();

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"devDependencies": {
88
"babel-core": "^6.7.6",
99
"babel-loader": "^6.2.4",
10+
"babel-polyfill": "^6.26.0",
1011
"babel-preset-es2015": "^6.6.0",
1112
"babel-preset-react": "^6.5.0",
1213
"babel-preset-stage-0": "^6.5.0",
@@ -17,6 +18,7 @@
1718
"webpack-dev-server": "2.11.5"
1819
},
1920
"dependencies": {
21+
"axios": "^0.19.0",
2022
"draft-js": "0.10.1",
2123
"draft-js-buttons-plugin": "^1.2.0",
2224
"draft-js-divider-plugin": "^0.2.1",

webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const HtmlWebpackPlugin = require('html-webpack-plugin');
22

33
module.exports = {
4-
entry: './index.js',
4+
entry: ['babel-polyfill', './index.js'],
55
devtool: 'inline-source-map',
66
output: { filename: 'bundle.js', publicPath: '' },
77
module: {

0 commit comments

Comments
 (0)