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

Commit

Permalink
Updating selection between rendering can crash
Browse files Browse the repository at this point in the history
There is a timing window that exists from when a new editorState is passed into draftjs as a prop until that new state is rendered. If an onSelect event fires in this time, then the code will throw because the editorState doesn't yet match the DOM state. getUpdatedSelectionState should detect this case and just return editorState.getSelection() in this scenario.
  • Loading branch information
colinjeanne committed Sep 10, 2018
1 parent d40ff40 commit 6409e85
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
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.

24 changes: 18 additions & 6 deletions src/component/selection/getUpdatedSelectionState.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,27 @@ function getUpdatedSelectionState(

const anchorPath = DraftOffsetKey.decode(anchorKey);
const anchorBlockKey = anchorPath.blockKey;
const anchorLeaf = editorState
.getBlockTree(anchorBlockKey)
.getIn([anchorPath.decoratorKey, 'leaves', anchorPath.leafKey]);
const anchorBlockTree = editorState.getBlockTree(anchorBlockKey);
if (!anchorBlockTree) {
return selection;
}
const anchorLeaf = anchorBlockTree.getIn([
anchorPath.decoratorKey,
'leaves',
anchorPath.leafKey,
]);

const focusPath = DraftOffsetKey.decode(focusKey);
const focusBlockKey = focusPath.blockKey;
const focusLeaf = editorState
.getBlockTree(focusBlockKey)
.getIn([focusPath.decoratorKey, 'leaves', focusPath.leafKey]);
const focusBlockTree = editorState.getBlockTree(focusBlockKey);
if (!focusBlockTree) {
return selection;
}
const focusLeaf = focusBlockTree.getIn([
focusPath.decoratorKey,
'leaves',
focusPath.leafKey,
]);

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

0 comments on commit 6409e85

Please # to comment.