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

Commit

Permalink
Warn if 'moveSelectionForward/Backward' called with non-collapsed sel…
Browse files Browse the repository at this point in the history
…ection

Summary:
Noticed that these helper methods are designed with the assumption they will
only be passed a collapsed selection, but I don't see anything enforcing that
requirement in the code. Since we will be tweaking these helpers and refactoring
things, want to make the code more sturdy and enforce this constraint.

Starting with a 'warning' in case something relies on broken behavior here, but
as a follow-up we should make this an invariant.

Reviewed By: vdurmont, mitermayer

Differential Revision: D9034617

fbshipit-source-id: 378390c308d1ab2657bea7a5cc9cae060e515658
  • Loading branch information
flarnie authored and facebook-github-bot committed Aug 1, 2018
1 parent 37dadd3 commit 99eca6b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/component/handlers/edit/commands/moveSelectionBackward.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import type EditorState from 'EditorState';
import type SelectionState from 'SelectionState';

const warning = require('warning');

/**
* Given a collapsed selection, move the focus `maxDistance` backward within
* the selected block. If the selection will go beyond the start of the block,
Expand All @@ -28,6 +30,11 @@ function moveSelectionBackward(
maxDistance: number,
): SelectionState {
const selection = editorState.getSelection();
// Should eventually make this an invariant
warning(
!selection.isCollapsed(),
'moveSelectionBackward should only be called with a collapsed SelectionState',
);
const content = editorState.getCurrentContent();
const key = selection.getStartKey();
const offset = selection.getStartOffset();
Expand Down
7 changes: 7 additions & 0 deletions src/component/handlers/edit/commands/moveSelectionForward.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import type EditorState from 'EditorState';
import type SelectionState from 'SelectionState';

const warning = require('warning');

/**
* Given a collapsed selection, move the focus `maxDistance` forward within
* the selected block. If the selection will go beyond the end of the block,
Expand All @@ -28,6 +30,11 @@ function moveSelectionForward(
maxDistance: number,
): SelectionState {
const selection = editorState.getSelection();
// Should eventually make this an invariant
warning(
!selection.isCollapsed(),
'moveSelectionForward should only be called with a collapsed SelectionState',
);
const key = selection.getStartKey();
const offset = selection.getStartOffset();
const content = editorState.getCurrentContent();
Expand Down

0 comments on commit 99eca6b

Please sign in to comment.