Browse Source

Fixed issues of focus managment.

Oskar Wróbel 8 years ago
parent
commit
721e768ddd
2 changed files with 30 additions and 9 deletions
  1. 7 6
      packages/ckeditor5-link/src/link.js
  2. 23 3
      packages/ckeditor5-link/tests/link.js

+ 7 - 6
packages/ckeditor5-link/src/link.js

@@ -240,20 +240,21 @@ export default class Link extends Plugin {
 	 *
 	 * @private
 	 * @param {Boolean} [focusInput=false] When `true`, link form will be focused on panel show.
+	 * @return {Promise} A promise resolved when the {#formView} {@link module:ui/view~View#init} is done.
 	 */
 	_showPanel( focusInput ) {
 		if ( this._balloon.hasView( this.formView ) ) {
-			return;
+			return Promise.resolve();
 		}
 
-		this._balloon.add( {
+		return this._balloon.add( {
 			view: this.formView,
 			position: this._getBalloonPositionData()
+		} ).then( () => {
+			if ( focusInput ) {
+				this.formView.urlInputView.select();
+			}
 		} );
-
-		if ( focusInput ) {
-			this.formView.urlInputView.select();
-		}
 	}
 
 	/**

+ 23 - 3
packages/ckeditor5-link/tests/link.js

@@ -139,7 +139,11 @@ describe( 'Link', () => {
 
 			linkButton.fire( 'execute' );
 
-			expect( selectUrlInputSpy.calledOnce ).to.true;
+			// Focus of url input is called async after internal promise resolve and we are
+			// not able to return this promise.
+			return wait().then( () => {
+				expect( selectUrlInputSpy.calledOnce ).to.true;
+			} );
 		} );
 	} );
 
@@ -185,7 +189,12 @@ describe( 'Link', () => {
 			editor.keystrokes.press( { keyCode: keyCodes.k, ctrlKey: true } );
 
 			expect( balloon.visibleView ).to.equal( formView );
-			expect( selectUrlInputSpy.calledOnce ).to.true;
+
+			// Focus of url input is called async after internal promise resolve and we are
+			// not able to return this promise.
+			return wait().then( () => {
+				expect( selectUrlInputSpy.calledOnce ).to.true;
+			} );
 		} );
 
 		it( 'should not add panel to ContextualBalloon more than once', () => {
@@ -341,7 +350,12 @@ describe( 'Link', () => {
 				observer.fire( 'click', { target: document.body } );
 
 				expect( balloon.visibleView ).to.equal( formView );
-				expect( selectUrlInputSpy.notCalled ).to.true;
+
+				// Focus of url input is called async after internal promise resolve and we are
+				// not able to return this promise.
+				return wait().then( () => {
+					expect( selectUrlInputSpy.notCalled ).to.true;
+				} );
 			} );
 
 			it( 'should keep open and update position until collapsed selection stay inside the same link element', () => {
@@ -569,3 +583,9 @@ describe( 'Link', () => {
 		} );
 	} );
 } );
+
+function wait( delay = 1 ) {
+	return new Promise( ( resolve ) => {
+		setTimeout( () => resolve(), delay );
+	} );
+}