Reputation: 155
I'm developing a real time editor with ace and call editor.setValue() when ever there is a change in the editor and the cursor positions itself at random positions below the text. What I want is pretty obvious, the cursor to position exactly where the text ends after the set value loads the new texts. Any ideas??
Upvotes: 0
Views: 598
Reputation: 24169
that is not very simple at all, because if the text have changed the same position
will not be the same anymore.
if you want to restore the same selection, you can use toJSON method
before = editor.selection.toJSON();
editor.setValue(editor.getValue() + "xxx")
editor.selection.fromJSON(before)
but that in addition to being wasteful for performance, will not work correctly in the case when lines are shifted e.g. editor.setValue("xxx\n" + editor.getValue())
In general it is a better to use session.insert/session.remove methods
editor.session.insert({row: 0, column: 0}, "xxx\n")
editor.session.remove({start: {row: 0, column: 1}, end: {row: 1, column: 1}})
Upvotes: 2