8
0

widgettoolbarrepository.js 10 KB

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