Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Fix: Link balloon should hide unlink button when creating a link. Clo…
Browse files Browse the repository at this point in the history
…ses #53.

t/53: Link balloon should hide unlink button when creating a link
  • Loading branch information
oskarwrobel committed Apr 21, 2017
2 parents c54e4a4 + acf80a4 commit 686e625
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
35 changes: 23 additions & 12 deletions src/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export default class Link extends Plugin {
// Keep panel open until selection will be inside the same link element.
this.listenTo( viewDocument, 'click', () => {
const viewSelection = viewDocument.selection;
const parentLink = getPositionParentLink( viewSelection.getFirstPosition() );
const parentLink = this._getSelectedLinkElement();

// When collapsed selection is inside link element (link element is clicked).
if ( viewSelection.isCollapsed && parentLink ) {
Expand All @@ -199,7 +199,7 @@ export default class Link extends Plugin {
// Start listen to view document changes and close the panel when selection will be moved
// out of the actual link element.
this.listenTo( viewDocument, 'render', () => {
const currentParentLink = getPositionParentLink( viewSelection.getFirstPosition() );
const currentParentLink = this._getSelectedLinkElement();

if ( !viewSelection.isCollapsed || parentLink !== currentParentLink ) {
this._hidePanel();
Expand Down Expand Up @@ -244,6 +244,9 @@ export default class Link extends Plugin {
* @return {Promise} A promise resolved when the {@link #formView} {@link module:ui/view~View#init} is done.
*/
_showPanel( focusInput ) {
// https://github.com/ckeditor/ckeditor5-link/issues/53
this.formView.unlinkButtonView.isVisible = !!this._getSelectedLinkElement();

if ( this._balloon.hasView( this.formView ) ) {
// Check if formView should be focused and focus it if is visible.
if ( focusInput && this._balloon.visibleView === this.formView ) {
Expand Down Expand Up @@ -296,7 +299,7 @@ export default class Link extends Plugin {
*/
_getBalloonPositionData() {
const viewDocument = this.editor.editing.view;
const targetLink = getPositionParentLink( viewDocument.selection.getFirstPosition() );
const targetLink = this._getSelectedLinkElement();

const target = targetLink ?
// When selection is inside link element, then attach panel to this element.
Expand All @@ -310,14 +313,22 @@ export default class Link extends Plugin {
limiter: viewDocument.domConverter.getCorrespondingDomElement( viewDocument.selection.editableElement )
};
}
}

// Try to find if one of the position parent ancestors is a LinkElement,
// if yes return this element.
//
// @private
// @param {engine.view.Position} position
// @returns {module:link/linkelement~LinkElement|null}
function getPositionParentLink( position ) {
return position.parent.getAncestors().find( ( ancestor ) => ancestor instanceof LinkElement );
/**
* Returns the {@link module:link/linkelement~LinkElement} at the first
* {@link module:engine/model/position~Position} of
* {@link module:engine/view/document~Document editing view's} selection or `null`
* if there's none.
*
* @private
* @returns {module:link/linkelement~LinkElement|null}
*/
_getSelectedLinkElement() {
return this.editor.editing.view
.selection
.getFirstPosition()
.parent
.getAncestors()
.find( ancestor => ancestor instanceof LinkElement );
}
}
17 changes: 17 additions & 0 deletions tests/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,23 @@ describe( 'Link', () => {
sinon.assert.calledOnce( spy );
} );
} );

// https://github.com/ckeditor/ckeditor5-link/issues/53
it( 'should set formView.unlinkButtonView#isVisible depending on the selection in a link or not', () => {
setModelData( editor.document, '<paragraph>f[]oo</paragraph>' );

return linkFeature._showPanel()
.then( () => {
expect( formView.unlinkButtonView.isVisible ).to.be.false;

setModelData( editor.document, '<paragraph><$text linkHref="url">f[]oo</$text></paragraph>' );

return linkFeature._showPanel()
.then( () => {
expect( formView.unlinkButtonView.isVisible ).to.be.true;
} );
} );
} );
} );

describe( '_hidePanel()', () => {
Expand Down

0 comments on commit 686e625

Please sign in to comment.