fix: improve handling of horizontal rule at document end, fix #248

This commit is contained in:
Philipp Kühn
2021-04-06 23:48:51 +02:00
parent 92c2c81b8e
commit af17f2c657
2 changed files with 21 additions and 2 deletions

View File

@@ -22,7 +22,9 @@
"dist" "dist"
], ],
"peerDependencies": { "peerDependencies": {
"@tiptap/core": "^2.0.0-beta.1", "@tiptap/core": "^2.0.0-beta.1"
"prosemirror-commands": "^1.1.3" },
"dependencies": {
"prosemirror-state": "^1.3.4"
} }
} }

View File

@@ -4,6 +4,7 @@ import {
nodeInputRule, nodeInputRule,
mergeAttributes, mergeAttributes,
} from '@tiptap/core' } from '@tiptap/core'
import { TextSelection } from 'prosemirror-state'
export interface HorizontalRuleOptions { export interface HorizontalRuleOptions {
HTMLAttributes: { HTMLAttributes: {
@@ -46,6 +47,22 @@ export const HorizontalRule = Node.create<HorizontalRuleOptions>({
setHorizontalRule: () => ({ tr, dispatch }) => { setHorizontalRule: () => ({ tr, dispatch }) => {
if (dispatch) { if (dispatch) {
tr.replaceSelectionWith(this.type.create()) 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 return true