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

Commit

Permalink
Merge pull request #157 from ckeditor/t/ckeditor5/1477
Browse files Browse the repository at this point in the history
Other: Remove `editor#pluginsReady` event. Closes ckeditor/ckeditor5#1477.

BREAKING CHANGE: The `editor#pluginsReady` event was removed. Use plugin `afterInit()` method instead.
  • Loading branch information
Piotr Jasiun authored Jan 29, 2019
2 parents 3da2f20 + 94f230c commit 6d63538
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 175 deletions.
73 changes: 9 additions & 64 deletions src/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import '@ckeditor/ckeditor5-utils/src/version';
* the specific editor implements also the {@link module:core/editor/editorwithui~EditorWithUI} interface
* (as most editor implementations do).
*
* @abstract
* @mixes module:utils/observablemixin~ObservableMixin
*/
export default class Editor {
Expand Down Expand Up @@ -218,36 +219,16 @@ export default class Editor {
/**
* Loads and initializes plugins specified in the config.
*
* @returns {Promise} A promise which resolves once the initialization is completed.
* @returns {Promise.<Array.<module:core/plugin~PluginInterface>>} returns.loadedPlugins A promise which resolves
* once the initialization is completed providing array of loaded plugins.
*/
initPlugins() {
const that = this;
const config = this.config;
const plugins = config.get( 'plugins' ) || [];
const removePlugins = config.get( 'removePlugins' ) || [];
const extraPlugins = config.get( 'extraPlugins' ) || [];

return loadPlugins()
.then( loadedPlugins => {
return initPlugins( loadedPlugins, 'init' )
.then( () => initPlugins( loadedPlugins, 'afterInit' ) );
} )
.then( () => this.fire( 'pluginsReady' ) );

function loadPlugins() {
const plugins = config.get( 'plugins' ) || [];
const removePlugins = config.get( 'removePlugins' ) || [];
const extraPlugins = config.get( 'extraPlugins' ) || [];

return that.plugins.load( plugins.concat( extraPlugins ), removePlugins );
}

function initPlugins( loadedPlugins, method ) {
return loadedPlugins.reduce( ( promise, plugin ) => {
if ( !plugin[ method ] ) {
return promise;
}

return promise.then( plugin[ method ].bind( plugin ) );
}, Promise.resolve() );
}
return this.plugins.init( plugins.concat( extraPlugins ), removePlugins );
}

/**
Expand Down Expand Up @@ -294,49 +275,13 @@ export default class Editor {
execute( ...args ) {
this.commands.execute( ...args );
}

/**
* Creates and initializes a new editor instance.
*
* @param {Object} config The editor config. You can find the list of config options in
* {@link module:core/editor/editorconfig~EditorConfig}.
* @returns {Promise} Promise resolved once editor is ready.
* @returns {module:core/editor/editor~Editor} return.editor The editor instance.
*/
static create( config ) {
return new Promise( resolve => {
const editor = new this( config );

resolve(
editor.initPlugins()
.then( () => {
editor.fire( 'dataReady' );
editor.fire( 'ready' );
} )
.then( () => editor )
);
} );
}
}

mix( Editor, ObservableMixin );

/**
* Fired after {@link #initPlugins plugins are initialized}.
*
* @event pluginsReady
*/

/**
* Fired when the data loaded to the editor is ready. If a specific editor doesn't load
* any data initially, this event will be fired right before {@link #event:ready}.
*
* @event dataReady
*/

/**
* Fired when {@link #event:pluginsReady plugins}, and {@link #event:dataReady data} and all additional
* editor components are ready.
* Fired when {@link module:core/plugincollection~PluginCollection#event:ready plugins},
* and {@link module:engine/controller/datacontroller~DataController#event:ready data} and all additional editor components are ready.
*
* Note: This event is most useful for plugin developers. When integrating the editor with your website or
* application you do not have to listen to `editor#ready` because when the promise returned by the static
Expand Down
4 changes: 2 additions & 2 deletions src/editor/editorui.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ export default class EditorUI {
/**
* Fired when the editor UI is ready.
*
* Fired after {@link module:core/editor/editor~Editor#event:pluginsReady} and before
* {@link module:core/editor/editor~Editor#event:dataReady}.
* Fired after {@link module:core/plugincollection~PluginCollection#event:ready} and before
* {@link module:engine/controller/datacontroller~DataController#event:ready}.
*
* @event ready
*/
Expand Down
2 changes: 1 addition & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mix( Plugin, ObservableMixin );
* // `listenTo()` and `editor` are available thanks to `Plugin`.
* // By using `listenTo()` you will ensure that the listener is removed when
* // the plugin is destroyed.
* this.listenTo( this.editor, 'dataReady', () => {
* this.listenTo( this.editor.data, 'ready', () => {
* // Do something when the data is ready.
* } );
* }
Expand Down
23 changes: 21 additions & 2 deletions src/plugincollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import log from '@ckeditor/ckeditor5-utils/src/log';

import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';

/**
* Manages a list of CKEditor plugins, including loading, resolving dependencies and initialization.
*
* @mixes module:utils/emittermixin~EmitterMixin
*/
export default class PluginCollection {
/**
Expand Down Expand Up @@ -139,7 +144,7 @@ export default class PluginCollection {
}

/**
* Loads a set of plugins and adds them to the collection.
* Initializes a set of plugins and adds them to the collection.
*
* @param {Array.<Function|String>} plugins An array of {@link module:core/plugin~PluginInterface plugin constructors}
* or {@link module:core/plugin~PluginInterface.pluginName plugin names}. The second option (names) works only if
Expand All @@ -150,7 +155,7 @@ export default class PluginCollection {
* collection.
* @returns {Promise.<Array.<module:core/plugin~PluginInterface>>} returns.loadedPlugins The array of loaded plugins.
*/
load( plugins, removePlugins = [] ) {
init( plugins, removePlugins = [] ) {
const that = this;
const editor = this._editor;
const loading = new Set();
Expand Down Expand Up @@ -191,6 +196,8 @@ export default class PluginCollection {
}

return Promise.all( pluginConstructors.map( loadPlugin ) )
.then( () => initPlugins( loaded, 'init' ) )
.then( () => initPlugins( loaded, 'afterInit' ) )
.then( () => loaded );

function loadPlugin( PluginConstructor ) {
Expand Down Expand Up @@ -231,6 +238,16 @@ export default class PluginCollection {
} );
}

function initPlugins( loadedPlugins, method ) {
return loadedPlugins.reduce( ( promise, plugin ) => {
if ( !plugin[ method ] ) {
return promise;
}

return promise.then( plugin[ method ].bind( plugin ) );
}, Promise.resolve() );
}

function instantiatePlugin( PluginConstructor ) {
return new Promise( resolve => {
loading.add( PluginConstructor );
Expand Down Expand Up @@ -363,3 +380,5 @@ export default class PluginCollection {
}
}
}

mix( PluginCollection, EmitterMixin );
11 changes: 7 additions & 4 deletions tests/_utils-tests/classictesteditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,13 @@ describe( 'ClassicTestEditor', () => {
const fired = [];

function spy( evt ) {
fired.push( evt.name );
fired.push( `${ evt.name }-${ evt.source.constructor.name.toLowerCase() }` );
}

class EventWatcher extends Plugin {
init() {
this.editor.on( 'pluginsReady', spy );
this.editor.ui.on( 'ready', spy );
this.editor.on( 'dataReady', spy );
this.editor.data.on( 'ready', spy );
this.editor.on( 'ready', spy );
}
}
Expand All @@ -133,7 +132,11 @@ describe( 'ClassicTestEditor', () => {
plugins: [ EventWatcher ]
} )
.then( editor => {
expect( fired ).to.deep.equal( [ 'pluginsReady', 'ready', 'dataReady', 'ready' ] );
expect( fired ).to.deep.equal( [
'ready-classictesteditorui',
'ready-datacontroller',
'ready-classictesteditor'
] );

return editor.destroy();
} );
Expand Down
1 change: 0 additions & 1 deletion tests/_utils/classictesteditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export default class ClassicTestEditor extends Editor {
.then( () => editor.editing.view.attachDomRoot( editor.ui.getEditableElement() ) )
.then( () => editor.data.init( getDataFromElement( element ) ) )
.then( () => {
editor.fire( 'dataReady' );
editor.state = 'ready';
editor.fire( 'ready' );
} )
Expand Down
16 changes: 16 additions & 0 deletions tests/_utils/modeltesteditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ export default class ModelTestEditor extends Editor {
// Create the ("main") root element of the model tree.
this.model.document.createRoot();
}

static create( config ) {
return new Promise( resolve => {
const editor = new this( config );

resolve(
editor.initPlugins()
.then( () => {
// Fire `data#ready` event manually as `data#init()` method is not used.
editor.data.fire( 'ready' );
editor.fire( 'ready' );
} )
.then( () => editor )
);
} );
}
}

mix( ModelTestEditor, DataApiMixin );
16 changes: 16 additions & 0 deletions tests/_utils/virtualtesteditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ export default class VirtualTestEditor extends Editor {
// Create the ("main") root element of the model tree.
this.model.document.createRoot();
}

static create( config ) {
return new Promise( resolve => {
const editor = new this( config );

resolve(
editor.initPlugins()
.then( () => {
// Fire `data#ready` event manually as `data#init()` method is not used.
editor.data.fire( 'ready' );
editor.fire( 'ready' );
} )
.then( () => editor )
);
} );
}
}

mix( VirtualTestEditor, DataApiMixin );
Loading

0 comments on commit 6d63538

Please sign in to comment.