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

Commit

Permalink
Other: Split Editor.build into Editor.builtinPlugins and `Editor.…
Browse files Browse the repository at this point in the history
…defaultConfig`. Closes #140.

BREAKING CHANGE: `Editor.build` was split to `Editor.builtinPlugins` and `Editor.defaultConfig`.
  • Loading branch information
Reinmar committed Jul 10, 2018
1 parent a1a9144 commit c13ec79
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 39 deletions.
81 changes: 72 additions & 9 deletions src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class Editor {
* @param {Object} config The editor config.
*/
constructor( config ) {
const availablePlugins = this.constructor.build && this.constructor.build.plugins;
const availablePlugins = this.constructor.builtinPlugins;

/**
* Holds all configurations specific to this editor instance.
Expand All @@ -62,7 +62,7 @@ export default class Editor {
* @readonly
* @member {module:utils/config~Config}
*/
this.config = new Config( config, this.constructor.build && this.constructor.build.config );
this.config = new Config( config, this.constructor.defaultConfig );

this.config.define( 'plugins', availablePlugins );

Expand Down Expand Up @@ -348,15 +348,78 @@ mix( Editor, ObservableMixin );
*/

/**
* Additional data built into the editor class. It's used while bundling the editor in order to provide
* the default set of plugins and config options which are later used during editor initialization.
* 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.
*
* Two properties are supported:
* They will be automatically initialized by the editor, unless listed in `config.removePlugins` and
* unless `config.plugins` is passed.
*
* * `plugins` – an array of plugin constructors. They will be automatically initialized by the editor, unless listed
* in `config.removePlugins` or unless `config.plugins` is passed.
* * `config` – the defalt config options.
* // Build some plugins into the editor class first.
* ClassicEditor.builtinPlugins = [ FooPlugin, BarPlugin ];
*
* // Normally, you need to define config.plugins, but since ClassicEditor.builtinPlugins was
* // defined, now you can call create() without any configuration.
* ClassicEditor
* .create( sourceElement )
* .then( editor => {
* editor.plugins.get( FooPlugin ); // -> instance of the Foo plugin
* editor.plugins.get( BarPlugin ); // -> instance of the Bar plugin
* } );
*
* ClassicEditor
* .create( sourceElement, {
* // Don't initialize this plugins (note: it's defined by a string):
* removePlugins: [ 'Foo' ]
* } )
* .then( editor => {
* editor.plugins.get( FooPlugin ); // -> undefined
* editor.config.get( BarPlugin ); // -> instance of the Bar plugin
* } );
*
* ClassicEditor
* .create( sourceElement, {
* // Load only this plugin. Can also be define by a string if
* // this plugin was built into the editor class.
* plugins: [ FooPlugin ]
* } )
* .then( editor => {
* editor.plugins.get( FooPlugin ); // -> instance of the Foo plugin
* editor.config.get( BarPlugin ); // -> undefined
* } );
*
* See also {@link module:core/editor/editor~Editor.defaultConfig}.
*
* @static
* @member {Array.<Function>} module:core/editor/editor~Editor.builtinPlugins
*/

/**
* The default config which is built into the editor class.
* It is used in CKEditor 5 builds to provide the default config options which are later used during editor initialization.
*
* ClassicEditor.defaultConfig = {
* foo: 1,
* bar: 2
* };
*
* ClassicEditor
* .create( sourceElement )
* .then( editor => {
* editor.config.get( 'foo' ); // -> 1
* editor.config.get( 'bar' ); // -> 2
* } );
*
* // The default options can be overridden by the config passed to create().
* ClassicEditor
* .create( sourceElement, { bar: 3 } )
* .then( editor => {
* editor.config.get( 'foo' ); // -> 1
* editor.config.get( 'bar' ); // -> 3
* } );
*
* See also {@link module:core/editor/editor~Editor.builtinPlugins}.
*
* @static
* @member {Object} module:core/editor/editor~Editor.build
* @member {Object} module:core/editor/editor~Editor.defaultConfig
*/
2 changes: 1 addition & 1 deletion src/editor/editorconfig.jsdoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
*
* You can check the list of plugins available in a build using this snippet:
*
* ClassicEditor.build.plugins.map( plugin => plugin.pluginName );
* ClassicEditor.builtinPlugins.map( plugin => plugin.pluginName );
*
* If you use an editor creator directly (imported from a package like `@ckeditor/ckeditor5-editor-classic`) or you
* want to load additional plugins which were not included in a build you use, then you need to specify
Expand Down
2 changes: 1 addition & 1 deletion src/plugincollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default class PluginCollection {
* Some plugins are not available and could not be loaded.
*
* Plugin classes (constructors) need to be provided to the editor before they can be loaded by name.
* This is usually done by the builder by setting the {@link module:core/editor/editor~Editor.build}
* This is usually done in CKEditor 5 builds by setting the {@link module:core/editor/editor~Editor.builtinPlugins}
* property.
*
* **If you see this warning when using one of the {@glink builds/index CKEditor 5 Builds}**, it means
Expand Down
41 changes: 13 additions & 28 deletions tests/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class PluginF {

describe( 'Editor', () => {
afterEach( () => {
delete Editor.build;
delete Editor.builtinPlugins;
delete Editor.defaultConfig;
} );

it( 'imports the version helper', () => {
Expand All @@ -117,12 +118,10 @@ describe( 'Editor', () => {
} );

it( 'should extend an editor configuration using built in config', () => {
Editor.build = {
config: {
foo: {
a: 1,
b: 2
}
Editor.defaultConfig = {
foo: {
a: 1,
b: 2
}
};

Expand Down Expand Up @@ -533,9 +532,7 @@ describe( 'Editor', () => {
} );

it( 'should load plugins built in the Editor even if the passed config is empty', () => {
Editor.build = {
plugins: [ PluginA, PluginB, PluginC ]
};
Editor.builtinPlugins = [ PluginA, PluginB, PluginC ];

const editor = new Editor();

Expand All @@ -550,9 +547,7 @@ describe( 'Editor', () => {
} );

it( 'should load plugins provided in the config and should ignore plugins built in the Editor', () => {
Editor.build = {
plugins: [ PluginA, PluginB, PluginC, PluginD ]
};
Editor.builtinPlugins = [ PluginA, PluginB, PluginC, PluginD ];

const editor = new Editor( {
plugins: [
Expand All @@ -571,9 +566,7 @@ describe( 'Editor', () => {
it( 'should load plugins built in the Editor using their names', () => {
class PrivatePlugin extends Plugin {}

Editor.build = {
plugins: [ PluginA, PluginB, PluginC, PluginD ]
};
Editor.builtinPlugins = [ PluginA, PluginB, PluginC, PluginD ];

const editor = new Editor( {
plugins: [
Expand All @@ -596,9 +589,7 @@ describe( 'Editor', () => {
} );

it( 'should load plugins inherited from the base Editor', () => {
Editor.build = {
plugins: [ PluginA, PluginB, PluginC, PluginD ]
};
Editor.builtinPlugins = [ PluginA, PluginB, PluginC, PluginD ];

class CustomEditor extends Editor {}

Expand All @@ -621,9 +612,7 @@ describe( 'Editor', () => {
it( 'should load plugins build into Editor\'s subclass', () => {
class CustomEditor extends Editor {}

CustomEditor.build = {
plugins: [ PluginA, PluginB, PluginC, PluginD ]
};
CustomEditor.builtinPlugins = [ PluginA, PluginB, PluginC, PluginD ];

const editor = new CustomEditor( {
plugins: [
Expand Down Expand Up @@ -655,9 +644,7 @@ describe( 'Editor', () => {
} );

it( 'should not load plugins built in the Editor when "removePlugins" option is specified', () => {
Editor.build = {
plugins: [ PluginA, PluginD ]
};
Editor.builtinPlugins = [ PluginA, PluginD ];

const editor = new Editor( {
removePlugins: [ 'D' ]
Expand All @@ -673,9 +660,7 @@ describe( 'Editor', () => {
it( 'should not load plugins build into Editor\'s subclass when "removePlugins" option is specified', () => {
class CustomEditor extends Editor {}

CustomEditor.build = {
plugins: [ PluginA, PluginD ]
};
CustomEditor.builtinPlugins = [ PluginA, PluginD ];

const editor = new CustomEditor( {
removePlugins: [ 'D' ]
Expand Down

0 comments on commit c13ec79

Please sign in to comment.