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

Commit

Permalink
Changed: Changes after model events refactor in ckeditor5-engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed Dec 27, 2017
1 parent 8f10f70 commit 62dd257
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 27 deletions.
28 changes: 15 additions & 13 deletions src/changebuffer.js
Expand Up @@ -21,7 +21,7 @@ import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
*
* To use the change buffer you need to let it know about the number of changes that were added to the batch:
*
* const buffer = new ChangeBuffer( document, LIMIT );
* const buffer = new ChangeBuffer( model, LIMIT );
*
* // Later on in your feature:
* buffer.batch.insert( pos, insertedCharacters );
Expand All @@ -35,14 +35,14 @@ export default class ChangeBuffer {
* @param {module:engine/model/document~Document} doc
* @param {Number} [limit=20] The maximum number of atomic changes which can be contained in one batch.
*/
constructor( doc, limit = 20 ) {
constructor( model, limit = 20 ) {
/**
* The document instance.
* The model instance.
*
* @readonly
* @member {module:engine/model/document~Document} #document
* @member {module:engine/model/model~Model} #model
*/
this.document = doc;
this.model = model;

/**
* The number of atomic changes in the buffer. Once it exceeds the {@link #limit},
Expand All @@ -69,19 +69,21 @@ export default class ChangeBuffer {
*/
this.isLocked = false;

this._changeCallback = ( evt, type, changes, batch ) => {
this._changeCallback = ( evt, args ) => {
const operation = args[ 0 ];
const batch = operation.delta.batch;

this._onBatch( batch );
};

this._selectionChangeCallback = () => {
this._reset();
};

doc.on( 'change', this._changeCallback );

doc.selection.on( 'change:range', this._selectionChangeCallback );
this.model.on( 'applyOperation', this._changeCallback );

doc.selection.on( 'change:attribute', this._selectionChangeCallback );
this.model.document.selection.on( 'change:range', this._selectionChangeCallback );
this.model.document.selection.on( 'change:attribute', this._selectionChangeCallback );

/**
* The current batch instance.
Expand Down Expand Up @@ -151,9 +153,9 @@ export default class ChangeBuffer {
* Destroys the buffer.
*/
destroy() {
this.document.off( 'change', this._changeCallback );
this.document.selection.off( 'change:range', this._selectionChangeCallback );
this.document.selection.off( 'change:attribute', this._selectionChangeCallback );
this.model.off( 'applyOperation', this._changeCallback );
this.model.document.selection.off( 'change:range', this._selectionChangeCallback );
this.model.document.selection.off( 'change:attribute', this._selectionChangeCallback );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/deletecommand.js
Expand Up @@ -47,7 +47,7 @@ export default class DeleteCommand extends Command {
* @private
* @member {typing.ChangeBuffer} #buffer
*/
this._buffer = new ChangeBuffer( editor.model.document, editor.config.get( 'typing.undoStep' ) );
this._buffer = new ChangeBuffer( editor.model, editor.config.get( 'typing.undoStep' ) );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/inputcommand.js
Expand Up @@ -33,7 +33,7 @@ export default class InputCommand extends Command {
* @private
* @member {module:typing/changebuffer~ChangeBuffer} #_buffer
*/
this._buffer = new ChangeBuffer( editor.model.document, undoStepSize );
this._buffer = new ChangeBuffer( editor.model, undoStepSize );
}

/**
Expand Down
12 changes: 6 additions & 6 deletions tests/bogusbr-integration.js
Expand Up @@ -49,7 +49,7 @@ describe( 'Typing – bogus BR integration', () => {
setData( editor.model, '<paragraph>Foo[]</paragraph>' );
} );

editor.model.document.once( 'changesDone', () => {
editor.model.document.once( 'change', () => {
expect( editor.getData() ).to.equal( '<p>Foo&nbsp;</p>' );
done();
}, { priority: 'low' } );
Expand All @@ -63,7 +63,7 @@ describe( 'Typing – bogus BR integration', () => {
setData( editor.model, '<paragraph>[]</paragraph>' );
} );

editor.model.document.once( 'changesDone', () => {
editor.model.document.once( 'change', () => {
expect( editor.getData() ).to.equal( '<p>&nbsp;</p>' );
done();
}, { priority: 'low' } );
Expand All @@ -77,7 +77,7 @@ describe( 'Typing – bogus BR integration', () => {
setData( editor.model, '<paragraph><$text bold="true">Foo[]</$text></paragraph>' );
} );

editor.model.document.once( 'changesDone', () => {
editor.model.document.once( 'change', () => {
expect( editor.getData() ).to.equal( '<p><strong>Foo&nbsp;</strong></p>' );
done();
}, { priority: 'low' } );
Expand All @@ -95,7 +95,7 @@ describe( 'Typing – bogus BR integration', () => {
setData( editor.model, '<paragraph>Foo[]</paragraph>' );
} );

editor.model.document.once( 'changesDone', () => {
editor.model.document.once( 'change', () => {
expect( editor.getData() ).to.equal( '<p>Foo&nbsp;</p>' );
done();
}, { priority: 'low' } );
Expand All @@ -121,7 +121,7 @@ describe( 'Typing – bogus BR integration', () => {
setData( editor.model, '<paragraph>Foo[]</paragraph>' );
} );

editor.model.document.once( 'changesDone', () => {
editor.model.document.once( 'change', () => {
expect( editor.getData() ).to.equal( '<p>Foo&nbsp;</p>' );
done();
}, { priority: 'low' } );
Expand Down Expand Up @@ -150,7 +150,7 @@ describe( 'Typing – bogus BR integration', () => {
setData( editor.model, '<paragraph>Foo hous[]</paragraph>' );
} );

editor.model.document.once( 'changesDone', () => {
editor.model.document.once( 'change', () => {
expect( editor.getData() ).to.equal( '<p>Foo house</p>' );
done();
}, { priority: 'low' } );
Expand Down
6 changes: 3 additions & 3 deletions tests/changebuffer.js
Expand Up @@ -15,19 +15,19 @@ describe( 'ChangeBuffer', () => {
model = new Model();
doc = model.document;
root = doc.createRoot();
buffer = new ChangeBuffer( doc, CHANGE_LIMIT );
buffer = new ChangeBuffer( model, CHANGE_LIMIT );
} );

describe( 'constructor()', () => {
it( 'sets all properties', () => {
expect( buffer ).to.have.property( 'document', doc );
expect( buffer ).to.have.property( 'model', model );
expect( buffer ).to.have.property( 'limit', CHANGE_LIMIT );
expect( buffer ).to.have.property( 'size', 0 );
expect( buffer ).to.have.property( 'isLocked', false );
} );

it( 'sets limit property according to default value', () => {
buffer = new ChangeBuffer( doc );
buffer = new ChangeBuffer( model );

expect( buffer ).to.have.property( 'limit', 20 );
} );
Expand Down
2 changes: 1 addition & 1 deletion tests/manual/nbsp.js
Expand Up @@ -31,7 +31,7 @@ ClassicEditor
} );
} );

editor.model.document.on( 'changesDone', () => {
editor.model.document.once( 'change', () => {
console.clear();

const modelData = getModelData( editor.model.document, { withoutSelection: true } );
Expand Down
4 changes: 2 additions & 2 deletions tests/spellchecking.js
Expand Up @@ -43,7 +43,7 @@ describe( 'Typing – spellchecking integration', () => {

beforeEach( () => {
if ( onChangesDone ) {
editor.model.document.off( 'changesDone', onChangesDone );
editor.model.document.off( 'change', onChangesDone );
onChangesDone = null;
}

Expand Down Expand Up @@ -261,7 +261,7 @@ function emulateSpellcheckerInsertText( editor, nodeIndex, rangeStart, rangeEnd,
] );
} );

editor.model.document.once( 'changesDone', onChangesDoneCallback, { priority: 'low' } );
model.document.once( 'change', onChangesDoneCallback, { priority: 'low' } );

window.document.execCommand( 'insertText', false, text );
}
Expand Down

0 comments on commit 62dd257

Please sign in to comment.