我在react中使用Slate js创建了一个编辑器。我正试图在编辑器内容的末尾插入一个块。我发现了一种在范围内插入块的方法。如何指定文档的范围,使我的自定义块添加在内容的末尾,但焦点保持在当前选择,而不是文档的末尾。
function insertFile(editor, src, target) {
editor.insertBlock({
type: 'file',
data: { src },
})
}
我的模式看起来像这个
const schema = {
blocks: {
file:{
isVoid: true
}
}
}
使用Transforms对象,它的行为就像您希望的那样。
import { Transforms } from 'slate'
Transforms.insertNodes(
editor,
{ type: 'paragraph', children: [{ text: 'xxxx' }] },
{ at: [editor.children.length] }
)
查看文档:Link1 Link2