/
counter.js
42 lines (32 loc) · 918 Bytes
/
counter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(function($)
{
$.Redactor.prototype.counter = function()
{
return {
init: function()
{
if (!this.opts.counterCallback) return;
this.$editor.on('keyup.redactor-limiter', $.proxy(function(e)
{
var words = 0, characters = 0, spaces = 0;
var html = this.code.get();
var text = html.replace(/<\/(.*?)>/gi, ' ');
text = text.replace(/<(.*?)>/gi, '');
text = text.replace(/\t/gi, '');
text = text.replace(/\n/gi, ' ');
text = text.replace(/\r/gi, ' ');
text = $.trim(text);
if (text !== '')
{
var arrWords = text.split(/\s+/);
var arrSpaces = text.match(/\s/g);
if (arrWords) words = arrWords.length;
if (arrSpaces) spaces = arrSpaces.length;
characters = text.length;
}
this.core.setCallback('counter', { words: words, characters: characters, spaces: spaces });
}, this));
}
};
};
})(jQuery);