Last active
July 10, 2024 14:09
-
-
Save ClementParis016/ab06847c41160c22f278e4cabe4a0879 to your computer and use it in GitHub Desktop.
TinyMCE characters counter plugin
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.init({ | |
plugins: 'charactercount', | |
elementpath: false | |
}); |
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
/** | |
* Credit: https://amystechnotes.com/2015/05/06/tinymce-add-character-count/ | |
* This is a slightly modified version to work with more recent TinyMCE version, fix some code styling and don't trim | |
* trailing and leading whitespaces from count. | |
*/ | |
tinymce.PluginManager.add('charactercount', function (editor) { | |
var _self = this; | |
function update() { | |
editor.theme.panel.find('#charactercount').text(['Characters: {0}', _self.getCount()]); | |
} | |
editor.on('init', function () { | |
var statusbar = editor.theme.panel && editor.theme.panel.find('#statusbar')[0]; | |
if (statusbar) { | |
window.setTimeout(function () { | |
statusbar.insert({ | |
type: 'label', | |
name: 'charactercount', | |
text: ['Characters: {0}', _self.getCount()], | |
classes: 'charactercount', | |
disabled: editor.settings.readonly | |
}, 0); | |
editor.on('setcontent beforeaddundo keyup', update); | |
}, 0); | |
} | |
}); | |
_self.getCount = function () { | |
var tx = editor.getContent({ format: 'raw' }); | |
var decoded = decodeHtml(tx); | |
var decodedStripped = decoded.replace(/(<([^>]+)>)/ig, ''); | |
var tc = decodedStripped.length; | |
return tc; | |
}; | |
function decodeHtml(html) { | |
var txt = document.createElement('textarea'); | |
txt.innerHTML = html; | |
return txt.value; | |
} | |
}); |
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
.mce-label.mce-charactercount | |
margin: 2px 0 2px 2px | |
padding: 8px | |
font-size: 12px | |
.mce-path | |
display: none !important |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had tried this code but it is not working for me can any once check my code.