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

Commit

Permalink
Changed: ChangeBuffer should use model.Document#event:change.
Browse files Browse the repository at this point in the history
  • Loading branch information
scofalik committed Jan 9, 2018
1 parent be47b69 commit c91e338
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/changebuffer.js
Expand Up @@ -7,7 +7,6 @@
* @module typing/changebuffer
*/

import count from '@ckeditor/ckeditor5-utils/src/count';
import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';

/**
Expand Down Expand Up @@ -69,18 +68,23 @@ export default class ChangeBuffer {
*/
this.isLocked = false;

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

this._onBatch( batch );
// The function to be called in order to notify the buffer about batches which appeared in the document.
// The callback will check whether it is a new batch and in that case the buffer will be flushed.
//
// The reason why the buffer needs to be flushed whenever a new batch appears is that the changes added afterwards
// should be added to a new batch. For instance, when the user types, then inserts an image, and then types again,
// the characters typed after inserting the image should be added to a different batch than the characters typed before.
this._changeCallback = ( evt, batch ) => {
if ( batch.type != 'transparent' && batch !== this._batch ) {
this._reset( true );
}
};

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

this.model.on( 'applyOperation', this._changeCallback );
this.model.document.on( 'change', this._changeCallback );

this.model.document.selection.on( 'change:range', this._selectionChangeCallback );
this.model.document.selection.on( 'change:attribute', this._selectionChangeCallback );
Expand Down Expand Up @@ -153,29 +157,11 @@ export default class ChangeBuffer {
* Destroys the buffer.
*/
destroy() {
this.model.off( 'applyOperation', this._changeCallback );
this.model.document.off( 'change', this._changeCallback );
this.model.document.selection.off( 'change:range', this._selectionChangeCallback );
this.model.document.selection.off( 'change:attribute', this._selectionChangeCallback );
}

/**
* The method to be called in order to notify the buffer about batches which appeared in the document.
* The method will check whether it is a new batch and in that case the buffer will be flushed.
*
* The reason why the buffer needs to be flushed whenever a new batch appears is that the changes added afterwards
* should be added to a new batch. For instance, when the user types, then inserts an image, and then types again,
* the characters typed after inserting the image should be added to a different batch than the characters typed before.
*
* @private
* @param {module:engine/model/batch~Batch} batch The batch which appears in the document.
*/
_onBatch( batch ) {
// One operation means a newly created batch.
if ( batch.type != 'transparent' && batch !== this._batch && count( batch.getOperations() ) <= 1 ) {
this._reset( true );
}
}

/**
* Resets the change buffer.
*
Expand Down

0 comments on commit c91e338

Please sign in to comment.