8
0
Pārlūkot izejas kodu

Add warning message to PluginCollection#get() when plugin is not loaded.

(cherry picked from commit decc5911f6050b288f6c1d92d75b2bc66be6c065)
Maciej Gołaszewski 7 gadi atpakaļ
vecāks
revīzija
d47d1f78d2

+ 26 - 1
packages/ckeditor5-core/src/plugincollection.js

@@ -76,7 +76,32 @@ export default class PluginCollection {
 	 * @returns {module:core/plugin~PluginInterface}
 	 */
 	get( key ) {
-		return this._plugins.get( key );
+		const plugin = this._plugins.get( key );
+
+		if ( !plugin ) {
+			/**
+			 * The plugin is not loaded and could not be obtained.
+			 *
+			 * Plugin classes (constructors) need to be provided to the editor and must be loaded before they can be obtained from
+			 * the plugin collection.
+			 * This is usually done in CKEditor 5 builds by setting the {@link module:core/editor/editor~Editor.builtinPlugins}
+			 * property.
+			 *
+			 * @error plugincollection-plugin-not-loaded
+			 * @param {String} plugin The name of the plugin which is not loaded.
+			 */
+			const warnMessage = 'plugincollection-plugin-not-loaded: The requested plugin is not loaded.';
+
+			let pluginName = key;
+
+			if ( typeof key == 'function' ) {
+				pluginName = key.pluginName || key.name;
+			}
+
+			log.warn( warnMessage, { plugin: pluginName } );
+		}
+
+		return plugin;
 	}
 
 	/**

+ 53 - 0
packages/ckeditor5-core/tests/plugincollection.js

@@ -448,6 +448,59 @@ describe( 'PluginCollection', () => {
 					expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
 				} );
 		} );
+
+		it( 'warns if plugin cannot be retrieved by name', () => {
+			const logSpy = testUtils.sinon.stub( log, 'warn' );
+
+			const plugins = new PluginCollection( editor, availablePlugins );
+
+			return plugins.load( [] ).then( () => {
+				const plugin = plugins.get( 'foo' );
+
+				expect( plugin ).to.be.undefined;
+
+				expect( logSpy.calledOnce ).to.equal( true );
+				expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-not-loaded:/ );
+				expect( logSpy.firstCall.args[ 1 ] ).to.deep.equal( { plugin: 'foo' } );
+			} );
+		} );
+
+		it( 'warns if plugin cannot be retrieved by class', () => {
+			class SomePlugin extends Plugin {}
+			SomePlugin.pluginName = 'foo';
+
+			const logSpy = testUtils.sinon.stub( log, 'warn' );
+
+			const plugins = new PluginCollection( editor, availablePlugins );
+
+			return plugins.load( [] ).then( () => {
+				const plugin = plugins.get( SomePlugin );
+
+				expect( plugin ).to.be.undefined;
+
+				expect( logSpy.calledOnce ).to.equal( true );
+				expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-not-loaded:/ );
+				expect( logSpy.firstCall.args[ 1 ] ).to.deep.equal( { plugin: 'foo' } );
+			} );
+		} );
+
+		it( 'logs function (class) name if plugin cannot be retrieved by class', () => {
+			class SomePlugin extends Plugin {}
+
+			const logSpy = testUtils.sinon.stub( log, 'warn' );
+
+			const plugins = new PluginCollection( editor, availablePlugins );
+
+			return plugins.load( [] ).then( () => {
+				const plugin = plugins.get( SomePlugin );
+
+				expect( plugin ).to.be.undefined;
+
+				expect( logSpy.calledOnce ).to.equal( true );
+				expect( logSpy.firstCall.args[ 0 ] ).to.match( /^plugincollection-plugin-not-loaded:/ );
+				expect( logSpy.firstCall.args[ 1 ] ).to.deep.equal( { plugin: 'SomePlugin' } );
+			} );
+		} );
 	} );
 
 	describe( 'destroy()', () => {