From 10a43b17d0923e9dd7779eb4a24fbfa54c2d3dee Mon Sep 17 00:00:00 2001 From: Sunny Walker Date: Wed, 19 Jun 2013 15:04:20 -1000 Subject: [PATCH 1/2] added support for mouse-driven paste events; cleaned up some issues when JSHinted; optimized space concatenation --- javascripts/jquery.autogrow-textarea.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/javascripts/jquery.autogrow-textarea.js b/javascripts/jquery.autogrow-textarea.js index 481041f..595e1f0 100644 --- a/javascripts/jquery.autogrow-textarea.js +++ b/javascripts/jquery.autogrow-textarea.js @@ -4,6 +4,8 @@ * Auto-growing textareas; technique ripped from Facebook * * http://github.com/jaz303/jquery-grab-bag/tree/master/javascripts/jquery.autogrow-textarea.js + * + * Requires jQuery 1.4+ */ $.fn.autogrow = function(options) { @@ -12,7 +14,7 @@ var self = this; var $self = $(self); var minHeight = $self.height(); - var noFlickerPad = $self.hasClass('autogrow-short') ? 0 : parseInt($self.css('lineHeight')) || 0; + var noFlickerPad = $self.hasClass('autogrow-short') ? 0 : parseInt($self.css('lineHeight'),10) || 0; var shadow = $('
').css({ position: 'absolute', @@ -24,23 +26,17 @@ fontWeight: $self.css('fontWeight'), lineHeight: $self.css('lineHeight'), resize: 'none', - 'word-wrap': 'break-word' + 'word-wrap': 'break-word' }).appendTo(document.body); var update = function(event) { - var times = function(string, number) - { - for (var i=0, r=''; i/g, '>') .replace(/&/g, '&') .replace(/\n$/, '
 ') .replace(/\n/g, '
') - .replace(/ {2,}/g, function(space){ return times(' ', space.length - 1) + ' ' }); + .replace(/ {2,}/g, function(space){ return new Array(space.length).join(' ') + ' '; }); // Did enter get pressed? Resize in this keydown event so that the flicker doesn't occur. if (event && event.data && event.data.event === 'keydown' && event.keyCode === 13) { @@ -50,9 +46,9 @@ shadow.css('width', $self.width()); shadow.html(val + (noFlickerPad === 0 ? '...' : '')); // Append '...' to resize pre-emptively. $self.height(Math.max(shadow.height() + noFlickerPad, minHeight)); - } + }; - $self.change(update).keyup(update).keydown({event:'keydown'},update); + $self.change(update).keyup(update).keydown({event:'keydown'},update).bind('input paste',update); $(window).resize(update); update(); From 5b672d272908ad0b319befcba097d7ff4379a2c0 Mon Sep 17 00:00:00 2001 From: Sunny Walker Date: Wed, 19 Jun 2013 15:11:50 -1000 Subject: [PATCH 2/2] another JSHint fix and minor optimization --- javascripts/jquery.autogrow-textarea.js | 4 ++-- javascripts/jquery.autogrow-textarea.min.js | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 javascripts/jquery.autogrow-textarea.min.js diff --git a/javascripts/jquery.autogrow-textarea.js b/javascripts/jquery.autogrow-textarea.js index 595e1f0..b5d1c13 100644 --- a/javascripts/jquery.autogrow-textarea.js +++ b/javascripts/jquery.autogrow-textarea.js @@ -7,7 +7,7 @@ * * Requires jQuery 1.4+ */ - $.fn.autogrow = function(options) + $.fn.autogrow = function() { return this.filter('textarea').each(function() { @@ -16,7 +16,7 @@ var minHeight = $self.height(); var noFlickerPad = $self.hasClass('autogrow-short') ? 0 : parseInt($self.css('lineHeight'),10) || 0; - var shadow = $('
').css({ + var shadow = $('
').css({ position: 'absolute', top: -10000, left: -10000, diff --git a/javascripts/jquery.autogrow-textarea.min.js b/javascripts/jquery.autogrow-textarea.min.js new file mode 100644 index 0000000..609559e --- /dev/null +++ b/javascripts/jquery.autogrow-textarea.min.js @@ -0,0 +1 @@ +(function(e){e.fn.autogrow=function(){return this.filter("textarea").each(function(){var t=this,n=e(t),r=n.height(),i=n.hasClass("autogrow-short")?0:parseInt(n.css("lineHeight"),10)||0,s=e("
").css({position:"absolute",top:-1e4,left:-1e4,width:n.width(),fontSize:n.css("fontSize"),fontFamily:n.css("fontFamily"),fontWeight:n.css("fontWeight"),lineHeight:n.css("lineHeight"),resize:"none","word-wrap":"break-word"}).appendTo(document.body),o=function(e){var o=t.value.replace(//g,">").replace(/&/g,"&").replace(/\n$/,"
 ").replace(/\n/g,"
").replace(/ {2,}/g,function(e){return(new Array(e.length)).join(" ")+" "});e&&e.data&&e.data.event==="keydown"&&e.keyCode===13&&(o+="
");s.css("width",n.width());s.html(o+(i===0?"...":""));n.height(Math.max(s.height()+i,r))};n.change(o).keyup(o).keydown({event:"keydown"},o).bind("input paste",o);e(window).resize(o);o()})}})(jQuery); \ No newline at end of file