React Draft-js块插入

时间:2019-08-06 03:03:55

标签: javascript reactjs react-native frontend draftjs

工作原理: 当用户点击空格键时,将为特定单词查询草稿JS文本内容。然后将该单词的所有实例包装在标签中。文本换行后,HTML会转换回去,并且Draft-JS编辑器状态会更新:

      const convertedFromHTML= convertFromHTML(newHTML);
      const editorState = this.state.editorState;

      // Set Editor and Content States
      const newContentState = ContentState.createFromBlockArray(
        convertedFromHTML.contentBlocks,
        convertedFromHTML.entityMap
      );

      const nextEditorState = EditorState.push(
        editorState,
        newContentState,
        'insert-text'
      );

      this.setState({ 
        editorState: nextEditorState
      });

块渲染图:

const blockRenderMap = Immutable.Map({
  'Atomic': {
    element: 'Atomic' ,
    wrapper: <GoogleCustomBlock />
  }
});

const myBlockStyleFn = (contentBlock) => {
   const type = contentBlock.getType();
   switch (type) {
     case 'atomic': {
      return 'GoogleCustomBlock';
   }
}

自定义阻止组件:

// React Components
import React, { Component } from 'react';

class GoogleCustomBlock extends Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div className='GoogleCustomBlock'>
        {this.props.children}
      </div>
    );
  }
}

导出默认的GoogleCustomBlock;

问题: 当用户点击空格键时,将发生此功能。文本被包装,正确的块被添加到DOM。我遇到以下两个困难:

  • 我需要在文本末尾插入一个空格。
  • 光标跳回到文档的开头,但仍停留在通过元素创建的新块中。我需要它来在新块之外和文本结尾处恢复编辑。

我已经在线搜索过,但到目前为止还没有运气,我们将为您提供任何帮助。

谢谢!

1 个答案:

答案 0 :(得分:0)

解决方案::突出显示文本并在空格键命令上插入空格:

   insertInlineStyles = (editorState, indexes, googleSearchTerm) => {
      const contentState = editorState.getCurrentContent()
      const selectionState = editorState.getSelection();

      // Loop Through indexes //
      const newSelection = selectionState.merge({
        anchorOffset: index[i],
        focusOffset: googleSearchTerm.length
      })

      // Create new Editor State with Selection on Targeted Word //
      const editorStateWithNewSelection = EditorState.forceSelection(editorState, newSelection);

      // Toggle Inline Style //
      const editorStateWithStyles = RichUtils.toggleInlineStyle(editorStateWithNewSelection,'GoogleStyle');

      // Set Selection Back to Previous //
      const editorStateWithStylesAndPreviousSelection = EditorState.forceSelection(
        editorStateWithStyles,
        selectionState
      )

      // Return Editor //
      return editorStateWithStylesAndPreviousSelection;
   }

    /// INSIDE OF ANOTHER FUNCTION

    /// GET INDEX OF WORD TO HIGHLIGHT
    var indexes = [INDEX OF WORD];

    /// INSERT INLINE STYLES
    var newEditorState = this.insertInlineStyles(this.state.editorState, indexes, googleSearchTerm);

    /// ADD SPACE to TEXT
    let newContentState = Modifier.replaceText(
      newEditorState.getCurrentContent(), // New content
      currentState.getSelection(), // End of old content selection
      " " // Text to add
    );
    let finalEditorState = EditorState.push(
      newEditorState, 
      newContentState, 
      'insert-characters'
    );

    this.setState({ 
      editorState: finalEditorState 
    });