textexpander.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. (function($)
  2. {
  3. $.Redactor.prototype.textexpander = function()
  4. {
  5. return {
  6. init: function()
  7. {
  8. if (!this.opts.textexpander) return;
  9. this.$editor.on('keyup.redactor-limiter', $.proxy(function(e)
  10. {
  11. var key = e.which;
  12. if (key == this.keyCode.SPACE)
  13. {
  14. var current = this.textexpander.getCurrent();
  15. var cloned = $(current).clone();
  16. var $div = $('<div>');
  17. $div.html(cloned);
  18. var text = $div.html();
  19. $div.remove();
  20. var len = this.opts.textexpander.length;
  21. var replaced = 0;
  22. for (var i = 0; i < len; i++)
  23. {
  24. var re = new RegExp(this.opts.textexpander[i][0]);
  25. if (text.search(re) != -1)
  26. {
  27. replaced++;
  28. text = text.replace(re, this.opts.textexpander[i][1]);
  29. $div = $('<div>');
  30. $div.html(text);
  31. $div.append(this.selection.getMarker());
  32. var html = $div.html().replace(/&nbsp;/, '');
  33. $(current).replaceWith(html);
  34. $div.remove();
  35. }
  36. }
  37. if (replaced !== 0)
  38. {
  39. this.selection.restore();
  40. }
  41. }
  42. }, this));
  43. },
  44. getCurrent: function()
  45. {
  46. var selection
  47. if (window.getSelection) selection = window.getSelection();
  48. else if (document.selection && document.selection.type != "Control") selection = document.selection;
  49. if (this.utils.browser('mozilla'))
  50. {
  51. return selection.anchorNode.previousSibling;
  52. }
  53. else
  54. {
  55. return selection.anchorNode;
  56. }
  57. }
  58. };
  59. };
  60. })(jQuery);