我和slatejs一起工作,我想知道:
如果我现在的Element.type是title,看起来像<h1></h1>,
但是当我单击enter时,它将创建一个新的<h1>标记。
如果我想要title类型之后的换行,而新的元素类型变成默认的话,我应该做什么呢?
发布于 2022-02-01 07:01:52
假设您的节点看起来类似于:{children : [{text: ''}], type: 'title'}或类似于此行的内容。
当您按enter时,它会添加一个空节点,如{子节点:{text:‘},键入:'title'}
您应该能够以这种方式修改insertBreak行为,以修改它添加的节点类型:
const { insertBreak } = editor
editor.insertBreak = () => {
const { selection } = editor
if (selection) {
const [title] = Editor.nodes(editor, {
match: n =>
!Editor.isEditor(n) &&
Element.isElement(n) &&
(n.type === 'title')
})
if(title){
Transforms.insertNodes(editor, {
children: [{text: ""}],
type: 'default'
})
return
}
}
insertBreak()
}https://stackoverflow.com/questions/70527179
复制相似问题