Skip to content

Commit

Permalink
fix: remove node before hr if it’s an empty text block (fix #1665)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Aug 12, 2021
1 parent 6547ced commit 6e76bb8
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/extension-horizontal-rule/src/horizontal-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,32 @@ export const HorizontalRule = Node.create<HorizontalRuleOptions>({
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()

Expand Down

0 comments on commit 6e76bb8

Please sign in to comment.