mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-24 03:39:01 +08:00
fix: remove node before hr if it’s an empty text block (fix #1665)
This commit is contained in:
parent
6547ced826
commit
6e76bb84fc
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user