8
0
Pārlūkot izejas kodu

Introduced `editor.plugins` and `editor.init()`.

fredck 10 gadi atpakaļ
vecāks
revīzija
c5f80ba962

+ 8 - 1
packages/ckeditor5-engine/src/ckeditor.js

@@ -62,7 +62,14 @@ CKEDITOR.define( [ 'editor', 'mvc/collection', 'promise', 'config' ], function(
 					that.instances.remove( editor );
 				} );
 
-				resolve( editor );
+				resolve(
+					// Initializes the editor, which returns a promise.
+					editor.init()
+						.then( function() {
+							// After initialization, return the created editor.
+							return editor;
+						} )
+				);
 			} );
 		},
 

+ 41 - 1
packages/ckeditor5-engine/src/editor.js

@@ -11,7 +11,7 @@
  * @class Editor
  */
 
-CKEDITOR.define( [ 'mvc/model', 'editorconfig' ], function( Model, EditorConfig ) {
+CKEDITOR.define( [ 'mvc/model', 'editorconfig', 'plugincollection' ], function( Model, EditorConfig, PluginCollection ) {
 	var Editor = Model.extend( {
 		/**
 		 * Creates a new instance of the Editor class.
@@ -42,6 +42,46 @@ CKEDITOR.define( [ 'mvc/model', 'editorconfig' ], function( Model, EditorConfig
 			 * @type {Config}
 			 */
 			this.config = new EditorConfig( config );
+
+			/**
+			 * The plugins loaded and in use by this editor instance.
+			 *
+			 * @type {PluginCollection}
+			 */
+			this.plugins = new PluginCollection( this );
+		},
+
+		/**
+		 * Initializes the editor instance object after its creation.
+		 *
+		 * The initialization consists on the following procedures:
+		 *
+		 *  * Load and initialize the configured plugins.
+		 *  * TODO: Add other procedures here.
+		 *
+		 * This method should be rarely used as `CKEDITOR.create` calls it one should never use the `Editor` contrusctor
+		 * directly.
+		 *
+		 * @returns {Promise} A promise which resolves once the initialization is completed.
+		 */
+		init: function() {
+			var that = this;
+			var config = this.config;
+
+			// Create and cache a promise that resolves when all initialization procedures get resolved.
+			this._initPromise = this._initPromise || Promise.all( [
+				initPlugins()
+			] );
+
+			return this._initPromise;
+
+			function initPlugins() {
+				if ( config.plugins ) {
+					return that.plugins.load( config.plugins );
+				}
+
+				return Promise.resolve();
+			}
 		},
 
 		/**

+ 65 - 2
packages/ckeditor5-engine/tests/editor/editor.js

@@ -3,15 +3,24 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals describe, it, expect, beforeEach, sinon, document */
+/* globals describe, it, expect, before, beforeEach, sinon, document */
 
 'use strict';
 
-var modules = bender.amd.require( 'editor', 'editorconfig' );
+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;
 
@@ -21,6 +30,18 @@ beforeEach( function() {
 	editor = new Editor( element );
 } );
 
+// Define two fake plugins to be used in tests.
+
+CKEDITOR.define( 'plugin!A', [ 'plugin' ], function() {
+	return PluginA;
+} );
+
+CKEDITOR.define( 'plugin!B', [ 'plugin' ], function() {
+	return PluginB;
+} );
+
+///////////////////
+
 describe( 'constructor', function() {
 	it( 'should create a new editor instance', function() {
 		expect( editor ).to.have.property( 'element' ).to.equal( element );
@@ -35,6 +56,48 @@ describe( 'config', function() {
 	} );
 } );
 
+describe( 'init', function() {
+	it( 'should return a promise that resolves properly', function() {
+		var Promise = modules.promise;
+
+		var promise = editor.init();
+
+		expect( promise ).to.be.an.instanceof( Promise );
+
+		return promise;
+	} );
+
+	it( 'should return the same promise for sucessive calls', function() {
+		var promise = editor.init();
+
+		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() {
+		var Editor = modules.editor;
+		var Plugin = modules.plugin;
+
+		editor = new Editor( element, {
+			plugins: 'A,B'
+		} );
+
+		expect( editor.plugins.length ).to.equal( 0 );
+
+		return editor.init().then( function() {
+			expect( editor.plugins.length ).to.equal( 2 );
+
+			expect( editor.plugins.get( 'A' ) ).to.be.an.instanceof( Plugin );
+			expect( editor.plugins.get( 'B' ) ).to.be.an.instanceof( Plugin );
+		} );
+	} );
+} );
+
 describe( 'destroy', function() {
 	it( 'should fire "destroy"', function() {
 		var spy = sinon.spy();