Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

How to append text to the end #58

Closed
ufo22940268 opened this issue Feb 24, 2016 · 5 comments
Closed

How to append text to the end #58

ufo22940268 opened this issue Feb 24, 2016 · 5 comments

Comments

@ufo22940268
Copy link

  _insertText() {
    const editorState = this.state.editorState;
    const selection = editorState.getSelection();
    const contentState = editorState.getCurrentContent();
    const ncs = Modifier.insertText(contentState, selection, "wefoiwjoefjwoiefjoiwjefio");
    const es = EditorState.push(editorState, ncs, 'insert-fragment');
    this.setState(es);
  }

With above codes, i can't get the wef.... to be append to the end. I have work on this problem for hours, but still can't found the solution. The above code is written according to Tex sample.

@ufo22940268
Copy link
Author

It's not a bug.

@ufo22940268
Copy link
Author

I found the solution.

My init method is

  getInitialState() {
    return {
      editorState: EditorState.createEmpty()
    };
  },

So when i want to update editorState i need to call this.setState({editorState: es}), not this.setState(es). Really love this framework, thank you.

@sibelius
Copy link

how to append to the end?

@jawblet
Copy link

jawblet commented Jun 16, 2021

For folks who found this thread from Google but are not having the state issue above—how to append text to the end:

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

@chenjiashrimp
Copy link

  _insertText() {
    const editorState = this.state.editorState;
    const selection = editorState.getSelection();
    const contentState = editorState.getCurrentContent();
    const ncs = Modifier.insertText(contentState, selection, "wefoiwjoefjwoiefjoiwjefio");
    const es = EditorState.push(editorState, ncs, 'insert-fragment');
    this.setState(es);
  }

With above codes, i can't get the wef.... to be append to the end. I have work on this problem for hours, but still can't found the solution. The above code is written according to Tex sample.

my code is the same as yours, but only can insert the text once, i don't know why

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants