8
0
Просмотр исходного кода

Introduced `plugins.ready` event.

Krzysztof Krztoń 7 лет назад
Родитель
Сommit
c074cdd94f

+ 12 - 31
packages/ckeditor5-core/src/editor/editor.js

@@ -224,30 +224,17 @@ export default class Editor {
 		const that = this;
 		const config = this.config;
 
-		return loadPlugins()
-			.then( loadedPlugins => {
-				return initPlugins( loadedPlugins, 'init' )
-					.then( () => initPlugins( loadedPlugins, 'afterInit' ) );
-			} )
-			.then( () => this.fire( 'pluginsReady' ) );
-
-		function loadPlugins() {
-			const plugins = config.get( 'plugins' ) || [];
-			const removePlugins = config.get( 'removePlugins' ) || [];
-			const extraPlugins = config.get( 'extraPlugins' ) || [];
-
-			return that.plugins.load( plugins.concat( extraPlugins ), removePlugins );
-		}
-
-		function initPlugins( loadedPlugins, method ) {
-			return loadedPlugins.reduce( ( promise, plugin ) => {
-				if ( !plugin[ method ] ) {
-					return promise;
-				}
+		return Promise.resolve()
+			.then( () => {
+				const plugins = config.get( 'plugins' ) || [];
+				const removePlugins = config.get( 'removePlugins' ) || [];
+				const extraPlugins = config.get( 'extraPlugins' ) || [];
 
-				return promise.then( plugin[ method ].bind( plugin ) );
-			}, Promise.resolve() );
-		}
+				return that.plugins.load( plugins.concat( extraPlugins ), removePlugins );
+			} )
+			.then( loadedPlugins => {
+				return this.plugins.init( loadedPlugins );
+			} );
 	}
 
 	/**
@@ -321,12 +308,6 @@ export default class Editor {
 
 mix( Editor, ObservableMixin );
 
-/**
- * Fired after {@link #initPlugins plugins are initialized}.
- *
- * @event pluginsReady
- */
-
 /**
  * Fired when the data loaded to the editor is ready. If a specific editor doesn't load
  * any data initially, this event will be fired right before {@link #event:ready}.
@@ -335,8 +316,8 @@ mix( Editor, ObservableMixin );
  */
 
 /**
- * Fired when {@link #event:pluginsReady plugins}, and {@link #event:dataReady data} and all additional
- * editor components are ready.
+ * Fired when {@link module:core/plugincollection~PluginCollection#event:ready plugins},
+ * and {@link #event:dataReady data} and all additional editor components are ready.
  *
  * Note: This event is most useful for plugin developers. When integrating the editor with your website or
  * application you do not have to listen to `editor#ready` because when the promise returned by the static

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

@@ -10,8 +10,13 @@
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import log from '@ckeditor/ckeditor5-utils/src/log';
 
+import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
+import mix from '@ckeditor/ckeditor5-utils/src/mix';
+
 /**
  * Manages a list of CKEditor plugins, including loading, resolving dependencies and initialization.
+ *
+ * @mixes module:utils/emittermixin~EmitterMixin
  */
 export default class PluginCollection {
 	/**
@@ -293,6 +298,29 @@ export default class PluginCollection {
 		}
 	}
 
+	/**
+	 * Runs the initialisation process ({@link module:core/plugin~Plugin#init `init()`}
+	 * and {@link module:core/plugin~Plugin#afterInit `afterInit()`} methods) for the given set of plugins.
+	 *
+	 * @param {<Array.<module:core/plugin~PluginInterface>} plugins The array of plugins to initialise.
+	 * @returns {Promise} A promise which resolves after all given plugins have been initialized.
+	 */
+	init( plugins ) {
+		return initPlugins( plugins, 'init' )
+			.then( () => initPlugins( plugins, 'afterInit' ) )
+			.then( () => { this.fire( 'ready' ); } );
+
+		function initPlugins( loadedPlugins, method ) {
+			return loadedPlugins.reduce( ( promise, plugin ) => {
+				if ( !plugin[ method ] ) {
+					return promise;
+				}
+
+				return promise.then( plugin[ method ].bind( plugin ) );
+			}, Promise.resolve() );
+		}
+	}
+
 	/**
 	 * Destroys all loaded plugins.
 	 *
@@ -363,3 +391,11 @@ export default class PluginCollection {
 		}
 	}
 }
+
+mix( PluginCollection, EmitterMixin );
+
+/**
+ * Fired after {@link #init plugins are initialized}.
+ *
+ * @event ready
+ */