在不移动选择的情况下将空块添加到 Draft.js
Add empty block to Draft.js without moving selection
在不更改 SelectionState 的情况下向 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())
)
}
在不更改 SelectionState 的情况下向 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())
)
}