Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Copy configuration from the context to the editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Jasiun committed Nov 21, 2019
1 parent ece4b30 commit accd4ac
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/context.js
Expand Up @@ -23,7 +23,7 @@ export default class Context {
*
* @param {Object} [config={}] The context config.
*/
constructor( config ) {
constructor( config = {} ) {
/**
* Holds all configurations specific to this context instance.
*
Expand Down Expand Up @@ -110,6 +110,25 @@ export default class Context {
return this.plugins.init( plugins );
}

/**
* Returns context configuration which will be copied to editors created using this context.
* The value returned by this method has removed plugin configuration - plugins are shared with all editors is
* a special way.
*
* @returns {Object} Configuration as a plain object.
*/
getEditorConfig() {
const ret = {};

for ( const key of this.config ) {
if ( ![ 'plugins', 'removePlugins', 'extraPlugins' ].includes( key ) ) {
ret[ key ] = this.config.get( key );
}
}

return ret;
}

/**
* Destroys the context instance, releasing all resources used by it.
*
Expand Down
3 changes: 2 additions & 1 deletion src/editor/editor.js
Expand Up @@ -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 );

/**
Expand All @@ -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.getEditorConfig() );

/**
* The plugins loaded and in use by this editor instance.
Expand Down
23 changes: 23 additions & 0 deletions tests/context.js
Expand Up @@ -26,6 +26,29 @@ describe( 'Context', () => {
} );
} );

describe( 'getEditorConfig', () => {
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.getEditorConfig() ).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();
Expand Down
37 changes: 36 additions & 1 deletion tests/editor/editor.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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', () => {
Expand Down

0 comments on commit accd4ac

Please sign in to comment.