Explorar o código

Removed decorable method for disabling BlockToolbar.

Oskar Wróbel %!s(int64=7) %!d(string=hai) anos
pai
achega
4cdc20ea59

+ 6 - 34
packages/ckeditor5-ui/src/toolbar/block/blocktoolbar.js

@@ -30,8 +30,10 @@ import iconPilcrow from '../../../theme/icons/pilcrow.svg';
  * {@link module:core/editor/editorconfig~EditorConfig#blockToolbar} appears.
  *
  * By default button is allowed to be displayed next to all elements marked in
- * {@link module:engine/model/schema~Schema} as `$block` elements that are not `objects`.
- * This behavior can be customise through decorable {@link ~BlockToolbar#checkAllowed} method.
+ * {@link module:engine/model/schema~Schema} as `$block` elements for which there is at least
+ * one available option in toolbar. E.g. Toolbar with {@link module:paragraph/paragraph~Paragraph} and
+ * {@link module:heading/heading~Heading} won't be displayed next to {@link module:image/image~Image} because
+ * {@link module:engine/model/schema~Schema} disallows to change format of {@link module:image/image~Image}.
  *
  * By default button right bound will be attached to the left bound of the
  * {@link module:engine/view/editableelement~EditableElement}:
@@ -110,10 +112,6 @@ export default class BlockToolbar extends Plugin {
 			}
 		} );
 
-		// Checking if button is allowed for displaying next to given element is event–driven.
-		// It is possible to override #checkAllowed method and apply custom validation.
-		this.decorate( 'checkAllowed' );
-
 		// Enable as default.
 		this._enable();
 	}
@@ -193,32 +191,6 @@ export default class BlockToolbar extends Plugin {
 		return buttonView;
 	}
 
-	/**
-	 * Checks if block button is allowed for displaying next to given element
-	 * (when element is a $block and is not an object).
-	 *
-	 * Fires {@link #event:checkAllowed} event which can be handled and overridden to apply custom validation.
-	 *
-	 * Example how to disallow button for `h2` element:
-	 *
-	 * 		const blockToolbar = editor.plugins.get( 'BlockToolbar' );
-	 *
-	 * 		blockToolbar.on( 'checkAllowed', ( evt, args ) => {
-	 *			const modelElement = args[ 0 ];
-	 *
-	 *			if ( modelElement && modelElement.name === 'heading1' ) {
-	 *				evt.return = false;
-	 *			}
-	 * 		}, { priority: 'high' } );
-	 *
-	 * @fires checkAllowed
-	 * @param {module:engine/model/element~Element} modelElement Element where the selection is.
-	 * @returns {Boolean} `true` when block button is allowed to be displayed `false` otherwise.
-	 */
-	checkAllowed( modelElement ) {
-		return modelElement && Array.from( this.toolbarView.items ).some( item => item.isEnabled );
-	}
-
 	/**
 	 * Starts displaying button next to allowed elements.
 	 *
@@ -241,8 +213,8 @@ export default class BlockToolbar extends Plugin {
 			// Get first selected block, button will be attached to this element.
 			modelTarget = Array.from( model.document.selection.getSelectedBlocks() )[ 0 ];
 
-			// Do not attach block button when is not allowed for the given target element.
-			if ( !this.checkAllowed( modelTarget ) ) {
+			// Do not attach block button when there is no enabled item in toolbar for current block element.
+			if ( !modelTarget || Array.from( this.toolbarView.items ).every( item => !item.isEnabled ) ) {
 				this.buttonView.isVisible = false;
 
 				return;

+ 0 - 28
packages/ckeditor5-ui/tests/toolbar/block/blocktoolbar.js

@@ -19,7 +19,6 @@ import ParagraphButtonUI from '@ckeditor/ckeditor5-paragraph/src/paragraphbutton
 import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
 import Image from '@ckeditor/ckeditor5-image/src/image';
 import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
-import List from '@ckeditor/ckeditor5-list/src/list';
 
 import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
@@ -56,19 +55,6 @@ describe( 'BlockToolbar', () => {
 		expect( editor.editing.view.getObserver( ClickObserver ) ).to.be.instanceOf( ClickObserver );
 	} );
 
-	it( 'should initialize properly without Heading plugin', () => {
-		const element = document.createElement( 'div' );
-		document.body.appendChild( element );
-
-		return ClassicTestEditor.create( element, {
-			plugins: [ BlockToolbar, Paragraph, ParagraphButtonUI, BlockQuote, List ],
-			blockToolbar: [ 'paragraph', 'blockQuote' ]
-		} ).then( editor => {
-			element.remove();
-			return editor.destroy();
-		} );
-	} );
-
 	describe( 'child views', () => {
 		describe( 'panelView', () => {
 			it( 'should create view instance', () => {
@@ -262,20 +248,6 @@ describe( 'BlockToolbar', () => {
 				return editor.destroy();
 			} );
 		} );
-
-		it( 'should make it possible to provide custom validation', () => {
-			blockToolbar.on( 'checkAllowed', ( evt, args ) => {
-				const modelElement = args[ 0 ];
-
-				if ( modelElement.name === 'heading1' ) {
-					evt.return = false;
-				}
-			} );
-
-			setData( editor.model, '<heading1>foo[]bar</heading1>' );
-
-			expect( blockToolbar.buttonView.isVisible ).to.false;
-		} );
 	} );
 
 	describe( 'attaching button to the content', () => {