How to bring the cursor to the start of the editor?

1 Answer 94 Views
Editor
Pranav
Top achievements
Rank 1
Pranav asked on 14 Feb 2022, 02:21 PM

Is it possible to get the cursor to the start of the editor once it is focused? 

I looked at the API and could not find a reliable manner of doing so.

My current use case is that I need to prefill the editor with (possibly) a lot of content. Once filled, I would need to bring the cursor back to the very start of the editor. 

Thanks in advance!

Shashank S
Top achievements
Rank 1
Iron
commented on 16 Feb 2022, 08:57 AM | edited

Hi Pranav,

The following code worked for me.

1 Answer, 1 is accepted

Sort by
0
Accepted
Shashank S
Top achievements
Rank 1
Iron
answered on 16 Feb 2022, 08:59 AM

Hi Pranav,

The following code worked for me.

public moveCursorToStart() {
    setTimeout(() => {
      const newSelection = new TextSelection(this.editor.view.state.doc.resolve(0));
      this.editor.view.dispatch(this.editor.view.state.tr.setSelection(newSelection));
    }, 0);
}
 
Tags
Editor
Asked by
Pranav
Top achievements
Rank 1
Answers by
Shashank S
Top achievements
Rank 1
Iron
Share this question
or