From a7cbb0cbdc974e6f230afee170ec00407f803eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Wr=C3=B3bel?= Date: Tue, 7 May 2019 21:26:36 +0200 Subject: [PATCH 1/3] Aligned code to rotatable contextual balloon. --- src/widgettoolbarrepository.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/widgettoolbarrepository.js b/src/widgettoolbarrepository.js index 690bcaf3..aace369f 100644 --- a/src/widgettoolbarrepository.js +++ b/src/widgettoolbarrepository.js @@ -151,8 +151,14 @@ export default class WidgetToolbarRepository extends Plugin { for ( const definition of this._toolbarDefinitions.values() ) { const relatedElement = definition.getRelatedElement( this.editor.editing.view.document.selection ); - if ( !this.editor.ui.focusTracker.isFocused || !relatedElement ) { - this._hideToolbar( definition ); + if ( !this.editor.ui.focusTracker.isFocused ) { + if ( this._isToolbarVisible( definition ) ) { + this._hideToolbar( definition ); + } + } else if ( !relatedElement ) { + if ( this._isToolbarInBalloon( definition ) ) { + this._hideToolbar( definition ); + } } else { const relatedElementDepth = relatedElement.getAncestors().length; @@ -180,10 +186,6 @@ export default class WidgetToolbarRepository extends Plugin { * @param {module:widget/widgettoolbarrepository~WidgetRepositoryToolbarDefinition} toolbarDefinition */ _hideToolbar( toolbarDefinition ) { - if ( !this._isToolbarVisible( toolbarDefinition ) ) { - return; - } - this._balloon.remove( toolbarDefinition.view ); } @@ -201,7 +203,7 @@ export default class WidgetToolbarRepository extends Plugin { _showToolbar( toolbarDefinition, relatedElement ) { if ( this._isToolbarVisible( toolbarDefinition ) ) { repositionContextualBalloon( this.editor, relatedElement ); - } else if ( !this._balloon.hasView( toolbarDefinition.view ) ) { + } else if ( !this._isToolbarInBalloon( toolbarDefinition.view ) ) { this._balloon.add( { view: toolbarDefinition.view, position: getBalloonPositionData( this.editor, relatedElement ), @@ -213,9 +215,19 @@ export default class WidgetToolbarRepository extends Plugin { /** * @private * @param {Object} toolbar + * @returns {Boolean} */ _isToolbarVisible( toolbar ) { - return this._balloon.visibleView == toolbar.view; + return this._balloon.visibleView === toolbar.view; + } + + /** + * @private + * @param {Object} toolbar + * @returns {Boolean} + */ + _isToolbarInBalloon( toolbar ) { + return this._balloon.hasView( toolbar.view ); } } From d235741764f8bdfab508341319da7c275d2110d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Wr=C3=B3bel?= Date: Wed, 8 May 2019 18:12:36 +0200 Subject: [PATCH 2/3] Changed invalid parameter. --- src/widgettoolbarrepository.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/widgettoolbarrepository.js b/src/widgettoolbarrepository.js index aace369f..53ec783d 100644 --- a/src/widgettoolbarrepository.js +++ b/src/widgettoolbarrepository.js @@ -203,7 +203,7 @@ export default class WidgetToolbarRepository extends Plugin { _showToolbar( toolbarDefinition, relatedElement ) { if ( this._isToolbarVisible( toolbarDefinition ) ) { repositionContextualBalloon( this.editor, relatedElement ); - } else if ( !this._isToolbarInBalloon( toolbarDefinition.view ) ) { + } else if ( !this._isToolbarInBalloon( toolbarDefinition ) ) { this._balloon.add( { view: toolbarDefinition.view, position: getBalloonPositionData( this.editor, relatedElement ), From 3d597b7617c687e687490c98885d3752c7dbd61d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Wr=C3=B3bel?= Date: Tue, 14 May 2019 08:45:43 +0200 Subject: [PATCH 3/3] Added missing tests. --- tests/widgettoolbarrepository.js | 65 ++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tests/widgettoolbarrepository.js b/tests/widgettoolbarrepository.js index 5036f1ec..5784b2b6 100644 --- a/tests/widgettoolbarrepository.js +++ b/tests/widgettoolbarrepository.js @@ -136,6 +136,71 @@ describe( 'WidgetToolbarRepository', () => { expect( balloon.visibleView ).to.equal( null ); } ); + it( 'toolbar should be removed from not visible balloon stack when the `getRelatedElement` callback returns null', () => { + balloon.add( { + view: new View(), + stackId: 'secondary', + position: { + target: {} + } + } ); + + widgetToolbarRepository.register( 'fake', { + items: editor.config.get( 'fake.toolbar' ), + getRelatedElement: getSelectedFakeWidget + } ); + + setData( model, 'foo[]' ); + + const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view; + + expect( balloon.hasView( fakeWidgetToolbarView ) ); + expect( balloon.visibleView ).to.not.equal( fakeWidgetToolbarView ); + + model.change( writer => { + // Select the foo. + writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' ); + } ); + + expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( false ); + } ); + + it( 'toolbar should be hidden when the editor ui lost focus', () => { + widgetToolbarRepository.register( 'fake', { + items: editor.config.get( 'fake.toolbar' ), + getRelatedElement: getSelectedFakeWidget + } ); + + setData( model, 'foo[]' ); + + editor.ui.focusTracker.isFocused = false; + + expect( balloon.visibleView ).to.equal( null ); + } ); + + it( 'toolbar should do nothing with toolbar when the editor ui lost focus but toolbar is not a visible view', () => { + balloon.add( { + view: new View(), + stackId: 'secondary', + position: { + target: {} + } + } ); + + widgetToolbarRepository.register( 'fake', { + items: editor.config.get( 'fake.toolbar' ), + getRelatedElement: getSelectedFakeWidget + } ); + + setData( model, 'foo[]' ); + + const fakeWidgetToolbarView = widgetToolbarRepository._toolbarDefinitions.get( 'fake' ).view; + + editor.ui.focusTracker.isFocused = false; + + expect( balloon.hasView( fakeWidgetToolbarView ) ).to.equal( true ); + } ); + it( 'toolbar should update its position when other widget is selected', () => { widgetToolbarRepository.register( 'fake', { items: editor.config.get( 'fake.toolbar' ),