lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Bug: When using an AOSP-based keyboard, the cursor jumps back to the start when you press at the end of a line

Open amanharwara opened this issue 3 years ago • 8 comments

Lexical version: 0.12.4

Steps To Reproduce

  1. Use an AOSP-based keyboard like OpenBoard. For some of our users it happens using Gboard too.
  2. Tap somewhere in the middle of the line first
  3. Tap somewhere in the whitespace at the end of that same line

Seems to happen when the next block is a different block type than the current one.

Link to code example: https://playground.lexical.dev

The current behavior

https://github.com/facebook/lexical/assets/8348101/5d190ac4-2d52-454d-b5ab-8c54555697d4

If your cursor is inside that line first and then you tap at the whitespace at the end of it, the cursor jumps back to the start. This doesn't happen if your cursor was in a different block. It also only seems to happen when the block type of the next block is different from the current one.

The expected behavior

Tapping in the whitespace at the end of a line should bring & keep the cursor at the end of that line

amanharwara avatar Nov 22 '23 08:11 amanharwara

Did some more testing and now I'm not sure if the block type of the next block has anything to do with it. See:

https://github.com/facebook/lexical/assets/8348101/0aeba7d9-8be8-4e75-ac71-ed3df8aa9af8

amanharwara avatar Nov 22 '23 11:11 amanharwara

It seems like this has something to do with the ZERO_WIDTH_SPACE that is added as a suffix when composing. The compositionstart event seems to be firing any time you tap on a word when using this keyboard. So when you tap at the end, it fires the compositionend event correctly, but then at the same fires the compositionstart event at the zero width space. And this for some reason is causing the cursor to jump to the start. This happens only in nodes where there is only one data-lexical-text child, and not in nodes which have multiple data-lexical-text children as the composition suffix doesn't seem to be added at the end for those.

amanharwara avatar Dec 06 '23 16:12 amanharwara

For me I'm only able to get this to happen when using the latest version of OpenBoard, or an old version of Gboard. When I use the latest version of Gboard it doesn't happen for me. However, for one of our users, this happens on multiple devices even with the latest version of Gboard.

amanharwara avatar Dec 06 '23 16:12 amanharwara

Looking into it more, I think the reason why the cursor seems to jump to the start is because when the compositionend event fires, the zero width space is removed so it can't keep the selection there anymore. The cursor does not jump if you either always have the composition suffix at the end or never have the composition suffix at the end so the selection doesn't become "invalid" so to speak.

amanharwara avatar Dec 06 '23 17:12 amanharwara

This might also be a Chromium-specific issue because I'm not able to replicate it on Firefox Android with any keyboard. Looks like this is because on Firefox, the compositionstart event seems to be working as intended and not firing on every tap.

amanharwara avatar Dec 06 '23 22:12 amanharwara

@acywatson any idea on how this can be fixed, or at least patched on our end?

amanharwara avatar Dec 30 '23 08:12 amanharwara

Looks like it also happens with yandex keyboard

vchirikov avatar Jul 21 '25 18:07 vchirikov

Tested on current main with HeliBoard (AOSP-based keyboard) on a Galaxy device — tapping mid-word then tapping at end of line no longer causes the cursor to jump. This is likely resolved by PR #7725 which clamps DOM selection offsets to valid TextNode bounds in $internalResolveSelectionPoint, preventing the offset mismatch that occurred when the ZWSP composition suffix was added/removed.

Note: tested with HeliBoard, not the original OpenBoard — if anyone can still reproduce with OpenBoard or another AOSP keyboard, please reopen.

mayrang avatar Jun 27 '26 13:06 mayrang