默认情况下,draft.js 将任何带有 2 个空行的原子类型块(1 个在 atimic 之前,1 个在 atimic 之后)。这种行为是由于 draft.js 中缺少原子选择,并在此处进行了描述。我怎样才能摆脱那些线?我在修改器中没有找到任何合适的功能。也许有人有另一种解决方案来解决这个问题,因为现在看起来不太好
更新:我找到适合我的解决方案:
const { editorState } = this.props;
const contentState = editorState.getCurrentContent();
const entityKey = Entity.create('image', 'IMMUTABLE', {src: this.state.url});
const with_atomic = AtomicBlockUtils.insertAtomicBlock(editorState, entityKey, ' ');
const new_content_state = with_atomic.getCurrentContent();
const block_map = new_content_state.getBlockMap();
const current_atomic_block = block_map.find(block => {
if (block.getEntityAt(0) === entityKey) {
return block
}
});
const atomic_block_key = current_atomic_block.getKey();
const block_before = new_content_state.getBlockBefore(atomic_block_key).getKey();
const new_block_map = block_map.filter(block => {
if ((block.getKey() !== block_before) ) {
return block
}
});
const newContentState = contentState.set('blockMap', new_block_map);
const newEditorState = EditorState.createWithContent(newContentState);
this.props.onChange(newEditorState);