Browse Source

The init() method of plugins is now called as part of editor.init().

fredck 10 years ago
parent
commit
2af76b51c1

+ 14 - 4
packages/ckeditor5-engine/src/editor.js

@@ -70,17 +70,27 @@ CKEDITOR.define( [ 'mvc/model', 'editorconfig', 'plugincollection' ], function(
 
 			// Create and cache a promise that resolves when all initialization procedures get resolved.
 			this._initPromise = this._initPromise || Promise.all( [
-				initPlugins()
+				loadPlugins().then( initPlugins )
 			] );
 
 			return this._initPromise;
 
+			function loadPlugins() {
+				return that.plugins.load( config.plugins );
+			}
+
 			function initPlugins() {
-				if ( config.plugins ) {
-					return that.plugins.load( config.plugins );
+				var rets = [];
+
+				// Call init() on every plugin, saving the return value in the rets array.
+				for ( var i = 0; i < that.plugins.length; i++ ) {
+					rets.push( that.plugins.get( i ).init() );
 				}
 
-				return Promise.resolve();
+				// Returns a promise that resolves when all initialization values are resolved. If they are promises,
+				// we will wait for them to resolve. If they are something else, they resolve immediately (as per
+				// Promise.all specs).
+				return Promise.all( rets );
 			}
 		},
 

+ 3 - 1
packages/ckeditor5-engine/src/plugin.js

@@ -19,7 +19,9 @@ CKEDITOR.define( [ 'mvc/model' ], function( Model ) {
 			Model.apply( this );
 
 			this.editor = editor;
-		}
+		},
+
+		init: function() {}
 	} );
 
 	return Plugin;

+ 1 - 1
packages/ckeditor5-engine/src/plugincollection.js

@@ -40,7 +40,7 @@ CKEDITOR.define( [ 'mvc/collection', 'promise' ], function( Collection, Promise
 			// The list of plugins which are being loaded (to avoid circular references issues).
 			var loading = {};
 
-			plugins = plugins.split( ',' );
+			plugins = plugins ? plugins.split( ',' ) : [];
 
 			// Creates a promise for the loading of each plugin and returns a main promise that resolves when all are
 			// done.

+ 44 - 20
packages/ckeditor5-engine/tests/editor/editor.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals describe, it, expect, before, beforeEach, sinon, document */
+/* globals describe, it, expect, beforeEach, sinon, document */
 
 'use strict';
 
@@ -12,15 +12,6 @@ var modules = bender.amd.require( 'editor', 'editorconfig', 'plugin', 'promise'
 var editor;
 var element;
 
-var PluginA, PluginB;
-
-before( function() {
-	var Plugin = modules.plugin;
-
-	PluginA = Plugin.extend();
-	PluginB = Plugin.extend();
-} );
-
 beforeEach( function() {
 	var Editor = modules.editor;
 
@@ -30,14 +21,30 @@ beforeEach( function() {
 	editor = new Editor( element );
 } );
 
-// Define two fake plugins to be used in tests.
+// Define fake plugins to be used in tests.
 
 CKEDITOR.define( 'plugin!A', [ 'plugin' ], function() {
-	return PluginA;
+	return modules.plugin.extend( {
+		init: sinon.spy().named( 'A' )
+	} );
 } );
 
 CKEDITOR.define( 'plugin!B', [ 'plugin' ], function() {
-	return PluginB;
+	return modules.plugin.extend( {
+		init: sinon.spy().named( 'B' )
+	} );
+} );
+
+CKEDITOR.define( 'plugin!C', [ 'plugin', 'plugin!B' ], function() {
+	return modules.plugin.extend( {
+		init: sinon.spy().named( 'C' )
+	} );
+} );
+
+CKEDITOR.define( 'plugin!D', [ 'plugin', 'plugin!C' ], function() {
+	return modules.plugin.extend( {
+		init: sinon.spy().named( 'D' )
+	} );
 } );
 
 ///////////////////
@@ -72,14 +79,8 @@ describe( 'init', function() {
 
 		expect( editor.init() ).to.equal( promise );
 	} );
-} );
 
-describe( 'plugins', function() {
-	it( 'should be empty on new editor', function() {
-		expect( editor.plugins.length ).to.equal( 0 );
-	} );
-
-	it( 'should be filled on `init()`', function() {
+	it( 'should fill `plugins`', function() {
 		var Editor = modules.editor;
 		var Plugin = modules.plugin;
 
@@ -96,6 +97,29 @@ describe( 'plugins', function() {
 			expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
 		} );
 	} );
+
+	it( 'should initialize plugins in the right order', function() {
+		var Editor = modules.editor;
+
+		editor = new Editor( element, {
+			plugins: 'A,D'
+		} );
+
+		return editor.init().then( function() {
+			sinon.assert.callOrder(
+				editor.plugins.get( 'A' ).init,
+				editor.plugins.get( 'B' ).init,
+				editor.plugins.get( 'C' ).init,
+				editor.plugins.get( 'D' ).init
+			);
+		} );
+	} );
+} );
+
+describe( 'plugins', function() {
+	it( 'should be empty on new editor', function() {
+		expect( editor.plugins.length ).to.equal( 0 );
+	} );
 } );
 
 describe( 'destroy', function() {