Ashit Vora
Ashit Vora

Reputation: 2922

Setting position of cursor in Ace Editor

I have a use case where a user clicks a button to insert text at the current cursor position. But after the text is inserted using session.insert(cursorPosition, textToAdd), the position of cursor moves to the very first character.

I tried placing the cursor after the recently added text by using the following approach but it didn't work.

renderer.scrollCursorIntoView({
    row: cursorPosition.row,
    column: cursorPosition.column + param.length
}, 0.5)

Any idea how to achieve this?

Basically, if a user keeps clicking the button, text to should be added at the current cursor position and then after the recently added text unless user places the cursor somewhere else explicitly.

Upvotes: 6

Views: 7618

Answers (4)

Mathanraj Naidu
Mathanraj Naidu

Reputation: 11

You need to add this line editor.moveCursorLineEnd() after

session.insert(cursorPosition, textToAdd)

Check out this fiddle

Upvotes: 1

Kickass
Kickass

Reputation: 1134

Here is your answer:

ace.edit('editor').moveCursorTo(lineNumber,colNumber);

https://jsfiddle.net/q27ebeeq/130/

Just tested and it works exactly as expected! It even scrolls the cursor into view automatically, although the console warns that this behaviour will change in future versions.

Automatically scrolling cursor into view after selection change this will be disabled in the next version set editor.$blockScrolling = Infinity to disable this message

Hope this fixes your problem.

Upvotes: 5

Harsha pps
Harsha pps

Reputation: 2208

When the user inserts text, you could use goToLine(), which takes in row and column as parameters and the cursor is set at that position.

editor.gotoLine(row, column); 

Upvotes: 1

a user
a user

Reputation: 24104

There is editor.selection.moveTo(row, column) but you do not need to use it after insert. Selection moving to the beginning of the document indicates that something in your code is calling setValue on the editor. (e.g it may be a buggy react component recreating the editor) but that's hard to say for sure without seeing the page with the error

Upvotes: 0

Related Questions