similiquedicta
Version:
A Plugin Architecture on top of Draft.JS
102 lines (89 loc) • 2.25 kB
JavaScript
import React, { useRef, useState } 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.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 */
const SimpleResizeableEditor = () => {
const [editorState, setEditorState] = useState(
EditorState.createWithContent(convertFromRaw(initialState))
);
const editor = useRef();
const onChange = (value) => {
setEditorState(value);
};
const focus = () => {
editor.current.focus();
};
return (
<div>
<div className={editorStyles.editor} onClick={focus}>
<Editor
editorState={editorState}
onChange={onChange}
plugins={plugins}
ref={(element) => {
editor.current = element;
}}
/>
</div>
</div>
);
};
export default SimpleResizeableEditor;