counter.js 918 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (function($)
  2. {
  3. $.Redactor.prototype.counter = function()
  4. {
  5. return {
  6. init: function()
  7. {
  8. if (!this.opts.counterCallback) return;
  9. this.$editor.on('keyup.redactor-limiter', $.proxy(function(e)
  10. {
  11. var words = 0, characters = 0, spaces = 0;
  12. var html = this.code.get();
  13. var text = html.replace(/<\/(.*?)>/gi, ' ');
  14. text = text.replace(/<(.*?)>/gi, '');
  15. text = text.replace(/\t/gi, '');
  16. text = text.replace(/\n/gi, ' ');
  17. text = text.replace(/\r/gi, ' ');
  18. text = $.trim(text);
  19. if (text !== '')
  20. {
  21. var arrWords = text.split(/\s+/);
  22. var arrSpaces = text.match(/\s/g);
  23. if (arrWords) words = arrWords.length;
  24. if (arrSpaces) spaces = arrSpaces.length;
  25. characters = text.length;
  26. }
  27. this.core.setCallback('counter', { words: words, characters: characters, spaces: spaces });
  28. }, this));
  29. }
  30. };
  31. };
  32. })(jQuery);