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