Procházet zdrojové kódy

Other: ToolbarView#fillFromConfig should warn when the factory does not provide a component. Closes #291.

Aleksander Nowodzinski před 8 roky
rodič
revize
d2a4482f1a

+ 24 - 3
packages/ckeditor5-ui/src/toolbar/toolbarview.js

@@ -14,6 +14,7 @@ import FocusCycler from '../focuscycler';
 import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
 import ToolbarSeparatorView from './toolbarseparatorview';
 import preventDefault from '../bindings/preventdefault.js';
+import log from '@ckeditor/ckeditor5-utils/src/log';
 
 /**
  * The toolbar view class.
@@ -126,9 +127,29 @@ export default class ToolbarView extends View {
 		}
 
 		config.map( name => {
-			const component = name == '|' ? new ToolbarSeparatorView() : factory.create( name );
-
-			this.items.add( component );
+			if ( name == '|' ) {
+				this.items.add( new ToolbarSeparatorView() );
+			} else if ( factory.has( name ) ) {
+				this.items.add( factory.create( name ) );
+			} else {
+				/**
+				 * There was a problem with expanding the toolbar configuration into toolbar items.
+				 * The provided factory does not provide a component of such a name and because of that
+				 * it has not been added to the {@link #items}.
+				 *
+				 * This warning usually shows up when the plugin that is supposed to register the toolbar
+				 * component in the factory has not been loaded or there's a typo in the configuration
+				 * of the toolbar.
+				 *
+				 * @error toolbarview-missing-component
+				 * @param {String} name The name of the component.
+				 * @param {module:ui/componentfactory~ComponentFactory} factory The factory that is missing the component.
+				 */
+				log.warn(
+					'toolbarview-missing-component: There is no such component in the factory.',
+					{ name, factory }
+				);
+			}
 		} );
 	}
 }

+ 21 - 0
packages/ckeditor5-ui/tests/toolbar/toolbarview.js

@@ -13,11 +13,15 @@ import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
 import FocusCycler from '../../src/focuscycler';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
 import ViewCollection from '../../src/viewcollection';
+import log from '@ckeditor/ckeditor5-utils/src/log';
 import View from '../../src/view';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 
 describe( 'ToolbarView', () => {
 	let locale, view;
 
+	testUtils.createSinonSandbox();
+
 	beforeEach( () => {
 		locale = {};
 		view = new ToolbarView( locale );
@@ -247,6 +251,23 @@ describe( 'ToolbarView', () => {
 			expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
 			expect( items.get( 3 ).name ).to.equal( 'foo' );
 		} );
+
+		it( 'warns if there is no such component in the factory', () => {
+			const items = view.items;
+			testUtils.sinon.stub( log, 'warn' );
+
+			view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
+
+			expect( items ).to.have.length( 2 );
+			expect( items.get( 0 ).name ).to.equal( 'foo' );
+			expect( items.get( 1 ).name ).to.equal( 'bar' );
+
+			sinon.assert.calledOnce( log.warn );
+			sinon.assert.calledWithExactly( log.warn,
+				sinon.match( /^toolbarview-missing-component/ ),
+				{ name: 'baz', factory }
+			);
+		} );
 	} );
 } );