Skip to content

Commit

Permalink
fix: improve handling of horizontal rule at document end, fix #248
Browse files Browse the repository at this point in the history
  • Loading branch information
philippkuehn committed Apr 6, 2021
1 parent 92c2c81 commit af17f2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/extension-horizontal-rule/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"dist"
],
"peerDependencies": {
"@tiptap/core": "^2.0.0-beta.1",
"prosemirror-commands": "^1.1.3"
"@tiptap/core": "^2.0.0-beta.1"
},
"dependencies": {
"prosemirror-state": "^1.3.4"
}
}
17 changes: 17 additions & 0 deletions packages/extension-horizontal-rule/src/horizontal-rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
nodeInputRule,
mergeAttributes,
} from '@tiptap/core'
import { TextSelection } from 'prosemirror-state'

export interface HorizontalRuleOptions {
HTMLAttributes: {
Expand Down Expand Up @@ -46,6 +47,22 @@ export const HorizontalRule = Node.create<HorizontalRuleOptions>({
setHorizontalRule: () => ({ tr, dispatch }) => {
if (dispatch) {
tr.replaceSelectionWith(this.type.create())

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()

if (node) {
tr.insert(posAfter, node)
tr.setSelection(TextSelection.create(tr.doc, posAfter))
}
}

tr.scrollIntoView()
}

return true
Expand Down

0 comments on commit af17f2c

Please sign in to comment.