*/\r
\r
(function() {\r
- tinymce.create('tinymce.plugins.WordCount', {\r
+ tinymce.create('tinymce.plugins.WordCount', {\r
block : 0,\r
id : null,\r
countre : null,\r
cleanre : null,\r
\r
init : function(ed, url) {\r
- var t = this, last = 0;\r
+ var t = this, last = 0, VK = tinymce.VK;\r
\r
- t.countre = ed.getParam('wordcount_countregex', /\S\s+/g);\r
- t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$¿'"_+=\\\/-]*/g);\r
+ t.countre = ed.getParam('wordcount_countregex', /[\w\u2019\'-]+/g); // u2019 == ’\r
+ t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?\'\"_+=\\\/-]*/g);\r
+ t.update_rate = ed.getParam('wordcount_update_rate', 2000);\r
+ t.update_on_delete = ed.getParam('wordcount_update_on_delete', false);\r
t.id = ed.id + '-word-count';\r
\r
ed.onPostRender.add(function(ed, cm) {\r
\r
if (row)\r
tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>');\r
- } else\r
+ } else {\r
tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>');\r
+ }\r
});\r
\r
- ed.onInit.add(function(ed) {\r
+ ed.onInit.add(function(ed) {\r
ed.selection.onSetContent.add(function() {\r
t._count(ed);\r
});\r
t._count(ed);\r
});\r
\r
- ed.onKeyUp.add(function(ed, e) {\r
- if (e.keyCode == last)\r
- return;\r
+ function checkKeys(key) {\r
+ return key !== last && (key === VK.ENTER || last === VK.SPACEBAR || checkDelOrBksp(last));\r
+ }\r
\r
- if (13 == e.keyCode || 8 == last || 46 == last)\r
+ function checkDelOrBksp(key) {\r
+ return key === VK.DELETE || key === VK.BACKSPACE;\r
+ }\r
+\r
+ ed.onKeyUp.add(function(ed, e) {\r
+ if (checkKeys(e.keyCode) || t.update_on_delete && checkDelOrBksp(e.keyCode)) {\r
t._count(ed);\r
+ }\r
\r
last = e.keyCode;\r
});\r
},\r
\r
+ _getCount : function(ed) {\r
+ var tc = 0;\r
+ var tx = ed.getContent({ format: 'raw' });\r
+\r
+ if (tx) {\r
+ tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces\r
+ tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' '); // remove html tags and space chars\r
+\r
+ // deal with html entities\r
+ tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');\r
+ tx = tx.replace(this.cleanre, ''); // remove numbers and punctuation\r
+\r
+ var wordArray = tx.match(this.countre);\r
+ if (wordArray) {\r
+ tc = wordArray.length;\r
+ }\r
+ }\r
+\r
+ return tc;\r
+ },\r
+\r
_count : function(ed) {\r
- var t = this, tc = 0;\r
+ var t = this;\r
\r
// Keep multiple calls from happening at the same time\r
if (t.block)\r
t.block = 1;\r
\r
setTimeout(function() {\r
- var tx = ed.getContent({format : 'raw'});\r
-\r
- if (tx) {\r
- tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' '); // remove html tags and space chars\r
- tx = tx.replace(t.cleanre, ''); // remove numbers and punctuation\r
- tx.replace(t.countre, function() {tc++;}); // count the words\r
+ if (!ed.destroyed) {\r
+ var tc = t._getCount(ed);\r
+ tinymce.DOM.setHTML(t.id, tc.toString());\r
+ setTimeout(function() {t.block = 0;}, t.update_rate);\r
}\r
-\r
- tinymce.DOM.setHTML(t.id, tc.toString());\r
-\r
- setTimeout(function() {t.block = 0;}, 2000);\r
}, 1);\r
},\r
\r
- getInfo: function() {\r
+ getInfo: function() {\r
return {\r
longname : 'Word Count plugin',\r
author : 'Moxiecode Systems AB',\r
infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',\r
version : tinymce.majorVersion + "." + tinymce.minorVersion\r
};\r
- }\r
- });\r
+ }\r
+ });\r
\r
- tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);\r
+ tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);\r
})();\r