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

Commit

Permalink
Prevent (but not fix) ‘Cannot read property 'getIn' of undefined’ in …
Browse files Browse the repository at this point in the history
…getUpdatedSelectionState

Summary:
`getBlockTree()` can sometimes return undefined, despite its Flow type saying it always returns a list (eg. `List<any>`). This change protects against this possibility, falling back to the dev-time console warning below.

We can address the root cause later.

Reviewed By: claudiopro

Differential Revision: D19799354

fbshipit-source-id: 39a04a5de80853e5be76020a906f08a6c213685d
  • Loading branch information
steveluscher authored and facebook-github-bot committed Feb 10, 2020
1 parent 3251017 commit a9fcbb2
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/component/selection/getUpdatedSelectionState.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,25 @@ function getUpdatedSelectionState(

const anchorPath = DraftOffsetKey.decode(anchorKey);
const anchorBlockKey = anchorPath.blockKey;
const anchorLeaf = editorState
.getBlockTree(anchorBlockKey)
.getIn([anchorPath.decoratorKey, 'leaves', anchorPath.leafKey]);
const anchorLeafBlockTree = editorState.getBlockTree(anchorBlockKey);
const anchorLeaf =
anchorLeafBlockTree &&
anchorLeafBlockTree.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 focusLeafBlockTree = editorState.getBlockTree(focusBlockKey);
const focusLeaf =
focusLeafBlockTree &&
focusLeafBlockTree.getIn([
focusPath.decoratorKey,
'leaves',
focusPath.leafKey,
]);

if (!anchorLeaf || !focusLeaf) {
// If we cannot make sense of the updated selection state, stick to the current one.
Expand Down

0 comments on commit a9fcbb2

Please sign in to comment.