Skip to content

Commit

Permalink
fix(core): take atom content entirely (#5321)
Browse files Browse the repository at this point in the history
  • Loading branch information
artursvonda authored Jul 19, 2024
1 parent b47df57 commit 4cca382
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/atom-text-content-in-full.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion packages/core/src/helpers/getTextContentFromNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
)

Expand Down
41 changes: 41 additions & 0 deletions tests/cypress/integration/core/getTextContentFromNodes.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})

0 comments on commit 4cca382

Please sign in to comment.