diff --git a/packages/extension-horizontal-rule/src/horizontal-rule.ts b/packages/extension-horizontal-rule/src/horizontal-rule.ts index 409b96d2..a460f96d 100644 --- a/packages/extension-horizontal-rule/src/horizontal-rule.ts +++ b/packages/extension-horizontal-rule/src/horizontal-rule.ts @@ -43,14 +43,32 @@ export const HorizontalRule = Node.create({ return { setHorizontalRule: () => ({ chain }) => { return chain() + // remove node before hr if it’s 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 it’s 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()