fix(core): take atom content entirely (#5321)

This commit is contained in:
Arturs Vonda 2024-07-19 14:49:15 +03:00 committed by GitHub
parent b47df57444
commit 4cca382695
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/core": patch
---
Make sure that atoms are used in-full without cutting the content. Node size for atoms is 1 which causes text to be cut unexpectedly.

View File

@ -24,7 +24,7 @@ export const getTextContentFromNodes = ($from: ResolvedPos, maxMatch = 500) => {
|| node.textContent
|| '%leaf%'
textBefore += chunk.slice(0, Math.max(0, sliceEndPos - pos))
textBefore += node.isAtom ? chunk : chunk.slice(0, Math.max(0, sliceEndPos - pos))
},
)

View File

@ -0,0 +1,41 @@
/// <reference types="cypress" />
import {
getSchemaByResolvedExtensions, getTextContentFromNodes,
} from '@tiptap/core'
import Document from '@tiptap/extension-document'
import Mention from '@tiptap/extension-mention'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import { Node } from '@tiptap/pm/model'
describe(getTextContentFromNodes.name, () => {
it('gets text', () => {
const schema = getSchemaByResolvedExtensions([
Document,
Paragraph,
Text,
Mention.configure({ renderText: ({ node }) => `@${node.attrs.label ?? 'Unknown'}` }),
])
const doc = Node.fromJSON(schema, {
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{ type: 'text', text: 'Start ' },
{ type: 'mention', attrs: { id: 1, label: 'Mention' } },
{ type: 'text', text: ' End' },
],
},
],
})
const pos = doc.resolve(12)
const text = getTextContentFromNodes(pos)
expect(text).to.eq('Start @Mention End')
})
})