| 136 | function autoResizeTextarea () { |
| 137 | // Add a hidden div. We'll copy over the text from the textarea to measure it's height |
| 138 | $('body').append( '<div class="quick-draft-textarea-clone" style="display: none;"></div>' ); |
| 139 | |
| 140 | var clone = $('.quick-draft-textarea-clone'), |
| 141 | editor = $('#content'), |
| 142 | editorCurrentHeight = editor.height(), |
| 143 | // 100px ensures that the editor isn't the exact same height as the window |
| 144 | // It allows the save draft button to be shown at max-height |
| 145 | editorMaxHeight = $(window).height() - 100; |
| 146 | editorOriginalHeight = editorCurrentHeight; |
| 147 | |
| 148 | // Match up textarea and clone div as much as possible |
| 149 | // Firefox is necessitates declaring each padding setting individually |
| 150 | clone.css({ 'font-family': editor.css('font-family'), |
| 151 | 'font-size': editor.css('font-size'), |
| 152 | 'line-height': editor.css('line-height'), |
| 153 | 'padding-bottom': editor.css('paddingBottom'), |
| 154 | 'padding-left': editor.css('paddingLeft'), |
| 155 | 'padding-right': editor.css('paddingRight'), |
| 156 | 'padding-top': editor.css('paddingTop') }); |
| 157 | |
| 158 | // Hide overflow on textarea |
| 159 | editor.css('overflow', 'hidden'); |
| 160 | |
| 161 | editor.on('keyup paste', function (e) { |
| 162 | var $this = $(this), |
| 163 | textareaContent = $this.val().replace(/\n/g, '<br>'), |
| 164 | cloneHeight = clone.css('width', $this.css('width')).html(textareaContent).height(); |
| 165 | |
| 166 | if (cloneHeight !== editorCurrentHeight) { |
| 167 | |
| 168 | // 2px is for border-top & border-bottom |
| 169 | var editorNewHeight = clone.outerHeight() + 2; |
| 170 | |
| 171 | // Don't allow editor to exceed height of window |
| 172 | if ( clone.outerHeight() > editorMaxHeight ) { |
| 173 | editorNewHeight = editorMaxHeight; |
| 174 | // re-enable scrollbar |
| 175 | $this.css('overflow', 'auto'); |
| 176 | } else { |
| 177 | $this.css('overflow', 'hidden'); |
| 178 | } |
| 179 | |
| 180 | $this.css('height', editorNewHeight + 'px'); |
| 181 | editorCurrentHeight = cloneHeight; |
| 182 | } |
| 183 | }); |
| 184 | } |
| 185 | |