Last active
October 26, 2023 03:55
-
-
Save 321zeno/f9105f7865b2b2f7097d to your computer and use it in GitHub Desktop.
Character counter plugin for TinyMCE 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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