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

Changed: LiveSelection#_updateAttributes called each time #setRanges, #removeAllRanges and #addRange is called.

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

+ 0 - 1
packages/ckeditor5-engine/src/model/composer/deletecontents.js

@@ -58,7 +58,6 @@ export default function deleteContents( batch, selection, options = {} ) {
 	}
 
 	selection.collapse( startPos );
-	selection.refreshAttributes();
 
 	// 3. Autoparagraphing.
 	// Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).

+ 24 - 7
packages/ckeditor5-engine/src/model/liveselection.js

@@ -65,13 +65,6 @@ export default class LiveSelection extends Selection {
 		 */
 		this._attributePriority = new Map();
 
-		// Whenever selection range changes, if the change comes directly from selection API (direct user change).
-		this.on( 'change:range', ( evt, data ) => {
-			if ( data.directChange ) {
-				this.refreshAttributes();
-			}
-		}, { priority: 'high' } );
-
 		// Whenever attribute operation is performed on document, update attributes. This is not the most efficient
 		// way to update selection attributes, but should be okay for now. `_updateAttributes` will be fired too often,
 		// but it won't change attributes or fire `change:attribute` event if not needed.
@@ -148,6 +141,30 @@ export default class LiveSelection extends Selection {
 		return super.getLastRange() || this._document._getDefaultRange();
 	}
 
+	/**
+	 * @inheritDoc
+	 */
+	addRange( range, isBackward = false ) {
+		super.addRange( range, isBackward );
+		this.refreshAttributes();
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	removeAllRanges() {
+		super.removeAllRanges();
+		this.refreshAttributes();
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	setRanges( newRanges, isLastBackward = false ) {
+		super.setRanges( newRanges, isLastBackward );
+		this.refreshAttributes();
+	}
+
 	/**
 	 * @inheritDoc
 	 */

+ 0 - 8
packages/ckeditor5-engine/tests/model/document/document.js

@@ -225,14 +225,6 @@ describe( 'Document', () => {
 	} );
 
 	describe( 'selection', () => {
-		it( 'should get updated attributes whenever selection gets updated', () => {
-			sinon.spy( doc.selection, '_updateAttributes' );
-
-			doc.selection.fire( 'change:range', { directChange: true } );
-
-			expect( doc.selection._updateAttributes.called ).to.be.true;
-		} );
-
 		it( 'should get updated attributes whenever attribute operation is applied', () => {
 			sinon.spy( doc.selection, '_updateAttributes' );
 

+ 24 - 0
packages/ckeditor5-engine/tests/model/liveselection.js

@@ -140,6 +140,14 @@ describe( 'LiveSelection', () => {
 
 			expect( selection._ranges.length ).to.equal( 0 );
 		} );
+
+		it( 'should refresh attributes', () => {
+			const spy = sinon.spy( selection, '_updateAttributes' );
+
+			selection.addRange( range );
+
+			expect( spy.called ).to.be.true;
+		} );
 	} );
 
 	describe( 'collapse', () => {
@@ -232,6 +240,14 @@ describe( 'LiveSelection', () => {
 			expect( ranges[ 0 ].detach.called ).to.be.true;
 			expect( ranges[ 1 ].detach.called ).to.be.true;
 		} );
+
+		it( 'should refresh attributes', () => {
+			const spy = sinon.spy( selection, '_updateAttributes' );
+
+			selection.removeAllRanges();
+
+			expect( spy.called ).to.be.true;
+		} );
 	} );
 
 	describe( 'setRanges', () => {
@@ -255,6 +271,14 @@ describe( 'LiveSelection', () => {
 			expect( oldRanges[ 0 ].detach.called ).to.be.true;
 			expect( oldRanges[ 1 ].detach.called ).to.be.true;
 		} );
+
+		it( 'should refresh attributes', () => {
+			const spy = sinon.spy( selection, '_updateAttributes' );
+
+			selection.setRanges( [ range ] );
+
+			expect( spy.called ).to.be.true;
+		} );
 	} );
 
 	describe( 'getFirstRange', () => {