uiwjs / react-codemirror

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
https://uiwjs.github.io/react-codemirror/
MIT License
1.61k stars 128 forks source link

Limit on the number of characters entered #371

Open Nonsmokers opened 2 years ago

Nonsmokers commented 2 years ago

Hello! Thanks for this great project!

It's not so clear how to set a limit on the number of characters entered? Isn’t there any built-in solution? Simple example: https://codesandbox.io/s/react-codemirror-example-codemirror-6-forked-kylrmk?file=/src/App.tsx

Thanks a lot.

jaywcjlove commented 2 years ago

@Nonsmokers https://discuss.codemirror.net/t/how-to-enable-and-disable-specific-lines-in-v6/4521

jaywcjlove commented 2 years ago

@Nonsmokers https://discuss.codemirror.net/t/content-length-limit-codemirror6/4183/3

jaywcjlove commented 2 years ago
function lengthLimit(tr: Transaction): boolean {
  return (tr.startState?.doc?.length + tr.newDoc.length) < 200;
}
Nonsmokers commented 2 years ago
function lengthLimit(tr: Transaction): boolean {
  return (tr.startState?.doc?.length + tr.newDoc.length) < 200;
}

Thanks for the answer, works correctly, but if you insert a value and it is more than the given limit (for example: 97 characters entered and paste 4 characters -> 101/100), the editor becomes uneditable.