代码之家  ›  专栏  ›  技术社区  ›  Elumalai Kaliyaperumal

React draft wysiwyg-清除editorState后无法在编辑器中键入文本

  •  1
  • Elumalai Kaliyaperumal  · 技术社区  · 6 年前

    我正在尝试在某些操作完成后重置编辑器内容,使用 react-draft-wysiwyg 编辑使用清除所有内容 clearEditorContent 方法来自 draftjs-utils . 但在清除内容后,我无法在编辑器中键入任何内容添加了下面的代码请帮忙解决这个问题。

    import React, { Component } from 'react';
    import { EditorState, convertToRaw, ContentState } from 'draft-js';
    import { clearEditorContent } from 'draftjs-utils'
    import { Editor } from 'react-draft-wysiwyg';
    import '../../../../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css';
    import draftToHtml from 'draftjs-to-html';
    import htmlToDraft from 'html-to-draftjs';
    
    export default class RTEditor extends Component {
      constructor(props) {
        super(props);
        this.state = {
          editorState: EditorState.createEmpty(),
        };
        this.setDomEditorRef = ref => this.domEditor = ref;
      }
    
      onEditorStateChange: Function = (editorState) => {
        this.setState({
          editorState,
        }, () => {
          this.props.sendResult(draftToHtml(convertToRaw(this.state.editorState.getCurrentContent())));
        });
      };
    
      componentWillReceiveProps(nextProps) {
        if(nextProps.reset) {
          this.reset();
        }
      }
    
      componentDidMount() {
        if(this.props.text) {
          const html = `${this.props.text}`;
          const contentBlock = htmlToDraft(html);
          if (contentBlock) {
            const contentState = ContentState.createFromBlockArray(contentBlock.contentBlocks);
            const editorState = EditorState.createWithContent(contentState);
            this.setState({ editorState, });
          }
        }
    
        this.domEditor.focusEditor();
      }
    
      reset = () => {
          let {editorState} = this.state;
          editorState = clearEditorContent(editorState);
          this.setState({ editorState });
      };
    
      render() {
        const { editorState } = this.state;
        return (
          <Editor
            ref={this.setDomEditorRef}
            editorState={editorState}
            wrapperClassName="rte-wrapper"
            editorClassName="rte-editor"
            onEditorStateChange={this.onEditorStateChange}
            toolbarCustomButtons={[this.props.UploadHandler]}
          />
        )
      }
    }
    

    我的父组件代码如下,

    import React, { Component } from 'react'
    import { addThreadPost } from '../../../api/thread-api'
    import { isEmpty } from '../../../api/common-api'
    import RTEditor from './Loadable'
    
    export default class ThreadActivity extends Component {
        constructor(props) {
            super(props)
            this.state = {
                clearEditor: false,
                threadPost: ''
            }
        }
    
        setPost = (post) => {
            this.setState({ threadPost: post })
        }
    
        addThreadPost = () => {
            let postText = this.state.threadPost.replace(/<[^>]+>/g, '');
            if(!isEmpty(postText)) {
                addThreadPost(this.props.match.params.id_thread, this.state.threadPost, this.state.postAttachments).then(response => {
                    this.setState({ 
                        clearEditor: true,
                        postAttachments: [],
                    })
                });
            }
            else {
                alert("Please enter some text in box.");
            }
        }
    
        render() {
            return [
                <div className="commentbox-container">
                    <div className="form-group">
                        <div className="form-control">
                            <RTEditor 
                                ref={node => { this.threadPost = node }} 
                                text={""} 
                                sendResult={this.setPost.bind(this)}
                                reset={this.state.clearEditor}
                            />
                            <button className="btn-add-post" onClick={this.addThreadPost}>Add Post</button>
                        </div>
                    </div>
                </div>
            ]
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Tadas Antanavicius    6 年前

    你的问题可能是一旦你确定 ThreadActivity state.clearEditor true ,你从未将其设置为false所以你的 this.reset() 每次组件收到道具时都会被调用顺便说一句,每次你试着输入的时候都是这样的,因为你调用的是 this.props.sendResult .

    最简单的解决方法是确保你改变 状态.clearEditor 结算完成后返回false。

    添加到ThreadActivity.js:

    constructor(props) {
      ...
      this.completeClear = this.completeClear.bind(this);
    }
    
    ...
    
    completeClear = () => {
      this.setState({clearEditor: false});
    }
    
    render() {
      ...
      <RTEditor
       ...
       completeClear={this.completeClear}
      />
      ...
    }

    在RTEditor.js中:

    reset = () => {
        let {editorState} = this.state;
        editorState = clearEditorContent(editorState);
        this.setState({ editorState });
        this.props.completeClear();
    };