javascript - MaxLength in Codemirror -
hi there way set maxlength of characters in codemirror v5.5.0. have tried following:
code:
cmutils.enforcemaxlength = 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.enforcemaxlength); css_editor.on("beforechange", cmutils.enforcemaxlength); js_editor.on("beforechange", cmutils.enforcemaxlength);
but doesn't work
p.s have written maxlength in code
var html_editor = codemirror.fromtextarea(document.getelementbyid("html"), { linenumbers: true, indentwithtabs: false, indentunit: 2, autoclosetags: true, theme: 'dracula', maxlenght: 25, mode: { htmlmode: true, name: "xml" } });
update 1 console says cmutils isn't defined. how define it?
i cmutils
gskinner created own work. there few ways use function, depending on how want function scoped.
the way written can work if following added beforehand:
window.cmutils = window.cmutils || {}; cmutils.enforcemaxlength = function(cm, change) { //.....
alternatively, keep function private, replace first line with:
var enforcemaxlength = function(cm, change) {
don't forget set option "maxlength"
on codemirror editor
Comments
Post a Comment