Ticket #8050: 8050_6.patch

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

     
    77 * @file AutoGrow plugin
    88 */
    99(function(){
    10         var resizeEditor = function( editor )
    11         {
    12                 if ( !editor.window )
    13                         return;
    14                 var doc = editor.document,
    15                         currentHeight = editor.window.getViewPaneSize().height,
    16                         newHeight;
     10
     11        // Actual content height, figured out by simply check the last element's document position.
     12        function docContentHeight( doc )
     13        {
     14                // Create a temporary marker element.
     15                var marker = CKEDITOR.dom.element.createFromHtml( '<span style="margin:0;padding:0;border:0;clear:both;width:1px;height:1px;display:block;">' + ( CKEDITOR.env.webkit ? '&nbsp;' : '' ) + '</span>', doc );
     16                doc.getBody().append( marker );
     17
     18                var height = marker.getDocumentPosition( doc ).y + marker.$.offsetHeight;
     19                marker.remove();
     20                return height;
     21        }
     22
     23        function scrollbarOffset( element, horizontal )
     24        {
     25                var side = horizontal ? 'Width' : 'Height';
     26                return element.$[ 'scroll' + side ] - element.$[ 'client' + side ];
     27        }
     28
     29        function hasScrollbar( element, horizontal )
     30        {
     31                if ( CKEDITOR.env.ie && horizontal )
     32                        element.setStyle( 'overflow-y', 'hidden' );
     33
     34                var retval = scrollbarOffset( element, horizontal ) > 0;
     35
     36                if ( CKEDITOR.env.ie && horizontal )
     37                        element.removeStyle( 'overflow-y' );
     38
     39                return retval;
     40        }
     41
     42
     43
     44        CKEDITOR.plugins.add( 'autogrow',
     45        {
     46                init : function( editor )
     47                {
     48                        // The blank spaces outside of content when just fit in frame document.
     49                        var contentMargin;
     50
     51                        var resizeEditor = function( editor )
     52                        {
     53                                if ( !editor.window )
     54                                        return;
     55
     56                                var doc = editor.document,
     57                                        resizeable = editor.getResizable( 1 ),
     58                                        currentHeight = resizeable.$.offsetHeight,
     59                                        body = doc.getBody(),
     60                                        htmlElement = doc.getDocumentElement();
     61
     62                                // Quirks mode overflows body except for IE9, standards overflows document element.
     63                                var scrollable = !CKEDITOR.env.ie9Compat && doc.$.compatMode == 'BackCompat' ? body : htmlElement,
     64                                        contentHeight = docContentHeight( doc ),
     65                                        hScrollbarHeight = hasScrollbar( scrollable, 1 ) ? 17 : 0,
     66                                        newHeight;
    1767
    18                 // We can not use documentElement to calculate the height for IE (#6061).
    19                 // It is not good for IE Quirks, yet using offsetHeight would also not work as expected (#6408).
    20                 // We do the same for FF because of the html height workaround (#6341).
    21                 if ( CKEDITOR.env.ie || CKEDITOR.env.gecko )
    22                         newHeight = doc.getBody().$.scrollHeight + ( CKEDITOR.env.ie && CKEDITOR.env.quirks ? 0 : 24 );
    23                 else
    24                         newHeight = doc.getDocumentElement().$.offsetHeight;
     68                                // For the first time, measures the margin size by growing the editor
     69                                // height lively until frame scrollbar disappears.
     70                                if ( contentMargin == undefined )
     71                                {
     72                                        var iframe = new CKEDITOR.dom.element( doc.getWindow().$.frameElement ),
     73                                                increase = contentHeight > scrollable.$.clientHeight,
     74                                                measureHeight = contentHeight;
    2575
    26                 var min = editor.config.autoGrow_minHeight,
    27                         max = editor.config.autoGrow_maxHeight;
    28                 ( min == undefined ) && ( editor.config.autoGrow_minHeight = min = 200 );
    29                 if ( min )
    30                         newHeight = Math.max( newHeight, min );
    31                 if ( max )
    32                         newHeight = Math.min( newHeight, max );
     76                                        try
     77                                        {
     78                                                do
     79                                                {
     80                                                        // When infinite growing is detected, simply stop the resize.
     81                                                        if ( increase && ( measureHeight - contentHeight > currentHeight ) )
     82                                                                throw 0;
     83
     84                                                        iframe.setStyle( 'height', ( increase ? measureHeight++ : measureHeight-- ) + 'px' );
     85                                                }
     86                                                while ( !increase ^ hasScrollbar( scrollable ) );
     87
     88                                                // Compensate the overhead when decreasing editor height.
     89                                                if ( !increase )
     90                                                {
     91                                                        while ( hasScrollbar( scrollable ) )
     92                                                        {
     93                                                                // Defend infinite growing.
     94                                                                if ( measureHeight - contentHeight > currentHeight )
     95                                                                        throw 0;
     96
     97                                                                iframe.setStyle( 'height', measureHeight++ + 'px' );
     98                                                        }
     99                                                }
     100                                        }
     101                                        catch( er ) { return; }
     102                                        finally { iframe.setStyle( 'height', '100%' ); }
     103
     104                                        contentMargin = measureHeight - contentHeight - hScrollbarHeight;
     105                                }
     106
     107                                newHeight = contentHeight + contentMargin + hScrollbarHeight;
     108
     109                                var min = editor.config.autoGrow_minHeight,
     110                                        max = editor.config.autoGrow_maxHeight;
     111                                ( min == undefined ) && ( editor.config.autoGrow_minHeight = min = 200 );
     112                                if ( min )
     113                                        newHeight = Math.max( newHeight, min );
     114                                if ( max )
     115                                        newHeight = Math.min( newHeight, max );
    33116
    34                 if ( newHeight != currentHeight )
    35                 {
    36                         newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight;
    37                         editor.resize( editor.container.getStyle( 'width' ), newHeight, true );
    38                 }
    39         };
    40         CKEDITOR.plugins.add( 'autogrow',
    41         {
    42                 init : function( editor )
    43                 {
     117                                if ( newHeight != currentHeight )
     118                                {
     119                                        newHeight = editor.fire( 'autoGrow', { currentHeight : currentHeight, newHeight : newHeight } ).newHeight;
     120                                        resizeable.setStyle( 'height', newHeight + 'px' );
     121                                        editor.fire( 'resize' );
     122                                }
     123                        };
     124
    44125                        for ( var eventName in { contentDom:1, key:1, selectionChange:1, insertElement:1 } )
    45126                        {
    46127                                editor.on( eventName, function( evt )
© 2003 – 2022, CKSource sp. z o.o. sp.k. All rights reserved. | Terms of use | Privacy policy