import { Modifier , EditorState , SelectionState } from 'draft-js' ; export default function removeBlock ( editorState , blockKey ) { let content = editorState . getCurrentContent ( ) ; const beforeKey = content . getKeyBefore ( blockKey ) ; const beforeBlock = content . getBlockForKey ( beforeKey ) ; if ( beforeBlock === undefined ) { const targetRange = new SelectionState ( { anchorKey : blockKey , anchorOffset : 0 , focusKey : blockKey , focusOffset : 1 , } ) ; content = Modifier . removeRange ( content , targetRange , 'backward' ) ; content = Modifier . setBlockType ( content , targetRange , 'unstyled' ) ; const newState = EditorState . push ( editorState , content , 'remove-range' ) ; const newSelection = new SelectionState ( { anchorKey : blockKey , anchorOffset : 0 , focusKey : blockKey , focusOffset : 0 , } ) ; return EditorState . forceSelection ( newState , newSelection ) ; } const targetRange = new SelectionState ( { anchorKey : beforeKey , anchorOffset : beforeBlock . getLength ( ) , focusKey : blockKey , focusOffset : 1 , } ) ; content = Modifier . removeRange ( content , targetRange , 'backward' ) ; const newState = EditorState . push ( editorState , content , 'remove-range' ) ; const newSelection = new SelectionState ( { anchorKey : beforeKey , anchorOffset : beforeBlock . getLength ( ) , focusKey : beforeKey , focusOffset : beforeBlock . getLength ( ) , } ) ; return EditorState . forceSelection ( newState , newSelection ) ; }