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 #8 from ckeditor/t/7
Browse files Browse the repository at this point in the history
Internal: Exposed the editor in the save callbacks. Closes #7.
  • Loading branch information
Reinmar committed Jul 13, 2018
2 parents 9a16977 + 04895a4 commit 7d39cbe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/autosave.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import throttle from './throttle';
* toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ],
* },
* autosave: {
* save() {
* save( editor ) {
* // Note: saveEditorsContentToDatabase function should return a promise
* // which should be resolved when the saving action is complete.
* return saveEditorsContentToDatabase( data );
* return saveEditorsContentToDatabase( editor.getData() );
* }
* }
* } );
Expand Down Expand Up @@ -61,9 +61,8 @@ export default class Autosave extends Plugin {
* the model's data changes. It might be called some time after the change,
* since the event is throttled for performance reasons.
*
* @type {module:autosave/autosave~Adapter}
* @member {module:autosave/autosave~Adapter} #adapter
*/
this.adapter = undefined;

/**
* Throttled save method.
Expand Down Expand Up @@ -101,9 +100,8 @@ export default class Autosave extends Plugin {
* An action that will be added to pending action manager for actions happening in that plugin.
*
* @private
* @type {Object|null}
* @member {Object} #_action
*/
this._action = null;

/**
* Plugins' config.
Expand Down Expand Up @@ -214,7 +212,7 @@ export default class Autosave extends Plugin {
// 2. Save callbacks are not called inside conversions or while editor's state changes.
Promise.resolve()
.then( () => Promise.all(
saveCallbacks.map( cb => cb() )
saveCallbacks.map( cb => cb( this.editor ) )
) )
.then( () => {
this._decrementCounter();
Expand Down Expand Up @@ -263,5 +261,6 @@ export default class Autosave extends Plugin {
* so the `Autosave` plugin will wait for that action before removing it from pending actions.
*
* @method #save
* @param {module:core/editor/editor~Editor} editor
* @returns {Promise.<*>}
*/
18 changes: 18 additions & 0 deletions tests/autosave.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ describe( 'Autosave', () => {
sinon.assert.calledOnce( editor.config.get( 'autosave' ).save );
} );
} );

it( 'config callback and adapter callback should be called with the editor as an argument', () => {
editor.config.get( 'autosave' ).save.resetHistory();

autosave.adapter = {
save: sinon.spy()
};

editor.model.change( writer => {
writer.setSelection( ModelRange.createIn( editor.model.document.getRoot().getChild( 0 ) ) );
editor.model.insertContent( new ModelText( 'foo' ), editor.model.document.selection );
} );

return wait().then( () => {
sinon.assert.calledWithExactly( autosave.adapter.save, editor );
sinon.assert.calledWithExactly( editor.config.get( 'autosave' ).save, editor );
} );
} );
} );

describe( 'autosaving', () => {
Expand Down

0 comments on commit 7d39cbe

Please sign in to comment.