Kaynağa Gözat

Used Context configuration as a default Editor configuration.

Oskar Wróbel 6 yıl önce
ebeveyn
işleme
087b9b63cc

+ 20 - 0
packages/ckeditor5-core/src/context.js

@@ -110,6 +110,26 @@ export default class Context {
 		return this.plugins.init( plugins );
 	}
 
+	/**
+	 * Returns context configuration which will be copied to editors created using this context.
+	 *
+	 * The configuration returned by this method has removed plugins configuration - plugins are shared with all editors
+	 * in a special way.
+	 *
+	 * @returns {Object} Configuration as a plain object.
+	 */
+	getConfigForEditor() {
+		const result = {};
+
+		for ( const name of this.config.names() ) {
+			if ( ![ 'plugins', 'removePlugins', 'extraPlugins' ].includes( name ) ) {
+				result[ name ] = this.config.get( name );
+			}
+		}
+
+		return result;
+	}
+
 	/**
 	 * Destroys the context instance, releasing all resources used by it.
 	 *

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

@@ -55,7 +55,7 @@ export default class Editor {
 		 * @readonly
 		 * @type {module:core/context~Context}
 		 */
-		this.context = config.context || new Context( config );
+		this.context = config.context || new Context();
 		this.context.addEditor( this );
 
 		/**
@@ -79,6 +79,7 @@ export default class Editor {
 		 */
 		this.config = new Config( config, this.constructor.defaultConfig );
 		this.config.define( 'plugins', availablePlugins );
+		this.config.define( this.context.getConfigForEditor() );
 
 		/**
 		 * The plugins loaded and in use by this editor instance.

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

@@ -26,6 +26,29 @@ describe( 'Context', () => {
 		} );
 	} );
 
+	describe( 'getConfigForEditor()', () => {
+		it( 'should return the configuration without plugin config', () => {
+			class FooPlugin extends ContextPlugin {}
+			class BarPlugin extends ContextPlugin {}
+			class BomPlugin extends ContextPlugin {}
+
+			const context = new Context( {
+				language: { ui: 'pl', content: 'ar' },
+				plugins: [ FooPlugin, BarPlugin ],
+				extraPlugins: [ BomPlugin ],
+				removePlugins: [ FooPlugin ],
+				foo: 1,
+				bar: 'bom'
+			} );
+
+			expect( context.getConfigForEditor() ).to.be.deep.equal( {
+				language: { ui: 'pl', content: 'ar' },
+				foo: 1,
+				bar: 'bom'
+			} );
+		} );
+	} );
+
 	describe( 'locale', () => {
 		it( 'is instantiated and t() is exposed', () => {
 			const context = new Context();

+ 36 - 1
packages/ckeditor5-core/tests/editor/editor.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  */
 
-/* globals window, setTimeout */
+/* globals document, window, setTimeout */
 
 import Editor from '../../src/editor/editor';
 import Context from '../../src/context';
@@ -226,6 +226,41 @@ describe( 'Editor', () => {
 
 			expect( editor.plugins._externalPlugins.has( ContextPlugin ) ).to.equal( true );
 		} );
+
+		it( 'should get configuration from the context', async () => {
+			const context = await Context.create( { cfoo: 'bar' } );
+			const editor = await TestEditor.create( { context } );
+
+			expect( editor.config.get( 'cfoo' ) ).to.equal( 'bar' );
+		} );
+
+		it( 'should not overwrite the default configuration', async () => {
+			const context = await Context.create( { cfoo: 'bar' } );
+			const editor = await TestEditor.create( { context, 'cfoo': 'bom' } );
+
+			expect( editor.config.get( 'cfoo' ) ).to.equal( 'bom' );
+		} );
+
+		it( 'should not copy plugins configuration', async () => {
+			class ContextPlugin {
+				static get isContextPlugin() {
+					return true;
+				}
+			}
+
+			const context = await Context.create( { plugins: [ ContextPlugin ] } );
+			const editor = await TestEditor.create( { context } );
+
+			expect( editor.config.get( 'plugins' ) ).to.be.undefined;
+		} );
+
+		it( 'should pass DOM element using reference, not copy', async () => {
+			const element = document.createElement( 'div' );
+			const context = await Context.create( { efoo: element } );
+			const editor = await TestEditor.create( { context } );
+
+			expect( editor.config.get( 'efoo' ) ).to.equal( element );
+		} );
 	} );
 
 	describe( 'plugins', () => {