2
在不更改SelectionState的情況下,向Draft.js編輯器添加一個空的無格式塊(最後一個)的最佳方式是什麼?將空塊添加到Draft.js,而不移動選擇
在不更改SelectionState的情況下,向Draft.js編輯器添加一個空的無格式塊(最後一個)的最佳方式是什麼?將空塊添加到Draft.js,而不移動選擇
這是我落得這樣做:
import { List } from 'immutable'
import {
EditorState,
ContentState,
ContentBlock,
genKey
} from 'draft-js'
const addEmptyBlock = (editorState) => {
const newBlock = new ContentBlock({
key: genKey(),
type: 'unstyled',
text: '',
characterList: List()
})
const contentState = editorState.getCurrentContent()
const newBlockMap = contentState.getBlockMap().set(newBlock.key, newBlock)
return EditorState.push(
editorState,
ContentState
.createFromBlockArray(newBlockMap.toArray())
.set('selectionBefore', contentState.getSelectionBefore())
.set('selectionAfter', contentState.getSelectionAfter())
)
}