Hello, I am looking for a way to limit the number of characters in CodeMirror v5.5.0. I attempted the following method:
Code:
CMUtils.limitCharacters = function(cm, change) {
var maxLength = cm.getOption("maxLength");
if (maxLength && change.update) {
var str = change.text.join("\n");
var delta = str.length - (cm.indexFromPos(change.to) - cm.indexFromPos(change.from));
if (delta <= 0) { return true; }
delta = cm.getValue().length + delta - maxLength;
if (delta > 0) {
str = str.substr(0, str.length - delta);
change.update(change.from, change.to, str.split("\n"));
}
}
return true;
}
html_editor.on("beforeChange", CMUtils.limitCharacters);
css_editor.on("beforeChange", CMUtils.limitCharacters);
js_editor.on("beforeChange", CMUtils.limitCharacters);
Unfortunately, it did not work as expected.
P.S: I have included maxLength in my code
var html_editor = CodeMirror.fromTextArea(document.getElementById("html"), {
lineNumbers: true,
indentWithTabs: false,
indentUnit: 2,
autoCloseTags: true,
theme: 'dracula',
maxLength: 25,
mode: {
htmlMode: true,
name: "xml"
}
});
UPDATE 1: I am receiving an error in the console stating that CMUtils is undefined. How can I define it?