Przeglądaj źródła

Feature: Added support for `Editor.build` property.

Kamil Piechaczek 9 lat temu
rodzic
commit
0fa7360dc7

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

@@ -31,6 +31,8 @@ export default class Editor {
 	 * @param {Object} config The editor config.
 	 */
 	constructor( config ) {
+		const availablePlugins = findAvailablePlugin( this.constructor.build );
+
 		/**
 		 * Holds all configurations specific to this editor instance.
 		 *
@@ -45,7 +47,7 @@ export default class Editor {
 		 * @readonly
 		 * @member {module:core/plugin~PluginCollection}
 		 */
-		this.plugins = new PluginCollection( this );
+		this.plugins = new PluginCollection( this, availablePlugins );
 
 		/**
 		 * Commands registered to the editor.
@@ -85,6 +87,8 @@ export default class Editor {
 		 */
 		this.data = new DataController( this.document );
 
+		extendEditorConfig( this.config, this.constructor.build, availablePlugins );
+
 		/**
 		 * Instance of the {@link module:engine/controller/editingcontroller~EditingController editing controller}.
 		 *
@@ -189,6 +193,42 @@ export default class Editor {
 
 mix( Editor, EmitterMixin );
 
+// @param {module:utils/config~Config} config
+// @param {Object} builtInConfig
+// @param {Object.<String,*>} builtInConfig.config
+// @param {Array.<module:core/plugin~Plugin>} availablePlugins
+function extendEditorConfig( config, builtInConfig, availablePlugins ) {
+	if ( !builtInConfig ) {
+		return;
+	}
+
+	if ( builtInConfig.config ) {
+		for ( const configKey of Object.keys( builtInConfig.config ) ) {
+			config.define( configKey, builtInConfig.config[ configKey ] );
+		}
+	}
+
+	if ( availablePlugins.length ) {
+		const configPlugins = config.get( 'plugins' ) || [];
+
+		config.set( 'plugins', configPlugins.concat( availablePlugins ) );
+	}
+}
+
+// @param {Object} builtInConfig
+// @param {Array.<module:core/plugin~Plugin>} builtInConfig.plugins
+function findAvailablePlugin( builtInConfig ) {
+	if ( !builtInConfig ) {
+		return [];
+	}
+
+	if ( !builtInConfig.plugins ) {
+		return [];
+	}
+
+	return builtInConfig.plugins;
+}
+
 /**
  * Fired after {@link #initPlugins plugins are initialized}.
  *

+ 67 - 0
packages/ckeditor5-core/tests/editor/editor.js

@@ -51,6 +51,10 @@ class PluginD extends Plugin {
 }
 
 describe( 'Editor', () => {
+	afterEach( () => {
+		delete Editor.build;
+	} );
+
 	describe( 'constructor()', () => {
 		it( 'should create a new editor instance', () => {
 			const editor = new Editor();
@@ -61,6 +65,30 @@ describe( 'Editor', () => {
 			expect( editor.plugins ).to.be.an.instanceof( PluginCollection );
 			expect( getPlugins( editor ) ).to.be.empty;
 		} );
+
+		it( 'should extend an editor configuration using built in config', () => {
+			Editor.build = {
+				config: {
+					'foo.a': 1,
+					'foo.b': 2
+				}
+			};
+
+			const editor = new Editor( {
+				bar: 'foo',
+				foo: {
+					c: 3
+				},
+			} );
+
+			expect( editor.config.get( 'foo' ) ).to.deep.equal( {
+				a: 1,
+				b: 2,
+				c: 3
+			} );
+
+			expect( editor.config.get( 'bar' ) ).to.equal( 'foo' );
+		} );
 	} );
 
 	describe( 'plugins', () => {
@@ -244,6 +272,45 @@ describe( 'Editor', () => {
 				);
 			} );
 		} );
+
+		it( 'should load plugins built in the Editor even if the passed config is empty', () => {
+			Editor.build = {
+				plugins: [ PluginA, PluginB, PluginC ]
+			};
+
+			const editor = new Editor( {} );
+
+			return editor.initPlugins()
+				.then( () => {
+					expect( getPlugins( editor ).length ).to.equal( 3 );
+
+					expect( editor.plugins.get( PluginA ) ).to.be.an.instanceof( Plugin );
+					expect( editor.plugins.get( PluginB ) ).to.be.an.instanceof( Plugin );
+					expect( editor.plugins.get( PluginC ) ).to.be.an.instanceof( Plugin );
+				} );
+		} );
+
+		it( 'should load plugins built in the Editor and specified in the config', () => {
+			Editor.build = {
+				plugins: [ PluginA ]
+			};
+
+			const editor = new Editor( {
+				plugins: [
+					PluginD
+				]
+			} );
+
+			return editor.initPlugins()
+				.then( () => {
+					expect( getPlugins( editor ).length ).to.equal( 4 );
+
+					expect( editor.plugins.get( PluginA ) ).to.be.an.instanceof( Plugin );
+					expect( editor.plugins.get( PluginB ) ).to.be.an.instanceof( Plugin );
+					expect( editor.plugins.get( PluginC ) ).to.be.an.instanceof( Plugin );
+					expect( editor.plugins.get( PluginD ) ).to.be.an.instanceof( Plugin );
+				} );
+		} );
 	} );
 } );