Set the cursor to the click location when editing a text field.
Review Request #13207 — Created Aug. 13, 2023 and submitted
Historically, when clicking a text field on a review request to begin
editing, the cursor position would be set to wherever the browser
chooses to set it. That meant if you wanted to edit a specific part of
the text, you had to click twice.
We now preserve the client X/Y coordinates of a mouse click in
InlineEditorViewand pass this along to the
subclass for connecting a
TextEditorViewnow passes this along to set
the cursor position.
For CodeMirror, this is fairly straight-forward. We can ask CodeMirror
to convert this into a line number/character offset, and then set that.
<textarea>s, a different approach is needed.
Browsers sort of have some functionality to help with this, but it's not
perfect. You can set a selection point, which is a single integer
covering a range between 0..length of the text. However, there are two
functions that can now convert a client X and Y coordinate to a
Document.caretPositionFromPoint: Standard but only on Firefox
Document.caretRangeFromPoint: Non-standard but on Chrome, Safari,
These are both error-prone in their own ways when used on a
(all documented in the code), so to do this correctly, a proxy element
<pre> must be created, carefully styled, and then used to get
the caret position.
By implementing these approaches, we now get a very smooth, natural
opening experience, where the user can begin typing exactly where they
Tested on Firefox, Chrome, and Safari.
Tested with some complex documents, with bullet points, wrapping, and
other formatting. In complex documents, as content shifts, the cursor
position may not be on the exact character the user wanted to click,
but it'll always be on the X/Y coordinate.
Removed an excess blank line.
Revision 2 (+416 -8)
Checks run (2 succeeded)