This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 245
/
image.js
70 lines (62 loc) · 1.69 KB
/
image.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import PropTypes from 'prop-types';
import React from 'react';
import { addNewBlock } from '../../model';
import { Block } from '../../util/constants';
export default class ImageButton extends React.Component {
static propTypes = {
setEditorState: PropTypes.func,
getEditorState: PropTypes.func,
close: PropTypes.func,
};
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
this.onChange = this.onChange.bind(this);
}
onClick() {
this.input.value = null;
this.input.click();
}
/*
This is an example of how an image button can be added
on the side control. This Button handles the image addition locally
by creating an object url. You can override this method to upload
images to your server first, then get the image url in return and then
add to the editor.
*/
onChange(e) {
// e.preventDefault();
const file = e.target.files[0];
if (file.type.indexOf('image/') === 0) {
// console.log(this.props.getEditorState());
// eslint-disable-next-line no-undef
const src = URL.createObjectURL(file);
this.props.setEditorState(addNewBlock(
this.props.getEditorState(),
Block.IMAGE, {
src,
}
));
}
this.props.close();
}
render() {
return (
<button
className="md-sb-button md-sb-img-button"
type="button"
onClick={this.onClick}
title="Add an Image"
>
<i className="fa fa-image" />
<input
type="file"
accept="image/*"
ref={(c) => { this.input = c; }}
onChange={this.onChange}
style={{ display: 'none' }}
/>
</button>
);
}
}