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

Added: model.LiveRange now passes data about change in document that caused the live range to change.

Szymon Cofalik 8 лет назад
Родитель
Сommit
3c7db47699

+ 12 - 3
packages/ckeditor5-engine/src/model/liverange.js

@@ -80,8 +80,13 @@ export default class LiveRange extends Range {
 	 * Fired when `LiveRange` instance is changed due to changes on {@link module:engine/model/document~Document}.
 	 *
 	 * @event change
-	 * @param {module:engine/model/range~Range} oldRange
-	 * Range with start and end position equal to start and end position of this live range before it got changed.
+	 * @param {module:engine/model/range~Range} oldRange Range with start and end position equal to start and end position of this live
+	 * range before it got changed.
+	 * @param {Object} data Object with additional information about the change. Those parameters are passed from
+	 * {@link module:engine/model/document~Document#event:change document change event}.
+	 * @param {String} data.type Change type.
+	 * @param {module:engine/model/range~Range} data.range Range containing the result of applied change.
+	 * @param {module:engine/model/position~Position} data.sourcePosition Source position for move, remove and reinsert change types.
 	 */
 }
 
@@ -152,7 +157,11 @@ function transform( changeType, deltaType, targetRange, sourcePosition ) {
 		this.start = updated.start;
 		this.end = updated.end;
 
-		this.fire( 'change', oldRange );
+		this.fire( 'change', oldRange, {
+			type: changeType,
+			range: targetRange,
+			sourcePosition
+		} );
 	}
 }
 

+ 26 - 0
packages/ckeditor5-engine/tests/model/liverange.js

@@ -88,6 +88,32 @@ describe( 'LiveRange', () => {
 		range.detach();
 	} );
 
+	it( 'should fire change event with proper data when it is changed', () => {
+		const live = new LiveRange( new Position( root, [ 0, 1, 4 ] ), new Position( root, [ 0, 2, 2 ] ) );
+		const copy = Range.createFromRange( live );
+
+		const spy = sinon.spy();
+		live.on( 'change', spy );
+
+		const moveSource = new Position( root, [ 2 ] );
+		const moveRange = new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 3 ] ) );
+
+		const changes = {
+			range: moveRange,
+			sourcePosition: moveSource
+		};
+		doc.fire( 'change', 'move', changes, null );
+
+		expect( spy.calledOnce ).to.be.true;
+
+		// First parameter available in event should be a range that is equal to the live range before the live range changed.
+		expect( spy.args[ 0 ][ 1 ].isEqual( copy ) ).to.be.true;
+
+		// Second parameter is an object with data about model changes that caused the live range to change.
+		expect( spy.args[ 0 ][ 2 ].range.isEqual( moveRange ) ).to.be.true;
+		expect( spy.args[ 0 ][ 2 ].sourcePosition.isEqual( moveSource ) ).to.be.true;
+	} );
+
 	// Examples may seem weird when you compare them with the tree structure generated at the beginning of tests.
 	// Since change event is fired _after_ operation is executed on tree model, you have to imagine that generated
 	// structure is representing what is _after_ operation is executed. So live LiveRange properties are describing