浏览代码

Added support for plugin names.

Piotrek Koszuliński 9 年之前
父节点
当前提交
a94f7b3e45

+ 22 - 1
packages/ckeditor5-core/src/plugin.js

@@ -42,7 +42,28 @@ export default class Plugin {
 	 *		}
 	 *
 	 * @static
-	 * @member {Function[]} core.Plugin.requires
+	 * @member {Function[]|undefined} core.Plugin.requires
+	 */
+
+	/**
+	 * Optional name of the plugin. If set, the plugin will be available in {@link PluginCollection#get} by its
+	 * name and its constructor. If not, then only by its constructor.
+	 *
+	 * The name should reflect the package name + path to that module. E.g. `ckeditor5-image/src/image.js` plugin
+	 * should be named `ckeditor5-image/image`.
+	 *
+	 * To keep a plugin class definition tight it's recommended to define this property as a static getter:
+	 *
+	 *		import Image from './image.js';
+	 *
+	 *		export default class ImageCaption extends Plugin {
+     *			static get pluginName() {
+     *				return 'ckeditor5-image/image';
+     *			}
+	 *		}
+	 *
+	 * @static
+	 * @member {String|undefined} core.Plugin.pluginName
 	 */
 
 	/**

+ 15 - 7
packages/ckeditor5-core/src/plugincollection.js

@@ -35,14 +35,18 @@ export default class PluginCollection {
 	/**
 	 * Collection iterator. Returns `[ PluginConstructor, pluginInstance ]` pairs.
 	 */
-	[ Symbol.iterator ]() {
-		return this._plugins[ Symbol.iterator ]();
+	*[ Symbol.iterator ]() {
+		for ( const entry of this._plugins ) {
+			if ( typeof entry[ 0 ] == 'function' ) {
+				yield entry;
+			}
+		}
 	}
 
 	/**
-	 * Gets the plugin instance by its constructor.
+	 * Gets the plugin instance by its constructor or name.
 	 *
-	 * @param {Function} key The plugin constructor.
+	 * @param {Function|String} key The plugin constructor or {@link core.Plugin.plugiName name}.
 	 * @returns {core.Plugin}
 	 */
 	get( key ) {
@@ -124,10 +128,14 @@ export default class PluginCollection {
 	 * Adds the plugin to the collection. Exposed mainly for testing purposes.
 	 *
 	 * @protected
-	 * @param {Function} key The plugin constructor.
+	 * @param {Function} PluginConstructor The plugin constructor.
 	 * @param {core.Plugin} plugin The instance of the plugin.
 	 */
-	_add( key, plugin ) {
-		this._plugins.set( key, plugin );
+	_add( PluginConstructor, plugin ) {
+		this._plugins.set( PluginConstructor, plugin );
+
+		if ( PluginConstructor.pluginName ) {
+			this._plugins.set( PluginConstructor.pluginName, plugin );
+		}
 	}
 }

+ 56 - 8
packages/ckeditor5-core/tests/plugincollection.js

@@ -48,7 +48,7 @@ before( () => {
 } );
 
 describe( 'PluginCollection', () => {
-	describe( 'load', () => {
+	describe( 'load()', () => {
 		it( 'should not fail when trying to load 0 plugins (empty array)', () => {
 			let plugins = new PluginCollection( editor );
 
@@ -191,19 +191,67 @@ describe( 'PluginCollection', () => {
 				} );
 		} );
 	} );
-} );
 
-function createPlugin( name, baseClass ) {
-	baseClass = baseClass || Plugin;
+	describe( 'get()', () => {
+		it( 'retrieves plugin by its constructor', () => {
+			let plugins = new PluginCollection( editor );
+
+			class SomePlugin extends Plugin {}
+
+			return plugins.load( [ SomePlugin ] )
+				.then( () => {
+					expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
+				} );
+		} );
+
+		it( 'retrieves plugin by its name and constructor', () => {
+			let plugins = new PluginCollection( editor );
+
+			class SomePlugin extends Plugin {}
+			SomePlugin.pluginName = 'foo/bar';
+
+			return plugins.load( [ SomePlugin ] )
+				.then( () => {
+					expect( plugins.get( 'foo/bar' ) ).to.be.instanceOf( SomePlugin );
+					expect( plugins.get( SomePlugin ) ).to.be.instanceOf( SomePlugin );
+				} );
+		} );
+	} );
+
+	describe( 'iterator', () => {
+		it( 'exists', () => {
+			let plugins = new PluginCollection( editor );
+
+			expect( plugins ).to.have.property( Symbol.iterator );
+		} );
+
+		it( 'returns only plugins by constructors', () => {
+			let plugins = new PluginCollection( editor );
+
+			class SomePlugin1 extends Plugin {}
+			class SomePlugin2 extends Plugin {}
+			SomePlugin2.pluginName = 'foo/bar';
+
+			return plugins.load( [ SomePlugin1, SomePlugin2 ] )
+				.then( () => {
+					const pluginConstructors = Array.from( plugins )
+						.map( entry => entry[ 0 ] );
+
+					expect( pluginConstructors ).to.have.members( [ SomePlugin1, SomePlugin2 ] );
+				} );
+		} );
+	} );
+} );
 
-	const P = class extends baseClass {
+function createPlugin( name ) {
+	const P = class extends Plugin {
 		constructor( editor ) {
 			super( editor );
-			this._pluginName = name;
+			this.pluginName = name;
 		}
 	};
 
-	P._pluginName = name;
+	P.pluginName = name;
 
 	return P;
 }
@@ -222,5 +270,5 @@ function getPluginsFromSpy( addSpy ) {
 }
 
 function getPluginNames( plugins ) {
-	return plugins.map( ( plugin ) => plugin._pluginName );
+	return plugins.map( ( plugin ) => plugin.pluginName );
 }