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

Remove magic from Transaction.register.

Piotr Jasiun 10 лет назад
Родитель
Сommit
f000bc7666

+ 12 - 6
packages/ckeditor5-utils/src/document/delta/changedelta.js

@@ -33,7 +33,11 @@ CKEDITOR.define( [
 	 * @param {Mixed} value Attribute new value.
 	 * @param {document.Node|document.Range} nodeOrRange Node or range on which the attribute will be set.
 	 */
-	register( 'setAttr', change );
+	register( 'setAttr', function( key, value, nodeOrRange ) {
+		change( this, key, value, nodeOrRange );
+
+		return this;
+	} );
 
 	/**
 	 * Removes an attribute from the range.
@@ -44,17 +48,19 @@ CKEDITOR.define( [
 	 * @param {String} key Attribute key.
 	 * @param {document.Node|document.Range} nodeOrRange Node or range on which the attribute will be removed.
 	 */
-	register( 'removeAttr', ( doc, transaction, key, nodeOrRange ) => {
-		change( doc, transaction, key, null, nodeOrRange );
+	register( 'removeAttr', function( key, nodeOrRange ) {
+		change( this, key, null, nodeOrRange );
+
+		return this;
 	} );
 
-	function change( doc, transaction, key, value, nodeOrRange ) {
+	function change( transaction, key, value, nodeOrRange ) {
 		const delta = new ChangeDelta();
 
 		if ( nodeOrRange instanceof Range ) {
-			changeRange( doc, delta, key, value, nodeOrRange );
+			changeRange( transaction.doc, delta, key, value, nodeOrRange );
 		} else {
-			changeNode( doc, delta, key, value, nodeOrRange );
+			changeNode( transaction.doc, delta, key, value, nodeOrRange );
 		}
 
 		transaction.addDelta( delta );

+ 6 - 4
packages/ckeditor5-utils/src/document/delta/insertdelta.js

@@ -28,14 +28,16 @@ CKEDITOR.define( [
 	 * @param {document.Node|document.Text|document.NodeList|String|Iterable} nodes The list of nodes to be inserted.
 	 * List of nodes can be of any type accepted by the {@link document.NodeList} constructor.
 	 */
-	register( 'insert', ( doc, transaction, position, nodes ) => {
+	register( 'insert', function( position, nodes ) {
 		const delta = new InsertDelta();
 
-		const operation = new InsertOperation( position, nodes, doc.version );
-		doc.applyOperation( operation );
+		const operation = new InsertOperation( position, nodes, this.doc.version );
+		this.doc.applyOperation( operation );
 		delta.addOperation( operation );
 
-		transaction.addDelta( delta );
+		this.addDelta( delta );
+
+		return this;
 	} );
 
 	return InsertDelta;

+ 8 - 6
packages/ckeditor5-utils/src/document/delta/mergedelta.js

@@ -33,7 +33,7 @@ CKEDITOR.define( [
 	 * @memberOf document.Transaction
 	 * @param {document.Position} position Position of merge.
 	 */
-	register( 'merge', ( doc, transaction, position ) => {
+	register( 'merge', function( position ) {
 		const delta = new MergeDelta();
 		const nodeBefore = position.nodeBefore;
 		const nodeAfter = position.nodeAfter;
@@ -61,15 +61,17 @@ CKEDITOR.define( [
 		const positionAfter = Position.createFromParentAndOffset( nodeAfter, 0 );
 		const positionBefore = Position.createFromParentAndOffset( nodeBefore, nodeBefore.getChildCount() );
 
-		const move = new MoveOperation( positionAfter, positionBefore, nodeAfter.getChildCount(), doc.version );
-		doc.applyOperation( move );
+		const move = new MoveOperation( positionAfter, positionBefore, nodeAfter.getChildCount(), this.doc.version );
+		this.doc.applyOperation( move );
 		delta.addOperation( move );
 
-		const remove = new RemoveOperation( position, 1, doc.version );
-		doc.applyOperation( remove );
+		const remove = new RemoveOperation( position, 1, this.doc.version );
+		this.doc.applyOperation( remove );
 		delta.addOperation( remove );
 
-		transaction.addDelta( delta );
+		this.addDelta( delta );
+
+		return this;
 	} );
 
 	return MergeDelta;

+ 6 - 4
packages/ckeditor5-utils/src/document/delta/removedelta.js

@@ -27,18 +27,20 @@ CKEDITOR.define( [
 	 * @param {document.Position} position Position before the first node to remove.
 	 * @param {Number} howMany How many nodes to remove.
 	 */
-	register( 'remove', ( doc, transaction, position, howMany ) => {
+	register( 'remove', function( position, howMany ) {
 		if ( typeof howMany !== 'number' ) {
 			howMany = 1;
 		}
 
 		const delta = new RemoveDelta();
 
-		const operation = new RemoveOperation( position, howMany, doc.version );
-		doc.applyOperation( operation );
+		const operation = new RemoveOperation( position, howMany, this.doc.version );
+		this.doc.applyOperation( operation );
 		delta.addOperation( operation );
 
-		transaction.addDelta( delta );
+		this.addDelta( delta );
+
+		return this;
 	} );
 
 	return RemoveDelta;

+ 8 - 6
packages/ckeditor5-utils/src/document/delta/splitdelta.js

@@ -33,7 +33,7 @@ CKEDITOR.define( [
 	 * @memberOf document.Transaction
 	 * @param {document.Position} position Position of split.
 	 */
-	register( 'split', ( doc, transaction, position ) => {
+	register( 'split', function( position ) {
 		const delta = new SplitDelta();
 		const splitElement = position.parent;
 
@@ -47,22 +47,24 @@ CKEDITOR.define( [
 		}
 
 		const copy = new Element( splitElement.name, splitElement.getAttrIterator() );
-		const insert = new InsertOperation( Position.createAfter( splitElement ), copy, doc.version );
+		const insert = new InsertOperation( Position.createAfter( splitElement ), copy, this.doc.version );
 
-		doc.applyOperation( insert );
+		this.doc.applyOperation( insert );
 		delta.addOperation( insert );
 
 		const move = new MoveOperation(
 			position,
 			Position.createFromParentAndOffset( copy, 0 ),
 			splitElement.getChildCount() - position.offset,
-			doc.version
+			this.doc.version
 		);
 
-		doc.applyOperation( move );
+		this.doc.applyOperation( move );
 		delta.addOperation( move );
 
-		transaction.addDelta( delta );
+		this.addDelta( delta );
+
+		return this;
 	} );
 
 	return SplitDelta;

+ 7 - 14
packages/ckeditor5-utils/src/document/delta/transaction-base.js

@@ -76,31 +76,28 @@ CKEDITOR.define( [ 'ckeditorerror' ], ( CKEditorError ) => {
 		 * This method checks if there is no naming collision and throw `transaction-register-taken` if the method name
 		 * is already taken.
 		 *
-		 * It also passes {@link document.Document} and {@link document.Transaction} do the creator class.
-		 *
-		 * Registered function returns `this` so they can be chainable by default.
-		 *
 		 * Beside that no magic happens here, the method is added to the `Transaction` class prototype.
 		 *
 		 * For example:
 		 *
-		 *		Transaction.register( 'insert', ( doc, transaction, position, nodes ) => {
+		 *		Transaction.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, doc.version );
+		 *			const operation = new InsertOperation( position, nodes, this.doc.version );
 		 *
 		 *			// Remember to apply every operation, no magic, you need to do it manually.
-		 *			doc.applyOperation( operation );
+		 *			this.doc.applyOperation( operation );
 		 *
 		 *			// Add operation to the delta.
 		 *			delta.addOperation( operation );
 		 *
 		 *			// Add delta to the transaction instance.
-		 *			transaction.addDelta( delta );
+		 *			this.addDelta( delta );
 		 *
-		 * 			// You do not need to return transaction, register method will take care to make the method chainable.
+		 * 			// Make this method chainable.
+		 * 			return this;
 		 *		} );
 		 *
 		 * @param {String} name Method name.
@@ -119,11 +116,7 @@ CKEDITOR.define( [ 'ckeditorerror' ], ( CKEditorError ) => {
 					{ name: name } );
 			}
 
-			Transaction.prototype[ name ] = function() {
-				creator.apply( this, [ this.doc, this ].concat( Array.from( arguments ) ) );
-
-				return this;
-			};
+			Transaction.prototype[ name ] = creator;
 		}
 	}
 

+ 20 - 0
packages/ckeditor5-utils/tests/document/deltas/changedelta.js

@@ -92,6 +92,11 @@ describe( 'Transaction', () => {
 				expect( getOperationsCount() ).to.equal( 0 );
 				expect( node.getAttr( 'a' ) ).to.equal( 1 );
 			} );
+
+			it( 'should be chainable', () => {
+				const chain = transaction.setAttr( 'b', 2, node );
+				expect( chain ).to.equal( transaction );
+			} );
 		} );
 
 		describe( 'removeAttr', () => {
@@ -111,6 +116,11 @@ describe( 'Transaction', () => {
 				transaction.removeAttr( 'b', node );
 				expect( getOperationsCount() ).to.equal( 0 );
 			} );
+
+			it( 'should be chainable', () => {
+				const chain = transaction.removeAttr( 'a', node );
+				expect( chain ).to.equal( transaction );
+			} );
 		} );
 	} );
 
@@ -200,6 +210,11 @@ describe( 'Transaction', () => {
 				expect( getChangesAttrsCount() ).to.equal( 9 );
 				expect( getCompressedAttrs() ).to.equal( '111111111111111111' );
 			} );
+
+			it( 'should be chainable', () => {
+				const chain = transaction.setAttr( 'a', 3, getRange( 3, 6 ) );
+				expect( chain ).to.equal( transaction );
+			} );
 		} );
 
 		describe( 'removeAttr', () => {
@@ -250,6 +265,11 @@ describe( 'Transaction', () => {
 				expect( getChangesAttrsCount() ).to.equal( 6 );
 				expect( getCompressedAttrs() ).to.equal( '111------------111' );
 			} );
+
+			it( 'should be chainable', () => {
+				const chain = transaction.removeAttr( 'a', getRange( 0, 2 ) );
+				expect( chain ).to.equal( transaction );
+			} );
 		} );
 	} );
 } );

+ 8 - 0
packages/ckeditor5-utils/tests/document/deltas/insertdelta.js

@@ -35,4 +35,12 @@ describe( 'Transaction', () => {
 		expect( root.getChild( 1 ).character ).to.equal( 'o' );
 		expect( root.getChild( 2 ).character ).to.equal( 'o' );
 	} );
+
+	it( 'should be chainable', () => {
+		const position = new Position( [ 0 ], root );
+		const transaction = doc.createTransaction();
+
+		const chain = transaction.insert( position, 'foo' );
+		expect( chain ).to.equal( transaction );
+	} );
 } );

+ 7 - 0
packages/ckeditor5-utils/tests/document/deltas/mergedelta.js

@@ -69,5 +69,12 @@ describe( 'Transaction', () => {
 				doc.createTransaction().merge( new Position( [ 0, 2 ], root ) );
 			} ).to.throw( CKEditorError, /^transaction-merge-no-element-before/ );
 		} );
+
+		it( 'should be chainable', () => {
+			const transaction = doc.createTransaction();
+
+			const chain = transaction.merge( new Position( [ 1 ], root ) );
+			expect( chain ).to.equal( transaction );
+		} );
 	} );
 } );

+ 8 - 0
packages/ckeditor5-utils/tests/document/deltas/removedelta.js

@@ -49,5 +49,13 @@ describe( 'Transaction', () => {
 			expect( root.getChild( 1 ).character ).to.equal( 'a' );
 			expect( root.getChild( 2 ).character ).to.equal( 'r' );
 		} );
+
+		it( 'should be chainable', () => {
+			const position = new Position( [ 1 ], root );
+			const transaction = doc.createTransaction();
+
+			const chain = transaction.remove( position );
+			expect( chain ).to.equal( transaction );
+		} );
 	} );
 } );

+ 7 - 0
packages/ckeditor5-utils/tests/document/deltas/splitdelta.js

@@ -90,5 +90,12 @@ describe( 'Transaction', () => {
 				doc.createTransaction().split( new Position( [ 0 ], root ) );
 			} ).to.throw( CKEditorError, /^transaction-split-root/ );
 		} );
