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="clear:both;width:1px;height:1px;display:block;">' + ( CKEDITOR.env.webkit ? ' ' : '' ) + '</span>', doc ); |
| 16 | doc.getBody().append( marker ); |
| 17 | |
| 18 | var height = marker.getDocumentPosition( doc ).y; |
| 19 | marker.remove(); |
| 20 | return height; |
| 21 | } |
| 22 | |
| 23 | function hasScrollbar( element ) |
| 24 | { |
| 25 | return element.$.clientHeight < element.$.scrollHeight; |
| 26 | } |
| 27 | |
| 28 | CKEDITOR.plugins.add( 'autogrow', |
| 29 | { |
| 30 | init : function( editor ) |
| 31 | { |
| 32 | // The offset size between content height and editor size. |
| 33 | var contentMargin = 0; |
| 34 | var resizeEditor = function( editor ) |
| 35 | { |
| 36 | if ( !editor.window ) |
| 37 | return; |
| 38 | |
| 39 | var doc = editor.document, |
| 40 | resizeable = editor.getResizable( 1 ), |
| 41 | body = doc.getBody(), |
| 42 | htmlElement = doc.getDocumentElement(), |
| 43 | iframe = new CKEDITOR.dom.element( doc.getWindow().$.frameElement ), |
| 44 | currentHeight = resizeable.$.offsetHeight, |
| 45 | newHeight; |
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; |
| 47 | // Quirks mode overflows body except for IE9, standards overflows document element. |
| 48 | var scrollable = !CKEDITOR.env.ie9Compat && doc.$.compatMode == 'BackCompat' ? body : htmlElement, |
| 49 | contentHeight = docContentHeight( doc ); |
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 ); |
| 51 | // For the first time, measures the margin size by growing the editor |
| 52 | // height lively until frame scrollbar disappears. |
| 53 | if ( !contentMargin ) |
| 54 | { |
| 55 | var increase = contentHeight > scrollable.$.clientHeight, |
| 56 | measureHeight = contentHeight; |
| 57 | do |
| 58 | iframe.setStyle( 'height', ( increase ? measureHeight++ : measureHeight-- ) + 'px' ); |
| 59 | while ( !increase ^ hasScrollbar( scrollable ) ); |
| 60 | |
| 61 | // Compensate the overhead from decreasing editor height. |
| 62 | if ( !increase ) |
| 63 | { |
| 64 | do |
| 65 | iframe.setStyle( 'height', measureHeight++ + 'px' ); |
| 66 | while ( hasScrollbar( scrollable ) ); |
| 67 | } |
| 68 | |
| 69 | contentMargin = measureHeight - contentHeight; |
| 70 | |
| 71 | // Restore editor frame size. |
| 72 | iframe.setStyle( 'height', '100%' ); |
| 73 | } |
| 74 | |
| 75 | newHeight = contentHeight + contentMargin; |
| 76 | |
| 77 | var min = editor.config.autoGrow_minHeight, |
| 78 | max = editor.config.autoGrow_maxHeight; |
| 79 | |
| 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 ); |
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 | | { |
| 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 | } |
| 92 | }; |
| 93 | |