UNPKG

similiquedicta

Version:
103 lines (91 loc) 2.26 kB
import React, { Component } from 'react'; import { convertFromRaw, EditorState } from 'draft-js'; import Editor, { composeDecorators } from '@draft-js-plugins/editor'; import createResizeablePlugin from '@draft-js-plugins/resizeable'; import createFocusPlugin from '@draft-js-plugins/focus'; import createColorBlockPlugin from './colorBlockPlugin'; import editorStyles from './editorStyles.module.css'; const focusPlugin = createFocusPlugin(); const resizeablePlugin = createResizeablePlugin(); const decorator = composeDecorators( resizeablePlugin.decorator, focusPlugin.decorator ); const colorBlockPlugin = createColorBlockPlugin({ decorator }); const plugins = [focusPlugin, resizeablePlugin, colorBlockPlugin]; /* eslint-disable */ const initialState = { entityMap: { 0: { type: 'colorBlock', mutability: 'IMMUTABLE', data: {}, }, }, blocks: [ { key: '9gm3s', text: 'This is a simple example. Hover the block and change the with by dragging the mouse.', type: 'unstyled', depth: 0, inlineStyleRanges: [], entityRanges: [], data: {}, }, { key: 'ov7r', text: ' ', type: 'atomic', depth: 0, inlineStyleRanges: [], entityRanges: [ { offset: 0, length: 1, key: 0, }, ], data: {}, }, { key: 'e23a8', text: 'More text here …', type: 'unstyled', depth: 0, inlineStyleRanges: [], entityRanges: [], data: {}, }, ], }; /* eslint-enable */ export default class SimpleResizeableEditor extends Component { state = { editorState: EditorState.createWithContent(convertFromRaw(initialState)), }; 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> ); } }