feat: add some improvements to CharacterCount extension (#2256), fix #1049, fix #1550, fix #1839, fix #2245
* fix a bug when exceeding the character limit * find a better way to limit the doc size * check paste events * add storage method * refactoring * use textBetween instead of textContent * return early if no limit is set * add words method to storage * show word count in charactercount demo Co-authored-by: Philipp Kühn <philippkuehn@MacBook-Pro-von-Philipp.local>
This commit is contained in:
@@ -18,7 +18,7 @@ npm install @tiptap/extension-character-count
|
||||
|
||||
### limit
|
||||
|
||||
The maximum number of characters that should be allowed. |
|
||||
The maximum number of characters that should be allowed.
|
||||
|
||||
Default: `0`
|
||||
|
||||
@@ -28,21 +28,45 @@ CharacterCount.configure({
|
||||
})
|
||||
```
|
||||
|
||||
### mode
|
||||
|
||||
The mode by which the size is calculated.
|
||||
|
||||
Default: `'textSize'`
|
||||
|
||||
```js
|
||||
CharacterCount.configure({
|
||||
mode: 'nodeSize',
|
||||
})
|
||||
```
|
||||
|
||||
## Storage
|
||||
|
||||
### characters()
|
||||
Get the number of characters for the current document.
|
||||
|
||||
```js
|
||||
editor.storage.characterCount.characters()
|
||||
|
||||
// Get the size of a specific node.
|
||||
editor.storage.characterCount.characters({ node: someCustomNode })
|
||||
|
||||
// Overwrite the default `mode`.
|
||||
editor.storage.characterCount.characters({ mode: 'nodeSize' })
|
||||
```
|
||||
|
||||
### words()
|
||||
Get the number of words for the current document.
|
||||
|
||||
```js
|
||||
editor.storage.characterCount.words()
|
||||
|
||||
// Get the number of words for a specific node.
|
||||
editor.storage.characterCount.words({ node: someCustomNode })
|
||||
```
|
||||
|
||||
## Source code
|
||||
[packages/extension-character-count/](https://github.com/ueberdosis/tiptap/blob/main/packages/extension-character-count/)
|
||||
|
||||
## Usage
|
||||
https://embed.tiptap.dev/preview/Extensions/CharacterCount
|
||||
|
||||
## Count words, emojis, letters …
|
||||
Want to count words instead? Or emojis? Or the letter *a*? Sure, no problem. You can access the `textContent` directly and count whatever you’re into.
|
||||
|
||||
```js
|
||||
new Editor({
|
||||
onUpdate({ editor }) {
|
||||
const wordCount = editor.state.doc.textContent.split(' ').length
|
||||
|
||||
console.log(wordCount)
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user