Ver código fonte

Type as a separate argument in the change event. Docs for change event.

Piotr Jasiun 10 anos atrás
pai
commit
f0d25ec863

+ 27 - 2
packages/ckeditor5-engine/src/document/document.js

@@ -92,8 +92,7 @@ CKEDITOR.define( [
 
 			this.version++;
 
-			changes.type = operation.type;
-			this.fire( 'change', changes );
+			this.fire( 'change', operation.type, changes );
 		}
 
 		/**
@@ -154,6 +153,32 @@ CKEDITOR.define( [
 
 			return this.roots.get( name );
 		}
+
+		/**
+		 * Fired when document changes by applying an operation.
+		 *
+		 * There are 5 types of change:
+		 *
+		 * * 'insert' when nodes are inserted,
+		 * * 'remove' when nodes are removed,
+		 * * 'reinsert' when remove is undone,
+		 * * 'move' when nodes are moved,
+		 * * 'attr' when attributes change.
+		 *
+		 * Change event is fired after the change is done.
+		 *
+		 * @event change
+		 * @param {String} type Change type, possible option: 'insert', 'remove', 'reinsert', 'move', 'attr'.
+		 * @param {Object} changeInfo Additional informations about the change.
+		 * @param {document.Range} changeInfo.range Range containing changed nodes. Note that for 'remove' the range will be in the
+		 * {@link #_graveyard graveyard root}.
+		 * @param {document.Range} [changeInfo.sourcePosition] Source position. For 'remove', 'reinsert' and 'move'. This property is
+		 * undefined for other node types. Note that for 'reinsert' the source position will be in the {@link #_graveyard graveyard root}.
+		 * @param {document.Attribute} [changeInfo.oldAttr] Only for 'attr' type. If the type is 'attr' and `oldAttr` is undefined it means
+		 * that new attribute was inserted. Otherwise it contains changed or removed attribute.
+		 * @param {document.Attribute} [changeInfo.newAttr] Only for 'attr' type. If the type is 'attr' and `newAttr` is undefined it means
+		 * that attribute was removed. Otherwise it contains changed or inserted attribute.
+		 */
 	}
 
 	utils.extend( Document.prototype, EmitterMixin );

+ 14 - 12
packages/ckeditor5-engine/tests/document/document-change-event.js

@@ -40,17 +40,19 @@ describe( 'Document change event', () => {
 		RemoveOperation = modules[ 'document/operation/removeoperation' ];
 	} );
 
