Scroll position jumping to end of long document after setTextInRange

I have a reproducible issue here with undesirable scrolling after inserting text programmatically. I’m on latest iPadOS running on an iPad Pro 12.9, latest Drafts (20.1), and the device is in landscape orientation. To reproduce:

  1. Create a long document. Doesn’t matter what the text is, as long as it’s more than will fit in the visible part of the editor all at once. Put the insertion point at the start of the document, so you’re scrolled to the top.

  2. Create an action with a keyboard shortcut, and the following javascript:

    editor.setTextInRange(10, 0, “NEW TEXT”);
    editor.setSelectedRange(10,0);

  3. Trigger the keyboard shortcut for your action.

On my machine, 100% reproducibly, the action runs as desired (inserting the text at the proper place, and setting the insertion point position properly), BUT it also scrolls the viewport to the very bottom of the document. If I then start typing, the viewports jumps back to the top again, because that’s where the insertion point is.

Any thoughts on what’s going wrong?

Thanks for the details. I was aware this was happening, but did not have a reproducible case - will see what I can figure out.

2 Likes

A workaround for this problem is to not use setTextInRange(), and instead to first use setSelectedRange() to select the target location for the new text, then just use setSelectedText() directly. This doesn’t exhibit the bug with moving the scroll position.

FYI, this issue should be resolved in the current next beta (12.2.1) as well.

2 Likes

This is indeed fixed in the current beta, but I notice that the scroll-position jump still happens if you then trigger Undo after running an action with a setSelectedRange call, as described above.