fix(core): resolve isNodeEmpty criteria #5415 (#5419)

This commit is contained in:
Nick Perez 2024-08-01 09:02:34 +02:00 committed by GitHub
parent 068559d0ac
commit 84ebd511d2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 6 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---
Fix change criteria for isNodeEmpty to resolve #5415

View File

@ -12,12 +12,12 @@ export function isNodeEmpty(
return !node.text
}
if (node.content.childCount === 0) {
return true
if (node.isAtom || node.isLeaf) {
return false
}
if (node.isLeaf) {
return false
if (node.content.childCount === 0) {
return true
}
if (checkChildren) {

View File

@ -3,9 +3,10 @@
import { getSchema, isNodeEmpty } from '@tiptap/core'
import Document from '@tiptap/extension-document'
import Image from '@tiptap/extension-image'
import Mention from '@tiptap/extension-mention'
import StarterKit from '@tiptap/starter-kit'
const schema = getSchema([StarterKit])
const schema = getSchema([StarterKit, Mention])
const modifiedSchema = getSchema([StarterKit.configure({ document: false }), Document.extend({ content: 'heading block*' })])
const imageSchema = getSchema([StarterKit.configure({ document: false }), Document.extend({ content: 'image block*' }), Image])
@ -26,6 +27,30 @@ describe('isNodeEmpty', () => {
expect(isNodeEmpty(node)).to.eq(false)
})
it('should return false when a paragraph has hardbreaks', () => {
const node = schema.nodeFromJSON({
type: 'paragraph',
content: [{ type: 'hardBreak' }],
})
expect(isNodeEmpty(node)).to.eq(false)
})
it('should return false when a paragraph has a mention', () => {
const node = schema.nodeFromJSON({
type: 'paragraph',
content: [{
type: 'mention',
attrs: {
id: 'Winona Ryder',
label: null,
},
}],
})
expect(isNodeEmpty(node)).to.eq(false)
})
it('should return true when a paragraph has no content', () => {
const node = schema.nodeFromJSON({
type: 'paragraph',
@ -177,7 +202,7 @@ describe('isNodeEmpty', () => {
],
})
expect(isNodeEmpty(node)).to.eq(true)
expect(isNodeEmpty(node)).to.eq(false)
})
})
})