Selaa lähdekoodia

Change core.treeModel.Selection#update event to change:range. Introduce change:attribute event.

Szymon Cofalik 9 vuotta sitten
vanhempi
sitoutus
edb5988503

+ 29 - 7
packages/ckeditor5-engine/src/treemodel/selection.js

@@ -113,7 +113,7 @@ export default class Selection {
 	 * to {@link core.treeModel.Range#end}. The flag is used to set {@link core.treeModel.Selection#anchor} and
 	 * {@link core.treeModel.Selection#focus} properties.
 	 *
-	 * @fires {@link core.treeModel.Selection.update update}
+	 * @fires {@link core.treeModel.Selection#change:range change:range}
 	 * @param {core.treeModel.Range} range Range to add.
 	 * @param {Boolean} [isBackward] Flag describing if added range was selected forward - from start to end (`false`)
 	 * or backward - from end to start (`true`). Defaults to `false`.
@@ -122,7 +122,7 @@ export default class Selection {
 		this._pushRange( range );
 		this._lastRangeBackward = !!isBackward;
 
-		this.fire( 'update' );
+		this.fire( 'change:range' );
 	}
 
 	/**
@@ -178,13 +178,13 @@ export default class Selection {
 	/**
 	 * Removes all ranges that were added to the selection. Fires update event.
 	 *
-	 * @fires {@link core.treeModel.Selection.update update}
+	 * @fires {@link core.treeModel.Selection#change:range change:range}
 	 */
 	removeAllRanges() {
 		this.destroy();
 		this._ranges = [];
 
-		this.fire( 'update' );
+		this.fire( 'change:range' );
 	}
 
 	/**
@@ -192,7 +192,7 @@ export default class Selection {
 	 * is treated like the last added range and is used to set {@link #anchor} and {@link #focus}. Accepts a flag
 	 * describing in which way the selection is made (see {@link #addRange}).
 	 *
-	 * @fires {@link core.treeModel.Selection.update update}
+	 * @fires {@link core.treeModel.Selection#change:range change:range}
 	 * @param {Array.<core.treeModel.Range>} newRanges Array of ranges to set.
 	 * @param {Boolean} [isLastBackward] Flag describing if last added range was selected forward - from start to end (`false`)
 	 * or backward - from end to start (`true`). Defaults to `false`.
@@ -206,15 +206,20 @@ export default class Selection {
 		}
 
 		this._lastRangeBackward = !!isLastBackward;
-		this.fire( 'update' );
+
+		this.fire( 'change:range' );
 	}
 
 	/**
 	 * Removes all attributes from the selection.
+	 *
+	 * @fires {@link core.treeModel.Selection#change:range change:range}
 	 */
 	clearAttributes() {
 		this._attrs.clear();
 		this._setStoredAttributesTo( new Map() );
+
+		this.fire( 'change:attribute' );
 	}
 
 	/**
@@ -249,32 +254,41 @@ export default class Selection {
 	/**
 	 * Removes an attribute with given key from the selection.
 	 *
+	 * @fires {@link core.treeModel.Selection#change:range change:range}
 	 * @param {String} key Key of attribute to remove.
 	 */
 	removeAttribute( key ) {
 		this._attrs.delete( key );
 		this._removeStoredAttribute( key );
+
+		this.fire( 'change:attribute' );
 	}
 
 	/**
 	 * Sets attribute on the selection. If attribute with the same key already is set, it overwrites its values.
 	 *
+	 * @fires {@link core.treeModel.Selection#change:range change:range}
 	 * @param {String} key Key of attribute to set.
 	 * @param {*} value Attribute value.
 	 */
 	setAttribute( key, value ) {
 		this._attrs.set( key, value );
 		this._storeAttribute( key, value );
+
+		this.fire( 'change:attribute' );
 	}
 
 	/**
 	 * Removes all attributes from the selection and sets given attributes.
 	 *
+	 * @fires {@link core.treeModel.Selection#change:range change:range}
 	 * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
 	 */
 	setAttributesTo( attrs ) {
 		this._attrs = utils.toMap( attrs );
 		this._setStoredAttributesTo( this._attrs );
+
+		this.fire( 'change:attribute' );
 	}
 
 	/**
@@ -465,6 +479,8 @@ export default class Selection {
 
 			return null;
 		}
+
+		this.fire( 'change:attribute' );
 	}
 
 	/**
@@ -499,5 +515,11 @@ utils.mix( Selection, EmitterMixin );
  * Fired whenever selection ranges are changed through {@link core.treeModel.Selection Selection API}. Not fired when
  * {@link core.treeModel.LiveRange live ranges} inserted in selection change because of Tree Model changes.
  *
- * @event core.treeModel.Selection.update
+ * @event core.treeModel.Selection#change:range
+ */
+
+/**
+ * Fired whenever selection attributes are changed.
+ *
+ * @event core.treeModel.Selection#change:attribute
  */

+ 50 - 5
packages/ckeditor5-engine/tests/treemodel/selection.js

@@ -134,9 +134,9 @@ describe( 'Selection', () => {
 		expect( ranges[ 0 ] ).to.be.instanceof( LiveRange );
 	} );
 
-	it( 'should fire update event when adding a range', () => {
+	it( 'should fire change:range event when adding a range', () => {
 		let spy = sinon.spy();
-		selection.on( 'update', spy );
+		selection.on( 'change:range', spy );
 
 		selection.addRange( range );
 
@@ -182,7 +182,7 @@ describe( 'Selection', () => {
 			selection.addRange( range );
 
 			spy = sinon.spy();
-			selection.on( 'update', spy );
+			selection.on( 'change:range', spy );
 
 			ranges = selection.getRanges();
 
@@ -228,7 +228,7 @@ describe( 'Selection', () => {
 			selection.addRange( range );
 
 			spy = sinon.spy();
-			selection.on( 'update', spy );
+			selection.on( 'change:range', spy );
 
 			oldRanges = selection.getRanges();
 
@@ -321,7 +321,7 @@ describe( 'Selection', () => {
 			selection.addRange( new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 4 ] ) ) );
 
 			spy = sinon.spy();
-			selection.on( 'update', spy );
+			selection.on( 'change:range', spy );
 		} );
 
 		describe( 'InsertOperation', () => {
@@ -487,6 +487,15 @@ describe( 'Selection', () => {
 
 				expect( emptyP.getAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
 			} );
+
+			it( 'should fire change:attribute event', () => {
+				let spy = sinon.spy();
+				selection.on( 'change:attribute', spy );
+
+				selection.setAttribute( 'foo', 'bar' );
+
+				expect( spy.called ).to.be.true;
+			} );
 		} );
 
 		describe( 'hasAttribute', () => {
@@ -544,6 +553,15 @@ describe( 'Selection', () => {
 				expect( emptyP.getAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.equal( 'bar' );
 				expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
 			} );
+
+			it( 'should fire change:attribute event', () => {
+				let spy = sinon.spy();
+				selection.on( 'change:attribute', spy );
+
+				selection.setAttributesTo( { foo: 'bar' } );
+
+				expect( spy.called ).to.be.true;
+			} );
 		} );
 
 		describe( 'removeAttribute', () => {
@@ -566,6 +584,15 @@ describe( 'Selection', () => {
 
 				expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
 			} );
+
+			it( 'should fire change:attribute event', () => {
+				let spy = sinon.spy();
+				selection.on( 'change:attribute', spy );
+
+				selection.removeAttribute( 'foo' );
+
+				expect( spy.called ).to.be.true;
+			} );
 		} );
 
 		describe( 'clearAttributes', () => {
@@ -596,6 +623,15 @@ describe( 'Selection', () => {
 				expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'foo' ) ) ).to.be.false;
 				expect( emptyP.hasAttribute( Selection._getStoreAttributeKey( 'abc' ) ) ).to.be.false;
 			} );
+
+			it( 'should fire change:attribute event', () => {
+				let spy = sinon.spy();
+				selection.on( 'change:attribute', spy );
+
+				selection.clearAttributes();
+
+				expect( spy.called ).to.be.true;
+			} );
 		} );
 	} );
 
@@ -650,6 +686,15 @@ describe( 'Selection', () => {
 			selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 0 ] ) ) ] );
 			expect( Array.from( selection.getAttributes() ) ).to.deep.equal( [] );
 		} );
+
+		it( 'should fire change:attribute event', () => {
+			let spy = sinon.spy();
+			selection.on( 'change:attribute', spy );
+
+			selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 5 ] ) ) ] );
+
+			expect( spy.called ).to.be.true;
+		} );
 	} );
 
 	describe( '_getStoredAttributes', () => {