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

fix(selection): Returns previous selection if either leaf is null #2189

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion meta/bundle-size-stats/Draft.js.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion meta/bundle-size-stats/Draft.min.js.json

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src/component/selection/getUpdatedSelectionState.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function getUpdatedSelectionState(
const selection: SelectionState = nullthrows(editorState.getSelection());
if (__DEV__) {
if (!anchorKey || !focusKey) {
/*eslint-disable no-console */
/* eslint-disable fb-www/no-console */
console.warn('Invalid selection state.', arguments, editorState.toJS());
/*eslint-enable no-console */
/* eslint-enable fb-www/no-console */
return selection;
}
}
Expand All @@ -47,6 +47,16 @@ function getUpdatedSelectionState(
.getBlockTree(focusBlockKey)
.getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);

if (!anchorLeaf || !focusLeaf) {
// If we cannot make sense of the updated selection state, stick to the current one.
if (__DEV__) {
/* eslint-disable fb-www/no-console */
console.warn('Invalid selection state.', arguments, editorState.toJS());
/* eslint-enable fb-www/no-console */
}
return selection;
}

const anchorLeafStart: number = anchorLeaf.get('start');
const focusLeafStart: number = focusLeaf.get('start');

Expand Down