4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.com/contributing
12 tinymce
.create('tinymce.plugins.WordCount', {
18 init : function(ed
, url
) {
19 var t
= this, last
= 0, VK
= tinymce
.VK
;
21 t
.countre
= ed
.getParam('wordcount_countregex', /[\w\u2019\'-]+/g); // u2019 == ’
22 t
.cleanre
= ed
.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?\'\"_+=\\\/-]*/g);
23 t
.update_rate
= ed
.getParam('wordcount_update_rate', 2000);
24 t
.update_on_delete
= ed
.getParam('wordcount_update_on_delete', false);
25 t
.id
= ed
.id
+ '-word-count';
27 ed
.onPostRender
.add(function(ed
, cm
) {
30 // Add it to the specified id or the theme advanced path
31 id
= ed
.getParam('wordcount_target_id');
33 row
= tinymce
.DOM
.get(ed
.id
+ '_path_row');
36 tinymce
.DOM
.add(row
.parentNode
, 'div', {'style': 'float: right'}, ed
.getLang('wordcount.words', 'Words: ') + '<span id="' + t
.id
+ '">0</span>');
38 tinymce
.DOM
.add(id
, 'span', {}, '<span id="' + t
.id
+ '">0</span>');
42 ed
.onInit
.add(function(ed
) {
43 ed
.selection
.onSetContent
.add(function() {
50 ed
.onSetContent
.add(function(ed
) {
54 function checkKeys(key
) {
55 return key
!== last
&& (key
=== VK
.ENTER
|| last
=== VK
.SPACEBAR
|| checkDelOrBksp(last
));
58 function checkDelOrBksp(key
) {
59 return key
=== VK
.DELETE
|| key
=== VK
.BACKSPACE
;
62 ed
.onKeyUp
.add(function(ed
, e
) {
63 if (checkKeys(e
.keyCode
) || t
.update_on_delete
&& checkDelOrBksp(e
.keyCode
)) {
71 _getCount : function(ed
) {
73 var tx
= ed
.getContent({ format
: 'raw' });
76 tx
= tx
.replace(/\.\.\./g, ' '); // convert ellipses to spaces
77 tx
= tx
.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' '); // remove html tags and space chars
79 // deal with html entities
80 tx
= tx
.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');
81 tx
= tx
.replace(this.cleanre
, ''); // remove numbers and punctuation
83 var wordArray
= tx
.match(this.countre
);
85 tc
= wordArray
.length
;
92 _count : function(ed
) {
95 // Keep multiple calls from happening at the same time
101 setTimeout(function() {
103 var tc
= t
._getCount(ed
);
104 tinymce
.DOM
.setHTML(t
.id
, tc
.toString());
105 setTimeout(function() {t
.block
= 0;}, t
.update_rate
);
110 getInfo: function() {
112 longname
: 'Word Count plugin',
113 author
: 'Moxiecode Systems AB',
114 authorurl
: 'http://tinymce.moxiecode.com',
115 infourl
: 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',
116 version
: tinymce
.majorVersion
+ "." + tinymce
.minorVersion
121 tinymce
.PluginManager
.add('wordcount', tinymce
.plugins
.WordCount
);