diff --git a/website/src/components/DraftEditorExample/index.js b/website/src/components/DraftEditorExample/index.js index 06c7ea1b01..96ce378da9 100644 --- a/website/src/components/DraftEditorExample/index.js +++ b/website/src/components/DraftEditorExample/index.js @@ -6,111 +6,102 @@ */ import React from 'react'; -import Link from '@docusaurus/Link'; -import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; -import useBaseUrl from '@docusaurus/useBaseUrl'; import {Editor, EditorState, RichUtils, getDefaultKeyBinding} from 'draft-js'; -import classnames from 'classnames'; - -import Layout from '@theme/Layout'; import './css/example.css'; import './css/draft.css'; import './css/rich-editor.css'; -class RichEditorExample extends React.Component { - constructor(props) { - super(props); - this.state = {editorState: EditorState.createEmpty()}; - - this.focus = () => this.editor.focus(); - this.onChange = editorState => this.setState({editorState}); - - this.handleKeyCommand = this._handleKeyCommand.bind(this); - this.mapKeyToEditorCommand = this._mapKeyToEditorCommand.bind(this); - this.toggleBlockType = this._toggleBlockType.bind(this); - this.toggleInlineStyle = this._toggleInlineStyle.bind(this); - } - - _handleKeyCommand(command, editorState) { - const newState = RichUtils.handleKeyCommand(editorState, command); - if (newState) { - this.onChange(newState); - return true; - } - return false; - } +const {useState, useRef, useCallback} = React; - _mapKeyToEditorCommand(e) { - switch (e.keyCode) { - case 9: // TAB - const newEditorState = RichUtils.onTab( - e, - this.state.editorState, - 4 /* maxDepth */, - ); - if (newEditorState !== this.state.editorState) { - this.onChange(newEditorState); - } - return; - } - return getDefaultKeyBinding(e); - } +function RichEditorExample(props) { + const [editorState, setEditorState] = useState(EditorState.createEmpty()); + const editor = useRef(null); - _toggleBlockType(blockType) { - this.onChange(RichUtils.toggleBlockType(this.state.editorState, blockType)); - } + const focus = () => { + if (editor.current) editor.current.focus(); + }; - _toggleInlineStyle(inlineStyle) { - this.onChange( - RichUtils.toggleInlineStyle(this.state.editorState, inlineStyle), - ); - } + const handleKeyCommand = useCallback( + (command, editorState) => { + const newState = RichUtils.handleKeyCommand(editorState, command); + if (newState) { + setEditorState(newState); + return 'handled'; + } + return 'not-handled'; + }, + [editorState, setEditorState], + ); - render() { - const {editorState} = this.state; - - // 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'; + 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 ( -