Ticket #8050: 8050_2.patch

File 8050_2.patch, 4.8 KB (added by Garry Yao, 13 years ago)
  • _source/plugins/autogrow/plugin.js

     
    88 */
    99(function(){
    1010
    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 
    3111        // Actual content height, figured out by simply check the last element's document position.
    3212        function docContentHeight( doc )
    3313        {
    3414                var last = doc.getBody().getLast();
    3515
    3616                // 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() )
    3818                {
    3919                        last = CKEDITOR.dom.element.createFromHtml( '<span>' + ( CKEDITOR.env.webkit ? '&nbsp;' : '' ) + '</span>', doc );
    4020                        doc.getBody().append( last );
    4121                        last.isMarker = 1;
    4222                }
    4323
    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 );
    4525                last.isMarker && last.remove();
    4626                return height;
    4727        }
    4828
     29        function hasScrollbar( element )
     30        {
     31                return element.$.clientHeight != element.$.scrollHeight;
     32        }
     33
    4934        CKEDITOR.plugins.add( 'autogrow',
    5035        {
    5136                init : function( editor )
    5237                {
     38                        // The offset size between content height and editor size.
    5339                        var contentMargin = 0;
    5440                        var resizeEditor = function( editor )
    5541                        {
     
    6551
    6652                                // Quirks mode overflows body except for IE9, standards overflows document element.
    6753                                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 );
    7055
    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 )
    7358                                {
    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;
    7779
    78                                         ( min == undefined ) && ( editor.config.autoGrow_minHeight = min = 200 );
    79                                         if ( min )
    80                                                 newHeight = Math.max( newHeight, min );
    81                                         if ( max )
    82                                                 newHeight = Math.min( newHeight, max );
     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 );
    8385
    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                                }
    9492                        };
    9593
    9694                        for ( var eventName in { contentDom:1, key:1, selectionChange:1, insertElement:1 } )
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy