| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /**
- * @module ui/toolbar/contextual/contextualtoolbar
- */
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import ContextualBalloon from '../../panel/balloon/contextualballoon';
- import ToolbarView from '../toolbarview';
- import BalloonPanelView from '../../panel/balloon/balloonpanelview.js';
- import debounce from '@ckeditor/ckeditor5-utils/src/lib/lodash/debounce';
- const defaultPositions = BalloonPanelView.defaultPositions;
- /**
- * The contextual toolbar.
- *
- * It uses the {@link module:ui/panel/balloon/contextualballoon~ContextualBalloon contextual balloon plugin}.
- *
- * @extends module:core/plugin~Plugin
- */
- export default class ContextualToolbar extends Plugin {
- /**
- * @inheritDoc
- */
- static get pluginName() {
- return 'contextualtoolbar';
- }
- /**
- * @inheritDoc
- */
- static get requires() {
- return [ ContextualBalloon ];
- }
- /**
- * @inheritDoc
- */
- init() {
- /**
- * The toolbar view displayed in the balloon.
- *
- * @member {module:ui/toolbar/toolbarview~ToolbarView}
- */
- this.toolbarView = new ToolbarView( this.editor.locale );
- /**
- * The contextual balloon plugin instance.
- *
- * @private
- * @member {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
- */
- this._balloon = this.editor.plugins.get( ContextualBalloon );
- /**
- * Fires {@link #event:_selectionChangeDebounced} event using `lodash#debounce`.
- *
- * This function is stored as a plugin property to make possible to cancel
- * trailing debounced invocation on destroy.
- *
- * @private
- * @member {Function}
- */
- this._fireSelectionChangeDebounced = debounce( () => this.fire( '_selectionChangeDebounced' ), 200 );
- // Attach lifecycle actions.
- this._handleSelectionChange();
- this._handleFocusChange();
- }
- /**
- * Creates toolbar components based on given configuration.
- * This needs to be done when all plugins will be ready.
- *
- * @inheritDoc
- */
- afterInit() {
- const config = this.editor.config.get( 'contextualToolbar' );
- const factory = this.editor.ui.componentFactory;
- return this.toolbarView.fillFromConfig( config, factory );
- }
- /**
- * Handles editor focus change and hides panel if it's needed.
- *
- * @private
- */
- _handleFocusChange() {
- const editor = this.editor;
- // Hide the panel View when editor loses focus but no the other way around.
- this.listenTo( editor.ui.focusTracker, 'change:isFocused', ( evt, name, isFocused ) => {
- if ( this._balloon.visibleView === this.toolbarView && !isFocused ) {
- this._hidePanel();
- }
- } );
- }
- /**
- * Handles {@link module:engine/model/document#selection} change and show or hide toolbar.
- *
- * Note that in this case it's better to listen to {@link module:engine/model/document modelDocument}
- * selection instead of {@link module:engine/view/document viewDocument} selection because the first one
- * doesn't fire `change` event after text style change (like bold or italic) and toolbar doesn't blink.
- *
- * @private
- */
- _handleSelectionChange() {
- const selection = this.editor.document.selection;
- this.listenTo( selection, 'change:range', ( evt, data ) => {
- // When the selection is not changed by a collaboration and when is not collapsed.
- if ( data.directChange || selection.isCollapsed ) {
- // Hide the toolbar when the selection starts changing.
- this._hidePanel();
- }
- // Fire internal `_selectionChangeDebounced` when the selection stops changing.
- this._fireSelectionChangeDebounced();
- } );
- // Hide the toolbar when the selection stops changing.
- this.listenTo( this, '_selectionChangeDebounced', () => this._showPanel() );
- }
- /**
- * Adds panel view to the {@link: #_balloon} and attaches panel to the selection.
- *
- * @private
- */
- _showPanel() {
- const editingView = this.editor.editing.view;
- // Do not add toolbar to the balloon stack twice.
- if ( this._balloon.hasView( this.toolbarView ) ) {
- return;
- }
- // This implementation assumes that only non–collapsed selections gets the contextual toolbar.
- if ( !editingView.isFocused || editingView.selection.isCollapsed ) {
- return;
- }
- // Add panel to the common editor contextual balloon.
- this._balloon.add( {
- view: this.toolbarView,
- position: this._getBalloonPositionData(),
- balloonClassName: 'ck-toolbar__container'
- } );
- // Update panel position when selection changes while balloon will be opened (by a collaboration).
- this.listenTo( this.editor.editing.view, 'render', () => {
- this._balloon.updatePosition( this._getBalloonPositionData() );
- } );
- }
- /**
- * Removes panel from the {@link: #_balloon}.
- *
- * @private
- */
- _hidePanel() {
- if ( this._balloon.hasView( this.toolbarView ) ) {
- this.stopListening( this.editor.editing.view, 'render' );
- this._balloon.remove( this.toolbarView );
- }
- }
- /**
- * Returns positioning options for the {@link #_balloon}. They control the way balloon is attached
- * to the selection.
- *
- * @private
- * @returns {module:utils/dom/position~Options}
- */
- _getBalloonPositionData() {
- const editingView = this.editor.editing.view;
- // Get direction of the selection.
- const isBackward = editingView.selection.isBackward;
- // getBoundingClientRect() makes no sense when the selection spans across number
- // of lines of text. Using getClientRects() allows us to browse micro–ranges
- // that would normally make up the bounding client rect.
- const rangeRects = editingView.domConverter.viewRangeToDom( editingView.selection.getFirstRange() ).getClientRects();
- // Select the proper range rect depending on the direction of the selection.
- const rangeRect = isBackward ? rangeRects.item( 0 ) : rangeRects.item( rangeRects.length - 1 );
- return {
- target: rangeRect,
- positions: isBackward ?
- [ defaultPositions.backwardSelection, defaultPositions.backwardSelectionAlternative ] :
- [ defaultPositions.forwardSelection, defaultPositions.forwardSelectionAlternative ],
- };
- }
- /**
- * @inheritDoc
- */
- destroy() {
- this._fireSelectionChangeDebounced.cancel();
- this.stopListening();
- super.destroy();
- }
- /**
- * This is internal plugin event which is fired 200 ms after model selection last change.
- * This is to makes easy test debounced action without need to use `setTimeout`.
- *
- * @protected
- * @event _selectionChangeDebounced
- */
- }
|