Ticket #8050: 8050_2.patch
File 8050_2.patch, 4.8 KB (added by , 13 years ago) |
---|
-
_source/plugins/autogrow/plugin.js
8 8 */ 9 9 (function(){ 10 10 11 var parts = [ "margin-top","margin-bottom","border-top-width", "border-bottom-width", "padding-top", "padding-bottom" ];12 13 function nonContentHeight( element )14 {15 var adjustment = 0;16 for ( var i = 0, len = parts.length; i < len; i++ )17 adjustment += parseInt( element.getComputedStyle( parts[ i ] ) || 0, 10 ) || 0;18 return adjustment;19 }20 21 // Count for spaces outside of the actual content.22 function extra( element )23 {24 var margin = 0;25 margin += nonContentHeight( element);26 if ( element.is( 'html' ) )27 margin += nonContentHeight( element.getDocument().getBody() );28 return margin;29 }30 31 11 // Actual content height, figured out by simply check the last element's document position. 32 12 function docContentHeight( doc ) 33 13 { 34 14 var last = doc.getBody().getLast(); 35 15 36 16 // Last node is not measurable, create a temporary marker element. 37 if ( !last || last.type != CKEDITOR.NODE_ELEMENT || last.is( 'br' ) )17 if ( !last || last.type != CKEDITOR.NODE_ELEMENT || last.is( 'br' ) || !last.isVisible() ) 38 18 { 39 19 last = CKEDITOR.dom.element.createFromHtml( '<span>' + ( CKEDITOR.env.webkit ? ' ' : '' ) + '</span>', doc ); 40 20 doc.getBody().append( last ); 41 21 last.isMarker = 1; 42 22 } 43 23 44 var height = last.getDocumentPosition( doc ).y + last.$.offsetHeight + parseInt( last.getComputedStyle( 'margin-bottom' ) || 0, 10 );24 var height = last.getDocumentPosition( doc ).y + last.$.offsetHeight + ( parseInt( last.getComputedStyle( 'margin-bottom' ), 10 ) || 0 ); 45 25 last.isMarker && last.remove(); 46 26 return height; 47 27 } 48 28 29 function hasScrollbar( element ) 30 { 31 return element.$.clientHeight != element.$.scrollHeight; 32 } 33 49 34 CKEDITOR.plugins.add( 'autogrow', 50 35 { 51 36 init : function( editor ) 52 37 { 38 // The offset size between content height and editor size. 53 39 var contentMargin = 0; 54 40 var resizeEditor = function( editor ) 55 41 { … … 65 51 66 52 // Quirks mode overflows body except for IE9, standards overflows document element. 67 53 var scrollable = !CKEDITOR.env.ie9Compat && doc.$.compatMode == 'BackCompat' ? body : htmlElement, 68 contentHeight = docContentHeight( doc ), 69 delta = contentHeight - scrollable.$.clientHeight + extra( scrollable ); 54 contentHeight = docContentHeight( doc ); 70 55 71 // Delta height from either increasing or decreasing.72 if ( delta)56 // For the first time, measures the margin size by growing the editor lively until scroll-bar disappears. 57 if ( !contentMargin ) 73 58 { 74 newHeight = currentHeight + delta; 75 var min = editor.config.autoGrow_minHeight, 76 max = editor.config.autoGrow_maxHeight; 59 var increase = contentHeight > scrollable.$.clientHeight, 60 measureHeight = contentHeight; 61 do 62 resizeable.setStyle( 'height', ( increase ? measureHeight++ : measureHeight-- ) + 'px' ); 63 while ( !increase ^ hasScrollbar( scrollable ) ); 64 65 // Compensate the overhead from decreasing editor height. 66 if ( !increase ) 67 { 68 do 69 resizeable.setStyle( 'height', measureHeight++ + 'px' ); 70 while ( hasScrollbar( scrollable ) ); 71 } 72 contentMargin = measureHeight - contentHeight; 73 } 74 75 newHeight = contentHeight + contentMargin; 76 77 var min = editor.config.autoGrow_minHeight, 78 max = editor.config.autoGrow_maxHeight; 77 79 78 79 80 81 82 80 ( min == undefined ) && ( editor.config.autoGrow_minHeight = min = 200 ); 81 if ( min ) 82 newHeight = Math.max( newHeight, min ); 83 if ( max ) 84 newHeight = Math.min( newHeight, max ); 83 85 84 if ( newHeight != currentHeight ) 85 { 86 newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight; 87 resizeable.setStyle( 'height', newHeight + 'px' ); 88 editor.fire( 'resize' ); 89 // Calculate and record now the margin between the actual content size and page size. 90 setTimeout( function() { contentMargin = scrollable.scrollHeight - body.offsetHeight; }, 0 ); 91 92 } 93 } 86 if ( newHeight != currentHeight ) 87 { 88 newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight; 89 resizeable.setStyle( 'height', newHeight + 'px' ); 90 editor.fire( 'resize' ); 91 } 94 92 }; 95 93 96 94 for ( var eventName in { contentDom:1, key:1, selectionChange:1, insertElement:1 } )