Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pasting content with newlines on Android #5359

Merged
merged 4 commits into from
Mar 16, 2023

Conversation

jason0x43
Copy link
Contributor

@jason0x43 jason0x43 commented Mar 14, 2023

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.

if (typeof data === 'string' && data.includes('\n')) {
return scheduleAction(() => Editor.insertSoftBreak(editor), {
at: Range.end(targetRange),
})
}

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

Current behavior New behavior
current behavior new behavior

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, or insertCompositionText events (Gboard, Swiftkey) when the return key is pressed (the Samsung keyboard still generates an insertParagraph for this case). An example that behaves differently would be very useful for testing.

Checks

  • The new code matches the existing patterns and styles.
  • The tests pass with yarn test.
  • The linter passes with yarn lint. (Fix errors with yarn fix.)
  • The relevant examples still work. (Run examples with yarn start.)
  • You've added a changeset if changing functionality. (Add one with yarn changeset add.)

@changeset-bot
Copy link

changeset-bot bot commented Mar 14, 2023

🦋 Changeset detected

Latest commit: a5ef299

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
slate-react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Collaborator

@dylans dylans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine, needs a changeset (set change level as patch) and then we can land it.

@jason0x43
Copy link
Contributor Author

Changeset added

@dylans dylans merged commit 9825d29 into ianstormtaylor:main Mar 16, 2023
@github-actions github-actions bot mentioned this pull request Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android] In some cases, the content pasted from the clipboard is empty.
2 participants