From 466be571606685b179207eb3e2c41478b4d3f3e5 Mon Sep 17 00:00:00 2001 From: Szymon Cofalik Date: Mon, 20 May 2019 19:02:19 +0200 Subject: [PATCH] Fix: Link balloon will not be shown if no link was added after command execution. --- src/linkui.js | 22 +++++++++++++++++++--- tests/linkui.js | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/linkui.js b/src/linkui.js index 9c880ed..f40b5b9 100644 --- a/src/linkui.js +++ b/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(); } ); @@ -306,6 +306,22 @@ export default class LinkUI extends Plugin { this.formView.urlInputView.inputView.element.value = linkCommand.value || ''; } + /** + * 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}. * diff --git a/tests/linkui.js b/tests/linkui.js index 33a37c6..c97c6ac 100644 --- a/tests/linkui.js +++ b/tests/linkui.js @@ -812,15 +812,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(), @@ -829,11 +840,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();