+
+		it( 'should be chainable', () => {
+			const transaction = doc.createTransaction();
+
+			const chain = transaction.split( new Position( [ 0, 3 ], root ) );
+			expect( chain ).to.equal( transaction );
+		} );
 	} );
 } );

+ 5 - 20
packages/ckeditor5-utils/tests/document/transaction.js

@@ -44,8 +44,8 @@ describe( 'Transaction', () => {
 		} );
 
 		it( 'should register function which return an delta', () => {
-			Transaction.register( 'foo', ( doc, t ) => {
-				t.addDelta( new TestDelta() );
+			Transaction.register( 'foo', function() {
+				this.addDelta( new TestDelta() );
 			} );
 
 			const transaction = new Transaction();
@@ -57,9 +57,9 @@ describe( 'Transaction', () => {
 		} );
 
 		it( 'should register function which return an multiple deltas', () => {
-			Transaction.register( 'foo', ( doc, t ) => {
-				t.addDelta( new TestDelta() );
-				t.addDelta( new TestDelta() );
+			Transaction.register( 'foo', function() {
+				this.addDelta( new TestDelta() );
+				this.addDelta( new TestDelta() );
 			} );
 
 			const transaction = new Transaction();
@@ -71,21 +71,6 @@ describe( 'Transaction', () => {
 			expect( transaction.deltas[ 1 ] ).to.be.instanceof( TestDelta );
 		} );
 
-		it( 'should pass arguments properly', () => {
-			const doc = 'doc';
-			const arg = 'arg';
-
-			const transaction = new Transaction( doc );
-
-			const stub = sinon.stub();
-
-			Transaction.register( 'foo', stub );
-
-			transaction.foo( arg );
-
-			sinon.assert.calledWith( stub, doc, transaction, arg );
-		} );
-
 		it( 'should throw if one try to register the same transaction twice', () => {
 			Transaction.register( 'foo', () => {} );