8
0

widgettoolbarrepository.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module widget/widgettoolbarrepository
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
  10. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  11. import BalloonPanelView from '@ckeditor/ckeditor5-ui/src/panel/balloon/balloonpanelview';
  12. import {
  13. isWidget,
  14. centeredBalloonPositionForLongWidgets
  15. } from './utils';
  16. import CKEditorError, { logWarning } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  17. /**
  18. * Widget toolbar repository plugin. A central point for registering widget toolbars. This plugin handles the whole
  19. * toolbar rendering process and exposes a concise API.
  20. *
  21. * To add a toolbar for your widget use the {@link ~WidgetToolbarRepository#register `WidgetToolbarRepository#register()`} method.
  22. *
  23. * The following example comes from the {@link module:image/imagetoolbar~ImageToolbar} plugin:
  24. *
  25. * class ImageToolbar extends Plugin {
  26. * static get requires() {
  27. * return [ WidgetToolbarRepository ];
  28. * }
  29. *
  30. * afterInit() {
  31. * const editor = this.editor;
  32. * const widgetToolbarRepository = editor.plugins.get( WidgetToolbarRepository );
  33. *
  34. * widgetToolbarRepository.register( 'image', {
  35. * items: editor.config.get( 'image.toolbar' ),
  36. * getRelatedElement: getSelectedImageWidget
  37. * } );
  38. * }
  39. * }
  40. */
  41. export default class WidgetToolbarRepository extends Plugin {
  42. /**
  43. * @inheritDoc
  44. */
  45. static get requires() {
  46. return [ ContextualBalloon ];
  47. }
  48. /**
  49. * @inheritDoc
  50. */
  51. static get pluginName() {
  52. return 'WidgetToolbarRepository';
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. init() {
  58. const editor = this.editor;
  59. // Disables the default balloon toolbar for all widgets.
  60. if ( editor.plugins.has( 'BalloonToolbar' ) ) {
  61. const balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
  62. this.listenTo( balloonToolbar, 'show', evt => {
  63. if ( isWidgetSelected( editor.editing.view.document.selection ) ) {
  64. evt.stop();
  65. }
  66. }, { priority: 'high' } );
  67. }
  68. /**
  69. * A map of toolbar definitions.
  70. *
  71. * @protected
  72. * @member {Map.<String,module:widget/widgettoolbarrepository~WidgetRepositoryToolbarDefinition>} #_toolbarDefinitions
  73. */
  74. this._toolbarDefinitions = new Map();
  75. /**
  76. * @private
  77. */
  78. this._balloon = this.editor.plugins.get( 'ContextualBalloon' );
  79. this.on( 'change:isEnabled', () => {
  80. this._updateToolbarsVisibility();
  81. } );
  82. this.listenTo( editor.ui, 'update', () => {
  83. this._updateToolbarsVisibility();
  84. } );
  85. // UI#update is not fired after focus is back in editor, we need to check if balloon panel should be visible.
  86. this.listenTo( editor.ui.focusTracker, 'change:isFocused', () => {
  87. this._updateToolbarsVisibility();
  88. }, { priority: 'low' } );
  89. }
  90. destroy() {
  91. super.destroy();
  92. for ( const toolbarConfig of this._toolbarDefinitions.values() ) {
  93. toolbarConfig.view.destroy();
  94. }
  95. }
  96. /**
  97. * Registers toolbar in the WidgetToolbarRepository. It renders it in the `ContextualBalloon` based on the value of the invoked
  98. * `getRelatedElement` function. Toolbar items are gathered from `items` array.
  99. * The balloon's CSS class is by default `ck-toolbar-container` and may be override with the `balloonClassName` option.
  100. *
  101. * Note: This method should be called in the {@link module:core/plugin~PluginInterface#afterInit `Plugin#afterInit()`}
  102. * callback (or later) to make sure that the given toolbar items were already registered by other plugins.
  103. *
  104. * @param {String} toolbarId An id for the toolbar. Used to
  105. * @param {Object} options
  106. * @param {String} [options.ariaLabel] Label used by assistive technologies to describe this toolbar element.
  107. * @param {Array.<String>} options.items Array of toolbar items.
  108. * @param {Function} options.getRelatedElement Callback which returns an element the toolbar should be attached to.
  109. * @param {String} [options.balloonClassName='ck-toolbar-container'] CSS class for the widget balloon.
  110. */
  111. register( toolbarId, { ariaLabel, items, getRelatedElement, balloonClassName = 'ck-toolbar-container' } ) {
  112. // Trying to register a toolbar without any item.
  113. if ( !items.length ) {
  114. /**
  115. * When {@link #register} a new toolbar, you need to provide a non-empty array with
  116. * the items that will be inserted into the toolbar.
  117. *
  118. * @error widget-toolbar-no-items
  119. */
  120. logWarning( 'widget-toolbar-no-items', { toolbarId } );
  121. return;
  122. }
  123. const editor = this.editor;
  124. const t = editor.t;
  125. const toolbarView = new ToolbarView( editor.locale );
  126. toolbarView.ariaLabel = ariaLabel || t( 'Widget toolbar' );
  127. if ( this._toolbarDefinitions.has( toolbarId ) ) {
  128. /**
  129. * Toolbar with the given id was already added.
  130. *
  131. * @error widget-toolbar-duplicated
  132. * @param toolbarId Toolbar id.
  133. */
  134. throw new CKEditorError( 'widget-toolbar-duplicated', this, { toolbarId } );
  135. }
  136. toolbarView.fillFromConfig( items, editor.ui.componentFactory );
  137. this._toolbarDefinitions.set( toolbarId, {
  138. view: toolbarView,
  139. getRelatedElement,
  140. balloonClassName
  141. } );
  142. }
  143. /**
  144. * Iterates over stored toolbars and makes them visible or hidden.
  145. *
  146. * @private
  147. */
  148. _updateToolbarsVisibility() {
  149. let maxRelatedElementDepth = 0;
  150. let deepestRelatedElement = null;
  151. let deepestToolbarDefinition = null;
  152. for ( const definition of this._toolbarDefinitions.values() ) {
  153. const relatedElement = definition.getRelatedElement( this.editor.editing.view.document.selection );
  154. if ( !this.isEnabled || !relatedElement ) {
  155. if ( this._isToolbarInBalloon( definition ) ) {
  156. this._hideToolbar( definition );
  157. }
  158. } else if ( !this.editor.ui.focusTracker.isFocused ) {
  159. if ( this._isToolbarVisible( definition ) ) {
  160. this._hideToolbar( definition );
  161. }
  162. } else {
  163. const relatedElementDepth = relatedElement.getAncestors().length;
  164. // Many toolbars can express willingness to be displayed but they do not know about
  165. // each other. Figure out which toolbar is deepest in the view tree to decide which
  166. // should be displayed. For instance, if a selected image is inside a table cell, display
  167. // the ImageToolbar rather than the TableToolbar (#60).
  168. if ( relatedElementDepth > maxRelatedElementDepth ) {
  169. maxRelatedElementDepth = relatedElementDepth;
  170. deepestRelatedElement = relatedElement;
  171. deepestToolbarDefinition = definition;
  172. }
  173. }
  174. }
  175. if ( deepestToolbarDefinition ) {
  176. this._showToolbar( deepestToolbarDefinition, deepestRelatedElement );
  177. }
  178. }
  179. /**
  180. * Hides the given toolbar.
  181. *
  182. * @private
  183. * @param {module:widget/widgettoolbarrepository~WidgetRepositoryToolbarDefinition} toolbarDefinition
  184. */
  185. _hideToolbar( toolbarDefinition ) {
  186. this._balloon.remove( toolbarDefinition.view );
  187. this.stopListening( this._balloon, 'change:visibleView' );
  188. }
  189. /**
  190. * Shows up the toolbar if the toolbar is not visible.
  191. * Otherwise, repositions the toolbar's balloon when toolbar's view is the most top view in balloon stack.
  192. *
  193. * It might happen here that the toolbar's view is under another view. Then do nothing as the other toolbar view
  194. * should be still visible after the {@link module:core/editor/editorui~EditorUI#event:update}.
  195. *
  196. * @private
  197. * @param {module:widget/widgettoolbarrepository~WidgetRepositoryToolbarDefinition} toolbarDefinition
  198. * @param {module:engine/view/element~Element} relatedElement
  199. */
  200. _showToolbar( toolbarDefinition, relatedElement ) {
  201. if ( this._isToolbarVisible( toolbarDefinition ) ) {
  202. repositionContextualBalloon( this.editor, relatedElement );
  203. } else if ( !this._isToolbarInBalloon( toolbarDefinition ) ) {
  204. this._balloon.add( {
  205. view: toolbarDefinition.view,
  206. position: getBalloonPositionData( this.editor, relatedElement ),
  207. balloonClassName: toolbarDefinition.balloonClassName
  208. } );
  209. // Update toolbar position each time stack with toolbar view is switched to visible.
  210. // This is in a case target element has changed when toolbar was in invisible stack
  211. // e.g. target image was wrapped by a block quote.
  212. // See https://github.com/ckeditor/ckeditor5-widget/issues/92.
  213. this.listenTo( this._balloon, 'change:visibleView', () => {
  214. for ( const definition of this._toolbarDefinitions.values() ) {
  215. if ( this._isToolbarVisible( definition ) ) {
  216. const relatedElement = definition.getRelatedElement( this.editor.editing.view.document.selection );
  217. repositionContextualBalloon( this.editor, relatedElement );
  218. }
  219. }
  220. } );
  221. }
  222. }
  223. /**
  224. * @private
  225. * @param {Object} toolbar
  226. * @returns {Boolean}
  227. */
  228. _isToolbarVisible( toolbar ) {
  229. return this._balloon.visibleView === toolbar.view;
  230. }
  231. /**
  232. * @private
  233. * @param {Object} toolbar
  234. * @returns {Boolean}
  235. */
  236. _isToolbarInBalloon( toolbar ) {
  237. return this._balloon.hasView( toolbar.view );
  238. }
  239. }
  240. function repositionContextualBalloon( editor, relatedElement ) {
  241. const balloon = editor.plugins.get( 'ContextualBalloon' );
  242. const position = getBalloonPositionData( editor, relatedElement );
  243. balloon.updatePosition( position );
  244. }
  245. function getBalloonPositionData( editor, relatedElement ) {
  246. const editingView = editor.editing.view;
  247. const defaultPositions = BalloonPanelView.defaultPositions;
  248. return {
  249. target: editingView.domConverter.mapViewToDom( relatedElement ),
  250. positions: [
  251. defaultPositions.northArrowSouth,
  252. defaultPositions.northArrowSouthWest,
  253. defaultPositions.northArrowSouthEast,
  254. defaultPositions.southArrowNorth,
  255. defaultPositions.southArrowNorthWest,
  256. defaultPositions.southArrowNorthEast,
  257. centeredBalloonPositionForLongWidgets
  258. ]
  259. };
  260. }
  261. function isWidgetSelected( selection ) {
  262. const viewElement = selection.getSelectedElement();
  263. return !!( viewElement && isWidget( viewElement ) );
  264. }
  265. /**
  266. * The toolbar definition object used by the toolbar repository to manage toolbars.
  267. * It contains information necessary to display the toolbar in the
  268. * {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon} and
  269. * update it during its life (display) cycle.
  270. *
  271. * @typedef {Object} module:widget/widgettoolbarrepository~WidgetRepositoryToolbarDefinition
  272. *
  273. * @property {module:ui/view~View} view The UI view of the toolbar.
  274. * @property {Function} getRelatedElement A function that returns an engine {@link module:engine/view/view~View}
  275. * element the toolbar is to be attached to. For instance, an image widget or a table widget (or `null` when
  276. * there is no such element). The function accepts an instance of {@link module:engine/view/selection~Selection}.
  277. * @property {String} balloonClassName CSS class for the widget balloon when a toolbar is displayed.
  278. */