admin管理员组文章数量:1429552
I've been trying to add a new block on return key. This is my code, it's placed in a switch case for checking the keyCode
. The shift + return works by adding in a new line. But just return, it needs to start a new block in the editor.
// SHIFT + RETURN <br/>
if(e.shiftKey) {
const newEditorState = RichUtils.insertSoftNewline(this.state.editorState);
if (newEditorState !== this.state.editorState) {
this.onChange(newEditorState);
}
} else {
const newBlock = new ContentBlock({
key: genKey(),
type: "unstyled",
text: ""
});
const contentState = this.state.editorState.getCurrentContent();
const newBlockMap = contentState.getBlockMap().set(newBlock.getKey(), newBlock);
EditorState.push(
this.state.editorState,
ContentState
.createFromBlockArray(newBlockMap.toArray(), contentState.getBlockMap())
.set("selectionAfter", contentState.getSelectionAfter().merge({
anchorKey: newBlock.getKey(),
anchorOffset: 0,
focusKey: newBlock.getKey(),
focusOffset: 0,
isBackward: false
})),
"split-block"
);
}
There is no error in the console. It just doesn't do anything when the return key is pressed.
I hope someone can assist.
I've been trying to add a new block on return key. This is my code, it's placed in a switch case for checking the keyCode
. The shift + return works by adding in a new line. But just return, it needs to start a new block in the editor.
// SHIFT + RETURN <br/>
if(e.shiftKey) {
const newEditorState = RichUtils.insertSoftNewline(this.state.editorState);
if (newEditorState !== this.state.editorState) {
this.onChange(newEditorState);
}
} else {
const newBlock = new ContentBlock({
key: genKey(),
type: "unstyled",
text: ""
});
const contentState = this.state.editorState.getCurrentContent();
const newBlockMap = contentState.getBlockMap().set(newBlock.getKey(), newBlock);
EditorState.push(
this.state.editorState,
ContentState
.createFromBlockArray(newBlockMap.toArray(), contentState.getBlockMap())
.set("selectionAfter", contentState.getSelectionAfter().merge({
anchorKey: newBlock.getKey(),
anchorOffset: 0,
focusKey: newBlock.getKey(),
focusOffset: 0,
isBackward: false
})),
"split-block"
);
}
There is no error in the console. It just doesn't do anything when the return key is pressed.
I hope someone can assist.
Share Improve this question edited Nov 22, 2019 at 7:54 designtocode asked Nov 22, 2019 at 7:49 designtocodedesigntocode 2,2454 gold badges23 silver badges37 bronze badges1 Answer
Reset to default 6I managed to get it working with:
// SHIFT + RETURN <br/>
if(e.shiftKey) {
const newEditorState = RichUtils.insertSoftNewline(this.state.editorState);
if (newEditorState !== this.state.editorState) {
this.onChange(newEditorState);
}
} else {
const editorState = this.state.editorState;
const currentContent = editorState.getCurrentContent();
const selection = editorState.getSelection();
const textWithEntity = Modifier.splitBlock(currentContent, selection);
this.setState({
editorState: EditorState.push(editorState, textWithEntity, "split-block")
});
}
Hope it helps the next person!
本文标签: javascriptDraft js add new block on return keyStack Overflow
版权声明:本文标题:javascript - Draft js add new block on return key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745486299a2660398.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论