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

Fixed autocorrect causing dupe text #655

Merged
merged 5 commits into from
Mar 23, 2017

Conversation

lxcid
Copy link
Collaborator

@lxcid lxcid commented Mar 7, 2017

For #540

Here's an explanation of what the following codes try to achieve.

During autocorrection (in iOS’s Safari), onSelect event is triggered after onBeforeInput event.
The plugins/core updates the state during onBeforeInput event, thus causing selection triggered by autocorrect to be lost/overridden.
This behaviour caused dupe text bug during autocorrection.

To overcome this issue, we try to query the selection and conditionally fix out of sync cases with an additional transform before inserting the text.

The con of this fix is that it adds an additional query to native selection per text input.

I choose this solution because it is less destructive to existing execution flow.

Feel free to reject or rewrite if this isn't the right way to fix this.

lxcid added 2 commits March 7, 2017 20:00
During autocorrection (in iOS’s Safari), `onSelect` event is triggered after `onBeforeInput` event.
The plugins/core updates the state during `onBeforeInput` event, thus causing selection triggered by autocorrect to be lost/overridden.
This behaviour caused dupe text bug during autocorrection.

To overcome this issue, we try to query the selection and conditionally fix out of sync cases with an additional transform before inserting the text.
Copy link
Owner

@ianstormtaylor ianstormtaylor left a comment

Choose a reason for hiding this comment

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

@lxcid thank you for this! I'm sorry for the delay. This is a seriously amazing pull request, with absolutely great research. Thank you for putting in the time!

I left a few comments inline, nothing big just little tweaks for our future selves.

I agree that it's unfortunate that we need to add more processing on every input, but it seems necessary.

Question for you: if we were to only trigger this extra processing when the native selection was expanded, would that have any negative effects? It seems like that could get around having to trigger it for 99% of the cases where people are just typing individual characters. And might be good for performance.

@@ -148,24 +148,7 @@ class Content extends React.Component {

getPoint(element, offset) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can we remove this #getPoint method entirely now, and just use the new utility function that you created inline in each of the places?

@@ -97,9 +98,35 @@ function Plugin(options = {}) {

const chars = initialChars.insert(startOffset, char)

let nextTransform = state.transform()
Copy link
Owner

Choose a reason for hiding this comment

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

Can you change this variable to just be called transform instead of nextTransform?

let nextTransform = state.transform()

// Determine if selection is out of sync, which can happen during autocorrect,
// and update accordingly.
Copy link
Owner

Choose a reason for hiding this comment

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

Can you change this comment to:

// COMPAT: In iOS, when choosing from the predictive text suggestions, the
// native selection will be changed to span the existing word, so that the word
// is replaced. But the `select` event for this change doesn't fire until after
// the `beforeInput` event, even though the native selection is updated. So we
// need to manually adjust the selection to be in sync. (03/18/2017)

@lxcid
Copy link
Collaborator Author

lxcid commented Mar 23, 2017

Hey, thanks for reviewing! I've made the necessary changes!

Question for you: if we were to only trigger this extra processing when the native selection was expanded, would that have any negative effects? It seems like that could get around having to trigger it for 99% of the cases where people are just typing individual characters. And might be good for performance.

I think that's a great improvement. It do sounds like a reasonable hypothesis, I couldn't think of a scenario where autocorrect would perform without expanding the selection.

So we just had to check for native.isCollapsed and trigger the logic when it is false. I made this change at f4aa680.

I think that is all of it! 😃

@lxcid
Copy link
Collaborator Author

lxcid commented Mar 23, 2017

Unfortunately, f4aa680 caused Android to regress and the autocorrect to work intermittently. 😞 Without f4aa680, Android is usable (autocorrect works), although it has it quirks (Some repeats may happen after the cursor at times, which will disappears as you continue typing at times as well, weird). I decides to remove f4aa680 from this PR.

@lxcid lxcid force-pushed the bug/autocorrect branch from f4aa680 to ef441f0 Compare March 23, 2017 14:18
@ianstormtaylor
Copy link
Owner

Ah damn, alright well we'll just have to keep an eye in the performance and if it becomes a problem we can figure something out!

Thanks @lxcid!

@ianstormtaylor ianstormtaylor merged commit a28075e into ianstormtaylor:master Mar 23, 2017
yalu pushed a commit to yalu/slate that referenced this pull request Apr 16, 2017
* Refactored out `getPoint` from components/content to utils so as to make it more reusable.

* Fixed autocorrect causing dupe text. (ianstormtaylor#540)

During autocorrection (in iOS’s Safari), `onSelect` event is triggered after `onBeforeInput` event.
The plugins/core updates the state during `onBeforeInput` event, thus causing selection triggered by autocorrect to be lost/overridden.
This behaviour caused dupe text bug during autocorrection.

To overcome this issue, we try to query the selection and conditionally fix out of sync cases with an additional transform before inserting the text.

* Removes Content#getPoint and use the new utility function instead.

* Renames local variable nextTransform to transform.

* Describe the solution to the autocorrect issue in a more descriptive manner.
XuHaoJun added a commit to XuHaoJun/slate that referenced this pull request May 15, 2017
@lxcid lxcid deleted the bug/autocorrect branch May 24, 2017 09:33
XuHaoJun added a commit to XuHaoJun/slate that referenced this pull request May 31, 2017
XuHaoJun added a commit to XuHaoJun/slate that referenced this pull request May 31, 2017
XuHaoJun added a commit to XuHaoJun/slate that referenced this pull request Jun 8, 2017
XuHaoJun added a commit to XuHaoJun/slate that referenced this pull request Jun 10, 2017
XuHaoJun added a commit to XuHaoJun/slate that referenced this pull request Jun 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants