npm install @draft-js-plugins/editor
npm install @draft-js-plugins/sticker
// It is important to import the Editor which accepts plugins.
import Editor from '@draft-js-plugins/editor';
import createStickerPlugin from '@draft-js-plugins/sticker';
import React from 'react';
import { fromJS } from 'immutable';
// Creates an Instance. Passing in an Immutable.js List of stickers as an
// argument.
const stickers = fromJS({
data: {
'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b': {
id: 'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b',
url: '../images/unicorn-4.png',
},
'adec3f13-823c-47c3-b4d1-be4f68dd9d6d': {
id: 'adec3f13-823c-47c3-b4d1-be4f68dd9d6d',
url: '../images/unicorn-1.png',
},
},
});
const stickerPlugin = createStickerPlugin({ stickers });
// The Editor accepts an array of plugins. In this case, only the stickerPlugin
// is passed in, although it is possible to pass in multiple plugins.
const MyEditor = ({ editorState, onChange }) => (
<Editor
editorState={editorState}
onChange={onChange}
plugins={[stickerPlugin]}
/>
);
export default MyEditor;
The plugin ships with a default styling available at this location in the installed package: node_modules/@draft-js-plugins/sticker/lib/plugin.css
npm i style-loader css-loader --save-dev
module.exports = {
module: {
loaders: [
{
test: /plugin\.css$/,
loaders: ['style-loader', 'css'],
},
],
},
};
import '@draft-js-plugins/sticker/lib/plugin.css';
import React, { Component } from 'react';
import { EditorState } from 'draft-js';
import Editor from '@draft-js-plugins/editor';
import createStickerPlugin from '@draft-js-plugins/sticker';
import editorStyles from './editorStyles.module.css';
import stickers from './stickers';
const stickerPlugin = createStickerPlugin({ stickers });
const plugins = [stickerPlugin];
const StickerSelect = stickerPlugin.StickerSelect;
export default class SimpleMentionEditor extends Component {
state = {
editorState: EditorState.createEmpty(),
};
onChange = (editorState) => {
this.setState({
editorState,
});
};
focus = () => {
this.editor.focus();
};
render() {
return (
<div>
<div className={editorStyles.editor} onClick={this.focus}>
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
plugins={plugins}
ref={(element) => {
this.editor = element;
}}
/>
</div>
<div className={editorStyles.options}>
<StickerSelect editor={this} />
</div>
</div>
);
}
}
import { fromJS } from 'immutable';
const stickers = fromJS({
data: {
'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b': {
id: 'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b',
url: '../images/unicorn-4.png',
},
'adec3f13-823c-47c3-b4d1-be4f68dd9d6d': {
id: 'adec3f13-823c-47c3-b4d1-be4f68dd9d6d',
url: '../images/unicorn-1.png',
},
'e14b5a20-1025-4952-b731-41cd4b118ba0': {
id: 'e14b5a20-1025-4952-b731-41cd4b118ba0',
url: '../images/unicorn-6.png',
},
'659a0dbf-5f85-4f32-999d-eb9ba6b0f417': {
id: '659a0dbf-5f85-4f32-999d-eb9ba6b0f417',
url: '../images/unicorn-2.png',
},
'fab0ae95-ae95-4775-b484-72c290437602': {
id: 'fab0ae95-ae95-4775-b484-72c290437602',
url: '../images/unicorn-5.png',
},
'71853190-8acd-4d3b-b4fd-63f7b0648daa': {
id: '71853190-8acd-4d3b-b4fd-63f7b0648daa',
url: '../images/unicorn-3.png',
},
},
});
export default stickers;
.editor {
box-sizing: border-box;
border: 1px solid #ddd;
cursor: text;
padding: 16px;
border-radius: 2px;
margin-bottom: 2em;
box-shadow: inset 0px 1px 8px -3px #ABABAB;
background: #fefefe;
}
.editor :global(.public-DraftEditor-content) {
min-height: 140px;
}
.options {
margin-bottom: 20px;
}
import React, { Component } from 'react';
import { EditorState } from 'draft-js';
import Editor from '@draft-js-plugins/editor';
import createStickerPlugin from '@draft-js-plugins/sticker';
import editorStyles from './editorStyles.module.css';
import stickers from './stickers';
const stickerPlugin = createStickerPlugin({ stickers });
const plugins = [stickerPlugin];
const StickerSelect = stickerPlugin.StickerSelect;
export default class CustomStickerEditor extends Component {
state = {
editorState: EditorState.createEmpty(),
};
onChange = (editorState) => {
this.setState({
editorState,
});
};
focus = () => {
this.editor.focus();
};
render() {
return (
<div>
<div className={editorStyles.editor} onClick={this.focus}>
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
plugins={plugins}
ref={(element) => {
this.editor = element;
}}
/>
</div>
<div className={editorStyles.options}>
<StickerSelect editor={this} />
</div>
</div>
);
}
}
import { fromJS } from 'immutable';
const stickers = fromJS({
data: {
'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b': {
id: 'b3aa388f-b9f4-45b0-bba5-d92cf2caa48b',
url: '../images/unicorn-4.png',
},
'adec3f13-823c-47c3-b4d1-be4f68dd9d6d': {
id: 'adec3f13-823c-47c3-b4d1-be4f68dd9d6d',
url: '../images/unicorn-1.png',
},
'e14b5a20-1025-4952-b731-41cd4b118ba0': {
id: 'e14b5a20-1025-4952-b731-41cd4b118ba0',
url: '../images/unicorn-6.png',
},
'659a0dbf-5f85-4f32-999d-eb9ba6b0f417': {
id: '659a0dbf-5f85-4f32-999d-eb9ba6b0f417',
url: '../images/unicorn-2.png',
},
'fab0ae95-ae95-4775-b484-72c290437602': {
id: 'fab0ae95-ae95-4775-b484-72c290437602',
url: '../images/unicorn-5.png',
},
'71853190-8acd-4d3b-b4fd-63f7b0648daa': {
id: '71853190-8acd-4d3b-b4fd-63f7b0648daa',
url: '../images/unicorn-3.png',
},
},
});
export default stickers;
.editor {
box-sizing: border-box;
border: 1px solid #ddd;
cursor: text;
padding: 16px;
border-radius: 2px;
margin-bottom: 2em;
box-shadow: inset 0px 1px 8px -3px #ABABAB;
background: #fefefe;
}
.editor :global(.public-DraftEditor-content) {
min-height: 140px;
}
.options {
margin-bottom: 20px;
}