Le repo des sources pour le site web des JM2L
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

210 lines
6.5 KiB

  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview The Auto Grow plugin.
  7. */
  8. 'use strict';
  9. ( function() {
  10. CKEDITOR.plugins.add( 'autogrow', {
  11. init: function( editor ) {
  12. // This feature is available only for themed ui instance.
  13. if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_INLINE )
  14. return;
  15. editor.on( 'instanceReady', function() {
  16. // Simply set auto height with div wysiwyg.
  17. if ( editor.editable().isInline() )
  18. editor.ui.space( 'contents' ).setStyle( 'height', 'auto' );
  19. // For classic (`iframe`-based) wysiwyg we need to resize the editor.
  20. else
  21. initIframeAutogrow( editor );
  22. } );
  23. }
  24. } );
  25. function initIframeAutogrow( editor ) {
  26. var lastHeight,
  27. doc,
  28. markerContainer,
  29. scrollable,
  30. marker,
  31. configBottomSpace = editor.config.autoGrow_bottomSpace || 0,
  32. configMinHeight = editor.config.autoGrow_minHeight !== undefined ? editor.config.autoGrow_minHeight : 200,
  33. configMaxHeight = editor.config.autoGrow_maxHeight || Infinity,
  34. maxHeightIsUnlimited = !editor.config.autoGrow_maxHeight;
  35. editor.addCommand( 'autogrow', {
  36. exec: resizeEditor,
  37. modes: { wysiwyg: 1 },
  38. readOnly: 1,
  39. canUndo: false,
  40. editorFocus: false
  41. } );
  42. var eventsList = { contentDom: 1, key: 1, selectionChange: 1, insertElement: 1, mode: 1 };
  43. for ( var eventName in eventsList ) {
  44. editor.on( eventName, function( evt ) {
  45. // Some time is required for insertHtml, and it gives other events better performance as well.
  46. if ( evt.editor.mode == 'wysiwyg' ) {
  47. setTimeout( function() {
  48. if ( isNotResizable() ) {
  49. lastHeight = null;
  50. return;
  51. }
  52. resizeEditor();
  53. // Second pass to make correction upon the first resize, e.g. scrollbar.
  54. // If height is unlimited vertical scrollbar was removed in the first
  55. // resizeEditor() call, so we don't need the second pass.
  56. if ( !maxHeightIsUnlimited )
  57. resizeEditor();
  58. }, 100 );
  59. }
  60. } );
  61. }
  62. // Coordinate with the "maximize" plugin. (#9311)
  63. editor.on( 'afterCommandExec', function( evt ) {
  64. if ( evt.data.name == 'maximize' && evt.editor.mode == 'wysiwyg' ) {
  65. if ( evt.data.command.state == CKEDITOR.TRISTATE_ON )
  66. scrollable.removeStyle( 'overflow-y' );
  67. else
  68. resizeEditor();
  69. }
  70. } );
  71. editor.on( 'contentDom', refreshCache );
  72. refreshCache();
  73. editor.config.autoGrow_onStartup && editor.execCommand( 'autogrow' );
  74. function refreshCache() {
  75. doc = editor.document;
  76. markerContainer = doc[ CKEDITOR.env.ie ? 'getBody' : 'getDocumentElement' ]();
  77. // Quirks mode overflows body, standards overflows document element.
  78. scrollable = CKEDITOR.env.quirks ? doc.getBody() : doc.getDocumentElement();
  79. marker = CKEDITOR.dom.element.createFromHtml(
  80. '<span style="margin:0;padding:0;border:0;clear:both;width:1px;height:1px;display:block;">' +
  81. ( CKEDITOR.env.webkit ? '&nbsp;' : '' ) +
  82. '</span>',
  83. doc );
  84. }
  85. function isNotResizable() {
  86. var maximizeCommand = editor.getCommand( 'maximize' );
  87. return (
  88. !editor.window ||
  89. // Disable autogrow when the editor is maximized. (#6339)
  90. maximizeCommand && maximizeCommand.state == CKEDITOR.TRISTATE_ON
  91. );
  92. }
  93. // Actual content height, figured out by appending check the last element's document position.
  94. function contentHeight() {
  95. // Append a temporary marker element.
  96. markerContainer.append( marker );
  97. var height = marker.getDocumentPosition( doc ).y + marker.$.offsetHeight;
  98. marker.remove();
  99. return height;
  100. }
  101. function resizeEditor() {
  102. // Hide scroll because we won't need it at all.
  103. // Thanks to that we'll need only one resizeEditor() call per change.
  104. if ( maxHeightIsUnlimited )
  105. scrollable.setStyle( 'overflow-y', 'hidden' );
  106. var currentHeight = editor.window.getViewPaneSize().height,
  107. newHeight = contentHeight();
  108. // Additional space specified by user.
  109. newHeight += configBottomSpace;
  110. newHeight = Math.max( newHeight, configMinHeight );
  111. newHeight = Math.min( newHeight, configMaxHeight );
  112. // #10196 Do not resize editor if new height is equal
  113. // to the one set by previous resizeEditor() call.
  114. if ( newHeight != currentHeight && lastHeight != newHeight ) {
  115. newHeight = editor.fire( 'autoGrow', { currentHeight: currentHeight, newHeight: newHeight } ).newHeight;
  116. editor.resize( editor.container.getStyle( 'width' ), newHeight, true );
  117. lastHeight = newHeight;
  118. }
  119. if ( !maxHeightIsUnlimited ) {
  120. if ( newHeight < configMaxHeight && scrollable.$.scrollHeight > scrollable.$.clientHeight )
  121. scrollable.setStyle( 'overflow-y', 'hidden' );
  122. else
  123. scrollable.removeStyle( 'overflow-y' );
  124. }
  125. }
  126. }
  127. } )();
  128. /**
  129. * The minimum height that the editor can assume when adjusting to content by using the Auto Grow
  130. * feature. This option accepts a value in pixels, without the unit (for example: `300`).
  131. *
  132. * config.autoGrow_minHeight = 300;
  133. *
  134. * @since 3.4
  135. * @cfg {Number} [autoGrow_minHeight=200]
  136. * @member CKEDITOR.config
  137. */
  138. /**
  139. * The maximum height that the editor can assume when adjusting to content by using the Auto Grow
  140. * feature. This option accepts a value in pixels, without the unit (for example: `600`).
  141. * Zero (`0`) means that the maximum height is not limited and the editor will expand infinitely.
  142. *
  143. * config.autoGrow_maxHeight = 400;
  144. *
  145. * @since 3.4
  146. * @cfg {Number} [autoGrow_maxHeight=0]
  147. * @member CKEDITOR.config
  148. */
  149. /**
  150. * Whether automatic editor height adjustment brought by the Auto Grow feature should happen on
  151. * editor creation.
  152. *
  153. * config.autoGrow_onStartup = true;
  154. *
  155. * @since 3.6.2
  156. * @cfg {Boolean} [autoGrow_onStartup=false]
  157. * @member CKEDITOR.config
  158. */
  159. /**
  160. * Extra vertical space to be added between the content and the editor bottom bar when adjusting
  161. * editor height to content by using the Auto Grow feature. This option accepts a value in pixels,
  162. * without the unit (for example: `50`).
  163. *
  164. * config.autoGrow_bottomSpace = 50;
  165. *
  166. * @since 3.6.2
  167. * @cfg {Number} [autoGrow_bottomSpace=0]
  168. * @member CKEDITOR.config
  169. */
  170. /**
  171. * Fired when the Auto Grow plugin is about to change the size of the editor.
  172. *
  173. * @event autogrow
  174. * @member CKEDITOR.editor
  175. * @param {CKEDITOR.editor} editor This editor instance.
  176. * @param data
  177. * @param {Number} data.currentHeight The current editor height (before resizing).
  178. * @param {Number} data.newHeight The new editor height (after resizing). It can be changed
  179. * to achieve a different height value to be used instead.
  180. */