Przeglądaj źródła

Feature: PluginCollection handles with plugins which do not extend the base Plugin class.

Kamil Piechaczek 8 lat temu
rodzic
commit
faa693dbae

+ 0 - 18
packages/ckeditor5-core/src/plugincollection.js

@@ -7,7 +7,6 @@
  * @module core/plugincollection
  */
 
-import Plugin from './plugin';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import log from '@ckeditor/ckeditor5-utils/src/log';
 
@@ -150,8 +149,6 @@ export default class PluginCollection {
 			return new Promise( resolve => {
 				loading.add( PluginConstructor );
 
-				assertIsPlugin( PluginConstructor );
-
 				if ( PluginConstructor.requires ) {
 					PluginConstructor.requires.forEach( RequiredPluginConstructorOrName => {
 						const RequiredPluginConstructor = getPluginConstructor( RequiredPluginConstructorOrName );
@@ -191,21 +188,6 @@ export default class PluginCollection {
 			return that._availablePlugins.get( PluginConstructorOrName );
 		}
 
-		function assertIsPlugin( PluginConstructor ) {
-			if ( !( PluginConstructor.prototype instanceof Plugin ) ) {
-				/**
-				 * The loaded plugin module is not an instance of {@link module:core/plugin~PluginInterface}.
-				 *
-				 * @error plugincollection-instance
-				 * @param {*} plugin The constructor which is meant to be loaded as a plugin.
-				 */
-				throw new CKEditorError(
-					'plugincollection-instance: The loaded plugin module is not an instance of Plugin.',
-					{ plugin: PluginConstructor }
-				);
-			}
-		}
-
 		function getMissingPluginNames( plugins ) {
 			const missingPlugins = [];
 

+ 36 - 23
packages/ckeditor5-core/tests/plugincollection.js

@@ -187,52 +187,65 @@ describe( 'PluginCollection', () => {
 				} );
 		} );
 
-		it( 'should set the `editor` property on loaded plugins', () => {
+		it( 'should load plugin which does not extend the base Plugin class', () => {
+			class Y { }
+
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ PluginA, PluginB ] )
+			return plugins.load( [ Y ] )
 				.then( () => {
-					expect( plugins.get( PluginA ).editor ).to.equal( editor );
-					expect( plugins.get( PluginB ).editor ).to.equal( editor );
+					expect( getPlugins( plugins ).length ).to.equal( 1 );
 				} );
 		} );
 
-		it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
-			const logSpy = testUtils.sinon.stub( log, 'error' );
+		it( 'should load plugin which is a simple function', () => {
+			function pluginAsFunction( editor ) {
+				this.editor = editor;
+			}
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ PluginA, PluginX, PluginB ] )
-				// Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
+			return plugins.load( [ pluginAsFunction ] )
 				.then( () => {
-					throw new Error( 'Test error: this promise should not be resolved successfully' );
-				} )
-				.catch( err => {
-					expect( err ).to.be.an.instanceof( TestError );
-					expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
-
-					sinon.assert.calledOnce( logSpy );
-					expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
+					expect( getPlugins( plugins ).length ).to.equal( 1 );
 				} );
 		} );
 
-		it( 'should reject when loading a module which is not a plugin', () => {
-			class Y {}
+		it( 'should set the `editor` property on loaded plugins', () => {
+			const plugins = new PluginCollection( editor, availablePlugins );
+
+			function pluginAsFunction( editor ) {
+				this.editor = editor;
+			}
 
-			availablePlugins.push( Y );
+			class Y {
+				constructor( editor ) {
+					this.editor = editor;
+				}
+			}
 
+			return plugins.load( [ PluginA, PluginB, pluginAsFunction, Y ] )
+				.then( () => {
+					expect( plugins.get( PluginA ).editor ).to.equal( editor );
+					expect( plugins.get( PluginB ).editor ).to.equal( editor );
+					expect( plugins.get( pluginAsFunction ).editor ).to.equal( editor );
+					expect( plugins.get( Y ).editor ).to.equal( editor );
+				} );
+		} );
+
+		it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
 			const logSpy = testUtils.sinon.stub( log, 'error' );
 
 			const plugins = new PluginCollection( editor, availablePlugins );
 
-			return plugins.load( [ Y ] )
+			return plugins.load( [ PluginA, PluginX, PluginB ] )
 				// Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
 				.then( () => {
 					throw new Error( 'Test error: this promise should not be resolved successfully' );
 				} )
 				.catch( err => {
-					expect( err ).to.be.an.instanceof( CKEditorError );
-					expect( err.message ).to.match( /^plugincollection-instance/ );
+					expect( err ).to.be.an.instanceof( TestError );
+					expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
 
 					sinon.assert.calledOnce( logSpy );
 					expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
@@ -307,7 +320,7 @@ describe( 'PluginCollection', () => {
 		} );
 
 		it( 'should load chosen plugins (plugins are names, removePlugins contains an anonymous plugin)', () => {
-			class AnonymousPlugin extends Plugin {}
+			class AnonymousPlugin {}
 
 			const plugins = new PluginCollection( editor, [ AnonymousPlugin ].concat( availablePlugins ) );