Browse Source

Merge pull request #127 from ckeditor/t/ckeditor5-engine/1207

Other: Changes after model events refactor in ckeditor5-engine.
Piotr Jasiun 8 years ago
parent
commit
e561d386e9

+ 21 - 33
packages/ckeditor5-typing/src/changebuffer.js

@@ -7,7 +7,6 @@
  * @module typing/changebuffer
  */
 
-import count from '@ckeditor/ckeditor5-utils/src/count';
 import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 
 /**
@@ -21,7 +20,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 );
@@ -35,14 +34,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},
@@ -69,19 +68,26 @@ export default class ChangeBuffer {
 		 */
 		this.isLocked = false;
 
-		this._changeCallback = ( evt, type, changes, 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();
 		};
 
-		doc.on( 'change', this._changeCallback );
+		this.model.document.on( 'change', this._changeCallback );
 
-		doc.selection.on( 'change:range', this._selectionChangeCallback );
-
-		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.
@@ -151,27 +157,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 );
-	}
-
-	/**
-	 * 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 );
-		}
+		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 );
 	}
 
 	/**

+ 1 - 1
packages/ckeditor5-typing/src/deletecommand.js

@@ -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' ) );
 	}
 
 	/**

+ 1 - 1
packages/ckeditor5-typing/src/inputcommand.js

@@ -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 );
 	}
 
 	/**

+ 6 - 6
packages/ckeditor5-typing/tests/bogusbr-integration.js

@@ -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' } );
@@ -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' } );
@@ -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' } );
@@ -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' } );
@@ -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' } );
@@ -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' } );

+ 3 - 3
packages/ckeditor5-typing/tests/changebuffer.js

@@ -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 );
 		} );

+ 1 - 1
packages/ckeditor5-typing/tests/manual/nbsp.js

@@ -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 } );

+ 6 - 4
packages/ckeditor5-typing/tests/spellchecking.js

@@ -3,7 +3,7 @@
  * For licensing, see LICENSE.md.
  */
 
-/* globals window, document */
+/* globals window, document, setTimeout */
 
 import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
 import Enter from '@ckeditor/ckeditor5-enter/src/enter';
@@ -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;
 		}
 
@@ -253,6 +253,7 @@ function emulateSpellcheckerInsertText( editor, nodeIndex, rangeStart, rangeEnd,
 	const model = editor.model;
 	const modelRoot = model.document.getRoot();
 
+	window.focus();
 	editor.editing.view.focus();
 
 	model.change( () => {
@@ -261,7 +262,9 @@ function emulateSpellcheckerInsertText( editor, nodeIndex, rangeStart, rangeEnd,
 		] );
 	} );
 
-	editor.model.document.once( 'changesDone', onChangesDoneCallback, { priority: 'low' } );
+	model.document.once( 'change', () => {
+		setTimeout( onChangesDoneCallback, 100 );
+	}, { priority: 'low' } );
 
 	window.document.execCommand( 'insertText', false, text );
 }
@@ -270,4 +273,3 @@ function expectContent( editor, expectedModel, expectedView ) {
 	expect( getModelData( editor.model ) ).to.equal( expectedModel );
 	expect( getViewData( editor.editing.view ) ).to.equal( expectedView );
 }
-