contextualtoolbar.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/contextual/contextualtoolbar
  7. */
  8. import Template from '../../template';
  9. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  10. import ContextualBalloon from '../../panel/balloon/contextualballoon';
  11. import ToolbarView from '../toolbarview';
  12. import BalloonPanelView from '../../panel/balloon/balloonpanelview.js';
  13. import debounce from '@ckeditor/ckeditor5-utils/src/lib/lodash/debounce';
  14. import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
  15. /**
  16. * The contextual toolbar.
  17. *
  18. * It uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon plugin}.
  19. *
  20. * @extends module:core/plugin~Plugin
  21. */
  22. export default class ContextualToolbar extends Plugin {
  23. /**
  24. * @inheritDoc
  25. */
  26. static get pluginName() {
  27. return 'ContextualToolbar';
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. static get requires() {
  33. return [ ContextualBalloon ];
  34. }
  35. /**
  36. * @inheritDoc
  37. */
  38. init() {
  39. /**
  40. * The toolbar view displayed in the balloon.
  41. *
  42. * @member {module:ui/toolbar/toolbarview~ToolbarView}
  43. */
  44. this.toolbarView = new ToolbarView( this.editor.locale );
  45. Template.extend( this.toolbarView.template, {
  46. attributes: {
  47. class: [
  48. 'ck-editor-toolbar',
  49. 'ck-toolbar_floating'
  50. ]
  51. }
  52. } );
  53. /**
  54. * The contextual balloon plugin instance.
  55. *
  56. * @private
  57. * @member {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
  58. */
  59. this._balloon = this.editor.plugins.get( ContextualBalloon );
  60. /**
  61. * Fires {@link #event:_selectionChangeDebounced} event using `lodash#debounce`.
  62. *
  63. * This function is stored as a plugin property to make possible to cancel
  64. * trailing debounced invocation on destroy.
  65. *
  66. * @private
  67. * @member {Function}
  68. */
  69. this._fireSelectionChangeDebounced = debounce( () => this.fire( '_selectionChangeDebounced' ), 200 );
  70. // Attach lifecycle actions.
  71. this._handleSelectionChange();
  72. this._handleFocusChange();
  73. // ContextualToolbar is displayed using event to make possible to prevent displaying it by stopping this event.
  74. this.on( 'show', () => this._show(), { priority: 'low' } );
  75. }
  76. /**
  77. * Creates toolbar components based on given configuration.
  78. * This needs to be done when all plugins will be ready.
  79. *
  80. * @inheritDoc
  81. */
  82. afterInit() {
  83. const config = this.editor.config.get( 'contextualToolbar' );
  84. const factory = this.editor.ui.componentFactory;
  85. this.toolbarView.fillFromConfig( config, factory );
  86. }
  87. /**
  88. * Handles editor focus change and hides panel if it's needed.
  89. *
  90. * @private
  91. */
  92. _handleFocusChange() {
  93. const editor = this.editor;
  94. // Hide the panel View when editor loses focus but no the other way around.
  95. this.listenTo( editor.ui.focusTracker, 'change:isFocused', ( evt, name, isFocused ) => {
  96. if ( this._balloon.visibleView === this.toolbarView && !isFocused ) {
  97. this.hide();
  98. }
  99. } );
  100. }
  101. /**
  102. * Handles {@link module:engine/model/document~Document#selection} change and show or hide toolbar.
  103. *
  104. * Note that in this case it's better to listen to {@link module:engine/model/document~Document model document}
  105. * selection instead of {@link module:engine/view/document~Document view document} selection because the first one
  106. * doesn't fire `change` event after text style change (like bold or italic) and toolbar doesn't blink.
  107. *
  108. * @private
  109. */
  110. _handleSelectionChange() {
  111. const selection = this.editor.document.selection;
  112. const editingView = this.editor.editing.view;
  113. this.listenTo( selection, 'change:range', ( evt, data ) => {
  114. // When the selection is not changed by a collaboration and when is not collapsed.
  115. if ( data.directChange || selection.isCollapsed ) {
  116. // Hide the toolbar when the selection starts changing.
  117. this.hide();
  118. }
  119. // Fire internal `_selectionChangeDebounced` when the selection stops changing.
  120. this._fireSelectionChangeDebounced();
  121. } );
  122. // Hide the toolbar when the selection stops changing.
  123. this.listenTo( this, '_selectionChangeDebounced', () => {
  124. // This implementation assumes that only non–collapsed selections gets the contextual toolbar.
  125. if ( editingView.isFocused && !editingView.selection.isCollapsed ) {
  126. this.show();
  127. }
  128. } );
  129. }
  130. /**
  131. * Shows the toolbar and attaches it to the selection.
  132. *
  133. * Fires {@link #event:show} event which can be stopped that prevents toolbar from being displayed.
  134. */
  135. show() {
  136. // Do not add toolbar to the balloon stack twice.
  137. if ( this._balloon.hasView( this.toolbarView ) ) {
  138. return;
  139. }
  140. this.fire( 'show' );
  141. }
  142. /**
  143. * Hides the toolbar.
  144. */
  145. hide() {
  146. if ( this._balloon.hasView( this.toolbarView ) ) {
  147. this.stopListening( this.editor.editing.view, 'render' );
  148. this._balloon.remove( this.toolbarView );
  149. }
  150. }
  151. /**
  152. * Executes showing toolbar.
  153. * When {@link #event:show} is not stopped then toolbar will be displayed.
  154. *
  155. * @private
  156. */
  157. _show() {
  158. // Don not show ContextualToolbar when all components inside are disabled
  159. // see https://github.com/ckeditor/ckeditor5-ui/issues/269.
  160. if ( Array.from( this.toolbarView.items ).every( item => item.isEnabled !== undefined && !item.isEnabled ) ) {
  161. return;
  162. }
  163. // Update panel position when selection changes (by external document changes) while balloon is opened.
  164. this.listenTo( this.editor.editing.view, 'render', () => {
  165. this._balloon.updatePosition( this._getBalloonPositionData() );
  166. } );
  167. // Add panel to the common editor contextual balloon.
  168. this._balloon.add( {
  169. view: this.toolbarView,
  170. position: this._getBalloonPositionData(),
  171. balloonClassName: 'ck-toolbar-container ck-editor-toolbar-container'
  172. } );
  173. }
  174. /**
  175. * Returns positioning options for the {@link #_balloon}. They control the way balloon is attached
  176. * to the selection.
  177. *
  178. * @private
  179. * @returns {module:utils/dom/position~Options}
  180. */
  181. _getBalloonPositionData() {
  182. const editingView = this.editor.editing.view;
  183. // Get direction of the selection.
  184. const isBackward = editingView.selection.isBackward;
  185. return {
  186. // Because the target for BalloonPanelView is a Rect (not DOMRange), it's geometry will stay fixed
  187. // as the window scrolls. To let the BalloonPanelView follow such Rect, is must be continuously
  188. // computed and hence, the target is defined as a function instead of a static value.
  189. // https://github.com/ckeditor/ckeditor5-ui/issues/195
  190. target: () => {
  191. const range = editingView.selection.getFirstRange();
  192. const rangeRects = Rect.getDomRangeRects( editingView.domConverter.viewRangeToDom( range ) );
  193. // Select the proper range rect depending on the direction of the selection.
  194. return rangeRects[ isBackward ? 0 : rangeRects.length - 1 ];
  195. },
  196. limiter: this.editor.ui.view.editable.element,
  197. positions: getBalloonPositions( isBackward )
  198. };
  199. }
  200. /**
  201. * @inheritDoc
  202. */
  203. destroy() {
  204. this._fireSelectionChangeDebounced.cancel();
  205. this.stopListening();
  206. super.destroy();
  207. }
  208. /**
  209. * This event is fired just before the toolbar shows.
  210. * Using this event, an external code can prevent ContextualToolbar
  211. * from being displayed by stopping it.
  212. *
  213. * @event show
  214. */
  215. /**
  216. * This is internal plugin event which is fired 200 ms after model selection last change.
  217. * This is to makes easy test debounced action without need to use `setTimeout`.
  218. *
  219. * @protected
  220. * @event _selectionChangeDebounced
  221. */
  222. }
  223. // Returns toolbar positions for the given direction of the selection.
  224. //
  225. // @private
  226. // @param {Boolean} isBackward
  227. // @returns {Array.<module:utils/dom/position~Position>}
  228. function getBalloonPositions( isBackward ) {
  229. const defaultPositions = BalloonPanelView.defaultPositions;
  230. return isBackward ? [
  231. defaultPositions.northWestArrowSouth,
  232. defaultPositions.northWestArrowSouthWest,
  233. defaultPositions.northWestArrowSouthEast,
  234. defaultPositions.southWestArrowNorth,
  235. defaultPositions.southWestArrowNorthWest,
  236. defaultPositions.southWestArrowNorthEast
  237. ] : [
  238. defaultPositions.southEastArrowNorth,
  239. defaultPositions.southEastArrowNorthEast,
  240. defaultPositions.southEastArrowNorthWest,
  241. defaultPositions.northEastArrowSouth,
  242. defaultPositions.northEastArrowSouthEast,
  243. defaultPositions.northEastArrowSouthWest
  244. ];
  245. }