Browse Source

Merge pull request #226 from ckeditor/t/171

Fix: Link balloon will not be shown if no link was added after command execution. Closes #171.
Oskar Wróbel 6 years ago
parent
commit
03ae20882d
2 changed files with 50 additions and 5 deletions
  1. 19 3
      packages/ckeditor5-link/src/linkui.js
  2. 31 2
      packages/ckeditor5-link/tests/linkui.js

+ 19 - 3
packages/ckeditor5-link/src/linkui.js

@@ -154,17 +154,17 @@ export default class LinkUI extends Plugin {
 		// Execute link command after clicking the "Save" button.
 		this.listenTo( formView, 'submit', () => {
 			editor.execute( 'link', formView.urlInputView.inputView.element.value );
-			this._removeFormView();
+			this._closeFormView();
 		} );
 
 		// Hide the panel after clicking the "Cancel" button.
 		this.listenTo( formView, 'cancel', () => {
-			this._removeFormView();
+			this._closeFormView();
 		} );
 
 		// Close the panel on esc key press when the **form has focus**.
 		formView.keystrokes.set( 'Esc', ( data, cancel ) => {
-			this._removeFormView();
+			this._closeFormView();
 			cancel();
 		} );
 
@@ -310,6 +310,22 @@ export default class LinkUI extends Plugin {
 	}
 
 	/**
+	 * Closes form view. Decides whether the balloon should be hidden completely or if action view should be shown. This is decided upon
+	 * link command value (which has value if the document selection is in link).
+	 *
+	 * @private
+	 */
+	_closeFormView() {
+		const linkCommand = this.editor.commands.get( 'link' );
+
+		if ( linkCommand.value !== undefined ) {
+			this._removeFormView();
+		} else {
+			this._hideUI();
+		}
+	}
+
+	/**
 	 * Removes the {@link #formView} from the {@link #_balloon}.
 	 *
 	 * @protected

+ 31 - 2
packages/ckeditor5-link/tests/linkui.js

@@ -845,15 +845,26 @@ describe( 'LinkUI', () => {
 				expect( focusEditableSpy.calledOnce ).to.be.true;
 			} );
 
-			it( 'should hide and reveal the #actionsView on formView#cancel event', () => {
+			it( 'should hide and reveal the #actionsView on formView#cancel event if link command has a value', () => {
 				linkUIFeature._showUI();
+
+				const command = editor.commands.get( 'link' );
+				command.value = 'http://foo.com';
+
 				formView.fire( 'cancel' );
 
 				expect( balloon.visibleView ).to.equal( actionsView );
 				expect( focusEditableSpy.calledOnce ).to.be.true;
 			} );
 
-			it( 'should hide after Esc key press', () => {
+			it( 'should hide the balloon on formView#cancel if link command does not have a value', () => {
+				linkUIFeature._showUI();
+				formView.fire( 'cancel' );
+
+				expect( balloon.visibleView ).to.be.null;
+			} );
+
+			it( 'should hide and reveal the #actionsView after Esc key press if link command has a value', () => {
 				const keyEvtData = {
 					keyCode: keyCodes.esc,
 					preventDefault: sinon.spy(),
@@ -862,11 +873,29 @@ describe( 'LinkUI', () => {
 
 				linkUIFeature._showUI();
 
+				const command = editor.commands.get( 'link' );
+				command.value = 'http://foo.com';
+
 				formView.keystrokes.press( keyEvtData );
+
 				expect( balloon.visibleView ).to.equal( actionsView );
 				expect( focusEditableSpy.calledOnce ).to.be.true;
 			} );
 
+			it( 'should hide the balloon after Esc key press if link command does not have a value', () => {
+				const keyEvtData = {
+					keyCode: keyCodes.esc,
+					preventDefault: sinon.spy(),
+					stopPropagation: sinon.spy()
+				};
+
+				linkUIFeature._showUI();
+
+				formView.keystrokes.press( keyEvtData );
+
+				expect( balloon.visibleView ).to.be.null;
+			} );
+
 			// https://github.com/ckeditor/ckeditor5/issues/1501
 			it( 'should blur url input element before hiding the view', () => {
 				linkUIFeature._showUI();