Răsfoiți Sursa

Introduced Transaction class.

Piotr Jasiun 10 ani în urmă
părinte
comite
412d575b9b

+ 12 - 0
packages/ckeditor5-engine/src/document/deltas/register.js

@@ -0,0 +1,12 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [
+	'document/deltas/transaction-base'
+], function( Transaction ) {
+	return Transaction.register;
+} );

+ 45 - 0
packages/ckeditor5-engine/src/document/deltas/transaction-base.js

@@ -0,0 +1,45 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [ 'utils' ], ( utils ) => {
+	/**
+	 * @class document.Transaction
+	 */
+	class Transaction {
+		/**
+		 * @constructor
+		 */
+		constructor( doc ) {
+			this.doc = doc;
+			this.deltas = [];
+		}
+
+		static register( name, creator ) {
+			if ( Transaction.prototype[ name ] ) {
+				throw 'error';
+			}
+
+			Transaction.prototype[ name ] = function() {
+				var deltas = creator( [ this.doc, this ].concat( arguments ) );
+
+				if ( !utils.isIterable( deltas ) ) {
+					deltas = [ deltas ];
+				}
+
+				for ( var delta of deltas ) {
+					delta._execute();
+
+					this.deltas.push( delta );
+				}
+
+				return this;
+			};
+		}
+	}
+
+	return Transaction;
+} );

+ 6 - 1
packages/ckeditor5-engine/src/document/document.js

@@ -8,10 +8,11 @@
 CKEDITOR.define( [
 	'document/element',
 	'document/rootelement',
+	'document/transaction',
 	'emittermixin',
 	'utils',
 	'ckeditorerror'
-], ( Element, RootElement, EmitterMixin, utils, CKEditorError ) => {
+], ( Element, RootElement, Tranaction, EmitterMixin, utils, CKEditorError ) => {
 	const graveyardSymbol = Symbol( 'graveyard' );
 
 	/**
@@ -131,6 +132,10 @@ CKEDITOR.define( [
 		get _graveyard() {
 			return this.getRoot( graveyardSymbol );
 		}
+
+		makeTransaction() {
+			return new Tranaction( this );
+		}
 	}
 
 	utils.extend( Document.prototype, EmitterMixin );

+ 6 - 1
packages/ckeditor5-engine/src/document/element.js

@@ -5,7 +5,12 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/node', 'document/nodelist' ], ( Node, NodeList ) => {
+CKEDITOR.define( [
+	'document/node',
+	'document/nodelist',
+	'document/range',
+	'document/position'
+], ( Node, NodeList, Range, Position ) => {
 	/**
 	 * Tree data model element.
 	 *

+ 2 - 0
packages/ckeditor5-engine/src/document/positioniterator.js

@@ -67,6 +67,8 @@ CKEDITOR.define( [
 			const position = this.position;
 			const parent = position.parent;
 
+			Element = CKEDITOR.require( 'document/element' );
+
 			// We are at the end of the root.
 			if ( parent.parent === null && position.offset === parent.getChildCount() ) {
 				return { done: true };

+ 8 - 1
packages/ckeditor5-engine/src/document/range.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'document/positioniterator' ], ( PositionIterator ) => {
+CKEDITOR.define( [ 'document/positioniterator', 'document/position' ], ( PositionIterator, Position ) => {
 	/**
 	 * Range class. Range is iterable.
 	 *
@@ -35,6 +35,13 @@ CKEDITOR.define( [ 'document/positioniterator' ], ( PositionIterator ) => {
 			this.end = end;
 		}
 
+		static createFromElement( element ) {
+			return new Range(
+					Position.createFromParentAndOffset( element, 0 ),
+					Position.createFromParentAndOffset( element, element.getChildCount() )
+				);
+		}
+
 		/**
 		 * Two ranges equal if their start and end positions equal.
 		 *

+ 12 - 0
packages/ckeditor5-engine/src/document/transaction.js

@@ -0,0 +1,12 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+'use strict';
+
+CKEDITOR.define( [
+	'document/deltas/transaction-base'
+], function( Transaction ) {
+	return Transaction;
+} );

+ 8 - 0
packages/ckeditor5-engine/tests/document/element.js

@@ -121,4 +121,12 @@ describe( 'Element', () => {
 			expect( element.getChildCount() ).to.equal( 3 );
 		} );
 	} );
+
+	describe( 'getChildrenIterator', function() {
+		it( 'should return iterator over children', function() {
+			getChildrenIterator
+
+			expect( element.getChildCount() ).to.equal( 3 );
+		} );
+	} );
 } );

+ 80 - 0
packages/ckeditor5-engine/tests/document/transaction.js

@@ -0,0 +1,80 @@
+/**
+ * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* bender-tags: document, delta */
+
+'use strict';
+
+var modules = bender.amd.require(
+	'document/transaction',
+	'document/deltas/delta' );
+
+describe( 'Transaction', () => {
+	var Transaction, Delta;
+
+	before( () => {
+		Transaction = modules[ 'document/transaction' ];
+		Delta = modules[ 'document/deltas/delta' ];
+	} );
+
+	it( 'should have registered basic methods', () => {
+		var transaction = new Transaction();
+
+		expect( transaction.setAttr ).to.be.a( 'function' );
+		expect( transaction.removeAttr ).to.be.a( 'function' );
+	} );
+
+	describe( 'Transaction.register', () => {
+		var executeCount = 0;
+		var TestDelta;
+
+		before( () => {
+			TestDelta = class extends Delta {
+				constructor( transaction ) {
+					super( transaction, [] );
+				}
+
+				_execute() {
+					executeCount++;
+				}
+			};
+		} );
+
+		afterEach( () => {
+			delete Transaction.prototype.foo;
+
+			executeCount = 0;
+		} );
+
+		it( 'should register function which return an delta', () => {
+			Transaction.register( 'foo', ( doc, t ) => {
+				return new TestDelta( t );
+			} );
+
+			var transaction = new Transaction();
+
+			transaction.foo();
+
+			expect( transaction.deltas.length ).to.equal( 1 );
+			expect( transaction.deltas[ 0 ] ).to.be.instanceof( TestDelta );
+			expect( executeCount ).to.equal( 1 );
+		} );
+
+		it( 'should register function which return an multiple deltas', () => {
+			Transaction.register( 'foo', ( doc, transaction ) => {
+				return [ new TestDelta( transaction ), new TestDelta( transaction ) ];
+			} );
+
+			var transaction = new Transaction();
+
+			transaction.foo();
+
+			expect( transaction.deltas.length ).to.equal( 2 );
+			expect( transaction.deltas[ 0 ] ).to.be.instanceof( TestDelta );
+			expect( transaction.deltas[ 1 ] ).to.be.instanceof( TestDelta );
+			expect( executeCount ).to.equal( 2 );
+		} );
+	} );
+} );