8
0
Pārlūkot izejas kodu

Added: core.treeModel.delta.WrapDelta#howMany and #_insertOperation.

Szymon Cofalik 9 gadi atpakaļ
vecāks
revīzija
ebbfecb79c

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

@@ -34,6 +34,27 @@ export default class WrapDelta extends Delta {
 		return moveOp ? Range.createFromPositionAndShift( moveOp.sourcePosition, moveOp.howMany ) : null;
 	}
 
+	/**
+	 * How many nodes is wrapped by the delta or `null` if there are no operations in delta.
+	 *
+	 * @type {Number}
+	 */
+	get howMany() {
+		let range = this.range;
+
+		return range ? range.end.offset - range.start.offset : 0;
+	}
+
+	/**
+	 * Operation that inserts wrapping element or `null` if there are no operations in the delta.
+	 *
+	 * @protected
+	 * @type {core.treeModel.operation.InsertOperation|core.treeModel.operation.ReinsertOperation}
+	 */
+	get _insertOperation() {
+		return this.operations[ 0 ] || null;
+	}
+
 	/**
 	 * Operation that moves wrapped nodes to their new parent or `null` if there are no operations in the delta.
 	 *

+ 30 - 0
packages/ckeditor5-engine/tests/treemodel/delta/wrapdelta.js

@@ -124,6 +124,21 @@ describe( 'WrapDelta', () => {
 		} );
 	} );
 
+	describe( 'howMany', () => {
+		it( 'should be equal to 0 if there are no operations in delta', () => {
+			expect( wrapDelta.howMany ).to.equal( 0 );
+		} );
+
+		it( 'should be equal to the number of wrapped elements', () => {
+			let howMany = 5;
+
+			wrapDelta.operations.push( new InsertOperation( new Position( root, [ 1, 6 ] ), 1 ) );
+			wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), howMany, new Position( root, [ 1, 6, 0 ] ) ) );
+
+			expect( wrapDelta.howMany ).to.equal( 5 );
+		} );
+	} );
+
 	describe( 'getReversed', () => {
 		it( 'should return empty UnwrapDelta if there are no operations in delta', () => {
 			let reversed = wrapDelta.getReversed();
@@ -151,5 +166,20 @@ describe( 'WrapDelta', () => {
 			expect( reversed.operations[ 1 ].howMany ).to.equal( 1 );
 		} );
 	} );
+
+	describe( '_insertOperation', () => {
+		it( 'should be null if there are no operations in the delta', () => {
+			expect( wrapDelta._insertOperation ).to.be.null;
+		} );
+
+		it( 'should be equal to the first operation in the delta', () => {
+			let insertOperation = new InsertOperation( new Position( root, [ 1, 6 ] ), 1 );
+
+			wrapDelta.operations.push( insertOperation );
+			wrapDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 5, new Position( root, [ 1, 6, 0 ] ) ) );
+
+			expect( wrapDelta._insertOperation ).to.equal( insertOperation );
+		} );
+	} );
 } );