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

Improve getCharacterRemovalRange.js #1645

Closed
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
57 changes: 43 additions & 14 deletions src/model/modifier/getCharacterRemovalRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,62 +48,53 @@ function getCharacterRemovalRange(
}
var newSelectionState = selectionState;
if (startEntityKey && startEntityKey === endEntityKey) {
newSelectionState = getEntityRemovalRange(
newSelectionState = getEntityRemovalRangeAtStart(
entityMap,
startBlock,
newSelectionState,
direction,
startEntityKey,
true,
true,
);
} else if (startEntityKey && endEntityKey) {
const startSelectionState = getEntityRemovalRange(
const startSelectionState = getEntityRemovalRangeAtStart(
entityMap,
startBlock,
newSelectionState,
direction,
startEntityKey,
false,
true,
);
const endSelectionState = getEntityRemovalRange(
const endSelectionState = getEntityRemovalRangeAtEnd(
entityMap,
endBlock,
newSelectionState,
direction,
endEntityKey,
false,
false,
);
newSelectionState = newSelectionState.merge({
anchorOffset: startSelectionState.getAnchorOffset(),
focusOffset: endSelectionState.getFocusOffset(),
isBackward: false,
});
} else if (startEntityKey) {
const startSelectionState = getEntityRemovalRange(
const startSelectionState = getEntityRemovalRangeAtStart(
entityMap,
startBlock,
newSelectionState,
direction,
startEntityKey,
false,
true,
);
newSelectionState = newSelectionState.merge({
anchorOffset: startSelectionState.getStartOffset(),
isBackward: false,
});
} else if (endEntityKey) {
const endSelectionState = getEntityRemovalRange(
const endSelectionState = getEntityRemovalRangeAtEnd(
entityMap,
endBlock,
newSelectionState,
direction,
endEntityKey,
false,
false,
);
newSelectionState = newSelectionState.merge({
focusOffset: endSelectionState.getEndOffset(),
Expand All @@ -113,6 +104,43 @@ function getCharacterRemovalRange(
return newSelectionState;
}

function getEntityRemovalRangeAtStart(
entityMap: EntityMap,
block: BlockNodeRecord,
selectionState: SelectionState,
direction: DraftRemovalDirection,
entityKey: string,
isEntireSelectionWithinEntity: boolean = false,
): SelectionState {
return getEntityRemovalRange(
entityMap,
block,
selectionState,
direction,
entityKey,
isEntireSelectionWithinEntity,
true,
);
}

function getEntityRemovalRangeAtEnd(
entityMap: EntityMap,
block: BlockNodeRecord,
selectionState: SelectionState,
direction: DraftRemovalDirection,
entityKey: string,
): SelectionState {
return getEntityRemovalRange(
entityMap,
block,
selectionState,
direction,
entityKey,
false,
false,
);
}

function getEntityRemovalRange(
entityMap: EntityMap,
block: BlockNodeRecord,
Expand All @@ -126,6 +154,7 @@ function getEntityRemovalRange(
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
Expand Down