Skip to content

Commit

Permalink
Merge pull request #7842 from ckeditor/i/7705
Browse files Browse the repository at this point in the history
Fix (link): Fixed a case where the link balloon would point invalid place after browser scroll or resize. Closes #7705.
  • Loading branch information
niegowski authored Aug 17, 2020
2 parents ccfaf5e + e980570 commit 5158209
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/ckeditor5-link/src/linkui.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ export default class LinkUI extends Plugin {
_showUI( forceVisible = false ) {
// When there's no link under the selection, go straight to the editing UI.
if ( !this._getSelectedLinkElement() ) {
// Show visual selection on a text without a link when the contextual balloon is displayed.
// See https://github.com/ckeditor/ckeditor5/issues/4721.
this._showFakeVisualSelection();

this._addActionsView();

// Be sure panel with link is visible.
Expand All @@ -402,9 +406,6 @@ export default class LinkUI extends Plugin {
}

this._addFormView();
// Show visual selection on a text without a link when the contextual balloon is displayed.
// See https://github.com/ckeditor/ckeditor5/issues/4721.
this._showFakeVisualSelection();
}
// If there's a link under the selection...
else {
Expand Down Expand Up @@ -586,14 +587,20 @@ export default class LinkUI extends Plugin {
*/
_getBalloonPositionData() {
const view = this.editor.editing.view;
const model = this.editor.model;
const viewDocument = view.document;
const targetLink = this._getSelectedLinkElement();
const range = model.markers.has( VISUAL_SELECTION_MARKER_NAME ) ?
// There are cases when we highlight selection using a marker (#7705, #4721).
this.editor.editing.mapper.toViewRange( model.markers.get( VISUAL_SELECTION_MARKER_NAME ).getRange() ) :
// If no markers are available refer to a regular selection.
viewDocument.selection.getFirstRange();

const target = targetLink ?
// When selection is inside link element, then attach panel to this element.
view.domConverter.mapViewToDom( targetLink ) :
// Otherwise attach panel to the selection.
view.domConverter.viewRangeToDom( viewDocument.selection.getFirstRange() );
view.domConverter.viewRangeToDom( range );

return { target };
}
Expand Down
17 changes: 17 additions & 0 deletions packages/ckeditor5-link/tests/linkui.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ describe( 'LinkUI', () => {
} );
} );

it( 'should pass a proper position target to the balloon toolbar', () => {
setModelData( editor.model, '<paragraph>f[o]o</paragraph>' );

linkUIFeature._showUI();

const markerModelRange = editor.model.markers.get( 'link-ui' ).getRange();
const markerViewRange = editor.editing.mapper.toViewRange( markerModelRange );
const domRange = editor.editing.view.domConverter.viewRangeToDom( markerViewRange );

expect( balloonAddSpy.calledWithExactly( {
view: formView,
position: {
target: domRange
}
} ), 'spy arguments' ).to.be.true;
} );

it( 'should add #actionsView to the balloon and attach the balloon to the link element when collapsed selection is inside ' +
'that link',
() => {
Expand Down

0 comments on commit 5158209

Please sign in to comment.