Skip to content

Commit

Permalink
Attempt to fix same charcter selection and replacing bug.
Browse files Browse the repository at this point in the history
Summary:
_Before_ submitting a pull request, please make sure the following is done...
1. Fork the repo and create your branch from `master`.
2. If you've added code that should be tested, add tests!
3. If you've changed APIs, update the documentation.
4. Ensure that:
   - The test suite passes (`npm test`)
   - Your code lints (`npm run lint`) and passes Flow (`npm run flow`)
   - You have followed the [testing guidelines](https://github.com/facebook/draft-js/wiki/Testing-for-Pull-Requests)
5. If you haven't already, complete the [CLA](https://code.facebook.com/cla).

Please use the simple form below as a guideline for describing your pull request.

Thanks for contributing to Draft.js!

**Summary**

Fixes #398

**Test Plan**

[...]
Closes facebookarchive/draft-js#719

Differential Revision: D6060888

fbshipit-source-id: c56fad0850a469f8f2dbb06bebad78a67750e47a
  • Loading branch information
karanjthakkar authored and facebook-github-bot committed Oct 16, 2017
1 parent c601ee9 commit 92fdf3f
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/component/handlers/edit/editOnBeforeInput.js
Expand Up @@ -111,21 +111,42 @@ function editOnBeforeInput(
// reduces re-renders and preserves spellcheck highlighting. If the selection
// is not collapsed, we will re-render.
var selection = editorState.getSelection();
var selectionStart = selection.getStartOffset();
var selectionEnd = selection.getEndOffset();
var anchorKey = selection.getAnchorKey();

if (!selection.isCollapsed()) {
e.preventDefault();
editor.update(
replaceText(
editorState,
chars,
editorState.getCurrentInlineStyle(),
getEntityKeyForSelection(
editorState.getCurrentContent(),
editorState.getSelection(),

// If the character that the user is trying to replace with
// is the same as the current selection text the just update the
// `SelectionState`. Else, update the ContentState with the new text
var currentlySelectedChars = editorState
.getCurrentContent()
.getPlainText()
.slice(selectionStart, selectionEnd);
if (chars === currentlySelectedChars) {
this.update(
EditorState.forceSelection(
editorState,
selection.merge({
focusOffset: selectionEnd,
}),
),
),
);
);
} else {
editor.update(
replaceText(
editorState,
chars,
editorState.getCurrentInlineStyle(),
getEntityKeyForSelection(
editorState.getCurrentContent(),
editorState.getSelection(),
),
),
);
}
return;
}

Expand Down

0 comments on commit 92fdf3f

Please sign in to comment.