contenido
Version:
Contenido is a library with a set of tools to help you create your own rich text editor on top of draft-js without thinking about how to handle things.
45 lines (34 loc) • 1.12 kB
text/typescript
// Core
import { EditorState, Modifier, SelectionState } from '../../core';
// Custom Utilities
import getSelectedBlocksMap from './getSelectedBlocksMap';
// Custom Types
import type { State, StateHandler } from '../../types';
const clearBlockTypes = (state: State, stateHandler: StateHandler) => {
const contentState = state.getCurrentContent();
const blocksMap = getSelectedBlocksMap(state);
const contentWithoutBlocks = blocksMap.reduce((newContentState, block) => {
const blockType = block.getType();
if (blockType !== 'unstyled') {
const selectionState = SelectionState.createEmpty(block.getKey());
const updatedSelection = selectionState.merge({
focusOffset: 0,
anchorOffset: block.getText().length,
});
return Modifier.setBlockType(
newContentState,
updatedSelection,
'unstyled'
);
}
return newContentState;
}, contentState);
const newState = EditorState.push(
state,
contentWithoutBlocks,
'change-block-type'
);
stateHandler(newState);
return newState;
};
export default clearBlockTypes;