8
0
Просмотр исходного кода

Merge pull request #151 from ckeditor/t/150-a

T/150 a
Piotr Jasiun 10 лет назад
Родитель
Сommit
fd23450d3f

+ 121 - 0
packages/ckeditor5-engine/src/treemodel/batch-base.js

@@ -0,0 +1,121 @@
+/**
+ * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+import CKEditorError from '../ckeditorerror.js';
+
+/**
+ * The Batch class groups document changes (deltas). All deltas grouped in a single Batch can be
+ * reverted together, so you can think about the Batch as a single undo step. If you want to extend one
+ * undo step you can call another method on the same Batch object. If you want to create a separate undo step
+ * you can create a new Batch.
+ *
+ * For example to create two separate undo steps you can call:
+ *
+ *		doc.batch().insert( firstPosition, 'foo' );
+ *		doc.batch().insert( secondPosition, 'bar' );
+ *
+ * To create a single undo step:
+ *
+ *		const batch = doc.batch();
+ *		batch.insert( firstPosition, 'foo' );
+ *		batch.insert( secondPosition, 'bar' );
+ *
+ * Note that all document modification methods (insert, remove, split, etc.) are chainable so you can shorten code to:
+ *
+ *		doc.batch().insert( firstPosition, 'foo' ).insert( secondPosition, 'bar' );
+ *
+ * @class treeModel.Batch
+ */
+export default class Batch {
+	/**
+	 * Creates Batch instance. Not recommended to use directly, use {@link treeModel.Document#batch} instead.
+	 *
+	 * @constructor
+	 * @param {treeModel.Document} doc Document which this Batch changes.
+	 */
+	constructor( doc ) {
+		/**
+		 * Document which this Batch changes.
+		 *
+		 * @readonly
+		 * @type {treeModel.Document}
+		 */
+		this.doc = doc;
+
+		/**
+		 * Array of deltas which compose Batch.
+		 *
+		 * @readonly
+		 * @type {Array.<treeModel.delta.Delta>}
+		 */
+		this.deltas = [];
+	}
+
+	/**
+	 * Adds delta to the Batch instance. All modification methods (insert, remove, split, etc.) use this method
+	 * to add created deltas.
+	 *
+	 * @param {treeModel.delta.Delta} delta Delta to add.
+	 * @return {treeModel.delta.Delta} Added delta.
+	 */
+	addDelta( delta ) {
+		delta.batch = this;
+		this.deltas.push( delta );
+
+		return delta;
+	}
+}
+
+/**
+ * Function to register Batch methods. To make code scalable Batch do not have modification
+ * methods built in. They can be registered using this method.
+ *
+ * This method checks if there is no naming collision and throws `batch-register-taken` if the method name
+ * is already taken.
+ *
+ * Besides that no magic happens here, the method is added to the `Batch` class prototype.
+ *
+ * For example:
+ *
+ *		Batch.register( 'insert', function( position, nodes ) {
+ *			// You can use a class inherit from Delta if that class should handle OT in the special way.
+ *			const delta = new Delta();
+ *
+ *			// Create operations which should be components of this delta.
+ *			const operation = new InsertOperation( position, nodes, this.doc.version );
+ *
+ *			// Remember to apply every operation, no magic, you need to do it manually.
+ *			this.doc.applyOperation( operation );
+ *
+ *			// Add operation to the delta.
+ *			delta.addOperation( operation );
+ *
+ *			// Add delta to the Batch instance.
+ *			this.addDelta( delta );
+ *
+ *			// Make this method chainable.
+ *			return this;
+ *		} );
+ *
+ * @param {String} name Method name.
+ * @param {Function} creator Method body.
+ */
+export function register( name, creator ) {
+	if ( Batch.prototype[ name ] ) {
+		/**
+		 * This batch method name is already taken.
+		 *
+		 * @error batch-register-taken
+		 * @param {String} name
+		 */
+		throw new CKEditorError(
+			'batch-register-taken: This batch method name is already taken.',
+			{ name: name } );
+	}
+
+	Batch.prototype[ name ] = creator;
+}

+ 7 - 113
packages/ckeditor5-engine/src/treemodel/batch.js

@@ -5,120 +5,11 @@
 
 'use strict';
 
-import CKEditorError from '../ckeditorerror.js';
+// Batch is split into two files because of circular dependencies reasons.
 
