fix: remove node before hr if it’s an empty text block (fix #1665)

This commit is contained in:
Philipp Kühn 2021-08-12 23:51:37 +02:00
parent 6547ced826
commit 6e76bb84fc

View File

@ -43,14 +43,32 @@ export const HorizontalRule = Node.create<HorizontalRuleOptions>({
return {
setHorizontalRule: () => ({ chain }) => {
return chain()
// remove node before hr if its an empty text block
.command(({ tr, dispatch }) => {
const { selection } = tr
const { empty, $anchor } = selection
const isEmptyTextBlock = $anchor.parent.isTextblock
&& !$anchor.parent.type.spec.code
&& !$anchor.parent.textContent
if (!empty || !isEmptyTextBlock || !dispatch) {
return true
}
const posBefore = $anchor.before()
tr.deleteRange(posBefore, posBefore + 1)
return true
})
.insertContent({ type: this.name })
// add node after hr if its the end of the document
.command(({ tr, dispatch }) => {
if (dispatch) {
const { parent, pos } = tr.selection.$from
const posAfter = pos + 1
const nodeAfter = tr.doc.nodeAt(posAfter)
// end of document
if (!nodeAfter) {
const node = parent.type.contentMatch.defaultType?.create()