8
0
Prechádzať zdrojové kódy

Added change.removeAttr. Simplified code.

Piotr Jasiun 10 rokov pred
rodič
commit
c2a1c889f6

+ 25 - 23
packages/ckeditor5-utils/src/document/deltas/changedelta.js

@@ -8,7 +8,7 @@
 CKEDITOR.define( [
 	'document/deltas/delta',
 	'document/deltas/register',
-	'document/operations/changeoperation',
+	'document/operation/changeoperation',
 	'document/range',
 	'document/attribute'
 ], ( Delta, register, ChangeOperation, Range, Attribute ) => {
@@ -17,8 +17,13 @@ CKEDITOR.define( [
 	 */
 	class ChangeDelta extends Delta {}
 
-	register( 'setAttr', ( doc, transaction, key, value, range ) => {
-		var ops = [];
+	register( 'setAttr', change );
+
+	register( 'removeAttr', ( doc, transaction, key, range ) => {
+		change( doc, transaction, key, null, range );
+	} );
+
+	function change( doc, transaction, key, value, range ) {
 		var lastSplitPosition = range.start;
 
 		var position;
@@ -28,17 +33,14 @@ CKEDITOR.define( [
 		var iterator = range[ Symbol.iterator ]();
 		var next = iterator.next();
 
+		var delta = new ChangeDelta();
+
 		while ( !next.done ) {
 			valueAfter = next.value.node.getAttr( key );
 
 			if ( position && valueBefore != valueAfter ) {
 				if ( valueBefore != value ) {
-					ops.push( new ChangeOperation(
-						new Range( lastSplitPosition, position ),
-						valueBefore ? new Attribute( key, valueBefore ) : null,
-						new Attribute( key, value ),
-						doc.version + ops.length
-					) );
+					split();
 				}
 
 				lastSplitPosition = position;
@@ -51,22 +53,22 @@ CKEDITOR.define( [
 		}
 
 		if ( position != lastSplitPosition && valueBefore != value ) {
-			ops.push( new ChangeOperation(
-				new Range( lastSplitPosition, position ),
-				valueBefore ? new Attribute( key, valueBefore ) : null,
-				new Attribute( key, value ),
-				doc.version + ops.length
-			) );
+			split();
 		}
 
-
-
-		return new ChangeDelta( transaction, ops );
-	} );
-
-	register( 'removeAttr', ( doc, transaction, key, range ) => {
-		return new ChangeDelta( transaction, {} );
-	} );
+		transaction.addDelta( delta );
+
+		function split() {
+			var operation = new ChangeOperation(
+					new Range( lastSplitPosition, position ),
+					valueBefore ? new Attribute( key, valueBefore ) : null,
+					value ? new Attribute( key, value ) : null,
+					doc.version
+				);
+			doc.applyOperation( operation );
+			delta.addOperation( operation );
+		}
+	}
 
 	return ChangeDelta;
 } );

+ 10 - 9
packages/ckeditor5-utils/src/document/deltas/delta.js

@@ -13,19 +13,20 @@ CKEDITOR.define( [], function() {
 		/**
 		 * @constructor
 		 */
-		constructor( transaction, operations ) {
-			this.transaction = transaction;
-			this.operations = Array.from( operations );
+		constructor() {
+			this.transaction = null;
+			this.operations = [];
 		}
 
-		[ Symbol.iterator ]() {
-			return this.operations[ Symbol.iterator ]();
+		addOperation( operation ) {
+			operation.delta = this;
+			this.operations.push( operation );
+
+			return operation;
 		}
 
-		_execute() {
-			for ( var operation of this.operations ) {
-				this.transaction.doc.applyOperation( operation );
-			}
+		[ Symbol.iterator ]() {
+			return this.operations[ Symbol.iterator ]();
 		}
 	}
 

+ 9 - 12
packages/ckeditor5-utils/src/document/deltas/transaction-base.js

@@ -5,7 +5,7 @@
 
 'use strict';
 
-CKEDITOR.define( [ 'utils', 'document/deltas/delta' ], ( utils, Delta ) => {
+CKEDITOR.define( [], () => {
 	/**
 	 * @class document.Transaction
 	 */
@@ -18,6 +18,13 @@ CKEDITOR.define( [ 'utils', 'document/deltas/delta' ], ( utils, Delta ) => {
 			this.deltas = [];
 		}
 
+		addDelta( delta ) {
+			delta.transaction = this;
+			this.deltas.push( delta );
+
+			return delta;
+		}
+
 		[ Symbol.iterator ]() {
 			return this.deltas[ Symbol.iterator ]();
 		}
@@ -28,17 +35,7 @@ CKEDITOR.define( [ 'utils', 'document/deltas/delta' ], ( utils, Delta ) => {
 			}
 
 			Transaction.prototype[ name ] = function() {
-				var deltas = creator.apply( this, [ this.doc, this ].concat( Array.from( arguments ) ) );
-
-				if ( deltas instanceof Delta ) {
-					deltas = [ deltas ];
-				}
-
-				for ( var delta of deltas ) {
-					delta._execute();
-
-					this.deltas.push( delta );
-				}
+				creator.apply( this, [ this.doc, this ].concat( Array.from( arguments ) ) );
 
 				return this;
 			};

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

@@ -153,7 +153,7 @@ describe( 'Transaction', () => {
 		it( 'should split the operations if parts of the part of the range have no attribute', () => {
 			transaction.removeAttr( 'a', getRange( 1, 7 ) );
 			expect( getOperationsCount() ).to.equal( 2 );
-			expect( getChangesAttrsCount() ).to.equal( 2 );
+			expect( getChangesAttrsCount() ).to.equal( 3 );
 			expect( getCompressedAttrs() ).to.equal( '1------11222---111' );
 		} );
 

+ 3 - 11
packages/ckeditor5-utils/tests/document/transaction.js

@@ -27,7 +27,6 @@ describe( 'Transaction', () => {
 	} );
 
 	describe( 'Transaction.register', () => {
-		var executeCount = 0;
 		var TestDelta;
 
 		before( () => {
@@ -35,22 +34,16 @@ describe( 'Transaction', () => {
 				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 );
+				t.addDelta( new TestDelta() );
 			} );
 
 			var transaction = new Transaction();
@@ -59,12 +52,12 @@ describe( 'Transaction', () => {
 
 			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, t ) => {
-				return [ new TestDelta( t ), new TestDelta( t ) ];
+				t.addDelta( new TestDelta() );
+				t.addDelta( new TestDelta() );
 			} );
 
 			var transaction = new Transaction();
@@ -74,7 +67,6 @@ describe( 'Transaction', () => {
 			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 );
 		} );
 
 		it( 'should pass arguments properly', () => {