Content length limit (CodeMirror6)

It was discussed here, for CM5: About the character length of codemirror input limit

Is there a good way to limit the total characters in CM6?

Yes, use a changeFilter to block changes that take the document length over the limit.

1 Like

Thanks.

I feel like there is a better way to do this. :grimacing: Any tips?

function lengthLimit(tr: Transaction): boolean {
  return (tr.startState?.doc?.length + tr.changes?.inserted?.length) < MAX_LENGTH;
}

Yeah, that’s not correct (changes.inserted is some internal array, and its length is the amount of inserted ranges, not their total length). But tr.newDoc.length < MAX_LENGTH should work.

1 Like

Thank you. I appreciate the help.