fix: [Bug]: While setting content directly while using CharacterCount with limit is not obeyed #5851

This commit is contained in:
Hari Haran 2024-11-22 11:02:07 +00:00
parent f8961a984f
commit bbc4743af6
2 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"@tiptap/extension-character-count": major
---
fix: #5851 - While setting `content` directly while using character-count

View File

@ -93,9 +93,39 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
}, },
addProseMirrorPlugins() { addProseMirrorPlugins() {
let initialEvaluationDone = false
return [ return [
new Plugin({ new Plugin({
key: new PluginKey('characterCount'), key: new PluginKey('characterCount'),
appendTransaction: (transactions, oldState, newState) => {
if (initialEvaluationDone) {
return
}
const limit = this.options.limit
if (limit === null || limit === undefined || limit === 0) {
initialEvaluationDone = true
return
}
const initialContentSize = this.storage.characters({ node: newState.doc })
if (initialContentSize > limit) {
const over = initialContentSize - limit
const from = 0
const to = over
console.warn(`[CharacterCount] Initial content exceeded limit of ${limit} characters. Content was automatically trimmed.`)
const tr = newState.tr.deleteRange(from, to)
initialEvaluationDone = true
return tr
}
initialEvaluationDone = true
},
filterTransaction: (transaction, state) => { filterTransaction: (transaction, state) => {
const limit = this.options.limit const limit = this.options.limit