8
0

contextualtoolbar.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module ui/toolbar/contextualtoolbar
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ContextualBalloon from '../contextualballoon';
  10. import ToolbarView from './toolbarview';
  11. import BalloonPanelView from '../panel/balloon/balloonpanelview.js';
  12. import debounce from '@ckeditor/ckeditor5-utils/src/lib/lodash/debounce';
  13. /**
  14. * The contextual toolbar.
  15. *
  16. * It uses the {@link module:ui/contextualballoon~ContextualBalloon contextual balloon plugin}.
  17. *
  18. * @extends module:core/plugin~Plugin
  19. */
  20. export default class ContextualToolbar extends Plugin {
  21. /**
  22. * @inheritDoc
  23. */
  24. static get pluginName() {
  25. return 'contextualtoolbar';
  26. }
  27. /**
  28. * @inheritDoc
  29. */
  30. static get requires() {
  31. return [ ContextualBalloon ];
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. init() {
  37. /**
  38. * The toolbar view displayed in the balloon.
  39. *
  40. * @member {module:ui/toolbar/toolbarview~ToolbarView}
  41. */
  42. this.toolbarView = new ToolbarView( this.editor.locale );
  43. /**
  44. * The contextual balloon plugin instance.
  45. *
  46. * @private
  47. * @member {module:ui/contextualballoon~ContextualBalloon}
  48. */
  49. this._balloon = this.editor.plugins.get( ContextualBalloon );
  50. /**
  51. * This is internal plugin event which is fired 200 ms after selection last change (lodash#debounce).
  52. * This is to makes easy test debounced action without need to use `setTimeout`. Lodash keeps time related
  53. * stuff in a closure and it's not possible to override it by sinon fake timers.
  54. *
  55. * This debounced function is stored as a plugin property to make possible to cancel
  56. * trailing debounced invocation on destroy.
  57. *
  58. * @private
  59. * @member {Function}
  60. */
  61. this._fireChangeDoneDebounced = debounce( () => this.fire( '_selectionChangeDone' ), 200 );
  62. // Attach lifecycle actions.
  63. this._handleSelectionChange();
  64. this._handleFocusChange();
  65. }
  66. /**
  67. * Creates toolbar components based on given configuration.
  68. * This need to be done when all plugins will be ready.
  69. *
  70. * @inheritDoc
  71. */
  72. afterInit() {
  73. const config = this.editor.config.get( 'contextualToolbar' );
  74. const factory = this.editor.ui.componentFactory;
  75. return this.toolbarView.fillFromConfig( config, factory );
  76. }
  77. /**
  78. * Handles editor focus change and hides panel if it's needed.
  79. *
  80. * @private
  81. */
  82. _handleFocusChange() {
  83. const editor = this.editor;
  84. // Hide the panel View when editor loses focus but no the other way around.
  85. this.listenTo( editor.ui.focusTracker, 'change:isFocused', ( evt, name, isFocused ) => {
  86. if ( this._balloon.visibleView === this.toolbarView && !isFocused ) {
  87. this._hidePanel();
  88. }
  89. } );
  90. }
  91. /**
  92. * Handles {@link modules:engine/model/document#selection} change and show or hide toolbar.
  93. *
  94. * Note that in this case it's better to listen to {@link modules:engine/model/document modelDocument}
  95. * selection instead of {@link modules:engine/view/document viewDocument} selection because the first one
  96. * doesn't fire `change` event after text styles changes (like bold or italic) and toolbar doesn't blink.
  97. *
  98. * @private
  99. */
  100. _handleSelectionChange() {
  101. const selection = this.editor.document.selection;
  102. this.listenTo( selection, 'change:range', ( evt, data ) => {
  103. // When the selection is not changed by a collaboration and when is not collapsed.
  104. if ( data.directChange || selection.isCollapsed ) {
  105. // Hide the toolbar when the selection starts changing.
  106. this._hidePanel();
  107. }
  108. // Fire internal `_selectionChangeDone` when the selection stops changing.
  109. this._fireChangeDoneDebounced();
  110. } );
  111. // Hide the toolbar when the selection stops changing.
  112. this.listenTo( this, '_selectionChangeDone', () => this._showPanel() );
  113. }
  114. /**
  115. * Adds panel view to the {@link: #_balloon} and attaches panel to the selection.
  116. *
  117. * @private
  118. */
  119. _showPanel() {
  120. const editingView = this.editor.editing.view;
  121. // Do not add toolbar to the balloon stack twice.
  122. if ( this._balloon.hasView( this.toolbarView ) ) {
  123. return;
  124. }
  125. // This implementation assumes that only non–collapsed selections gets the contextual toolbar.
  126. if ( !editingView.isFocused || editingView.selection.isCollapsed ) {
  127. return;
  128. }
  129. // Add panel to the common editor contextual balloon.
  130. this._balloon.add( {
  131. view: this.toolbarView,
  132. position: this._getBalloonPositionData()
  133. } );
  134. // Update panel position when selection changes while balloon will be opened (by a collaboration).
  135. this.listenTo( this.editor.editing.view, 'render', () => {
  136. this._balloon.updatePosition( this._getBalloonPositionData() );
  137. } );
  138. }
  139. /**
  140. * Removes panel from the {@link: #_balloon}.
  141. *
  142. * @private
  143. */
  144. _hidePanel() {
  145. if ( this._balloon.hasView( this.toolbarView ) ) {
  146. this.stopListening( this.editor.editing.view, 'render' );
  147. this._balloon.remove( this.toolbarView );
  148. }
  149. }
  150. /**
  151. * Returns positioning options for the {@link #_balloon}. They control the way balloon is attached
  152. * to the selection.
  153. *
  154. * @private
  155. * @returns {module:utils/dom/position~Options}
  156. */
  157. _getBalloonPositionData() {
  158. const editingView = this.editor.editing.view;
  159. // Get direction of the selection.
  160. const isBackward = editingView.selection.isBackward;
  161. // getBoundingClientRect() makes no sense when the selection spans across number
  162. // of lines of text. Using getClientRects() allows us to browse micro–ranges
  163. // that would normally make up the bounding client rect.
  164. const rangeRects = editingView.domConverter.viewRangeToDom( editingView.selection.getFirstRange() ).getClientRects();
  165. // Select the proper range rect depending on the direction of the selection.
  166. const rangeRect = isBackward ? rangeRects.item( 0 ) : rangeRects.item( rangeRects.length - 1 );
  167. return {
  168. target: rangeRect,
  169. positions: isBackward ?
  170. [ positions.backwardSelection, positions.backwardSelectionAlternative ] :
  171. [ positions.forwardSelection, positions.forwardSelectionAlternative ],
  172. };
  173. }
  174. /**
  175. * @inheritDoc
  176. */
  177. destroy() {
  178. this._fireChangeDoneDebounced.cancel();
  179. this.stopListening();
  180. super.destroy();
  181. }
  182. }
  183. // List of available toolbar positions.
  184. const arrowVOffset = BalloonPanelView.arrowVerticalOffset;
  185. const positions = {
  186. // [text range]
  187. // ^
  188. // +-----------------+
  189. // | Balloon |
  190. // +-----------------+
  191. forwardSelection: ( targetRect, balloonRect ) => ( {
  192. top: targetRect.bottom + arrowVOffset,
  193. left: targetRect.right - balloonRect.width / 2,
  194. name: 's'
  195. } ),
  196. // +-----------------+
  197. // | Balloon |
  198. // +-----------------+
  199. // V
  200. // [text range]
  201. forwardSelectionAlternative: ( targetRect, balloonRect ) => ( {
  202. top: targetRect.top - balloonRect.height - arrowVOffset,
  203. left: targetRect.right - balloonRect.width / 2,
  204. name: 'n'
  205. } ),
  206. // +-----------------+
  207. // | Balloon |
  208. // +-----------------+
  209. // V
  210. // [text range]
  211. backwardSelection: ( targetRect, balloonRect ) => ( {
  212. top: targetRect.top - balloonRect.height - arrowVOffset,
  213. left: targetRect.left - balloonRect.width / 2,
  214. name: 'n'
  215. } ),
  216. // [text range]
  217. // ^
  218. // +-----------------+
  219. // | Balloon |
  220. // +-----------------+
  221. backwardSelectionAlternative: ( targetRect, balloonRect ) => ( {
  222. top: targetRect.bottom + arrowVOffset,
  223. left: targetRect.left - balloonRect.width / 2,
  224. name: 's'
  225. } )
  226. };