feat(character-count): add options for configuring counting (#5674)

This commit is contained in:
이호연 2024-09-30 23:38:29 +09:00 committed by GitHub
parent 45bac80328
commit 93bc93353b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/extension-character-count": minor
---
Make counter function accessible from outside character-count extension

View File

@ -16,6 +16,18 @@ export interface CharacterCountOptions {
* @example 'textSize'
*/
mode: 'textSize' | 'nodeSize'
/**
* The text counter function to use. Defaults to a simple character count.
* @default (text) => text.length
* @example (text) => [...new Intl.Segmenter().segment(text)].length
*/
textCounter: (text: string) => number
/**
* The word counter function to use. Defaults to a simple word count.
* @default (text) => text.split(' ').filter(word => word !== '').length
* @example (text) => text.split(/\s+/).filter(word => word !== '').length
*/
wordCounter: (text: string) => number
}
export interface CharacterCountStorage {
@ -46,6 +58,8 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
return {
limit: null,
mode: 'textSize',
textCounter: text => text.length,
wordCounter: text => text.split(' ').filter(word => word !== '').length,
}
},
@ -64,7 +78,7 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
if (mode === 'textSize') {
const text = node.textBetween(0, node.content.size, undefined, ' ')
return text.length
return this.options.textCounter(text)
}
return node.nodeSize
@ -73,9 +87,8 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
this.storage.words = options => {
const node = options?.node || this.editor.state.doc
const text = node.textBetween(0, node.content.size, ' ', ' ')
const words = text.split(' ').filter(word => word !== '')
return words.length
return this.options.wordCounter(text)
}
},