/pigeoncms/Plugins/tiny_mce/plugins/wordcount/editor_plugin_src.js

http://pigeoncms.googlecode.com/ · JavaScript · 98 lines · 86 code · 3 blank · 9 comment · 0 complexity · 5338cdc14550a6c5936ffddbbcff7e5a MD5 · raw file

  1. /**
  2. * editor_plugin_src.js
  3. *
  4. * Copyright 2009, Moxiecode Systems AB
  5. * Released under LGPL License.
  6. *
  7. * License: http://tinymce.moxiecode.com/license
  8. * Contributing: http://tinymce.moxiecode.com/contributing
  9. */
  10. (function() {
  11. tinymce.create('tinymce.plugins.WordCount', {
  12. block : 0,
  13. id : null,
  14. countre : null,
  15. cleanre : null,
  16. init : function(ed, url) {
  17. var t = this, last = 0;
  18. t.countre = ed.getParam('wordcount_countregex', /\S\s+/g);
  19. t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?'"_+=\\\/-]*/g);
  20. t.id = ed.id + '-word-count';
  21. ed.onPostRender.add(function(ed, cm) {
  22. var row, id;
  23. // Add it to the specified id or the theme advanced path
  24. id = ed.getParam('wordcount_target_id');
  25. if (!id) {
  26. row = tinymce.DOM.get(ed.id + '_path_row');
  27. if (row)
  28. tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>');
  29. } else
  30. tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>');
  31. });
  32. ed.onInit.add(function(ed) {
  33. ed.selection.onSetContent.add(function() {
  34. t._count(ed);
  35. });
  36. t._count(ed);
  37. });
  38. ed.onSetContent.add(function(ed) {
  39. t._count(ed);
  40. });
  41. ed.onKeyUp.add(function(ed, e) {
  42. if (e.keyCode == last)
  43. return;
  44. if (13 == e.keyCode || 8 == last || 46 == last)
  45. t._count(ed);
  46. last = e.keyCode;
  47. });
  48. },
  49. _count : function(ed) {
  50. var t = this, tc = 0;
  51. // Keep multiple calls from happening at the same time
  52. if (t.block)
  53. return;
  54. t.block = 1;
  55. setTimeout(function() {
  56. var tx = ed.getContent({format : 'raw'});
  57. if (tx) {
  58. tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars
  59. tx = tx.replace(t.cleanre, ''); // remove numbers and punctuation
  60. tx.replace(t.countre, function() {tc++;}); // count the words
  61. }
  62. tinymce.DOM.setHTML(t.id, tc.toString());
  63. setTimeout(function() {t.block = 0;}, 2000);
  64. }, 1);
  65. },
  66. getInfo: function() {
  67. return {
  68. longname : 'Word Count plugin',
  69. author : 'Moxiecode Systems AB',
  70. authorurl : 'http://tinymce.moxiecode.com',
  71. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',
  72. version : tinymce.majorVersion + "." + tinymce.minorVersion
  73. };
  74. }
  75. });
  76. tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);
  77. })();