similiquedicta
Version:
A Plugin Architecture on top of Draft.JS
51 lines (44 loc) • 1.36 kB
JavaScript
import React, { Component } from 'react';
import Editor, { createEditorStateWithText } from '@draft-js-plugins/editor';
import createSideToolbarPlugin from '@draft-js-plugins/side-toolbar';
import editorStyles from './editorStyles.module.css';
const sideToolbarPlugin = createSideToolbarPlugin();
const { SideToolbar } = sideToolbarPlugin;
const plugins = [sideToolbarPlugin];
const text =
'Once you click into the text field the sidebar plugin will show up …';
export default class SimpleSideToolbarEditor extends Component {
state = {
editorState: createEditorStateWithText(text),
};
componentDidMount() {
// fixing issue with SSR https://github.com/facebook/draft-js/issues/2332#issuecomment-761573306
// eslint-disable-next-line react/no-did-mount-set-state
this.setState({
editorState: createEditorStateWithText(text),
});
}
onChange = (editorState) => {
this.setState({
editorState,
});
};
focus = () => {
this.editor.focus();
};
render() {
return (
<div className={editorStyles.editor} onClick={this.focus}>
<Editor
editorState={this.state.editorState}
onChange={this.onChange}
plugins={plugins}
ref={(element) => {
this.editor = element;
}}
/>
<SideToolbar />
</div>
);
}
}