-/**
- * The Batch class groups document changes (deltas). All deltas grouped in a single Batch can be
- * reverted together, so you can think about the Batch as a single undo step. If you want to extend one
- * undo step you can call another method on the same Batch object. If you want to create a separate undo step
- * you can create a new Batch.
- *
- * For example to create two separate undo steps you can call:
- *
- *		doc.batch().insert( firstPosition, 'foo' );
- *		doc.batch().insert( secondPosition, 'bar' );
- *
- * To create a single undo step:
- *
- *		const batch = doc.batch();
- *		batch.insert( firstPosition, 'foo' );
- *		batch.insert( secondPosition, 'bar' );
- *
- * Note that all document modification methods (insert, remove, split, etc.) are chainable so you can shorten code to:
- *
- *		doc.batch().insert( firstPosition, 'foo' ).insert( secondPosition, 'bar' );
- *
- * @class treeModel.Batch
- */
-export default class Batch {
-	/**
-	 * Creates Batch instance. Not recommended to use directly, use {@link treeModel.Document#batch} instead.
-	 *
-	 * @constructor
-	 * @param {treeModel.Document} doc Document which this Batch changes.
-	 */
-	constructor( doc ) {
-		/**
-		 * Document which this Batch changes.
-		 *
-		 * @readonly
-		 * @type {treeModel.Document}
-		 */
-		this.doc = doc;
-
-		/**
-		 * Array of deltas which compose Batch.
-		 *
-		 * @readonly
-		 * @type {Array.<treeModel.delta.Delta>}
-		 */
-		this.deltas = [];
-	}
-
-	/**
-	 * Adds delta to the Batch instance. All modification methods (insert, remove, split, etc.) use this method
-	 * to add created deltas.
-	 *
-	 * @param {treeModel.delta.Delta} delta Delta to add.
-	 * @return {treeModel.delta.Delta} Added delta.
-	 */
-	addDelta( delta ) {
-		delta.batch = this;
-		this.deltas.push( delta );
-
-		return delta;
-	}
-}
-
-/**
- * Function to register Batch methods. To make code scalable Batch do not have modification
- * methods built in. They can be registered using this method.
- *
- * This method checks if there is no naming collision and throws `batch-register-taken` if the method name
- * is already taken.
- *
- * Besides that no magic happens here, the method is added to the `Batch` class prototype.
- *
- * For example:
- *
- *		Batch.register( 'insert', function( position, nodes ) {
- *			// You can use a class inherit from Delta if that class should handle OT in the special way.
- *			const delta = new Delta();
- *
- *			// Create operations which should be components of this delta.
- *			const operation = new InsertOperation( position, nodes, this.doc.version );
- *
- *			// Remember to apply every operation, no magic, you need to do it manually.
- *			this.doc.applyOperation( operation );
- *
- *			// Add operation to the delta.
- *			delta.addOperation( operation );
- *
- *			// Add delta to the Batch instance.
- *			this.addDelta( delta );
- *
- *			// Make this method chainable.
- *			return this;
- *		} );
- *
- * @param {String} name Method name.
- * @param {Function} creator Method body.
- */
-export function register( name, creator ) {
-	if ( Batch.prototype[ name ] ) {
-		/**
-		 * This batch method name is already taken.
-		 *
-		 * @error batch-register-taken
-		 * @param {String} name
-		 */
-		throw new CKEditorError(
-			'batch-register-taken: This batch method name is already taken.',
-			{ name: name } );
-	}
-
-	Batch.prototype[ name ] = creator;
-}
+// Deltas require `register` method that require `Batch` class and is defined in batch-base.js.
+// We would like to group all deltas files in one place, so we would only have to include batch.js
+// which would already have all default deltas registered.
 
 // Import default suite of deltas so a feature have to include only Batch class file.
 import d1 from './delta/insertdelta.js';
@@ -131,3 +22,6 @@ import d7 from './delta/mergedelta.js';
 import d8 from './delta/wrapdelta.js';
 import d9 from './delta/unwrapdelta.js';
 /*jshint unused: false*/
+
+import Batch from './batch-base.js';
+export default Batch;

+ 1 - 2
packages/ckeditor5-engine/src/treemodel/delta/attributedelta.js

@@ -4,9 +4,8 @@
  */
 
 'use strict';
