fix: Improve behavior when using insertContent (#2147)

* check if we can replace the wrapping node by the newly inserted content
    /

* we dont need this anymore

* set selection to inserted content instead of below

* improve hr cursor behavior
This commit is contained in:
Philipp Kühn
2021-11-08 20:37:12 +01:00
committed by GitHub
parent 926cfcd602
commit 8fc915cade
2 changed files with 32 additions and 24 deletions

View File

@@ -45,35 +45,18 @@ 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 from = $anchor.before()
const to = $anchor.start()
tr.deleteRange(from, to)
tr.setSelection(TextSelection.create(tr.doc, from))
return true
})
.insertContent({ type: this.name })
// add node after hr if its the end of the document
// set cursor after horizontal rule
.command(({ tr, dispatch }) => {
if (dispatch) {
const { parent, pos } = tr.selection.$from
const posAfter = pos + 1
const nodeAfter = tr.doc.nodeAt(posAfter)
if (!nodeAfter) {
if (nodeAfter) {
tr.setSelection(TextSelection.create(tr.doc, posAfter))
} else {
// add node after horizontal rule if its the end of the document
const node = parent.type.contentMatch.defaultType?.create()
if (node) {