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

Added: LivePosition#event:change.

Szymon Cofalik 9 лет назад
Родитель
Сommit
224a60caa9

+ 15 - 2
packages/ckeditor5-engine/src/model/liveposition.js

@@ -111,6 +111,13 @@ export default class LivePosition extends Position {
 	 * @param {engine.model.Position} position
 	 * @param {engine.model.Position} position
 	 * @returns {engine.model.LivePosition}
 	 * @returns {engine.model.LivePosition}
 	 */
 	 */
+
+	/**
+	 * Fired when `LivePosition` instance is changed due to changes on {@link engine.model.Document}.
+	 *
+	 * @event engine.model.LivePosition#change
+	 * @param {Position} oldPosition Position equal to this live position before it got changed.
+	 */
 }
 }
 
 
 /**
 /**
@@ -178,8 +185,14 @@ function transform( type, range, position ) {
 			break;
 			break;
 	}
 	}
 
 
-	this.path = transformed.path;
-	this.root = transformed.root;
+	if ( !this.isEqual( transformed ) ) {
+		const oldPosition = Position.createFromPosition( this );
+
+		this.path = transformed.path;
+		this.root = transformed.root;
+
+		this.fire( 'change', oldPosition );
+	}
 }
 }
 
 
 mix( LivePosition, EmitterMixin );
 mix( LivePosition, EmitterMixin );

+ 30 - 6
packages/ckeditor5-engine/tests/model/liveposition.js

@@ -89,10 +89,13 @@ describe( 'LivePosition', () => {
 	} );
 	} );
 
 
 	describe( 'should get transformed if', () => {
 	describe( 'should get transformed if', () => {
-		let live;
+		let live, spy;
 
 
 		beforeEach( () => {
 		beforeEach( () => {
 			live = new LivePosition( root, [ 1, 4, 6 ] );
 			live = new LivePosition( root, [ 1, 4, 6 ] );
+
+			spy = sinon.spy();
+			live.on( 'change', spy );
 		} );
 		} );
 
 
 		afterEach( () => {
 		afterEach( () => {
@@ -106,6 +109,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 
 
 				expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
 				expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 
 
 			it( 'is at the same position and live position is sticking to right side', () => {
 			it( 'is at the same position and live position is sticking to right side', () => {
@@ -114,6 +118,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 
 
 				expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
 				expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 
 
 			it( 'is before a node from the live position path', () => {
 			it( 'is before a node from the live position path', () => {
@@ -122,6 +127,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 
 
 				expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
 				expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 		} );
 		} );
 
 
@@ -137,6 +143,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
 				expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 
 
 			it( 'is at the same position and live position is sticking to right side', () => {
 			it( 'is at the same position and live position is sticking to right side', () => {
@@ -150,6 +157,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
 				expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 
 
 			it( 'is at a position before a node from the live position path', () => {
 			it( 'is at a position before a node from the live position path', () => {
@@ -163,6 +171,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
 				expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 
 
 			it( 'is from the same parent and closer offset', () => {
 			it( 'is from the same parent and closer offset', () => {
@@ -176,6 +185,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
 				expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 
 
 			it( 'is from a position before a node from the live position path', () => {
 			it( 'is from a position before a node from the live position path', () => {
@@ -189,6 +199,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
 				expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 
 
 			it( 'contains live position (same level)', () => {
 			it( 'contains live position (same level)', () => {
@@ -202,6 +213,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( [ 2, 2 ] );
 				expect( live.path ).to.deep.equal( [ 2, 2 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 
 
 			it( 'contains live position (deep)', () => {
 			it( 'contains live position (deep)', () => {
@@ -215,12 +227,13 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( [ 2, 1, 6 ] );
 				expect( live.path ).to.deep.equal( [ 2, 1, 6 ] );
+				expect( spy.calledOnce ).to.be.true;
 			} );
 			} );
 		} );
 		} );
 	} );
 	} );
 
 
 	describe( 'should not get transformed if', () => {
 	describe( 'should not get transformed if', () => {
-		let path, otherRoot;
+		let path, otherRoot, spy;
 
 
 		before( () => {
 		before( () => {
 			path = [ 1, 4, 6 ];
 			path = [ 1, 4, 6 ];
@@ -231,6 +244,9 @@ describe( 'LivePosition', () => {
 
 
 		beforeEach( () => {
 		beforeEach( () => {
 			live = new LivePosition( root, path );
 			live = new LivePosition( root, path );
+
+			spy = sinon.spy();
+			live.on( 'change', spy );
 		} );
 		} );
 
 
 		afterEach( () => {
 		afterEach( () => {
@@ -244,6 +260,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is at the same position and live position is sticking to left side', () => {
 			it( 'is at the same position and live position is sticking to left side', () => {
@@ -253,8 +270,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
-
-				live.detach();
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is after a node from the position path', () => {
 			it( 'is after a node from the position path', () => {
@@ -263,6 +279,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is in different root', () => {
 			it( 'is in different root', () => {
@@ -271,6 +288,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 				doc.fire( 'change', 'insert', { range: insertRange }, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 		} );
 		} );
 
 
@@ -286,6 +304,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is at the same position and live position is sticking to left side', () => {
 			it( 'is at the same position and live position is sticking to left side', () => {
@@ -300,8 +319,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
-
-				live.detach();
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is at a position after a node from the live position path', () => {
 			it( 'is at a position after a node from the live position path', () => {
@@ -315,6 +333,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is from the same parent and further offset', () => {
 			it( 'is from the same parent and further offset', () => {
@@ -328,6 +347,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is from a position after a node from the live position path', () => {
 			it( 'is from a position after a node from the live position path', () => {
@@ -341,6 +361,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is to different root', () => {
 			it( 'is to different root', () => {
@@ -354,6 +375,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 
 
 			it( 'is from different root', () => {
 			it( 'is from different root', () => {
@@ -367,6 +389,7 @@ describe( 'LivePosition', () => {
 				doc.fire( 'change', 'move', changes, null );
 				doc.fire( 'change', 'move', changes, null );
 
 
 				expect( live.path ).to.deep.equal( path );
 				expect( live.path ).to.deep.equal( path );
+				expect( spy.called ).to.be.false;
 			} );
 			} );
 		} );
 		} );
 
 
@@ -381,6 +404,7 @@ describe( 'LivePosition', () => {
 			doc.fire( 'change', 'setAttribute', changes, null );
 			doc.fire( 'change', 'setAttribute', changes, null );
 
 
 			expect( live.path ).to.deep.equal( path );
 			expect( live.path ).to.deep.equal( path );
+			expect( spy.called ).to.be.false;
 		} );
 		} );
 	} );
 	} );
 } );
 } );