gpt4 book ai didi

javascript - Draftjs 尝试删除原子 block

转载 作者:行者123 更新时间:2023-12-03 03:59:40 27 4
gpt4 key购买 nike

我正在尝试使用 Modifier.removeRange 从 Draftjs 编辑器中删除原子 block 。据我所知,我传递了所有正确的参数,但要删除的 SelectionState 永远不会被删除。这是我的代码。这还添加了一个新的原子 block 。该部分工作正常,它是删除方面返回我传入的相同 contentState。

upload_block_selection_state 是要删除的 SelectionState 对象。该对象在渲染时从 editorState.getSelection() 获取。看起来像这样。

upload_block_selection_state = {
anchorKey: "c1kpk",
anchorOffset: 0,
focusKey: "c1kpk",
focusOffset: 0,
isBackward: false
}

这是应该删除上传 block 然后添加新 block 的函数。添加作品、删除作品没有任何作用。

function addAndRemoveMediaBlock(
entityURL,
entityType,
editorState,
setEditorState,
upload_block_selection_state
){
const contentBeforeAtomicBlock = editorState.getCurrentContent();
const contentAfterSelectionRemoval = Modifier.removeRange(
contentBeforeAtomicBlock,
upload_block_selection_state,
'forward'
);


const contentStateWithEntity = contentAfterSelectionRemoval.createEntity(entityType, 'IMMUTABLE', { src: entityURL });
const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
const editorStateAfterAtomicBlock = AtomicBlockUtils.insertAtomicBlock(
editorState,
entityKey,
entityKey
);

/*
below here relevant only to removing the empty block.
*/
const selectionStateBeforeAtomicBlock = editorState.getSelection();
const anchorKeyBeforeAtomicBlock = selectionStateBeforeAtomicBlock.getAnchorKey();
const contentAfterAtomicBlock = editorStateAfterAtomicBlock.getCurrentContent();
const blockSelectedBefore = contentAfterAtomicBlock.getBlockForKey(anchorKeyBeforeAtomicBlock);

const finalEditorState = (() => {
if(blockSelectedBefore.getLength() === 0){
const keyBefore = blockSelectedBefore.getKey();
const newBlockMap = contentAfterAtomicBlock.blockMap.delete(keyBefore);
const contentWithoutEmptyBlock = contentAfterAtomicBlock.set('blockMap', newBlockMap);
const editorStateWithoutEmptyBlock = EditorState.push(editorStateAfterAtomicBlock, contentWithoutEmptyBlock, 'remove-block');
return EditorState.forceSelection(editorStateWithoutEmptyBlock, contentWithoutEmptyBlock.getSelectionAfter());
}else{
return editorStateAfterAtomicBlock;
}
})();
setEditorState({
editorState: finalEditorState,
});
};

最佳答案

我明白了。

const contentAfterSelectionRemoval  = contentBeforeAtomicBlock.blockMap.delete(blockToRemoveKey);

这似乎是不可变状态中的反模式,所以我最终从另一个 Angular 解决了这个问题,并且实际上没有使用这个工作解决方案。

关于javascript - Draftjs 尝试删除原子 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44781976/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com