| 5237 | // Allow tabs to be entered in Custom CSS textarea. |
| 5238 | api.control( 'custom_css', function ( control ) { |
| 5239 | control.deferred.embedded.done( function allowTabs() { |
| 5240 | control.container.find( 'textarea' ).on( 'keydown', function onKeydown( event ) { |
| 5241 | var textarea = this, selStart, selEnd, val, scroll, sel; |
| 5242 | |
| 5243 | if ( 27 === event.keyCode ) { // Escape key. |
| 5244 | // When pressing Escape: Opera 12 and 27 blur form fields, IE 8 clears them. |
| 5245 | event.preventDefault(); |
| 5246 | |
| 5247 | if ( true !== $( textarea ).data( 'tab-out' ) ) { |
| 5248 | $( textarea ).data( 'tab-out', true ); |
| 5249 | event.stopPropagation(); // Prevent collapsing the section. |
| 5250 | } |
| 5251 | return; |
| 5252 | } |
| 5253 | |
| 5254 | if ( 9 !== event.keyCode || event.ctrlKey || event.altKey || event.shiftKey ) { // Tab key. |
| 5255 | return; |
| 5256 | } |
| 5257 | |
| 5258 | if ( $( textarea ).data( 'tab-out' ) ) { |
| 5259 | $( textarea ).data( 'tab-out', false ); |
| 5260 | return; |
| 5261 | } |
| 5262 | |
| 5263 | selStart = textarea.selectionStart; |
| 5264 | selEnd = textarea.selectionEnd; |
| 5265 | val = textarea.value; |
| 5266 | |
| 5267 | if ( document.selection ) { |
| 5268 | textarea.focus(); |
| 5269 | sel = document.selection.createRange(); |
| 5270 | sel.text = '\t'; |
| 5271 | } else if ( selStart >= 0 ) { |
| 5272 | scroll = textarea.scrollTop; |
| 5273 | textarea.value = val.substring( 0, selStart ).concat( '\t', val.substring( selEnd ) ); |
| 5274 | textarea.selectionStart = textarea.selectionEnd = selStart + 1; |
| 5275 | textarea.scrollTop = scroll; |
| 5276 | } |
| 5277 | |
| 5278 | event.stopPropagation(); |
| 5279 | event.preventDefault(); |
| 5280 | } ); |
| 5281 | } ); |
| 5282 | } ); |
| 5283 | |