editor_plugin_src.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, VK = tinymce.VK;
  18. t.countre = ed.getParam('wordcount_countregex', /[\w\u2019\'-]+/g); // u2019 == ’
  19. t.cleanre = ed.getParam('wordcount_cleanregex', /[0-9.(),;:!?%#$?\'\"_+=\\\/-]*/g);
  20. t.update_rate = ed.getParam('wordcount_update_rate', 2000);
  21. t.update_on_delete = ed.getParam('wordcount_update_on_delete', false);
  22. t.id = ed.id + '-word-count';
  23. ed.onPostRender.add(function(ed, cm) {
  24. var row, id;
  25. // Add it to the specified id or the theme advanced path
  26. id = ed.getParam('wordcount_target_id');
  27. if (!id) {
  28. row = tinymce.DOM.get(ed.id + '_path_row');
  29. if (row)
  30. tinymce.DOM.add(row.parentNode, 'div', {'style': 'float: right'}, ed.getLang('wordcount.words', 'Words: ') + '<span id="' + t.id + '">0</span>');
  31. } else {
  32. tinymce.DOM.add(id, 'span', {}, '<span id="' + t.id + '">0</span>');
  33. }
  34. });
  35. ed.onInit.add(function(ed) {
  36. ed.selection.onSetContent.add(function() {
  37. t._count(ed);
  38. });
  39. t._count(ed);
  40. });
  41. ed.onSetContent.add(function(ed) {
  42. t._count(ed);
  43. });
  44. function checkKeys(key) {
  45. return key !== last && (key === VK.ENTER || last === VK.SPACEBAR || checkDelOrBksp(last));
  46. }
  47. function checkDelOrBksp(key) {
  48. return key === VK.DELETE || key === VK.BACKSPACE;
  49. }
  50. ed.onKeyUp.add(function(ed, e) {
  51. if (checkKeys(e.keyCode) || t.update_on_delete && checkDelOrBksp(e.keyCode)) {
  52. t._count(ed);
  53. }
  54. last = e.keyCode;
  55. });
  56. },
  57. _getCount : function(ed) {
  58. var tc = 0;
  59. var tx = ed.getContent({ format: 'raw' });
  60. if (tx) {
  61. tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces
  62. tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars
  63. // deal with html entities
  64. tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');
  65. tx = tx.replace(this.cleanre, ''); // remove numbers and punctuation
  66. var wordArray = tx.match(this.countre);
  67. if (wordArray) {
  68. tc = wordArray.length;
  69. }
  70. }
  71. return tc;
  72. },
  73. _count : function(ed) {
  74. var t = this;
  75. // Keep multiple calls from happening at the same time
  76. if (t.block)
  77. return;
  78. t.block = 1;
  79. setTimeout(function() {
  80. if (!ed.destroyed) {
  81. var tc = t._getCount(ed);
  82. tinymce.DOM.setHTML(t.id, tc.toString());
  83. setTimeout(function() {t.block = 0;}, t.update_rate);
  84. }
  85. }, 1);
  86. },
  87. getInfo: function() {
  88. return {
  89. longname : 'Word Count plugin',
  90. author : 'Moxiecode Systems AB',
  91. authorurl : 'http://tinymce.moxiecode.com',
  92. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',
  93. version : tinymce.majorVersion + "." + tinymce.minorVersion
  94. };
  95. }
  96. });
  97. tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);
  98. })();