49 lines
1.9 KiB
JavaScript
49 lines
1.9 KiB
JavaScript
tinymce.PluginManager.add('charactercount', function (editor) {
|
||
|
||
var _self = this;
|
||
|
||
function update() {
|
||
editor.theme.panel.find('#charactercount').text('Количество символов: '+ _self.getCount() + '(с пробелами) | ' + _self.getCountWithoutSpaces() + '(без пробелов)' );
|
||
}
|
||
|
||
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: 'Количество символов: '+ _self.getCount() + '(с пробелами) | ' + _self.getCountWithoutSpaces() + '(без пробелов)',
|
||
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;
|
||
};
|
||
_self.getCountWithoutSpaces = function () {
|
||
var tx = editor.getContent({ format: 'raw' });
|
||
var decoded = decodeHtml(tx);
|
||
var decodedStripped = decoded.replace(/(<([^>]+)>)/ig, '');
|
||
var tc = decodedStripped.replace(/(?:\r\n|\r|\n)/g, '').replace('/ /g', '').split(' ').join('').replace(/\s\s+/g, '');
|
||
console.log(tc);
|
||
return tc.length;
|
||
};
|
||
|
||
|
||
function decodeHtml(html) {
|
||
var txt = document.createElement('textarea');
|
||
txt.innerHTML = html;
|
||
return txt.value;
|
||
}
|
||
}); |