This repository has been archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix updating editor when editing selected text #1512
Closed
+282
−4
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
src/component/handlers/edit/__tests__/__snapshots__/editOnBeforeInput.test.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`editor is updated with new text if it does not match 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": "Orsenal", | ||
"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": 1, | ||
"isBackward": false, | ||
"hasFocus": false, | ||
}, | ||
"selectionAfter": Immutable.Record { | ||
"anchorKey": "a", | ||
"anchorOffset": 1, | ||
"focusKey": "a", | ||
"focusOffset": 1, | ||
"isBackward": false, | ||
"hasFocus": false, | ||
}, | ||
} | ||
`; | ||
|
||
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, | ||
}, | ||
} | ||
`; |
129 changes: 129 additions & 0 deletions
129
src/component/handlers/edit/__tests__/editOnBeforeInput.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/** | ||
* Copyright 2004-present Facebook. All Rights Reserved. | ||
* | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+ui_infra | ||
* @format | ||
*/ | ||
|
||
'use strict'; | ||
|
||
jest.disableAutomock(); | ||
|
||
const ContentBlock = require('ContentBlock'); | ||
const ContentState = require('ContentState'); | ||
const EditorState = require('EditorState'); | ||
const SelectionState = require('SelectionState'); | ||
const onBeforeInput = require('editOnBeforeInput'); | ||
|
||
const DEFAULT_SELECTION = { | ||
anchorKey: 'a', | ||
anchorOffset: 0, | ||
focusKey: 'a', | ||
focusOffset: 0, | ||
isBackward: false, | ||
}; | ||
|
||
//const collapsedSelection = new SelectionState(DEFAULT_SELECTION); | ||
|
||
const rangedSelection = new SelectionState({ | ||
...DEFAULT_SELECTION, | ||
focusOffset: 1, | ||
}); | ||
|
||
const getEditorState = () => { | ||
return EditorState.createWithContent( | ||
ContentState.createFromBlockArray([ | ||
new ContentBlock({ | ||
key: 'a', | ||
text: 'Arsenal', | ||
}), | ||
]), | ||
); | ||
}; | ||
|
||
const getInputEvent = data => ({ | ||
data, | ||
preventDefault: () => {}, | ||
}); | ||
|
||
test('editor is not updated if no character data is provided', () => { | ||
const editorState = EditorState.acceptSelection( | ||
getEditorState(), | ||
rangedSelection, | ||
); | ||
|
||
const editor = { | ||
_latestEditorState: editorState, | ||
props: {}, | ||
update: jest.fn(), | ||
}; | ||
|
||
onBeforeInput(editor, getInputEvent()); | ||
|
||
expect(editor.update).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('editor is not updated if handled by handleBeforeInput', () => { | ||
const editorState = EditorState.acceptSelection( | ||
getEditorState(), | ||
rangedSelection, | ||
); | ||
|
||
const editor = { | ||
_latestEditorState: editorState, | ||
props: { | ||
handleBeforeInput: () => true, | ||
}, | ||
update: jest.fn(), | ||
}; | ||
|
||
onBeforeInput(editor, getInputEvent('O')); | ||
|
||
expect(editor.update).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('editor is updated with new text if it does not match current selection', () => { | ||
const editorState = EditorState.acceptSelection( | ||
getEditorState(), | ||
rangedSelection, | ||
); | ||
|
||
const editor = { | ||
_latestEditorState: editorState, | ||
props: {}, | ||
update: jest.fn(), | ||
}; | ||
|
||
onBeforeInput(editor, getInputEvent('O')); | ||
|
||
expect(editor.update).toHaveBeenCalledTimes(1); | ||
|
||
const newEditorState = editor.update.mock.calls[0][0]; | ||
expect(newEditorState.getCurrentContent()).toMatchSnapshot(); | ||
}); | ||
|
||
test('editor selectionstate is updated if new text matches current selection', () => { | ||
const editorState = EditorState.acceptSelection( | ||
getEditorState(), | ||
rangedSelection, | ||
); | ||
|
||
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.getCurrentContent()).toMatchSnapshot(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we mean to remove this?