React Draft-js Block Insert

3

工作原理:用户按下空格键后,将查询特定单词的Draft-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中。我遇到了以下两个困难:

  • 我需要在文本结束后插入一个空格。
  • 光标跳回到文档的开头,但仍停留在通过元素创建的新块内。我需要它在新块外继续编辑并在文本末尾。

我已经在网上搜索了一圈,但没有运气,任何帮助都将不胜感激。

谢谢!


请编辑问题,将其限制为具有足够细节以确定充分答案的特定问题。避免同时提出多个不同的问题。 同时,请修复您的标题,使其描述问题或疑问。 - Rob
1个回答

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 
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接