Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Character counter plugin for TinyMCE 4
tinymce.PluginManager.add('charcount', function(editor) {
editor.on('init', function() {
function getCharacterLength() {
var text = editor.getContent({format: 'text'});
// trim and transform newlines ("\n" has a length of 2) to spaces (length = 1)
text = text.trim().replace(/(\n)+/g, " ");
return text.length;
}
function update() {
editor.theme.panel.find('#charcount').text(['Characters: {0}', getCharacterLength()]);
}
var statusbar = editor.theme.panel && editor.theme.panel.find('#statusbar')[0];
if (statusbar) {
window.setTimeout(function() {
statusbar.insert({
type: 'label',
name: 'charcount',
text: ['Characters: {0}', getCharacterLength()],
classes: 'wordcount',
disabled: editor.settings.readonly
}, 0);
editor.on('keyup setcontent beforeaddundo', function(){
update();
});
}, 0);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment