draft-js-modifiers
Version:
Modular state modifiers for Draft.js
31 lines (25 loc) • 1.44 kB
JavaScript
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import { EditorState, ContentState, CharacterMetadata, Modifier } from 'draft-js';
import getCurrentBlock from './utils/getCurrentBlock';
var removeInlineStyles = function removeInlineStyles(editorState) {
var inlineStyles = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var selection = editorState.getSelection();
var content = editorState.getCurrentContent();
var newContent = void 0;
if (selection.isCollapsed()) {
var block = getCurrentBlock(editorState);
var updatedCharacterList = block.getCharacterList().map(function (c) {
return inlineStyles.reduce(function (characterMetadata, style) {
return CharacterMetadata.removeStyle(characterMetadata, style);
}, c);
});
var updatedBlock = block.set('characterList', updatedCharacterList);
newContent = content.merge({ blockMap: content.getBlockMap().merge(_defineProperty({}, block.getKey(), updatedBlock)) });
} else {
newContent = inlineStyles.reduce(function (contentState, style) {
return Modifier.removeInlineStyle(contentState, selection, style);
}, content);
}
return EditorState.push(editorState, newContent, 'change-inline-style');
};
export default removeInlineStyles;