Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Selecting a single character then typing it doesn't replace it #398

Closed
gdehmlow opened this issue May 18, 2016 · 3 comments
Closed

Selecting a single character then typing it doesn't replace it #398

gdehmlow opened this issue May 18, 2016 · 3 comments

Comments

@gdehmlow
Copy link

gdehmlow commented May 18, 2016

I can replicate it on the DraftJS demo page by typing a single character (such as "t"), selecting it, then trying to type "t" again. "t" should get replaced, with the selection collapsing after the "t". Instead, this does not happen until I type "t" again. This does not happen if the character is different.

The editorState passed into onChange seems to think its selection's anchorOffset and focusOffset are the same after entering the character, which would be the correct behavior, but this isn't reflected in the selection itself.

@sophiebits
Copy link
Contributor

Weird bug, thanks for reporting.

@steviesama
Copy link

steviesama commented May 26, 2016

If I understand this correctly, I reproduced it. Select the character and have to key it twice before it replaces it and not happening if the character is different. Seems to me this is maybe something in how the editor detects changes in state. I watched the draft.js talk about how it handles intermediary states between transforms while you are typing and it said that it throws them out.

If this is the case, maybe it doesn't realize that the state has actually changed when you key it in the first time. What seems to support that, is if you select 2 of the same character, like tt and hit t, it will replace it. I think it could be a matter of the selection being in the same state as the state you are trying to transform it to.

@hellendag
Copy link

Nice catch! I've never noticed this before. I believe @steviesama is correct here, and the editor doesn't think the state has changed within the beforeInput handler.

This seems fixable by checking whether the typed character matches the selected character, then simply updating the SelectionState instead of changing the ContentState.

flarnie added a commit to karanjthakkar/draft-js that referenced this issue Oct 14, 2017
**what is the change?:**
When rebasing we accidentally removed a variable and this commit adds it
back.

There was also ~5 lint errors introduced by this PR and this commit
fixes those.

**why make this change?:**
To get CI passing and make this PR mergable.

**test plan:**
`yarn lint && yarn test && flow src`
And did some manual testing.

**issue:**
Related to PR fixing facebookarchive#398
facebook-github-bot pushed a commit that referenced this issue Aug 8, 2018
Summary:
Fixes #1830 (bug introduced in #719; this is a proper fix for #398).

* When we're allowing native insertion, we don't want to modify the selection because doing so blocks browser features like spellcheck from working
* In all other cases when typing, we *do* want to force the selection – failing to do this is why the original issue occurred
* We use the `insert-characters` change type for both native and non-native insertions
* Instead of guessing in `EditorState.push` whether to force a selection, accept it as a parameter
* Gate all changes by `draft_non_native_insertion_forces_selection`

I verified with the feature flag disabled (same behavior as before):

* Typing over a character works, and the cursor moves as expected (because of the `chars === currentlySelectedChars` check)
* With editor contents "ABC\ndef", replacing "d", "e", or "f" with the character above *fails* to replace the char (because that check is buggy).

And with the feature flag enabled (new behavior):

* Typing over a character still works (but does so without the buggy check)
* With editor contents "ABC\ndef", replacing "d", "e", or "f" with the character above *succeeds* at replacing the char.

Reviewed By: flarnie

Differential Revision: D9209691

fbshipit-source-id: f63551dbad689391aa9c2a69f1d6ba395b8bf1ac
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants