draft-js-modifiers
Version:
Modular state modifiers for Draft.js
21 lines (19 loc) • 794 B
JavaScript
import { EditorState, SelectionState, Modifier } from 'draft-js';
/**
* Remove range also change block type to 'unstyled'
*/
var resetBlock = function resetBlock(editorState, block) {
var content = editorState.getCurrentContent();
var key = block.getKey();
var targetRange = new SelectionState({
anchorKey: key,
anchorOffset: 0,
focusKey: key,
focusOffset: block.getLength()
});
var withoutTargetContent = Modifier.removeRange(content, targetRange, 'backward');
var resetBlock = Modifier.setBlockType(withoutTargetContent, withoutTargetContent.getSelectionAfter(), 'unstyled');
var newState = EditorState.push(editorState, resetBlock, 'remove-range');
return EditorState.forceSelection(newState, resetBlock.getSelectionAfter());
};
export default resetBlock;