Fix pasting content with newlines on Android #5359
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This is another potential solution for #5190 (see also #5195).
The root issue is with how text with newlines is handled on Android. If an InputEvent's data is a simple string containing newlines, the event handler inserts a soft break and then drops the rest of the event data.
slate/packages/slate-react/src/hooks/android-input-manager/android-input-manager.ts
Lines 586 to 590 in 1d8010b
This PR updates the InputEvent handling code on Android so that when newlines are involved, the insertion is broken into single-line text insertions with soft breaks between them. It also makes some minor type improvements around the affected code.
Issue
Fixes: #5190
Example
Context
#5195 fixed this issue by scheduling an
insertText
for text containing newlines rather than updating the diff. This PR is similar, but it replaces any newlines with soft breaks. It also strips the trailing newline that Android appends to clipboard text that contains newlines. This approach seemed reasonable given its simplicity, and given that inserts involving newlines are much less common than inserts without newlines.This fix has been tested on a Pixel 4 running Android 12 and a couple of Samsung Galaxies running Android 12 and 13, with Gboard, Swiftkey, and Samsung keyboard. Copying and then pasting text within a paragraph, after a paragraph, and pasting over a text selection all appear to work as expected, whether the copied text contains newlines or not.
In a comment, @BitPhinix suggested there might be issues with this general approach with certain IMEs when inserting newlines in paragraphs, but I've been unable reproduce this behavior. The keyboards I've tested generate
insertParagraph
events (Gboard, Swiftkey, Samsung) when pressing enter at the beginning of a word or within a word, orinsertCompositionText
events (Gboard, Swiftkey) when the return key is pressed (the Samsung keyboard still generates aninsertParagraph
for this case). An example that behaves differently would be very useful for testing.Checks
yarn test
.yarn lint
. (Fix errors withyarn fix
.)yarn start
.)yarn changeset add
.)