-	let doc, root, graveyard, changes;
+	let doc, root, graveyard, types, changes;
 
 	beforeEach( () => {
 		doc = new Document();
 		root = doc.createRoot( 'root' );
 		graveyard = doc._graveyard;
 
+		types = [];
 		changes = [];
 
-		doc.on( 'change', ( evt, data ) => {
-			changes.push( data );
+		doc.on( 'change', ( evt, type, change ) => {
+			types.push( type );
+			changes.push( change );
 		} );
 	} );
 
@@ -58,7 +60,7 @@ describe( 'Document change event', () => {
 		doc.applyOperation( new InsertOperation( new Position( [ 0 ], root ), 'foo', doc.version ) );
 
 		expect( changes ).to.have.length( 1 );
-		expect( changes[ 0 ].type ).to.equal( 'insert' );
+		expect( types[ 0 ] ).to.equal( 'insert' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
 	} );
 
@@ -67,7 +69,7 @@ describe( 'Document change event', () => {
 		doc.applyOperation( new InsertOperation( new Position( [ 0 ], root ), element, doc.version ) );
 
 		expect( changes ).to.have.length( 1 );
-		expect( changes[ 0 ].type ).to.equal( 'insert' );
+		expect( types[ 0 ] ).to.equal( 'insert' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
 	} );
 
@@ -76,7 +78,7 @@ describe( 'Document change event', () => {
 		doc.applyOperation( new InsertOperation( new Position( [ 0 ], root ), [ element, 'foo' ], doc.version ) );
 
 		expect( changes ).to.have.length( 1 );
-		expect( changes[ 0 ].type ).to.equal( 'insert' );
+		expect( types[ 0 ] ).to.equal( 'insert' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 4 ) );
 	} );
 
@@ -98,7 +100,7 @@ describe( 'Document change event', () => {
 		);
 
 		expect( changes ).to.have.length( 1 );
-		expect( changes[ 0 ].type ).to.equal( 'move' );
+		expect( types[ 0 ] ).to.equal( 'move' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( p2, 0, p2, 3 ) );
 		expect( changes[ 0 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( p1, 0 ) );
 	} );
@@ -114,11 +116,11 @@ describe( 'Document change event', () => {
 
 		expect( changes ).to.have.length( 2 );
 
-		expect( changes[ 0 ].type ).to.equal( 'remove' );
+		expect( types[ 0 ] ).to.equal( 'remove' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( graveyard, 0, graveyard, 3 ) );
 		expect( changes[ 0 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( root, 0 ) );
 
-		expect( changes[ 1 ].type ).to.equal( 'reinsert' );
+		expect( types[ 1 ] ).to.equal( 'reinsert' );
 		expect( changes[ 1 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
 		expect( changes[ 1 ].sourcePosition ).to.deep.equal( Position.createFromParentAndOffset( graveyard, 0 ) );
 	} );
@@ -136,7 +138,7 @@ describe( 'Document change event', () => {
 		);
 
 		expect( changes ).to.have.length( 1 );
-		expect( changes[ 0 ].type ).to.equal( 'attr' );
+		expect( types[ 0 ] ).to.equal( 'attr' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
 		expect( changes[ 0 ].oldAttr ).to.be.undefined;
 		expect( changes[ 0 ].newAttr ).to.deep.equal( new Attribute( 'key', 'new' ) );
@@ -156,7 +158,7 @@ describe( 'Document change event', () => {
 		);
 
 		expect( changes ).to.have.length( 1 );
-		expect( changes[ 0 ].type ).to.equal( 'attr' );
+		expect( types[ 0 ] ).to.equal( 'attr' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
 		expect( changes[ 0 ].oldAttr ).to.deep.equal( new Attribute( 'key', 'old' ) );
 		expect( changes[ 0 ].newAttr ).to.be.undefined;
@@ -176,7 +178,7 @@ describe( 'Document change event', () => {
 		);
 
 		expect( changes ).to.have.length( 1 );
-		expect( changes[ 0 ].type ).to.equal( 'attr' );
+		expect( types[ 0 ] ).to.equal( 'attr' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
 		expect( changes[ 0 ].oldAttr ).to.deep.equal( new Attribute( 'key', 'old' ) );
 		expect( changes[ 0 ].newAttr ).to.deep.equal( new Attribute( 'key', 'new' ) );

+ 8 - 4
packages/ckeditor5-engine/tests/document/document.js

@@ -78,11 +78,14 @@ describe( 'Document', () => {
 
 	describe( 'applyOperation', () => {
 		it( 'should increase document version and execute operation', () => {
-			let changeCallback = sinon.spy();
+			const changeCallback = sinon.spy();
+			const type = 't';
+			const data = { data: 'x' };
+
 			let operation = {
-				type: 't',
+				type: type,
 				baseVersion: 0,
-				_execute: sinon.stub().returns( { data: 'x' } )
+				_execute: sinon.stub().returns( data )
 			};
 
 			document.on( 'change', changeCallback );
@@ -92,7 +95,8 @@ describe( 'Document', () => {
 			sinon.assert.calledOnce( operation._execute );
 
 			sinon.assert.calledOnce( changeCallback );
-			expect( changeCallback.args[ 0 ][ 1 ] ).to.deep.equal( { data: 'x', type: 't' } );
+			expect( changeCallback.args[ 0 ][ 1 ] ).to.equals( type );
+			expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
 		} );
 
 		it( 'should throw an error on the operation base version and the document version is different', () => {