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;
21 t.countre = ed.getParam('wordcount_countregex', /[\w\u2019\'-]+/g); // u2019 == ’
22 t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?\'\"_+=\\\/-]*/g);
23 t.id = ed.id + '-word-count';
25 ed.onPostRender.add(function(ed, cm) {
28 // Add it to the specified id or the theme advanced path
29 id = ed.getParam('wordcount_target_id');
31 row = tinymce.DOM.get(ed.id + '_path_row');
34 tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>');
36 tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>');
40 ed.onInit.add(function(ed) {
41 ed.selection.onSetContent.add(function() {
48 ed.onSetContent.add(function(ed) {
52 ed.onKeyUp.add(function(ed, e) {
53 if (e.keyCode == last)
56 if (13 == e.keyCode || 8 == last || 46 == last)
63 _getCount : function(ed) {
65 var tx = ed.getContent({ format: 'raw' });
68 tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces
69 tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/ | /gi, ' '); // remove html tags and space chars
71 // deal with html entities
72 tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');
73 tx = tx.replace(this.cleanre, ''); // remove numbers and punctuation
75 var wordArray = tx.match(this.countre);
77 tc = wordArray.length;
84 _count : function(ed) {
87 // Keep multiple calls from happening at the same time
93 setTimeout(function() {
95 var tc = t._getCount(ed);
96 tinymce.DOM.setHTML(t.id, tc.toString());
97 setTimeout(function() {t.block = 0;}, 2000);
102 getInfo: function() {
104 longname : 'Word Count plugin',
105 author : 'Moxiecode Systems AB',
106 authorurl : 'http://tinymce.moxiecode.com',
107 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',
108 version : tinymce.majorVersion + "." + tinymce.minorVersion
113 tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);