Sfoglia il codice sorgente

Feature: Added default configuration prameter for Config.

Kamil Piechaczek 8 anni fa
parent
commit
791de9562a

+ 6 - 1
packages/ckeditor5-utils/src/config.js

@@ -18,7 +18,7 @@ export default class Config {
 	 *
 	 * @param {Object} [configurations] The initial configurations to be set.
 	 */
-	constructor( configurations ) {
+	constructor( configurations, defaultConfigurations ) {
 		/**
 		 * Store for the whole configuration.
 		 *
@@ -31,6 +31,11 @@ export default class Config {
 		if ( configurations ) {
 			this._setObjectToTarget( this._config, configurations );
 		}
+
+		// Set default configuration.
+		if ( defaultConfigurations ) {
+			this._setObjectToTarget( this._config, defaultConfigurations, true );
+		}
 	}
 
 	/**

+ 30 - 4
packages/ckeditor5-utils/tests/config.js

@@ -23,7 +23,7 @@ describe( 'Config', () => {
 		} );
 	} );
 
-	describe( 'constructor', () => {
+	describe( 'constructor()', () => {
 		it( 'should set configurations', () => {
 			expect( config.get( 'creator' ) ).to.equal( 'inline' );
 			expect( config.get( 'language' ) ).to.equal( 'pl' );
@@ -41,9 +41,35 @@ describe( 'Config', () => {
 			// No error should be thrown.
 			config = new Config();
 		} );
+
+		it( 'should set default parameters', () => {
+			const defaultConfig = {
+				foo: 1,
+				bar: 2,
+			};
+
+			config = new Config( {}, defaultConfig );
+
+			expect( config.get( 'foo' ) ).to.equal( 1 );
+			expect( config.get( 'bar' ) ).to.equal( 2 );
+		} );
+
+		it( 'passed parameters should override default parameters', () => {
+			const defaultConfig = {
+				foo: 1,
+				bar: 2,
+			};
+
+			config = new Config( {
+				foo: 10,
+			}, defaultConfig );
+
+			expect( config.get( 'foo' ) ).to.equal( 10 );
+			expect( config.get( 'bar' ) ).to.equal( 2 );
+		} );
 	} );
 
-	describe( 'set', () => {
+	describe( 'set()', () => {
 		it( 'should set configurations when passing objects', () => {
 			config.set( {
 				option1: 1,
@@ -158,7 +184,7 @@ describe( 'Config', () => {
 		} );
 	} );
 
-	describe( 'define', () => {
+	describe( 'define()', () => {
 		it( 'should set configurations when passing objects', () => {
 			config.set( {
 				option1: 1,
@@ -252,7 +278,7 @@ describe( 'Config', () => {
 		} );
 	} );
 
-	describe( 'get', () => {
+	describe( 'get()', () => {
 		it( 'should retrieve a configuration', () => {
 			expect( config.get( 'creator' ) ).to.equal( 'inline' );
 		} );