in website/src/components/DraftEditorExample/index.js [17:105]
function RichEditorExample(props) {
const [editorState, setEditorState] = useState(EditorState.createEmpty());
const editor = useRef(null);
const focus = () => {
if (editor.current) editor.current.focus();
};
const handleKeyCommand = useCallback(
(command, editorState) => {
const newState = RichUtils.handleKeyCommand(editorState, command);
if (newState) {
setEditorState(newState);
return 'handled';
}
return 'not-handled';
},
[editorState, setEditorState],
);
const mapKeyToEditorCommand = useCallback(
e => {
switch (e.keyCode) {
case 9: // TAB
const newEditorState = RichUtils.onTab(
e,
editorState,
4 /* maxDepth */,
);
if (newEditorState !== editorState) {
setEditorState(newEditorState);
}
return null;
}
return getDefaultKeyBinding(e);
},
[editorState, setEditorState],
);
// If the user changes block type before entering any text, we can
// either style the placeholder or hide it. Let's just hide it now.
let className = 'RichEditor-editor';
var contentState = editorState.getCurrentContent();
if (!contentState.hasText()) {
if (
contentState
.getBlockMap()
.first()
.getType() !== 'unstyled'
) {
className += ' RichEditor-hidePlaceholder';
}
}
return (
<div className="RichEditor-root">
<BlockStyleControls
editorState={editorState}
onToggle={blockType => {
const newState = RichUtils.toggleBlockType(editorState, blockType);
setEditorState(newState);
}}
/>
<InlineStyleControls
editorState={editorState}
onToggle={inlineStyle => {
const newState = RichUtils.toggleInlineStyle(
editorState,
inlineStyle,
);
setEditorState(newState);
}}
/>
<div className={className} onClick={focus}>
<Editor
blockStyleFn={getBlockStyle}
customStyleMap={styleMap}
editorState={editorState}
handleKeyCommand={handleKeyCommand}
keyBindingFn={mapKeyToEditorCommand}
onChange={setEditorState}
placeholder="Tell a story..."
ref={editor}
spellCheck={true}
/>
</div>
</div>
);
}