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

Fixed bugs with deletion of entities between blocks and the atomic block deletion test case #1108

Merged
merged 1 commit into from
Apr 3, 2017
Merged
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions src/model/modifier/DraftModifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,17 @@ var DraftModifier = {
removalDirection: DraftRemovalDirection,
): ContentState {
let startKey, endKey, startBlock, endBlock;
startKey = removalDirection === 'forward'
? rangeToRemove.getAnchorKey()
: rangeToRemove.getFocusKey();
endKey = removalDirection === 'forward'
? rangeToRemove.getFocusKey()
: rangeToRemove.getAnchorKey();
if (rangeToRemove.getIsBackward()) {
rangeToRemove = rangeToRemove.merge({
anchorKey: rangeToRemove.getFocusKey(),
anchorOffset: rangeToRemove.getFocusOffset(),
focusKey: rangeToRemove.getAnchorKey(),
focusOffset: rangeToRemove.getAnchorOffset(),
isBackward: false,
});
}
startKey = rangeToRemove.getAnchorKey();
endKey = rangeToRemove.getFocusKey();
startBlock = contentState.getBlockForKey(startKey);
endBlock = contentState.getBlockForKey(endKey);
const startOffset = rangeToRemove.getStartOffset();
Expand Down
6 changes: 5 additions & 1 deletion src/model/modifier/__tests__/RichTextEditorUtil-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ describe('RichTextEditorUtil', () => {
const {editorState, selectionState} = getSampleStateForTesting();

function insertAtomicBlock(targetEditorState) {
const entityKey = 'foo';
const entityKey = targetEditorState.getCurrentContent().createEntity(
'TEST',
'IMMUTABLE',
null,
).getLastCreatedEntityKey();
const character = ' ';
const movedSelection = EditorState.moveSelectionToEnd(targetEditorState);
return AtomicBlockUtils.insertAtomicBlock(
Expand Down
32 changes: 22 additions & 10 deletions src/model/modifier/getCharacterRemovalRange.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,15 @@ function getCharacterRemovalRange(
return selectionState;
}
var newSelectionState = selectionState;
if (selectionState.getIsBackward()) {
newSelectionState = selectionState.merge({
anchorKey: selectionState.getFocusKey(),
anchorOffset: selectionState.getFocusOffset(),
focusKey: selectionState.getAnchorKey(),
focusOffset: selectionState.getAnchorOffset(),
isBackward: false,
});
}
if (startEntityKey && (startEntityKey === endEntityKey)) {
newSelectionState = getEntityRemovalRange(
entityMap,
startBlock,
newSelectionState,
direction,
startEntityKey,
true,
true,
);
} else if (startEntityKey && endEntityKey) {
const startSelectionState = getEntityRemovalRange(
Expand All @@ -71,13 +64,17 @@ function getCharacterRemovalRange(
newSelectionState,
direction,
startEntityKey,
false,
true,
);
const endSelectionState = getEntityRemovalRange(
entityMap,
endBlock,
newSelectionState,
direction,
endEntityKey,
false,
false,
);
newSelectionState = newSelectionState.merge({
anchorOffset: startSelectionState.getAnchorOffset(),
Expand All @@ -91,6 +88,8 @@ function getCharacterRemovalRange(
newSelectionState,
direction,
startEntityKey,
false,
true,
);
newSelectionState = newSelectionState.merge({
anchorOffset: startSelectionState.getStartOffset(),
Expand All @@ -103,6 +102,8 @@ function getCharacterRemovalRange(
newSelectionState,
direction,
endEntityKey,
false,
false,
);
newSelectionState = newSelectionState.merge({
focusOffset: endSelectionState.getEndOffset(),
Expand All @@ -118,11 +119,14 @@ function getEntityRemovalRange(
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.
Expand All @@ -132,7 +136,7 @@ function getEntityRemovalRange(

// Find the entity range that overlaps with our removal range.
var entityRanges = getRangesForDraftEntity(block, entityKey).filter(
(range) => start < range.end && end > range.start,
(range) => sideToConsider <= range.end && sideToConsider >= range.start,
);

invariant(
Expand All @@ -153,6 +157,14 @@ function getEntityRemovalRange(

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

var removalRange = DraftEntitySegments.getRemovalRange(
start,
end,
Expand Down