Browse Source

Added: engine.model.Batch#type. Initial batch has now 'ignore' type.

Szymon Cofalik 9 years ago
parent
commit
49550d9e48

+ 2 - 1
packages/ckeditor5-engine/src/datacontroller.js

@@ -171,7 +171,8 @@ export default class DataController {
 		const modelRoot = this.model.getRoot( rootName );
 
 		this.model.enqueueChanges( () => {
-			this.model.batch()
+			// Initial batch should be ignored by features like undo, etc.
+			this.model.batch( 'ignore' )
 				.remove( ModelRange.createFromElement( modelRoot ) )
 				.insert( ModelPosition.createAt( modelRoot, 0 ), this.parse( data ) );
 		} );

+ 19 - 3
packages/ckeditor5-engine/src/model/batch.js

@@ -35,23 +35,39 @@ export default class Batch {
 	 * Creates Batch instance. Not recommended to use directly, use {@link engine.model.Document#batch} instead.
 	 *
 	 * @param {engine.model.Document} doc Document which this Batch changes.
+	 * @param {'ignore'|'undo'|'redo'|'default'} type Type of batch. Defaults to `'default'`.
 	 */
-	constructor( doc ) {
+	constructor( doc, type = 'default' ) {
 		/**
 		 * Document which this Batch changes.
 		 *
-		 * @member {engine.model.Document} engine.model.Batch#doc
 		 * @readonly
+		 * @member {engine.model.Document} engine.model.Batch#doc
 		 */
 		this.doc = doc;
 
 		/**
 		 * Array of deltas which compose Batch.
 		 *
-		 * @member {Array.<engine.model.delta.Delta>} engine.model.Batch#deltas
 		 * @readonly
+		 * @member {Array.<engine.model.delta.Delta>} engine.model.Batch#deltas
 		 */
 		this.deltas = [];
+
+		/**
+		 * Type of batch.
+		 *
+		 * Can be one of the following values:
+		 * * `'default'` - all "normal" batches. Most commonly used type.
+		 * * `'undo'` - batch created by undo command.
+		 * * `'redo'` - batch created by redo command.
+		 * * `'ignore'` - batch that should be ignored by other features.
+		 *
+		 * @readonly
+		 * @member {'ignore'|'undo'|'redo'|'default'} engine.model.Batch#type
+		 */
+		this.type = type;
+	}
 	}
 
 	/**

+ 3 - 2
packages/ckeditor5-engine/src/model/document.js

@@ -172,10 +172,11 @@ export default class Document {
 	/**
 	 * Creates a {@link engine.model.Batch} instance which allows to change the document.
 	 *
+	 * @param {String} [type] Batch type. See {@link engine.model.Batch#type}.
 	 * @returns {engine.model.Batch} Batch instance.
 	 */
-	batch() {
-		return new Batch( this );
+	batch( type ) {
+		return new Batch( this, type );
 	}
 
 	/**

+ 14 - 0
packages/ckeditor5-engine/tests/model/batch.js

@@ -24,6 +24,20 @@ describe( 'Batch', () => {
 		expect( batch.removeAttr ).to.be.a( 'function' );
 	} );
 
+	describe( 'type', () => {
+		it( 'should default to "default"', () => {
+			const batch = new Batch( new Document() );
+
+			expect( batch.type ).to.equal( 'default' );
+		} );
+
+		it( 'should be set to the value set in constructor', () => {
+			const batch = new Batch( new Document(), 'ignore' );
+
+			expect( batch.type ).to.equal( 'ignore' );
+		} );
+	} );
+
 	describe( 'register', () => {
 		afterEach( () => {
 			delete Batch.prototype.foo;

+ 6 - 0
packages/ckeditor5-engine/tests/model/document/document.js

@@ -155,6 +155,12 @@ describe( 'Document', () => {
 			expect( batch ).to.be.instanceof( Batch );
 			expect( batch ).to.have.property( 'doc' ).that.equals( doc );
 		} );
+
+		it( 'should set given batch type', () => {
+			const batch = doc.batch( 'ignore' );
+
+			expect( batch ).to.have.property( 'type' ).that.equals( 'ignore' );
+		} );
 	} );
 
 	describe( 'enqueue', () => {