How to - Insert Cursor At The End of Inserted Text

I have a scripted action that ends with editor.setSelectedText(AD);

When the text is inserted, the focus is at the top of the (long) inserted text. How do I tell Drafts to insert the cursor or focus at the bottom of the newly inserted text?

For the bottom

editor.setSelectedRange(editor.getText().length, 0);

For after the newly inserted text, you would need to get the cursor position (check the editor documentation to find the function) before (store it) and add the length of the text being inserted, then use the same set range function to place it.

Hmm, the problem is that the text being inserted is generated from a RegEx search, which changes all the time.

The heading that follows after the inserted text in my document is static. Is there perhaps a way to do a search and have the cursors inserted based on found text?

Really? Does it not get set as a particular length when it is inserted? i.e. the text you choose varies in length until the point you choose to insert it, and at that point it is fixed.

I can’t believe you cannot get the length of the text to insert and the position of the cursor at the moment you need to insert the text.

Yes. You could insert text before a header. It would be as exasy as replacing that header with your text to insert, plus the header. Of course, that is based on the assumption that the header is unique.

If it is non-unique, you will have to build some logic to traverse the structure of your note. You can do that with string splits for example.

I’m not sure I follow what you are trying to do, but I think you just want the cursor to be placed at the end of the text you just inserted, right? If so…

// get the current range before making changes
let [start, length] = editor.getSelectedRange()
// set your text
// update the cursor to be at the end of that text
// by adding the length of it to the start position
// of the old range
editor.setSelectedRange(start + AD.length, 0)

That works. Thank you.