Explorar o código

Moved TableToolbar logic to the WidgetToolbar.

Maciej Bukowski %!s(int64=7) %!d(string=hai) anos
pai
achega
79c6d3b471
Modificáronse 1 ficheiros con 6 adicións e 113 borrados
  1. 6 113
      packages/ckeditor5-table/src/tabletoolbar.js

+ 6 - 113
packages/ckeditor5-table/src/tabletoolbar.js

@@ -12,8 +12,7 @@ import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
 import ContextualBalloon from '@ckeditor/ckeditor5-ui/src/panel/balloon/contextualballoon';
 import { isTableWidgetSelected, isTableContentSelected } from './utils';
 import { repositionContextualBalloon, getBalloonPositionData } from './ui/utils';
-
-const balloonClassName = 'ck-toolbar-container';
+import WidgetToolbar from '@ckeditor/ckeditor5-widget/src/widgettoolbar';
 
 /**
  * The table toolbar class. It creates a table toolbar that shows up when the table widget is selected.
@@ -31,7 +30,7 @@ export default class TableToolbar extends Plugin {
 	 * @inheritDoc
 	 */
 	static get requires() {
-		return [ ContextualBalloon ];
+		return [ WidgetToolbar ];
 	}
 
 	/**
@@ -41,123 +40,17 @@ export default class TableToolbar extends Plugin {
 		return 'TableToolbar';
 	}
 
-	/**
-	 * @inheritDoc
-	 */
-	init() {
-		const editor = this.editor;
-		const balloonToolbar = editor.plugins.get( 'BalloonToolbar' );
-
-		// If the `BalloonToolbar` plugin is loaded, it should be disabled for tables
-		// which have their own toolbar to avoid duplication.
-		// https://github.com/ckeditor/ckeditor5-image/issues/110
-		if ( balloonToolbar ) {
-			this.listenTo( balloonToolbar, 'show', evt => {
-				if ( isTableWidgetSelected( editor.editing.view.document.selection ) ) {
-					evt.stop();
-				}
-			}, { priority: 'high' } );
-		}
-	}
-
 	/**
 	 * @inheritDoc
 	 */
 	afterInit() {
 		const editor = this.editor;
-		const toolbarConfig = editor.config.get( 'table.toolbar' );
-
-		// Don't add the toolbar if there is no configuration.
-		if ( !toolbarConfig || !toolbarConfig.length ) {
-			return;
-		}
+		const widgetToolbar = editor.plugins.get( 'WidgetToolbar' );
 
-		/**
-		 * A contextual balloon plugin instance.
-		 *
-		 * @private
-		 * @member {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
-		 */
-		this._balloon = this.editor.plugins.get( 'ContextualBalloon' );
-
-		/**
-		 * A toolbar view instance used to display the buttons specific for table editing.
-		 *
-		 * @protected
-		 * @type {module:ui/toolbar/toolbarview~ToolbarView}
-		 */
-		this._toolbar = new ToolbarView();
-
-		// Add buttons to the toolbar.
-		this._toolbar.fillFromConfig( toolbarConfig, editor.ui.componentFactory );
-
-		// Show balloon panel each time table widget is selected.
-		this.listenTo( editor.ui, 'update', () => {
-			this._checkIsVisible();
+		widgetToolbar.add( {
+			toolbarItems: editor.config.get( 'table.toolbar' ) || [],
+			isSelected: isTableContentSelected,
 		} );
-
-		// There is no render method after focus is back in editor, we need to check if balloon panel should be visible.
-		this.listenTo( editor.ui.focusTracker, 'change:isFocused', () => {
-			this._checkIsVisible();
-		}, { priority: 'low' } );
-	}
-
-	/**
-	 * Checks whether the toolbar should show up or hide depending on the current selection.
-	 *
-	 * @private
-	 */
-	_checkIsVisible() {
-		const editor = this.editor;
-		const viewSelection = editor.editing.view.document.selection;
-
-		if ( !editor.ui.focusTracker.isFocused || !isTableContentSelected( viewSelection ) ) {
-			this._hideToolbar();
-		} else {
-			this._showToolbar();
-		}
-	}
-
-	/**
-	 * Shows the {@link #_toolbar} in the {@link #_balloon}.
-	 *
-	 * @private
-	 */
-	_showToolbar() {
-		const editor = this.editor;
-
-		if ( this._isVisible ) {
-			repositionContextualBalloon( editor );
-		} else if ( !this._balloon.hasView( this._toolbar ) ) {
-			this._balloon.add( {
-				view: this._toolbar,
-				position: getBalloonPositionData( editor ),
-				balloonClassName
-			} );
-		}
-	}
-
-	/**
-	 * Removes the {@link #_toolbar} from the {@link #_balloon}.
-	 *
-	 * @private
-	 */
-	_hideToolbar() {
-		if ( !this._isVisible ) {
-			return;
-		}
-
-		this._balloon.remove( this._toolbar );
-	}
-
-	/**
-	 * Returns `true` when the {@link #_toolbar} is the visible view in the {@link #_balloon}.
-	 *
-	 * @private
-	 * @type {Boolean}
-	 */
-	get _isVisible() {
-		return this._balloon.visibleView == this._toolbar;
 	}
 }