Skip to content

Commit

Permalink
fix(core): adjust the splitBlock to return false when failed (#5371)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreystone authored Jul 22, 2024
1 parent 2104f0f commit 618bca9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-needles-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---

Adjust the `splitBlock` command to return `false` when it was unsuccessful.
62 changes: 31 additions & 31 deletions packages/core/src/commands/splitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,45 +61,45 @@ export const splitBlock: RawCommands['splitBlock'] = ({ keepMarks = true } = {})
return false
}

if (dispatch) {
const atEnd = $to.parentOffset === $to.parent.content.size

if (selection instanceof TextSelection) {
tr.deleteSelection()
}

const deflt = $from.depth === 0
? undefined
: defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1)))

let types = atEnd && deflt
const atEnd = $to.parentOffset === $to.parent.content.size

const deflt = $from.depth === 0
? undefined
: defaultBlockAt($from.node(-1).contentMatchAt($from.indexAfter(-1)))

let types = atEnd && deflt
? [
{
type: deflt,
attrs: newAttributes,
},
]
: undefined

let can = canSplit(tr.doc, tr.mapping.map($from.pos), 1, types)

if (
!types
&& !can
&& canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{ type: deflt }] : undefined)
) {
can = true
types = deflt
? [
{
type: deflt,
attrs: newAttributes,
},
]
: undefined
}

let can = canSplit(tr.doc, tr.mapping.map($from.pos), 1, types)

if (
!types
&& !can
&& canSplit(tr.doc, tr.mapping.map($from.pos), 1, deflt ? [{ type: deflt }] : undefined)
) {
can = true
types = deflt
? [
{
type: deflt,
attrs: newAttributes,
},
]
: undefined
}

if (dispatch) {
if (can) {
if (selection instanceof TextSelection) {
tr.deleteSelection()
}

tr.split(tr.mapping.map($from.pos), 1, types)

if (deflt && !atEnd && !$from.parentOffset && $from.parent.type !== deflt) {
Expand All @@ -119,5 +119,5 @@ export const splitBlock: RawCommands['splitBlock'] = ({ keepMarks = true } = {})
tr.scrollIntoView()
}

return true
return can
}

0 comments on commit 618bca9

Please sign in to comment.