Skip to content

Commit 8cd420b

Browse files
mmmoussaflarnie
authored andcommitted
Fixed bugs with deletion between blocks and atomic block deletion test case (facebookarchive#1108)
1 parent 6f2bf66 commit 8cd420b

3 files changed

Lines changed: 38 additions & 17 deletions

File tree

src/model/modifier/DraftModifier.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,17 @@ var DraftModifier = {
140140
removalDirection: DraftRemovalDirection,
141141
): ContentState {
142142
let startKey, endKey, startBlock, endBlock;
143-
startKey = removalDirection === 'forward'
144-
? rangeToRemove.getAnchorKey()
145-
: rangeToRemove.getFocusKey();
146-
endKey = removalDirection === 'forward'
147-
? rangeToRemove.getFocusKey()
148-
: rangeToRemove.getAnchorKey();
143+
if (rangeToRemove.getIsBackward()) {
144+
rangeToRemove = rangeToRemove.merge({
145+
anchorKey: rangeToRemove.getFocusKey(),
146+
anchorOffset: rangeToRemove.getFocusOffset(),
147+
focusKey: rangeToRemove.getAnchorKey(),
148+
focusOffset: rangeToRemove.getAnchorOffset(),
149+
isBackward: false,
150+
});
151+
}
152+
startKey = rangeToRemove.getAnchorKey();
153+
endKey = rangeToRemove.getFocusKey();
149154
startBlock = contentState.getBlockForKey(startKey);
150155
endBlock = contentState.getBlockForKey(endKey);
151156
const startOffset = rangeToRemove.getStartOffset();

src/model/modifier/__tests__/RichTextEditorUtil-test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ describe('RichTextEditorUtil', () => {
2323
const {editorState, selectionState} = getSampleStateForTesting();
2424

2525
function insertAtomicBlock(targetEditorState) {
26-
const entityKey = 'foo';
26+
const entityKey = targetEditorState.getCurrentContent().createEntity(
27+
'TEST',
28+
'IMMUTABLE',
29+
null,
30+
).getLastCreatedEntityKey();
2731
const character = ' ';
2832
const movedSelection = EditorState.moveSelectionToEnd(targetEditorState);
2933
return AtomicBlockUtils.insertAtomicBlock(

src/model/modifier/getCharacterRemovalRange.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,15 @@ function getCharacterRemovalRange(
4747
return selectionState;
4848
}
4949
var newSelectionState = selectionState;
50-
if (selectionState.getIsBackward()) {
51-
newSelectionState = selectionState.merge({
52-
anchorKey: selectionState.getFocusKey(),
53-
anchorOffset: selectionState.getFocusOffset(),
54-
focusKey: selectionState.getAnchorKey(),
55-
focusOffset: selectionState.getAnchorOffset(),
56-
isBackward: false,
57-
});
58-
}
5950
if (startEntityKey && (startEntityKey === endEntityKey)) {
6051
newSelectionState = getEntityRemovalRange(
6152
entityMap,
6253
startBlock,
6354
newSelectionState,
6455
direction,
6556
startEntityKey,
57+
true,
58+
true,
6659
);
6760
} else if (startEntityKey && endEntityKey) {
6861
const startSelectionState = getEntityRemovalRange(
@@ -71,13 +64,17 @@ function getCharacterRemovalRange(
7164
newSelectionState,
7265
direction,
7366
startEntityKey,
67+
false,
68+
true,
7469
);
7570
const endSelectionState = getEntityRemovalRange(
7671
entityMap,
7772
endBlock,
7873
newSelectionState,
7974
direction,
8075
endEntityKey,
76+
false,
77+
false,
8178
);
8279
newSelectionState = newSelectionState.merge({
8380
anchorOffset: startSelectionState.getAnchorOffset(),
@@ -91,6 +88,8 @@ function getCharacterRemovalRange(
9188
newSelectionState,
9289
direction,
9390
startEntityKey,
91+
false,
92+
true,
9493
);
9594
newSelectionState = newSelectionState.merge({
9695
anchorOffset: startSelectionState.getStartOffset(),
@@ -103,6 +102,8 @@ function getCharacterRemovalRange(
103102
newSelectionState,
104103
direction,
105104
endEntityKey,
105+
false,
106+
false,
106107
);
107108
newSelectionState = newSelectionState.merge({
108109
focusOffset: endSelectionState.getEndOffset(),
@@ -118,11 +119,14 @@ function getEntityRemovalRange(
118119
selectionState: SelectionState,
119120
direction: DraftRemovalDirection,
120121
entityKey: string,
122+
isEntireSelectionWithinEntity: boolean,
123+
isEntityAtStart: boolean,
121124
): SelectionState {
122125
var start = selectionState.getStartOffset();
123126
var end = selectionState.getEndOffset();
124127
var entity = entityMap.__get(entityKey);
125128
var mutability = entity.getMutability();
129+
const sideToConsider = isEntityAtStart ? start : end;
126130

127131
// `MUTABLE` entities can just have the specified range of text removed
128132
// directly. No adjustments are needed.
@@ -132,7 +136,7 @@ function getEntityRemovalRange(
132136

133137
// Find the entity range that overlaps with our removal range.
134138
var entityRanges = getRangesForDraftEntity(block, entityKey).filter(
135-
(range) => start < range.end && end > range.start,
139+
(range) => sideToConsider <= range.end && sideToConsider >= range.start,
136140
);
137141

138142
invariant(
@@ -153,6 +157,14 @@ function getEntityRemovalRange(
153157

154158
// For `SEGMENTED` entity types, determine the appropriate segment to
155159
// remove.
160+
if (!isEntireSelectionWithinEntity) {
161+
if (isEntityAtStart) {
162+
end = entityRange.end;
163+
} else {
164+
start = entityRange.start;
165+
}
166+
}
167+
156168
var removalRange = DraftEntitySegments.getRemovalRange(
157169
start,
158170
end,

0 commit comments

Comments
 (0)