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

Commit

Permalink
Ensure selection collapses if user tries to replace with matching text
Browse files Browse the repository at this point in the history
Summary:
**Summary**

This PR ensures that the selection is always collapsed for the cases where a user replaces the current selection with matching text.

I admittedly didn't really dig into the original issue when fixing the previous crash :)

**Test Plan**

Small repro:

1. Type in "abc"
1. Select "c" from right-to-left
1. Type "c"
1. Select "c" from left-to-right
1. Type "c"

Thanks to JLarky for bringing this to my intention!
Closes #1661

Differential Revision: D7055502

fbshipit-source-id: ee4cf80d292ee715e40c017e4c6bd18f806b0e24
  • Loading branch information
existentialism authored and facebook-github-bot committed Feb 23, 2018
1 parent 04c2b9c commit 084bdb6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 72 deletions.
Expand Up @@ -76,74 +76,22 @@ Immutable.Record {

exports[`editor selectionstate is updated if new text matches current selection 1`] = `
Immutable.Record {
"entityMap": Object {
"__add": [Function],
"__create": [Function],
"__get": [Function],
"__getLastCreatedEntityKey": [Function],
"__mergeData": [Function],
"__replaceData": [Function],
"add": [Function],
"create": [Function],
"get": [Function],
"getLastCreatedEntityKey": [Function],
"mergeData": [Function],
"replaceData": [Function],
},
"blockMap": Immutable.OrderedMap {
"a": Immutable.Record {
"key": "a",
"type": "unstyled",
"text": "Arsenal",
"characterList": Immutable.List [
Immutable.Record {
"style": Immutable.OrderedSet [],
"entity": null,
},
Immutable.Record {
"style": Immutable.OrderedSet [],
"entity": null,
},
Immutable.Record {
"style": Immutable.OrderedSet [],
"entity": null,
},
Immutable.Record {
"style": Immutable.OrderedSet [],
"entity": null,
},
Immutable.Record {
"style": Immutable.OrderedSet [],
"entity": null,
},
Immutable.Record {
"style": Immutable.OrderedSet [],
"entity": null,
},
Immutable.Record {
"style": Immutable.OrderedSet [],
"entity": null,
},
],
"depth": 0,
"data": Immutable.Map {},
},
},
"selectionBefore": Immutable.Record {
"anchorKey": "a",
"anchorOffset": 0,
"focusKey": "a",
"focusOffset": 0,
"isBackward": false,
"hasFocus": false,
},
"selectionAfter": Immutable.Record {
"anchorKey": "a",
"anchorOffset": 0,
"focusKey": "a",
"focusOffset": 0,
"isBackward": false,
"hasFocus": false,
},
"anchorKey": "a",
"anchorOffset": 1,
"focusKey": "a",
"focusOffset": 1,
"isBackward": false,
"hasFocus": true,
}
`;

exports[`editor selectionstate is updated if new text matches current selection and user selected backwards 1`] = `
Immutable.Record {
"anchorKey": "a",
"anchorOffset": 1,
"focusKey": "a",
"focusOffset": 1,
"isBackward": true,
"hasFocus": true,
}
`;
30 changes: 27 additions & 3 deletions src/component/handlers/edit/__tests__/editOnBeforeInput.test.js
Expand Up @@ -29,13 +29,17 @@ const DEFAULT_SELECTION = {
isBackward: false,
};

//const collapsedSelection = new SelectionState(DEFAULT_SELECTION);

const rangedSelection = new SelectionState({
...DEFAULT_SELECTION,
focusOffset: 1,
});

const rangedSelectionBackwards = new SelectionState({
...DEFAULT_SELECTION,
anchorOffset: 1,
isBackward: true,
});

const getEditorState = () => {
return EditorState.createWithContent(
ContentState.createFromBlockArray([
Expand Down Expand Up @@ -125,5 +129,25 @@ test('editor selectionstate is updated if new text matches current selection', (
expect(editor.update).toHaveBeenCalledTimes(1);

const newEditorState = editor.update.mock.calls[0][0];
expect(newEditorState.getCurrentContent()).toMatchSnapshot();
expect(newEditorState.getSelection()).toMatchSnapshot();
});

test('editor selectionstate is updated if new text matches current selection and user selected backwards', () => {
const editorState = EditorState.acceptSelection(
getEditorState(),
rangedSelectionBackwards,
);

const editor = {
_latestEditorState: editorState,
props: {},
update: jest.fn(),
};

onBeforeInput(editor, getInputEvent('A'));

expect(editor.update).toHaveBeenCalledTimes(1);

const newEditorState = editor.update.mock.calls[0][0];
expect(newEditorState.getSelection()).toMatchSnapshot();
});
1 change: 1 addition & 0 deletions src/component/handlers/edit/editOnBeforeInput.js
Expand Up @@ -129,6 +129,7 @@ function editOnBeforeInput(
EditorState.forceSelection(
editorState,
selection.merge({
anchorOffset: selectionEnd,
focusOffset: selectionEnd,
}),
),
Expand Down

0 comments on commit 084bdb6

Please sign in to comment.