mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-11-24 03:31:47 +08:00
fix: [Bug]: While setting content
directly while using CharacterCount with limit is not obeyed #5851
This commit is contained in:
parent
f8961a984f
commit
bbc4743af6
5
.changeset/chilled-pigs-drum.md
Normal file
5
.changeset/chilled-pigs-drum.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@tiptap/extension-character-count": major
|
||||
---
|
||||
|
||||
fix: #5851 - While setting `content` directly while using character-count
|
@ -93,9 +93,39 @@ export const CharacterCount = Extension.create<CharacterCountOptions, CharacterC
|
||||
},
|
||||
|
||||
addProseMirrorPlugins() {
|
||||
let initialEvaluationDone = false
|
||||
|
||||
return [
|
||||
new Plugin({
|
||||
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) => {
|
||||
const limit = this.options.limit
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user