PageRenderTime 62ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/BlogEngine/BlogEngine.NET/editors/tiny_mce_3_4_3_1/plugins/wordcount/editor_plugin_src.js

#
JavaScript | 114 lines | 77 code | 25 blank | 12 comment | 14 complexity | 4c972b1045c97c1b0326efb65a953def MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, BSD-3-Clause
  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', /[\w\u2019\'-]+/g); // u2019 == ’
  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. });
  33. ed.onInit.add(function(ed) {
  34. ed.selection.onSetContent.add(function() {
  35. t._count(ed);
  36. });
  37. t._count(ed);
  38. });
  39. ed.onSetContent.add(function(ed) {
  40. t._count(ed);
  41. });
  42. ed.onKeyUp.add(function(ed, e) {
  43. if (e.keyCode == last)
  44. return;
  45. if (13 == e.keyCode || 8 == last || 46 == last)
  46. t._count(ed);
  47. last = e.keyCode;
  48. });
  49. },
  50. _getCount : function(ed) {
  51. var tc = 0;
  52. var tx = ed.getContent({ format: 'raw' });
  53. if (tx) {
  54. tx = tx.replace(/\.\.\./g, ' '); // convert ellipses to spaces
  55. tx = tx.replace(/<.[^<>]*?>/g, ' ').replace(/&nbsp;|&#160;/gi, ' '); // remove html tags and space chars
  56. // deal with html entities
  57. tx = tx.replace(/(\w+)(&.+?;)+(\w+)/, "$1$3").replace(/&.+?;/g, ' ');
  58. tx = tx.replace(this.cleanre, ''); // remove numbers and punctuation
  59. var wordArray = tx.match(this.countre);
  60. if (wordArray) {
  61. tc = wordArray.length;
  62. }
  63. }
  64. return tc;
  65. },
  66. _count : function(ed) {
  67. var t = this;
  68. // Keep multiple calls from happening at the same time
  69. if (t.block)
  70. return;
  71. t.block = 1;
  72. setTimeout(function() {
  73. var tc = t._getCount(ed);
  74. tinymce.DOM.setHTML(t.id, tc.toString());
  75. setTimeout(function() {t.block = 0;}, 2000);
  76. }, 1);
  77. },
  78. getInfo: function() {
  79. return {
  80. longname : 'Word Count plugin',
  81. author : 'Moxiecode Systems AB',
  82. authorurl : 'http://tinymce.moxiecode.com',
  83. infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/wordcount',
  84. version : tinymce.majorVersion + "." + tinymce.minorVersion
  85. };
  86. }
  87. });
  88. tinymce.PluginManager.add('wordcount', tinymce.plugins.WordCount);
  89. })();