📜  草稿js插入文本示例 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:03:43.839000             🧑  作者: Mango

代码示例1
// get current editor state 
    const currentContent = editorState.getCurrentContent();

 // create new selection state where focus is at the end
    const blockMap = currentContent.getBlockMap();
    const key = blockMap.last().getKey();
    const length = blockMap.last().getLength();
    const selection = new SelectionState({
        anchorKey: key,
        anchorOffset: length,
        focusKey: key,
        focusOffset: length,
      });

   //insert text at the selection created above 
    const textWithInsert = Modifier.insertText(currentContent, selection, 'text to be inserted', null);
    const editorWithInsert = EditorState.push(editorState, textWithInsert, 'insert-characters');

    //also focuses cursor at the end of the editor 
    const newEditorState = EditorState.moveSelectionToEnd(editorWithInsert, textWithInsert.getSelectionAfter());
    setEditorState(newEditorState);