Browse Source

Merge pull request #212 from ckeditor/i/6209

Internal: Added `Context.builtinPlugins` and `Context.defaultConfig`. Closes ckeditor/ckeditor5#6209.
Piotrek Koszuliński 5 years ago
parent
commit
c4a94c85ee

+ 64 - 2
packages/ckeditor5-core/src/context.js

@@ -55,7 +55,11 @@ export default class Context {
 		 * @readonly
 		 * @type {module:utils/config~Config}
 		 */
-		this.config = new Config( config );
+		this.config = new Config( config, this.constructor.defaultConfig );
+
+		const availablePlugins = this.constructor.builtinPlugins;
+
+		this.config.define( 'plugins', availablePlugins );
 
 		/**
 		 * The plugins loaded and in use by this context instance.
@@ -63,7 +67,7 @@ export default class Context {
 		 * @readonly
 		 * @type {module:core/plugincollection~PluginCollection}
 		 */
-		this.plugins = new PluginCollection( this );
+		this.plugins = new PluginCollection( this, availablePlugins );
 
 		const languageConfig = this.config.get( 'language' ) || {};
 
@@ -289,3 +293,61 @@ export default class Context {
 		} );
 	}
 }
+
+/**
+ * An array of plugins built into the `Context` class.
+ *
+ * It is used in CKEditor 5 builds featuring `Context` to provide a list of context plugins which are later automatically initialized
+ * during the context initialization.
+ *
+ * They will be automatically initialized by `Context` unless `config.plugins` is passed.
+ *
+ *		// Build some context plugins into the Context class first.
+ *		Context.builtinPlugins = [ FooPlugin, BarPlugin ];
+ *
+ *		// Normally, you need to define config.plugins, but since Context.builtinPlugins was
+ *		// defined, now you can call create() without any configuration.
+ *		Context
+ *			.create()
+ *			.then( context => {
+ *				context.plugins.get( FooPlugin ); // -> An instance of the Foo plugin.
+ *				context.plugins.get( BarPlugin ); // -> An instance of the Bar plugin.
+ *			} );
+ *
+ * See also {@link module:core/editor/editor~Editor.builtinPlugins} and {@link module:core/context~Context.defaultConfig}.
+ *
+ * @static
+ * @member {Array.<Function>} module:core/context~Context.builtinPlugins
+ */
+
+/**
+ * The default configuration which is built into the `Context` class.
+ *
+ * It is used in CKEditor 5 builds featuring `Context` to provide the default configuration options which are later used during the
+ * context initialization.
+ *
+ *		Context.defaultConfig = {
+ *			foo: 1,
+ *			bar: 2
+ *		};
+ *
+ *		Context
+ *			.create()
+ *			.then( context => {
+ *				context.config.get( 'foo' ); // -> 1
+ *				context.config.get( 'bar' ); // -> 2
+ *			} );
+ *
+ *		// The default options can be overridden by the configuration passed to create().
+ *		Context
+ *			.create( { bar: 3 } )
+ *			.then( context => {
+ *				context.config.get( 'foo' ); // -> 1
+ *				context.config.get( 'bar' ); // -> 3
+ *			} );
+ *
+ * See also {@link module:core/editor/editor~Editor.defaultConfig} and {@link module:core/context~Context.builtinPlugins}.
+ *
+ * @static
+ * @member {Object} module:core/context~Context.defaultConfig
+ */

+ 2 - 0
packages/ckeditor5-core/src/editor/editor.js

@@ -348,6 +348,7 @@ mix( Editor, ObservableMixin );
 
 /**
  * An array of plugins built into this editor class.
+ *
  * It is used in CKEditor 5 builds to provide a list of plugins which are later automatically initialized
  * during the editor initialization.
  *
@@ -395,6 +396,7 @@ mix( Editor, ObservableMixin );
 
 /**
  * The default configuration which is built into the editor class.
+ *
  * It is used in CKEditor 5 builds to provide the default configuration options which are later used during the editor initialization.
  *
  *		ClassicEditor.defaultConfig = {

+ 66 - 0
packages/ckeditor5-core/tests/context.js

@@ -296,4 +296,70 @@ describe( 'Context', () => {
 			await editor.destroy();
 		} );
 	} );
+
+	describe( 'builtinPlugins', () => {
+		class PluginA extends ContextPlugin {}
+		class PluginB extends ContextPlugin {}
+		class PluginC extends ContextPlugin {}
+
+		beforeEach( () => {
+			Context.builtinPlugins = [ PluginA, PluginB, PluginC ];
+		} );
+
+		afterEach( () => {
+			delete Context.builtinPlugins;
+		} );
+
+		it( 'should load plugins built in the Context even if the passed config is empty', () => {
+			const context = new Context();
+
+			return context.initPlugins()
+				.then( () => {
+					expect( getPlugins( context ).length ).to.equal( 3 );
+
+					expect( context.plugins.get( PluginA ) ).to.be.an.instanceof( ContextPlugin );
+					expect( context.plugins.get( PluginB ) ).to.be.an.instanceof( ContextPlugin );
+					expect( context.plugins.get( PluginC ) ).to.be.an.instanceof( ContextPlugin );
+				} );
+		} );
+
+		it( 'should load plugins provided in the config and should ignore plugins built in the Editor', () => {
+			const context = new Context( {
+				plugins: [
+					PluginA
+				]
+			} );
+
+			return context.initPlugins()
+				.then( () => {
+					expect( getPlugins( context ).length ).to.equal( 1 );
+
+					expect( context.plugins.get( PluginA ) ).to.be.an.instanceof( ContextPlugin );
+				} );
+		} );
+	} );
+
+	describe( 'defaultConfig', () => {
+		beforeEach( () => {
+			Context.defaultConfig = { foo: 1, bar: 2 };
+		} );
+
+		afterEach( () => {
+			delete Context.defaultConfig;
+		} );
+
+		it( 'should extend an editor configuration using built in config', () => {
+			const context = new Context( {
+				foo: 4
+			} );
+
+			expect( context.config.get( 'foo' ) ).to.equal( 4 );
+			expect( context.config.get( 'bar' ) ).to.equal( 2 );
+		} );
+	} );
 } );
+
+function getPlugins( editor ) {
+	return Array.from( editor.plugins )
+		.map( entry => entry[ 1 ] ); // Get instances.
+}