8
0
فهرست منبع

Updated Document.change event.

Piotr Jasiun 9 سال پیش
والد
کامیت
93a8189b58

+ 22 - 18
packages/ckeditor5-engine/src/treemodel/document.js

@@ -288,27 +288,31 @@ export default class Document {
 	 * * 'remove' when nodes are removed,
 	 * * 'reinsert' when remove is undone,
 	 * * 'move' when nodes are moved,
-	 * * 'attribute' when attributes change,
-	 * * 'rootattribute' when attributes for root element change.
+	 * * 'addAttribute' when attributes are added,
+	 * * 'removeAttribute' when attributes are removed,
+	 * * 'changeAttribute' when attributes change,
+	 * * 'addRootAttribute' when attribute for root is added,
+	 * * 'removeRootAttribute' when attribute for root is removed,
+	 * * 'changeRootAttribute' when attribute for root changes.
 	 *
 	 * Change event is fired after the change is done. This means that any ranges or positions passed in
-	 * `changeInfo` are referencing nodes and paths in updated tree model.
+	 * `data` are referencing nodes and paths in updated tree model.
 	 *
-	 * @event engine.treeModel.Document#change
-	 * @param {String} type Change type, possible option: `'insert'`, `'remove'`, `'reinsert'`, `'move'`, `'attribute'`.
-	 * @param {Object} changeInfo Additional information about the change.
-	 * @param {engine.treeModel.Range} [changeInfo.range] Range containing changed nodes. Note that for `'remove'` the range will be in the
-	 * {@link engine.treeModel.Document#graveyard graveyard root}. This is undefined for `'rootattribute'` type.
-	 * @param {engine.treeModel.RootElement} [changeInfo.root] Root element which attributes got changed. This is defined
-	 * only for `'rootattribute'` type.
-	 * @param {engine.treeModel.Position} [changeInfo.sourcePosition] Change source position. Exists for `'remove'`, `'reinsert'` and `'move'`.
-	 * Note that for 'reinsert' the source position will be in the {@link engine.treeModel.Document#graveyard graveyard root}.
-	 * @param {String} [changeInfo.key] Only for `'attribute'` type. Key of changed / inserted / removed attribute.
-	 * @param {*} [changeInfo.oldValue] Only for `'attribute'` type. If the type is `'attribute'` and `oldValue`
-	 * is `undefined` it means that new attribute was inserted. Otherwise it contains changed or removed attribute value.
-	 * @param {*} [changeInfo.newValue] Only for `'attribute'` type. If the type is `'attribute'` and `newValue`
-	 * is `undefined` it means that attribute was removed. Otherwise it contains changed or inserted attribute value.
-	 * @param {engine.treeModel.Batch} batch A batch of changes which this change is a part of.
+	 * @event core.treeModel.Document.change
+	 * @param {String} type Change type, possible option: 'insert', 'remove', 'reinsert', 'move', 'attribute'.
+	 * @param {Object} data Additional information about the change.
+	 * @param {core.treeModel.Range} data.range Range containing changed nodes. Note that for 'remove' the range will be in the
+	 * {@link core.treeModel.Document#graveyard graveyard root}. This is undefined for root types.
+	 * @param {core.treeModel.Position} [data.sourcePosition] Change source position. Exists for 'remove', 'reinsert' and 'move'.
+	 * Note that for 'reinsert' the source position will be in the {@link core.treeModel.Document#graveyard graveyard root}.
+	 * @param {String} [data.key] Only for attribute types. Key of changed / inserted / removed attribute.
+	 * @param {*} [data.oldValue] Only for 'removeAttribute', 'removeRootAttribute', 'changeAttribute' or
+	 * 'changeRootAttribute' type.
+	 * @param {*} [data.newValue] Only for 'addAttribute', 'addRootAttribute', 'changeAttribute' or
+	 * 'changeRootAttribute' type.
+	 * @param {core.treeModel.RootElement} [changeInfo.root] Root element which attributes got changed. This is defined
+	 * only for root types.
+	 * @param {core.treeModel.Batch} batch A {@link core.treeModel.Batch batch} of changes which this change is a part of.
 	 */
 
 	/**

+ 7 - 1
packages/ckeditor5-engine/src/treemodel/operation/attributeoperation.js

@@ -71,7 +71,13 @@ export default class AttributeOperation extends Operation {
 	}
 
 	get type() {
-		return 'attribute';
+		if ( this.oldValue === null ) {
+			return 'addAttribute';
+		} else if ( this.newValue === null ) {
+			return 'removeAttribute';
+		} else {
+			return 'changeAttribute';
+		}
 	}
 
 	/**

+ 7 - 1
packages/ckeditor5-engine/src/treemodel/operation/rootattributeoperation.js

@@ -67,7 +67,13 @@ export default class RootAttributeOperation extends Operation {
 	}
 
 	get type() {
-		return 'rootattribute';
+		if ( this.oldValue === null ) {
+			return 'addRootAttribute';
+		} else if ( this.newValue === null ) {
+			return 'removeRootAttribute';
+		} else {
+			return 'changeRootAttribute';
+		}
 	}
 
 	/**

+ 3 - 3
packages/ckeditor5-engine/tests/treemodel/document/change-event.js

@@ -116,7 +116,7 @@ describe( 'Document change event', () => {
 		);
 
 		expect( changes ).to.have.length( 1 );
-		expect( types[ 0 ] ).to.equal( 'attribute' );
+		expect( types[ 0 ] ).to.equal( 'addAttribute' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, root, 3 ) );
 		expect( changes[ 0 ].key ).to.equal( 'key' );
 		expect( changes[ 0 ].oldValue ).to.be.null;
@@ -138,7 +138,7 @@ describe( 'Document change event', () => {
 		);
 
 		expect( changes ).to.have.length( 1 );
-		expect( types[ 0 ] ).to.equal( 'attribute' );
+		expect( types[ 0 ] ).to.equal( 'removeAttribute' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
 		expect( changes[ 0 ].key ).to.equal( 'key' );
 		expect( changes[ 0 ].oldValue ).to.equal( 'old' );
@@ -160,7 +160,7 @@ describe( 'Document change event', () => {
 		);
 
 		expect( changes ).to.have.length( 1 );
-		expect( types[ 0 ] ).to.equal( 'attribute' );
+		expect( types[ 0 ] ).to.equal( 'changeAttribute' );
 		expect( changes[ 0 ].range ).to.deep.equal( Range.createFromParentsAndOffsets( root, 0, elem, 0 ) );
 		expect( changes[ 0 ].key ).to.equal( 'key' );
 		expect( changes[ 0 ].oldValue ).to.equal( 'old' );

+ 35 - 9
packages/ckeditor5-engine/tests/treemodel/operation/attributeoperation.js

@@ -23,16 +23,42 @@ describe( 'AttributeOperation', () => {
 		root = doc.createRoot( 'root' );
 	} );
 
-	it( 'should have proper type', () => {
-		const op = new AttributeOperation(
-			new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
-			'isNew',
-			null,
-			true,
-			doc.version
-		);
+	describe( 'type', () => {
+		it( 'should be addAttribute for adding attribute', () => {
+			const op = new AttributeOperation(
+				new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
+				'key',
+				null,
+				'newValue',
+				doc.version
+			);
+
+			expect( op.type ).to.equal( 'addAttribute' );
+		} );
+
+		it( 'should be removeAttribute for removing attribute', () => {
+			const op = new AttributeOperation(
+				new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
+				'key',
+				'oldValue',
+				null,
+				doc.version
+			);
+
+			expect( op.type ).to.equal( 'removeAttribute' );
+		} );
+
+		it( 'should be changeAttribute for removing attribute', () => {
+			const op = new AttributeOperation(
+				new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
+				'key',
+				'oldValue',
+				'newValue',
+				doc.version
+			);
 
-		expect( op.type ).to.equal( 'attribute' );
+			expect( op.type ).to.equal( 'changeAttribute' );
+		} );
 	} );
 
 	it( 'should insert attribute to the set of nodes', () => {

+ 35 - 9
packages/ckeditor5-engine/tests/treemodel/operation/rootattributeoperation.js

@@ -19,16 +19,42 @@ describe( 'RootAttributeOperation', () => {
 		root = doc.createRoot( 'root' );
 	} );
 
-	it( 'should have proper type', () => {
-		const op = new RootAttributeOperation(
-			root,
-			'isNew',
-			null,
-			true,
-			doc.version
-		);
+	describe( 'type', () => {
+		it( 'should be addRootAttribute for adding attribute', () => {
+			const op = new RootAttributeOperation(
+				root,
+				'key',
+				null,
+				'newValue',
+				doc.version
+			);
+
+			expect( op.type ).to.equal( 'addRootAttribute' );
+		} );
+
+		it( 'should be removeRootAttribute for removing attribute', () => {
+			const op = new RootAttributeOperation(
+				root,
+				'key',
+				'oldValue',
+				null,
+				doc.version
+			);
+
+			expect( op.type ).to.equal( 'removeRootAttribute' );
+		} );
+
+		it( 'should be changeRootAttribute for removing attribute', () => {
+			const op = new RootAttributeOperation(
+				root,
+				'key',
+				'oldValue',
+				'newValue',
+				doc.version
+			);
 
-		expect( op.type ).to.equal( 'rootattribute' );
+			expect( op.type ).to.equal( 'changeRootAttribute' );
+		} );
 	} );
 
 	it( 'should add attribute on the root element', () => {