Skip to content

Commit

Permalink
Refactor getEntityRemovalRange in getCharacterRemovalRange.js
Browse files Browse the repository at this point in the history
* get rid of the two boolean flags added in facebookarchive#1108
* rearrange code in a procedural/imperative style to be more understandable
this fixes facebookarchive#1116
  • Loading branch information
jackyho112 committed Apr 17, 2017
1 parent 3894c64 commit 8acd368
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/model/modifier/getCharacterRemovalRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,29 @@ function getCharacterRemovalRange(
var newSelectionState = selectionState;
if (startEntityKey && (startEntityKey === endEntityKey)) {
newSelectionState = getEntityRemovalRange(
'newSelection',
entityMap,
startBlock,
newSelectionState,
direction,
startEntityKey,
true,
true,
);
} else if (startEntityKey && endEntityKey) {
const startSelectionState = getEntityRemovalRange(
'startSelection',
entityMap,
startBlock,
newSelectionState,
direction,
startEntityKey,
false,
true,
);
const endSelectionState = getEntityRemovalRange(
'endSelection',
entityMap,
endBlock,
newSelectionState,
direction,
endEntityKey,
false,
false,
);
newSelectionState = newSelectionState.merge({
anchorOffset: startSelectionState.getAnchorOffset(),
Expand All @@ -83,27 +80,25 @@ function getCharacterRemovalRange(
});
} else if (startEntityKey) {
const startSelectionState = getEntityRemovalRange(
'startSelection',
entityMap,
startBlock,
newSelectionState,
direction,
startEntityKey,
false,
true,
);
newSelectionState = newSelectionState.merge({
anchorOffset: startSelectionState.getStartOffset(),
isBackward: false,
});
} else if (endEntityKey) {
const endSelectionState = getEntityRemovalRange(
'endSelection',
entityMap,
endBlock,
newSelectionState,
direction,
endEntityKey,
false,
false,
);
newSelectionState = newSelectionState.merge({
focusOffset: endSelectionState.getEndOffset(),
Expand All @@ -114,26 +109,32 @@ function getCharacterRemovalRange(
}

function getEntityRemovalRange(
type: string,
entityMap: EntityMap,
block: ContentBlock,
selectionState: SelectionState,
direction: DraftRemovalDirection,
entityKey: string,
isEntireSelectionWithinEntity: boolean,
isEntityAtStart: boolean,
): SelectionState {
var start = selectionState.getStartOffset();
var end = selectionState.getEndOffset();
var entity = entityMap.__get(entityKey);
var mutability = entity.getMutability();
const sideToConsider = isEntityAtStart ? start : end;

// `MUTABLE` entities can just have the specified range of text removed
// directly. No adjustments are needed.
if (mutability === 'MUTABLE') {
return selectionState;
}

var start = selectionState.getStartOffset();
var end = selectionState.getEndOffset();
var sideToConsider;

if (type === 'endSelection') {
sideToConsider = end;
} else {
sideToConsider = start;
}

// Find the entity range that overlaps with our removal range.
var entityRanges = getRangesForDraftEntity(block, entityKey).filter(
(range) => sideToConsider <= range.end && sideToConsider >= range.start,
Expand All @@ -157,12 +158,10 @@ function getEntityRemovalRange(

// For `SEGMENTED` entity types, determine the appropriate segment to
// remove.
if (!isEntireSelectionWithinEntity) {
if (isEntityAtStart) {
end = entityRange.end;
} else {
start = entityRange.start;
}
if (type === 'endSelection') {
start = entityRange.start;
} else if (type === 'startSelection') {
end = entityRange.end;
}

var removalRange = DraftEntitySegments.getRemovalRange(
Expand Down

0 comments on commit 8acd368

Please sign in to comment.