-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremoveBlock.js
37 lines (34 loc) · 1.12 KB
/
removeBlock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { Modifier, SelectionState } from 'draft-js';
export default function (contentState, blockKey) {
const afterKey = contentState.getKeyAfter(blockKey);
const afterBlock = contentState.getBlockForKey(afterKey);
let targetRange;
// Only if the following block the last with no text then the whole block
// should be removed. Otherwise the block should be reduced to an unstyled block
// without any characters.
if (afterBlock &&
afterBlock.getType() === 'unstyled' &&
afterBlock.getLength() === 0 &&
afterBlock === contentState.getBlockMap().last()) {
targetRange = new SelectionState({
anchorKey: blockKey,
anchorOffset: 0,
focusKey: afterKey,
focusOffset: 0,
});
} else {
targetRange = new SelectionState({
anchorKey: blockKey,
anchorOffset: 0,
focusKey: blockKey,
focusOffset: 1,
});
}
// change the blocktype and remove the characterList entry with the block
const newContentState = Modifier.setBlockType(
contentState,
targetRange,
'unstyled'
);
return Modifier.removeRange(newContentState, targetRange, 'backward');
}