-
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import AttributeOperation from '../operation/attributeoperation.js';
 import Position from '../position.js';
 import Range from '../range.js';

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/insertdelta.js

@@ -6,7 +6,7 @@
 'use strict';
 
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import InsertOperation from '../operation/insertoperation.js';
 
 /**

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/mergedelta.js

@@ -6,7 +6,7 @@
 'use strict';
 
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import Position from '../position.js';
 import Element from '../element.js';
 import RemoveOperation from '../operation/removeoperation.js';

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/movedelta.js

@@ -6,7 +6,7 @@
 'use strict';
 
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import MoveOperation from '../operation/moveoperation.js';
 import Position from '../position.js';
 import Range from '../range.js';

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/removedelta.js

@@ -6,7 +6,7 @@
 'use strict';
 
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import RemoveOperation from '../operation/removeoperation.js';
 import Position from '../position.js';
 import Range from '../range.js';

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/splitdelta.js

@@ -6,7 +6,7 @@
 'use strict';
 
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import Position from '../position.js';
 import Element from '../element.js';
 import InsertOperation from '../operation/insertoperation.js';

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/unwrapdelta.js

@@ -6,7 +6,7 @@
 'use strict';
 
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import Position from '../position.js';
 import RemoveOperation from '../operation/removeoperation.js';
 import MoveOperation from '../operation/moveoperation.js';

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/weakinsertdelta.js

@@ -6,7 +6,7 @@
 'use strict';
 
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import InsertOperation from '../operation/insertoperation.js';
 import NodeList from '../nodelist.js';
 

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/wrapdelta.js

@@ -6,7 +6,7 @@
 'use strict';
 
 import Delta from './delta.js';
-import register from '../batch.js';
+import { register } from '../batch-base.js';
 import Position from '../position.js';
 import Element from '../element.js';
 import InsertOperation from '../operation/insertoperation.js';

+ 2 - 2
packages/ckeditor5-engine/src/treemodel/document.js

@@ -29,7 +29,7 @@ const graveyardSymbol = Symbol( 'graveyard' );
  */
 export default class Document {
 	/**
-	 * Creates an empty document instance with no {@link #roots}.
+	 * Creates an empty document instance with no {@link #roots} (other than graveyard).
 	 *
 	 * @constructor
 	 */
@@ -229,4 +229,4 @@ export default class Document {
 	 */
 }
 
-objectUtils.extend( Document.prototype, EmitterMixin );
+objectUtils.extend( Document.prototype, EmitterMixin );

+ 6 - 6
packages/ckeditor5-engine/tests/treemodel/batch.js

@@ -6,8 +6,8 @@
 /* bender-tags: treemodel, delta */
 
 'use strict';
-
 import Batch from '/ckeditor5/core/treemodel/batch.js';
+import { register } from '/ckeditor5/core/treemodel/batch-base.js';
 import Delta from '/ckeditor5/core/treemodel/delta/delta.js';
 import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
 
@@ -19,7 +19,7 @@ describe( 'Batch', () => {
 		expect( batch.removeAttr ).to.be.a( 'function' );
 	} );
 
-	describe( 'Batch.register', () => {
+	describe( 'register', () => {
 		let TestDelta;
 
 		before( () => {
@@ -35,7 +35,7 @@ describe( 'Batch', () => {
 		} );
 
 		it( 'should register function which return an delta', () => {
-			Batch.register( 'foo', function() {
+			register( 'foo', function() {
 				this.addDelta( new TestDelta() );
 			} );
 
@@ -48,7 +48,7 @@ describe( 'Batch', () => {
 		} );
 
 		it( 'should register function which return an multiple deltas', () => {
-			Batch.register( 'foo', function() {
+			register( 'foo', function() {
 				this.addDelta( new TestDelta() );
 				this.addDelta( new TestDelta() );
 			} );
@@ -63,10 +63,10 @@ describe( 'Batch', () => {
 		} );
 
 		it( 'should throw if one try to register the same batch twice', () => {
-			Batch.register( 'foo', () => {} );
+			register( 'foo', () => {} );
 
 			expect( () => {
-				Batch.register( 'foo', () => {} );
+				register( 'foo', () => {} );
 			} ).to.throw( CKEditorError, /^batch-register-taken/ );
 		} );
 	} );