Skip to content

Instantly share code, notes, and snippets.

@321zeno
Last active October 26, 2023 03:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 321zeno/f9105f7865b2b2f7097d to your computer and use it in GitHub Desktop.
Save 321zeno/f9105f7865b2b2f7097d to your computer and use it in GitHub Desktop.
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