Skip to content

Commit e56456b

Browse files
committed
Button to select the image (Plugin interface)
1 parent 72da9f9 commit e56456b

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
"type": "git",
2727
"url": "git+https://github.com/jimmycodesocial/draft-js-select-image-plugin.git"
2828
},
29+
"license": "MIT",
30+
"main": "lib/index.js",
2931
"scripts": {
3032
"clean": "node_modules/.bin/rimraf lib",
3133
"build": "npm run clean && npm run build:js",
@@ -68,6 +70,7 @@
6870
"webpack-node-externals": "^1.7.2"
6971
},
7072
"dependencies": {
73+
"decorate-component-with-props": "^1.1.0",
7174
"prop-types": "^15.6.1"
7275
}
7376
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
/**
5+
* Icon by: https://www.iconfinder.com/icons/290132/gallery_image_photo_photography_picture_pictures_icon#size=24
6+
*/
7+
class SelectImageButton extends React.PureComponent {
8+
onClick = () => {
9+
this.input.value = null;
10+
this.input.click();
11+
}
12+
13+
onMouseDown = (event) => {
14+
event.preventDefault();
15+
}
16+
17+
/**
18+
* When a file is selected.
19+
* @param {*} event
20+
*/
21+
onChange = (event) => {
22+
const file = event.target.files[0];
23+
const { getEditorState, setEditorState, entityType, addImage } = this.props;
24+
25+
if (file.type.indexOf('image/') === 0) {
26+
const src = URL.createObjectURL(file);
27+
setEditorState(addImage(getEditorState(), entityType, { src }));
28+
}
29+
30+
if (this.props.close) {
31+
this.props.close();
32+
}
33+
}
34+
35+
blockTypeIsActive = () => {
36+
const editorState = this.props.getEditorState();
37+
const blockType = editorState
38+
.getCurrentContent()
39+
.getBlockForKey(editorState.getSelection().getStartKey())
40+
.getType();
41+
42+
return blockType === this.props.entityType;
43+
};
44+
45+
render() {
46+
const { theme } = this.props;
47+
const className = this.blockTypeIsActive()
48+
? `${theme.button} ${theme.active}`
49+
: theme.button;
50+
51+
return (
52+
<div className={theme.buttonWrapper} onMouseDown={this.onMouseDown}>
53+
<button
54+
className={className}
55+
onClick={this.onClick}
56+
title="Add an Image"
57+
type="button">
58+
59+
<svg
60+
xmlns="http://www.w3.org/2000/svg"
61+
version="1.1"
62+
viewBox="0 0 48 48"
63+
enableBackground="new 0 0 48 48"
64+
height="24"
65+
width="24">
66+
<path
67+
clipRule="evenodd"
68+
fillRule="evenodd"
69+
d="M43,41H5c-2.209,0-4-1.791-4-4V11c0-2.209,1.791-4,4-4h38c2.209,0,4,1.791,4,4v26 C47,39.209,45.209,41,43,41z M45,11c0-1.104-0.896-2-2-2H5c-1.104,0-2,0.896-2,2v26c0,1.104,0.896,2,2,2h38c1.104,0,2-0.896,2-2V11z M41.334,34.715L35,28.381L31.381,32l3.334,3.334c0.381,0.381,0.381,0.999,0,1.381c-0.382,0.381-1,0.381-1.381,0L19,22.381 L6.666,34.715c-0.381,0.381-0.999,0.381-1.381,0c-0.381-0.382-0.381-1,0-1.381L18.19,20.429c0.032-0.048,0.053-0.101,0.095-0.144 c0.197-0.197,0.457-0.287,0.715-0.281c0.258-0.006,0.518,0.084,0.715,0.281c0.042,0.043,0.062,0.096,0.095,0.144L30,30.619 l4.19-4.19c0.033-0.047,0.053-0.101,0.095-0.144c0.197-0.196,0.457-0.287,0.715-0.281c0.258-0.006,0.518,0.085,0.715,0.281 c0.042,0.043,0.062,0.097,0.095,0.144l6.905,6.905c0.381,0.381,0.381,0.999,0,1.381C42.333,35.096,41.715,35.096,41.334,34.715z M29,19c-2.209,0-4-1.791-4-4s1.791-4,4-4s4,1.791,4,4S31.209,19,29,19z M29,13c-1.104,0-2,0.896-2,2s0.896,2,2,2s2-0.896,2-2 S30.104,13,29,13z" />
70+
</svg>
71+
72+
<input
73+
ref={ref => this.input = ref}
74+
type="file"
75+
accept="image/*"
76+
onChange={this.onChange}
77+
style={{ display: 'none' }} />
78+
</button>
79+
</div>
80+
);
81+
}
82+
}
83+
84+
SelectImageButton.propTypes = {
85+
theme: PropTypes.object,
86+
entityType: PropTypes.string.isRequired,
87+
addImage: PropTypes.func.isRequired,
88+
close: PropTypes.func
89+
};
90+
91+
SelectImageButton.defaultProps = {
92+
theme: {},
93+
};
94+
95+
export default SelectImageButton;

src/createSelectImagePlugin.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import decorateComponentWithProps from 'decorate-component-with-props';
2+
import SelectImageButton from './components/SelectImageButton';
3+
import addImage from './modifiers/addImage';
4+
5+
export default ({ imageType = 'IMAGE' } = {}) => {
6+
return {
7+
SelectImageButton: decorateComponentWithProps(SelectImageButton, {
8+
entityType: imageType,
9+
addImage
10+
})
11+
};
12+
};

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import createSelectImagePlugin from './createSelectImagePlugin';
2+
3+
export default createSelectImagePlugin;

0 commit comments

Comments
 (0)