@Vikram Mevasiya 的解决方案无法正确清除阻止列表样式
@Sahil 的解决方案与下一个输入上的光标混淆
我发现这是唯一可行的解决方案:
// https://github.com/jpuri/draftjs-utils/blob/master/js/block.js
const removeSelectedBlocksStyle = (editorState) => {
const newContentState = RichUtils.tryToRemoveBlockStyle(editorState);
if (newContentState) {
return EditorState.push(editorState, newContentState, 'change-block-type');
}
return editorState;
}
// https://github.com/jpuri/draftjs-utils/blob/master/js/block.js
export const getResetEditorState = (editorState) => {
const blocks = editorState
.getCurrentContent()
.getBlockMap()
.toList();
const updatedSelection = editorState.getSelection().merge({
anchorKey: blocks.first().get('key'),
anchorOffset: 0,
focusKey: blocks.last().get('key'),
focusOffset: blocks.last().getLength(),
});
const newContentState = Modifier.removeRange(
editorState.getCurrentContent(),
updatedSelection,
'forward'
);
const newState = EditorState.push(editorState, newContentState, 'remove-range');
return removeSelectedBlocksStyle(newState)
}
它结合了https://github.com/jpuri/draftjs-utils提供的两个辅助函数。不想为此npm install
使用整个包裹。它重置光标状态但保留阻止列表样式。这是通过应用程序删除的,removeSelectedBlocksStyle()
我无法相信这个成熟的库如何不提供单行重置功能。