-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chars limit / chars count #629
Comments
Find the highest-most node, and get the length of its textContent. |
Is there an easy way to enforce a character limit for the editor? |
Did you try this solution mentioned in that thread? #293 (comment) What's being proposed there is to create a plugin which handlesTextInput and overrides the default behavior by returning true. It should be easy to understand if you know the ProseMirror API which TipTap builds on. |
Thanks @BrianHung. Do you happen to know of a way to cut the pasted text to keep everything that fits within the character limit? Couldn't find anything related to ProseMirror on that subject |
On a paste event (handlePaste), use the length of text being pasted and the current document length to create a cutoff number. Then use that cutoff number to slice into the pasted text, followed by inserting the remaining text via a ProseMirror transaction (tr.insertText). You’ll have to look into ProseMirror docs for api on that. Alternatively, another strategy would be to create a plugin that monitors all transactions, and deletes text at the end of the document if the limit is exceeded. Of course, the decision will depend on how you want to handle edge cases. For example, what if you pasted into the middle of a document which causes the limit to be exceeded: should you cut the pasted text, or cut the end of the document? The former strategy of handleTextInput and handlePaste offers more nuance in dealing with that. |
@BrianHung Thanks! I still have a hard time figuring out how to cut the I propose that I'm willing to help write up the guide (if that's acceptable) once I have a solution to share |
I had once built such an extension. Maybe this helps you? import { Extension, Plugin } from 'tiptap'
export default class MaxSize extends Extension {
get name() {
return 'maxSize'
}
get defaultOptions() {
return {
maxSize: null,
}
}
get plugins() {
return [
new Plugin({
filterTransaction: transaction => {
if (!transaction.docChanged || !this.options.maxSize) {
return true
}
const size = transaction.doc && transaction.doc.textContent.length
const maxSizeReached = size > this.options.maxSize
return !maxSizeReached
}
}),
]
}
} Use it: |
@philippkuehn thanks for sharing! Would there be any way to not cancel the transaction completely and only insert part of the text that fits? Or since |
same problem, any solutions? |
@simpsonphile this is the way we've currently handled it
The paste experience isn't the greatest (we don't handle the edge cases mentioned in #629 (comment)), but it suffices for now |
Ok I think I have found a solution. It also support pasting import { Extension, Plugin } from 'tiptap'
export default class MaxSize extends Extension {
get name () {
return 'maxSize'
}
get defaultOptions () {
return {
maxSize: null
}
}
get plugins () {
return [
new Plugin({
appendTransaction: (transactions, oldState, newState) => {
const max = this.options.maxSize
const oldLength = oldState.doc.content.size
const newLength = newState.doc.content.size
if (newLength > max && newLength > oldLength) {
let newTr = newState.tr
newTr.insertText('', max + 1, newLength)
return newTr
}
}
})
]
}
} |
Thanks for sharing! |
Size limit (not text length). It counts not only text symbols, but supports cutting pasted content if it doesnt fits.
|
I’m closing this here for now. This issue is added to #819 and we’ll consider to add it to tiptap v2. Thanks everyone! |
Just wanted let you know that we added a |
Is there any way to get the number of chars in the editor?
Not working well when adding elements (
<span> aaa </span>
) into itThe text was updated successfully, but these errors were encountered: