This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Added beforeShow event to the ContextualToolbar plugin #226
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7caf0b0
Stubbed balloon#remove method which was throwing errors.
oskarwrobel 9c88303
Improved test clean up.
oskarwrobel 22e3f75
Added beforeShow event.
oskarwrobel 096a297
Improved property name.
oskarwrobel 3f1ce0a
Improved docs.
oskarwrobel c11cd33
Improved docs.
oskarwrobel 5f94979
Changed property name.
oskarwrobel 21e84fc
Drop not necessary stop() methd and simplified the code.
oskarwrobel 45701ed
Improved docs.
oskarwrobel 6031385
Code style and docs fixes.
oleq 827edae
Removed not necessary value resetting.
oskarwrobel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ContextualBalloon from '../../panel/balloon/contextualballoon'; | |
import ToolbarView from '../toolbarview'; | ||
import BalloonPanelView from '../../panel/balloon/balloonpanelview.js'; | ||
import debounce from '@ckeditor/ckeditor5-utils/src/lib/lodash/debounce'; | ||
import spy from '@ckeditor/ckeditor5-utils/src/spy'; | ||
|
||
const defaultPositions = BalloonPanelView.defaultPositions; | ||
|
||
|
@@ -67,6 +68,15 @@ export default class ContextualToolbar extends Plugin { | |
*/ | ||
this._fireSelectionChangeDebounced = debounce( () => this.fire( '_selectionChangeDebounced' ), 200 ); | ||
|
||
/** | ||
* Resolve {@link #_showPanel} promise. When panel is prevented of being shown we need to resolve | ||
* this promise otherwise will be pending forever. | ||
* | ||
* @private | ||
* @member {Function} | ||
*/ | ||
this._showPanelPromiseResolver = spy(); | ||
|
||
// Attach lifecycle actions. | ||
this._handleSelectionChange(); | ||
this._handleFocusChange(); | ||
|
@@ -128,11 +138,25 @@ export default class ContextualToolbar extends Plugin { | |
this.listenTo( this, '_selectionChangeDebounced', () => this._showPanel() ); | ||
} | ||
|
||
/** | ||
* Prevents panel of being displayed. This should be used together with {@link #event:beforeShow}` event. | ||
* | ||
* @param {module:utils/eventinfo~EventInfo} evt Event object provided by the {@link #event:beforeShow} event. | ||
*/ | ||
stop( evt ) { | ||
evt.stop(); | ||
this._showPanelPromiseResolver(); | ||
this.stopListening( this, 'beforeShow' ); | ||
} | ||
|
||
/** | ||
* Adds panel view to the {@link: #_balloon} and attaches panel to the selection. | ||
* | ||
* Fires {@link #event:beforeShow} event just before displaying the panel. | ||
* | ||
* @protected | ||
* @return {Promise} A promise resolved when the {@link #toolbarView} {@link module:ui/view~View#init} is done. | ||
* @return {Promise} A promise resolved when the {@link #toolbarView} {@link module:ui/view~View#init} is done | ||
* or rejected when panel will be prevented of being displayed. | ||
*/ | ||
_showPanel() { | ||
const editingView = this.editor.editing.view; | ||
|
@@ -147,17 +171,35 @@ export default class ContextualToolbar extends Plugin { | |
return Promise.resolve(); | ||
} | ||
|
||
// Update panel position when selection changes while balloon will be opened (by a collaboration). | ||
this.listenTo( this.editor.editing.view, 'render', () => { | ||
this._balloon.updatePosition( this._getBalloonPositionData() ); | ||
} ); | ||
const showPromise = new Promise( ( resolve ) => { | ||
this._showPanelPromiseResolver = resolve; | ||
|
||
// Add panel to the common editor contextual balloon. | ||
return this._balloon.add( { | ||
view: this.toolbarView, | ||
position: this._getBalloonPositionData(), | ||
balloonClassName: 'ck-toolbar-container' | ||
} ); | ||
// If `beforeShow` event is not stopped by any external code then panel will be displayed. | ||
this.listenTo( this, 'beforeShow', ( evt ) => { | ||
// Update panel position when selection changes while balloon will be opened | ||
// (by an external document changes). | ||
this.listenTo( editingView, 'render', () => { | ||
this._balloon.updatePosition( this._getBalloonPositionData() ); | ||
} ); | ||
|
||
resolve( | ||
// Add panel to the common editor contextual balloon. | ||
this._balloon.add( { | ||
view: this.toolbarView, | ||
position: this._getBalloonPositionData(), | ||
balloonClassName: 'ck-toolbar-container' | ||
} ) | ||
); | ||
|
||
// Stop listening on `beforeShow` event. | ||
evt.off(); | ||
} ); | ||
}, { priority: 'lowest' } ); | ||
|
||
// Fire this event to inform interested plugins that `ContextualToolbar` is going to be shown. | ||
this.fire( 'beforeShow' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we pass the stopping function along this event to avoid the additional |
||
|
||
return showPromise; | ||
} | ||
|
||
/** | ||
|
@@ -210,6 +252,14 @@ export default class ContextualToolbar extends Plugin { | |
super.destroy(); | ||
} | ||
|
||
/** | ||
* This event is fired just before balloon shows. | ||
* It makes possible to listen to this event by an external code and prevent | ||
* ContextualToolbar of being displayed by calling {@link #stop} method. | ||
* | ||
* @event beforeShow | ||
*/ | ||
|
||
/** | ||
* This is internal plugin event which is fired 200 ms after model selection last change. | ||
* This is to makes easy test debounced action without need to use `setTimeout`. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't
once()
mean thatevt.off()
at the end is gone?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
once()
useson()
buton()
is not cleared onstopListening()
(https://github.com/ckeditor/ckeditor5-utils/issues/144). That's why I usedevt.off()
.