8
0
Pārlūkot izejas kodu

Changed the way of fiering selectionChangeEnd on every selection change.

Oskar Wróbel 8 gadi atpakaļ
vecāks
revīzija
543c3d1f4b

+ 29 - 11
packages/ckeditor5-engine/src/view/observer/selectionobserver.js

@@ -122,14 +122,6 @@ export default class SelectionObserver extends Observer {
 			this._handleSelectionChange( domDocument );
 		} );
 
-		// Call` #_fireSelectionChangeDoneDebounced` on each `selectionChange` event.
-		// This function is debounced what means that `selectionChangeDone` event will be fired only when
-		// defined int the function time will elapse since the last time the function was called.
-		// So `selectionChangeDone` will be fired when selection will stop changing.
-		this.listenTo( this.document, 'selectionChange', ( evt, data ) => {
-			this._fireSelectionChangeDoneDebounced( data );
-		} );
-
 		this._documents.add( domDocument );
 	}
 
@@ -182,12 +174,20 @@ export default class SelectionObserver extends Observer {
 			return;
 		}
 
-		// Should be fired only when selection change was the only document change.
-		this.document.fire( 'selectionChange', {
+		const data = {
 			oldSelection: this.selection,
 			newSelection: newViewSelection,
 			domSelection: domSelection
-		} );
+		};
+
+		// Should be fired only when selection change was the only document change.
+		this.document.fire( 'selectionChange', data );
+
+		// Call` #_fireSelectionChangeDoneDebounced` every time when `selectionChange` event is fired.
+		// This function is debounced what means that `selectionChangeDone` event will be fired only when
+		// defined int the function time will elapse since the last time the function was called.
+		// So `selectionChangeDone` will be fired when selection will stop changing.
+		this._fireSelectionChangeDoneDebounced( data );
 	}
 
 	/**
@@ -251,3 +251,21 @@ export default class SelectionObserver extends Observer {
  * @param {module:engine/view/selection~Selection} data.newSelection New View selection which is converted DOM selection.
  * @param {Selection} data.domSelection Native DOM selection.
  */
+
+/**
+ * Fired when selection stop changing.
+ *
+ * Introduced by {@link module:engine/view/observer/selectionobserver~SelectionObserver}.
+ *
+ * Note that because {@link module:engine/view/observer/selectionobserver~SelectionObserver} is attached by the
+ * {@link module:engine/view/document~Document}
+ * this event is available by default.
+ *
+ * @see module:engine/view/observer/selectionobserver~SelectionObserver
+ * @event module:engine/view/document~Document#event:selectionChangeDone
+ * @param {Object} data
+ * @param {module:engine/view/selection~Selection} data.oldSelection Old View selection which is
+ * {@link module:engine/view/document~Document#selection}.
+ * @param {module:engine/view/selection~Selection} data.newSelection New View selection which is converted DOM selection.
+ * @param {Selection} data.domSelection Native DOM selection.
+ */

+ 25 - 9
packages/ckeditor5-engine/tests/view/observer/selectionobserver.js

@@ -235,29 +235,45 @@ describe( 'SelectionObserver', () => {
 
 		viewDocument.on( 'selectionChangeDone', spy );
 
-		// Fire `selectionChange` event.
-		viewDocument.fire( 'selectionChange' );
+		// Change selection.
+		changeDomSelection();
 
 		// Wait 100ms.
 		setTimeout( () => {
 			// Check if spy was called.
 			expect( spy.notCalled ).to.true;
 
-			// Fire event again.
-			viewDocument.fire( 'selectionChange' );
+			// Change selection.
+			changeDomSelection();
 
 			// Wait 100ms.
 			setTimeout( () => {
 				// Check if spy was called.
 				expect( spy.notCalled ).to.true;
 
-				// Fire event again.
-				viewDocument.fire( 'selectionChange', { foo: 'bar' } );
+				// Change selection.
+				changeDomSelection();
 
 				// Wait 210ms (debounced function should be called).
 				setTimeout( () => {
+					const data = spy.firstCall.args[ 1 ];
+
 					expect( spy.calledOnce ).to.true;
-					expect( spy.firstCall.args[ 1 ] ).to.deep.equal( { foo: 'bar' } );
+					expect( data ).to.have.property( 'domSelection' ).to.equal( document.getSelection() );
+
+					expect( data ).to.have.property( 'oldSelection' ).to.instanceof( ViewSelection );
+					expect( data.oldSelection.rangeCount ).to.equal( 0 );
+
+					expect( data ).to.have.property( 'newSelection' ).to.instanceof( ViewSelection );
+					expect( data.newSelection.rangeCount ).to.equal( 1 );
+
+					const newViewRange = data.newSelection.getFirstRange();
+					const viewFoo = viewDocument.getRoot().getChild( 0 ).getChild( 0 );
+
+					expect( newViewRange.start.parent ).to.equal( viewFoo );
+					expect( newViewRange.start.offset ).to.equal( 2 );
+					expect( newViewRange.end.parent ).to.equal( viewFoo );
+					expect( newViewRange.end.offset ).to.equal( 2 );
 
 					done();
 				}, 210 );
@@ -273,8 +289,8 @@ describe( 'SelectionObserver', () => {
 
 		viewDocument.on( 'selectionChangeDone', spy );
 
-		// Fire `selectionChange` event.
-		viewDocument.fire( 'selectionChange' );
+		// Change selection.
+		changeDomSelection();
 
 		// Wait 100ms.
 		setTimeout( () => {