@ckeditor/ckeditor5-ui 35.2.0 → 35.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/package.json +31 -23
  2. package/src/bindings/addkeyboardhandlingforgrid.js +45 -57
  3. package/src/bindings/clickoutsidehandler.js +15 -21
  4. package/src/bindings/injectcsstransitiondisabler.js +16 -20
  5. package/src/bindings/preventdefault.js +6 -8
  6. package/src/bindings/submithandler.js +5 -7
  7. package/src/button/button.js +5 -0
  8. package/src/button/buttonview.js +220 -259
  9. package/src/button/switchbuttonview.js +56 -71
  10. package/src/colorgrid/colorgridview.js +135 -197
  11. package/src/colorgrid/colortileview.js +37 -47
  12. package/src/colorgrid/utils.js +57 -66
  13. package/src/componentfactory.js +79 -93
  14. package/src/dropdown/button/dropdownbutton.js +5 -0
  15. package/src/dropdown/button/dropdownbuttonview.js +44 -57
  16. package/src/dropdown/button/splitbuttonview.js +159 -207
  17. package/src/dropdown/dropdownpanelfocusable.js +5 -0
  18. package/src/dropdown/dropdownpanelview.js +101 -112
  19. package/src/dropdown/dropdownview.js +396 -438
  20. package/src/dropdown/utils.js +164 -213
  21. package/src/editableui/editableuiview.js +125 -141
  22. package/src/editableui/inline/inlineeditableuiview.js +44 -54
  23. package/src/editorui/bodycollection.js +61 -75
  24. package/src/editorui/boxed/boxededitoruiview.js +91 -104
  25. package/src/editorui/editoruiview.js +30 -39
  26. package/src/focuscycler.js +214 -245
  27. package/src/formheader/formheaderview.js +58 -70
  28. package/src/icon/iconview.js +145 -111
  29. package/src/iframe/iframeview.js +37 -49
  30. package/src/index.js +0 -17
  31. package/src/input/inputview.js +170 -198
  32. package/src/inputnumber/inputnumberview.js +48 -56
  33. package/src/inputtext/inputtextview.js +14 -18
  34. package/src/label/labelview.js +44 -53
  35. package/src/labeledfield/labeledfieldview.js +212 -235
  36. package/src/labeledfield/utils.js +39 -57
  37. package/src/labeledinput/labeledinputview.js +190 -221
  38. package/src/list/listitemview.js +40 -50
  39. package/src/list/listseparatorview.js +15 -19
  40. package/src/list/listview.js +94 -115
  41. package/src/model.js +19 -25
  42. package/src/notification/notification.js +151 -202
  43. package/src/panel/balloon/balloonpanelview.js +535 -628
  44. package/src/panel/balloon/contextualballoon.js +611 -732
  45. package/src/panel/sticky/stickypanelview.js +238 -270
  46. package/src/template.js +1049 -1479
  47. package/src/toolbar/balloon/balloontoolbar.js +337 -424
  48. package/src/toolbar/block/blockbuttonview.js +32 -42
  49. package/src/toolbar/block/blocktoolbar.js +375 -477
  50. package/src/toolbar/normalizetoolbarconfig.js +17 -21
  51. package/src/toolbar/toolbarlinebreakview.js +15 -19
  52. package/src/toolbar/toolbarseparatorview.js +15 -19
  53. package/src/toolbar/toolbarview.js +866 -1053
  54. package/src/tooltipmanager.js +324 -353
  55. package/src/view.js +389 -430
  56. package/src/viewcollection.js +147 -178
  57. package/src/button/button.jsdoc +0 -165
  58. package/src/dropdown/button/dropdownbutton.jsdoc +0 -22
  59. package/src/dropdown/dropdownpanelfocusable.jsdoc +0 -27
@@ -2,22 +2,16 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module ui/tooltipmanager
8
7
  */
9
-
10
8
  import View from './view';
11
9
  import BalloonPanelView, { generatePositions } from './panel/balloon/balloonpanelview';
12
-
13
- import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
14
- import { global, isVisible, mix, first } from '@ckeditor/ckeditor5-utils';
10
+ import { Emitter as DomEmitter } from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';
11
+ import { global, isVisible, first, ResizeObserver } from '@ckeditor/ckeditor5-utils';
15
12
  import { isElement, debounce } from 'lodash-es';
16
-
17
13
  import '../theme/components/tooltip/tooltip.css';
18
-
19
14
  const BALLOON_CLASS = 'ck-tooltip';
20
-
21
15
  /**
22
16
  * A tooltip manager class for the UI of the editor.
23
17
  *
@@ -65,324 +59,306 @@ const BALLOON_CLASS = 'ck-tooltip';
65
59
  *
66
60
  * @mixes module:utils/domemittermixin~DomEmitterMixin
67
61
  */
68
- export default class TooltipManager {
69
- /**
70
- * Creates an instance of the tooltip manager.
71
- *
72
- * @param {module:core/editor/editor~Editor} editor
73
- */
74
- constructor( editor ) {
75
- TooltipManager._editors.add( editor );
76
-
77
- // TooltipManager must be a singleton. Multiple instances would mean multiple tooltips attached
78
- // to the same DOM element with data-cke-tooltip-* attributes.
79
- if ( TooltipManager._instance ) {
80
- return TooltipManager._instance;
81
- }
82
-
83
- TooltipManager._instance = this;
84
-
85
- /**
86
- * The view rendering text of the tooltip.
87
- *
88
- * @readonly
89
- * @member {module:ui/view~View} #tooltipTextView
90
- */
91
- this.tooltipTextView = new View( editor.locale );
92
- this.tooltipTextView.set( 'text', '' );
93
- this.tooltipTextView.setTemplate( {
94
- tag: 'span',
95
- attributes: {
96
- class: [
97
- 'ck',
98
- 'ck-tooltip__text'
99
- ]
100
- },
101
- children: [
102
- {
103
- text: this.tooltipTextView.bindTemplate.to( 'text' )
104
- }
105
- ]
106
- } );
107
-
108
- /**
109
- * The instance of the balloon panel that renders and positions the tooltip.
110
- *
111
- * @readonly
112
- * @member {module:ui/panel/balloon/balloonpanelview~BalloonPanelView} #balloonPanelView
113
- */
114
- this.balloonPanelView = new BalloonPanelView( editor.locale );
115
- this.balloonPanelView.class = BALLOON_CLASS;
116
- this.balloonPanelView.content.add( this.tooltipTextView );
117
-
118
- /**
119
- * Stores the reference to the DOM element the tooltip is attached to. `null` when there's no tooltip
120
- * in the UI.
121
- *
122
- * @private
123
- * @readonly
124
- * @member {HTMLElement|null} #_currentElementWithTooltip
125
- */
126
- this._currentElementWithTooltip = null;
127
-
128
- /**
129
- * Stores the current tooltip position. `null` when there's no tooltip in the UI.
130
- *
131
- * @private
132
- * @readonly
133
- * @member {String|null} #_currentTooltipPosition
134
- */
135
- this._currentTooltipPosition = null;
136
-
137
- /**
138
- * A debounced version of {@link #_pinTooltip}. Tooltips show with a delay to avoid flashing and
139
- * to improve the UX.
140
- *
141
- * @private
142
- * @readonly
143
- * @member {Function} #_pinTooltipDebounced
144
- */
145
- this._pinTooltipDebounced = debounce( this._pinTooltip, 600 );
146
-
147
- this.listenTo( global.document, 'mouseenter', this._onEnterOrFocus.bind( this ), { useCapture: true } );
148
- this.listenTo( global.document, 'mouseleave', this._onLeaveOrBlur.bind( this ), { useCapture: true } );
149
-
150
- this.listenTo( global.document, 'focus', this._onEnterOrFocus.bind( this ), { useCapture: true } );
151
- this.listenTo( global.document, 'blur', this._onLeaveOrBlur.bind( this ), { useCapture: true } );
152
-
153
- this.listenTo( global.document, 'scroll', this._onScroll.bind( this ), { useCapture: true } );
154
-
155
- // Because this class is a singleton, its only instance is shared across all editors and connects them through the reference.
156
- // This causes issues with the ContextWatchdog. When an error is thrown in one editor, the watchdog traverses the references
157
- // and (because of shared tooltip manager) figures that the error affects all editors and restarts them all.
158
- // This flag, excludes tooltip manager instance from the traversal and brings ContextWatchdog back to normal.
159
- // More in https://github.com/ckeditor/ckeditor5/issues/12292.
160
- this._watchdogExcluded = true;
161
- }
162
-
163
- /**
164
- * Destroys the tooltip manager.
165
- *
166
- * **Note**: The manager singleton cannot be destroyed until all editors that use it are destroyed.
167
- *
168
- * @param {module:core/editor/editor~Editor} editor The editor the manager was created for.
169
- */
170
- destroy( editor ) {
171
- TooltipManager._editors.delete( editor );
172
- this.stopListening( editor.ui );
173
-
174
- if ( !TooltipManager._editors.size ) {
175
- this._unpinTooltip();
176
- this.balloonPanelView.destroy();
177
- this.stopListening();
178
-
179
- TooltipManager._instance = null;
180
- }
181
- }
182
-
183
- /**
184
- * Handles displaying tooltips on `mouseenter` and `focus` in DOM.
185
- *
186
- * @private
187
- * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
188
- * @param {Event} domEvent The DOM event.
189
- */
190
- _onEnterOrFocus( evt, { target } ) {
191
- const elementWithTooltipAttribute = getDescendantWithTooltip( target );
192
-
193
- // Abort when there's no descendant needing tooltip.
194
- if ( !elementWithTooltipAttribute ) {
195
- return;
196
- }
197
-
198
- // Abort to avoid flashing when, for instance:
199
- // * a tooltip is displayed for a focused element, then the same element gets mouseentered,
200
- // * a tooltip is displayed for an element via mouseenter, then the focus moves to the same element.
201
- if ( elementWithTooltipAttribute === this._currentElementWithTooltip ) {
202
- return;
203
- }
204
-
205
- this._unpinTooltip();
206
-
207
- this._pinTooltipDebounced( elementWithTooltipAttribute, getTooltipData( elementWithTooltipAttribute ) );
208
- }
209
-
210
- /**
211
- * Handles hiding tooltips on `mouseleave` and `blur` in DOM.
212
- *
213
- * @private
214
- * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
215
- * @param {Event} domEvent The DOM event.
216
- */
217
- _onLeaveOrBlur( evt, { target, relatedTarget } ) {
218
- if ( evt.name === 'mouseleave' ) {
219
- // Don't act when the event does not concern a DOM element (e.g. a mouseleave out of an entire document),
220
- if ( !isElement( target ) ) {
221
- return;
222
- }
223
-
224
- // If a tooltip is currently visible, don't act for a targets other than the one it is attached to.
225
- // For instance, a random mouseleave far away in the page should not unpin the tooltip that was pinned because
226
- // of a previous focus. Only leaving the same element should hide the tooltip.
227
- if ( this._currentElementWithTooltip && target !== this._currentElementWithTooltip ) {
228
- return;
229
- }
230
-
231
- const descendantWithTooltip = getDescendantWithTooltip( target );
232
- const relatedDescendantWithTooltip = getDescendantWithTooltip( relatedTarget );
233
-
234
- // Unpin when the mouse was leaving element with a tooltip to a place which does not have or has a different tooltip.
235
- // Note that this should happen whether the tooltip is already visible or not, for instance, it could be invisible but queued
236
- // (debounced): it should get canceled.
237
- if ( descendantWithTooltip && descendantWithTooltip !== relatedDescendantWithTooltip ) {
238
- this._unpinTooltip();
239
- }
240
- }
241
- else {
242
- // If a tooltip is currently visible, don't act for a targets other than the one it is attached to.
243
- // For instance, a random blur in the web page should not unpin the tooltip that was pinned because of a previous mouseenter.
244
- if ( this._currentElementWithTooltip && target !== this._currentElementWithTooltip ) {
245
- return;
246
- }
247
-
248
- // Note that unpinning should happen whether the tooltip is already visible or not, for instance, it could be invisible but
249
- // queued (debounced): it should get canceled (e.g. quick focus then quick blur using the keyboard).
250
- this._unpinTooltip();
251
- }
252
- }
253
-
254
- /**
255
- * Handles hiding tooltips on `scroll` in DOM.
256
- *
257
- * @private
258
- * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
259
- * @param {Event} domEvent The DOM event.
260
- */
261
- _onScroll( evt, { target } ) {
262
- // No tooltip, no reason to react on scroll.
263
- if ( !this._currentElementWithTooltip ) {
264
- return;
265
- }
266
-
267
- // When scrolling a container that has both the balloon and the current element (common ancestor), the balloon can remain
268
- // visible (e.g. scrolling ≤body>). Otherwise, to avoid glitches (clipping, lagging) better just hide the tooltip.
269
- // Also, don't do anything when scrolling an unrelated DOM element that has nothing to do with the current element and the balloon.
270
- if ( target.contains( this.balloonPanelView.element ) && target.contains( this._currentElementWithTooltip ) ) {
271
- return;
272
- }
273
-
274
- this._unpinTooltip();
275
- }
276
-
277
- /**
278
- * Pins the tooltip to a specific DOM element.
279
- *
280
- * @private
281
- * @param {Element} targetDomElement
282
- * @param {Object} options
283
- * @param {String} options.text Text of the tooltip to display.
284
- * @param {String} options.position The position of the tooltip.
285
- * @param {String} options.cssClass Additional CSS class of the balloon with the tooltip.
286
- */
287
- _pinTooltip( targetDomElement, { text, position, cssClass } ) {
288
- // Use the body collection of the first editor.
289
- const bodyViewCollection = first( TooltipManager._editors.values() ).ui.view.body;
290
-
291
- if ( !bodyViewCollection.has( this.balloonPanelView ) ) {
292
- bodyViewCollection.add( this.balloonPanelView );
293
- }
294
-
295
- this.tooltipTextView.text = text;
296
-
297
- this.balloonPanelView.pin( {
298
- target: targetDomElement,
299
- positions: TooltipManager.getPositioningFunctions( position )
300
- } );
301
-
302
- this.balloonPanelView.class = [ BALLOON_CLASS, cssClass ]
303
- .filter( className => className )
304
- .join( ' ' );
305
-
306
- // Start responding to changes in editor UI or content layout. For instance, when collaborators change content
307
- // and a contextual toolbar attached to a content starts to move (and so should move the tooltip).
308
- // Note: Using low priority to let other listeners that position contextual toolbars etc. to react first.
309
- for ( const editor of TooltipManager._editors ) {
310
- this.listenTo( editor.ui, 'update', this._updateTooltipPosition.bind( this ), { priority: 'low' } );
311
- }
312
-
313
- this._currentElementWithTooltip = targetDomElement;
314
- this._currentTooltipPosition = position;
315
- }
316
-
317
- /**
318
- * Unpins the tooltip and cancels all queued pinning.
319
- *
320
- * @private
321
- */
322
- _unpinTooltip() {
323
- this._pinTooltipDebounced.cancel();
324
-
325
- this.balloonPanelView.unpin();
326
-
327
- for ( const editor of TooltipManager._editors ) {
328
- this.stopListening( editor.ui, 'update' );
329
- }
330
-
331
- this._currentElementWithTooltip = null;
332
- this._currentTooltipPosition = null;
333
- }
334
-
335
- /**
336
- * Updates the position of the tooltip so it stays in sync with the element it is pinned to.
337
- *
338
- * Hides the tooltip when the element is no longer visible in DOM.
339
- *
340
- * @private
341
- */
342
- _updateTooltipPosition() {
343
- // This could happen if the tooltip was attached somewhere in a contextual content toolbar and the toolbar
344
- // disappeared (e.g. removed an image).
345
- if ( !isVisible( this._currentElementWithTooltip ) ) {
346
- this._unpinTooltip();
347
-
348
- return;
349
- }
350
-
351
- this.balloonPanelView.pin( {
352
- target: this._currentElementWithTooltip,
353
- positions: TooltipManager.getPositioningFunctions( this._currentTooltipPosition )
354
- } );
355
- }
356
-
357
- /**
358
- * Returns {@link #balloonPanelView} {@link module:utils/dom/position~PositioningFunction positioning functions} for a given position
359
- * name.
360
- *
361
- * @static
362
- * @param {String} position Name of the position (`s`, `se`, `sw`, `n`, `e`, or `w`).
363
- * @returns {Array.<module:utils/dom/position~PositioningFunction>} Positioning functions to be used by the {@link #balloonPanelView}.
364
- */
365
- static getPositioningFunctions( position ) {
366
- const defaultPositions = TooltipManager.defaultBalloonPositions;
367
-
368
- return {
369
- // South is most popular. We can use positioning heuristics to avoid clipping by the viewport with the sane fallback.
370
- s: [
371
- defaultPositions.southArrowNorth,
372
- defaultPositions.southArrowNorthEast,
373
- defaultPositions.southArrowNorthWest
374
- ],
375
- n: [ defaultPositions.northArrowSouth ],
376
- e: [ defaultPositions.eastArrowWest ],
377
- w: [ defaultPositions.westArrowEast ],
378
- sw: [ defaultPositions.southArrowNorthEast ],
379
- se: [ defaultPositions.southArrowNorthWest ]
380
- }[ position ];
381
- }
62
+ export default class TooltipManager extends DomEmitter {
63
+ /**
64
+ * Creates an instance of the tooltip manager.
65
+ *
66
+ * @param {module:core/editor/editor~Editor} editor
67
+ */
68
+ constructor(editor) {
69
+ super();
70
+ TooltipManager._editors.add(editor);
71
+ // TooltipManager must be a singleton. Multiple instances would mean multiple tooltips attached
72
+ // to the same DOM element with data-cke-tooltip-* attributes.
73
+ if (TooltipManager._instance) {
74
+ return TooltipManager._instance;
75
+ }
76
+ TooltipManager._instance = this;
77
+ /**
78
+ * The view rendering text of the tooltip.
79
+ *
80
+ * @readonly
81
+ * @member {module:ui/view~View} #tooltipTextView
82
+ */
83
+ this.tooltipTextView = new View(editor.locale);
84
+ this.tooltipTextView.set('text', '');
85
+ this.tooltipTextView.setTemplate({
86
+ tag: 'span',
87
+ attributes: {
88
+ class: [
89
+ 'ck',
90
+ 'ck-tooltip__text'
91
+ ]
92
+ },
93
+ children: [
94
+ {
95
+ text: this.tooltipTextView.bindTemplate.to('text')
96
+ }
97
+ ]
98
+ });
99
+ /**
100
+ * The instance of the balloon panel that renders and positions the tooltip.
101
+ *
102
+ * @readonly
103
+ * @member {module:ui/panel/balloon/balloonpanelview~BalloonPanelView} #balloonPanelView
104
+ */
105
+ this.balloonPanelView = new BalloonPanelView(editor.locale);
106
+ this.balloonPanelView.class = BALLOON_CLASS;
107
+ this.balloonPanelView.content.add(this.tooltipTextView);
108
+ /**
109
+ * An instance of the resize observer that keeps track on target element visibility,
110
+ * when it hides the tooltip should also disappear.
111
+ *
112
+ * {@link module:core/editor/editorconfig~EditorConfig#balloonToolbar configuration}.
113
+ *
114
+ * @protected
115
+ * @member {module:utils/dom/resizeobserver~ResizeObserver|null}
116
+ *
117
+ */
118
+ this._resizeObserver = null;
119
+ /**
120
+ * Stores the reference to the DOM element the tooltip is attached to. `null` when there's no tooltip
121
+ * in the UI.
122
+ *
123
+ * @private
124
+ * @readonly
125
+ * @member {HTMLElement|null} #_currentElementWithTooltip
126
+ */
127
+ this._currentElementWithTooltip = null;
128
+ /**
129
+ * Stores the current tooltip position. `null` when there's no tooltip in the UI.
130
+ *
131
+ * @private
132
+ * @readonly
133
+ * @member {String|null} #_currentTooltipPosition
134
+ */
135
+ this._currentTooltipPosition = null;
136
+ /**
137
+ * A debounced version of {@link #_pinTooltip}. Tooltips show with a delay to avoid flashing and
138
+ * to improve the UX.
139
+ *
140
+ * @private
141
+ * @readonly
142
+ * @member {Function} #_pinTooltipDebounced
143
+ */
144
+ this._pinTooltipDebounced = debounce(this._pinTooltip, 600);
145
+ this.listenTo(global.document, 'mouseenter', this._onEnterOrFocus.bind(this), { useCapture: true });
146
+ this.listenTo(global.document, 'mouseleave', this._onLeaveOrBlur.bind(this), { useCapture: true });
147
+ this.listenTo(global.document, 'focus', this._onEnterOrFocus.bind(this), { useCapture: true });
148
+ this.listenTo(global.document, 'blur', this._onLeaveOrBlur.bind(this), { useCapture: true });
149
+ this.listenTo(global.document, 'scroll', this._onScroll.bind(this), { useCapture: true });
150
+ // Because this class is a singleton, its only instance is shared across all editors and connects them through the reference.
151
+ // This causes issues with the ContextWatchdog. When an error is thrown in one editor, the watchdog traverses the references
152
+ // and (because of shared tooltip manager) figures that the error affects all editors and restarts them all.
153
+ // This flag, excludes tooltip manager instance from the traversal and brings ContextWatchdog back to normal.
154
+ // More in https://github.com/ckeditor/ckeditor5/issues/12292.
155
+ this._watchdogExcluded = true;
156
+ }
157
+ /**
158
+ * Destroys the tooltip manager.
159
+ *
160
+ * **Note**: The manager singleton cannot be destroyed until all editors that use it are destroyed.
161
+ *
162
+ * @param {module:core/editor/editor~Editor} editor The editor the manager was created for.
163
+ */
164
+ destroy(editor) {
165
+ const editorBodyViewCollection = editor.ui.view && editor.ui.view.body;
166
+ TooltipManager._editors.delete(editor);
167
+ this.stopListening(editor.ui);
168
+ // Prevent the balloon panel from being destroyed in the EditorUI#destroy() cascade. It should be destroyed along
169
+ // with the last editor only (https://github.com/ckeditor/ckeditor5/issues/12602).
170
+ if (editorBodyViewCollection && editorBodyViewCollection.has(this.balloonPanelView)) {
171
+ editorBodyViewCollection.remove(this.balloonPanelView);
172
+ }
173
+ if (!TooltipManager._editors.size) {
174
+ this._unpinTooltip();
175
+ this.balloonPanelView.destroy();
176
+ this.stopListening();
177
+ TooltipManager._instance = null;
178
+ }
179
+ }
180
+ /**
181
+ * Returns {@link #balloonPanelView} {@link module:utils/dom/position~PositioningFunction positioning functions} for a given position
182
+ * name.
183
+ *
184
+ * @static
185
+ * @param {String} position Name of the position (`s`, `se`, `sw`, `n`, `e`, or `w`).
186
+ * @returns {Array.<module:utils/dom/position~PositioningFunction>} Positioning functions to be used by the {@link #balloonPanelView}.
187
+ */
188
+ static getPositioningFunctions(position) {
189
+ const defaultPositions = TooltipManager.defaultBalloonPositions;
190
+ return {
191
+ // South is most popular. We can use positioning heuristics to avoid clipping by the viewport with the sane fallback.
192
+ s: [
193
+ defaultPositions.southArrowNorth,
194
+ defaultPositions.southArrowNorthEast,
195
+ defaultPositions.southArrowNorthWest
196
+ ],
197
+ n: [defaultPositions.northArrowSouth],
198
+ e: [defaultPositions.eastArrowWest],
199
+ w: [defaultPositions.westArrowEast],
200
+ sw: [defaultPositions.southArrowNorthEast],
201
+ se: [defaultPositions.southArrowNorthWest]
202
+ }[position];
203
+ }
204
+ /**
205
+ * Handles displaying tooltips on `mouseenter` and `focus` in DOM.
206
+ *
207
+ * @private
208
+ * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
209
+ * @param {Event} domEvent The DOM event.
210
+ */
211
+ _onEnterOrFocus(evt, { target }) {
212
+ const elementWithTooltipAttribute = getDescendantWithTooltip(target);
213
+ // Abort when there's no descendant needing tooltip.
214
+ if (!elementWithTooltipAttribute) {
215
+ return;
216
+ }
217
+ // Abort to avoid flashing when, for instance:
218
+ // * a tooltip is displayed for a focused element, then the same element gets mouseentered,
219
+ // * a tooltip is displayed for an element via mouseenter, then the focus moves to the same element.
220
+ if (elementWithTooltipAttribute === this._currentElementWithTooltip) {
221
+ return;
222
+ }
223
+ this._unpinTooltip();
224
+ this._pinTooltipDebounced(elementWithTooltipAttribute, getTooltipData(elementWithTooltipAttribute));
225
+ }
226
+ /**
227
+ * Handles hiding tooltips on `mouseleave` and `blur` in DOM.
228
+ *
229
+ * @private
230
+ * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
231
+ * @param {Event} domEvent The DOM event.
232
+ */
233
+ _onLeaveOrBlur(evt, { target, relatedTarget }) {
234
+ if (evt.name === 'mouseleave') {
235
+ // Don't act when the event does not concern a DOM element (e.g. a mouseleave out of an entire document),
236
+ if (!isElement(target)) {
237
+ return;
238
+ }
239
+ // If a tooltip is currently visible, don't act for a targets other than the one it is attached to.
240
+ // For instance, a random mouseleave far away in the page should not unpin the tooltip that was pinned because
241
+ // of a previous focus. Only leaving the same element should hide the tooltip.
242
+ if (this._currentElementWithTooltip && target !== this._currentElementWithTooltip) {
243
+ return;
244
+ }
245
+ const descendantWithTooltip = getDescendantWithTooltip(target);
246
+ const relatedDescendantWithTooltip = getDescendantWithTooltip(relatedTarget);
247
+ // Unpin when the mouse was leaving element with a tooltip to a place which does not have or has a different tooltip.
248
+ // Note that this should happen whether the tooltip is already visible or not, for instance, it could be invisible but queued
249
+ // (debounced): it should get canceled.
250
+ if (descendantWithTooltip && descendantWithTooltip !== relatedDescendantWithTooltip) {
251
+ this._unpinTooltip();
252
+ }
253
+ }
254
+ else {
255
+ // If a tooltip is currently visible, don't act for a targets other than the one it is attached to.
256
+ // For instance, a random blur in the web page should not unpin the tooltip that was pinned because of a previous mouseenter.
257
+ if (this._currentElementWithTooltip && target !== this._currentElementWithTooltip) {
258
+ return;
259
+ }
260
+ // Note that unpinning should happen whether the tooltip is already visible or not, for instance, it could be invisible but
261
+ // queued (debounced): it should get canceled (e.g. quick focus then quick blur using the keyboard).
262
+ this._unpinTooltip();
263
+ }
264
+ }
265
+ /**
266
+ * Handles hiding tooltips on `scroll` in DOM.
267
+ *
268
+ * @private
269
+ * @param {module:utils/eventinfo~EventInfo} evt An object containing information about the fired event.
270
+ * @param {Event} domEvent The DOM event.
271
+ */
272
+ _onScroll(evt, { target }) {
273
+ // No tooltip, no reason to react on scroll.
274
+ if (!this._currentElementWithTooltip) {
275
+ return;
276
+ }
277
+ // When scrolling a container that has both the balloon and the current element (common ancestor), the balloon can remain
278
+ // visible (e.g. scrolling ≤body>). Otherwise, to avoid glitches (clipping, lagging) better just hide the tooltip.
279
+ // Also, don't do anything when scrolling an unrelated DOM element that has nothing to do with the current element and the balloon.
280
+ if (target.contains(this.balloonPanelView.element) && target.contains(this._currentElementWithTooltip)) {
281
+ return;
282
+ }
283
+ this._unpinTooltip();
284
+ }
285
+ /**
286
+ * Pins the tooltip to a specific DOM element.
287
+ *
288
+ * @private
289
+ * @param {Element} targetDomElement
290
+ * @param {Object} options
291
+ * @param {String} options.text Text of the tooltip to display.
292
+ * @param {String} options.position The position of the tooltip.
293
+ * @param {String} options.cssClass Additional CSS class of the balloon with the tooltip.
294
+ */
295
+ _pinTooltip(targetDomElement, { text, position, cssClass }) {
296
+ // Use the body collection of the first editor.
297
+ const bodyViewCollection = first(TooltipManager._editors.values()).ui.view.body;
298
+ if (!bodyViewCollection.has(this.balloonPanelView)) {
299
+ bodyViewCollection.add(this.balloonPanelView);
300
+ }
301
+ this.tooltipTextView.text = text;
302
+ this.balloonPanelView.pin({
303
+ target: targetDomElement,
304
+ positions: TooltipManager.getPositioningFunctions(position)
305
+ });
306
+ this._resizeObserver = new ResizeObserver(targetDomElement, () => {
307
+ // The ResizeObserver will call its callback when the target element hides and the tooltip
308
+ // should also disappear (https://github.com/ckeditor/ckeditor5/issues/12492).
309
+ if (!isVisible(targetDomElement)) {
310
+ this._unpinTooltip();
311
+ }
312
+ });
313
+ this.balloonPanelView.class = [BALLOON_CLASS, cssClass]
314
+ .filter(className => className)
315
+ .join(' ');
316
+ // Start responding to changes in editor UI or content layout. For instance, when collaborators change content
317
+ // and a contextual toolbar attached to a content starts to move (and so should move the tooltip).
318
+ // Note: Using low priority to let other listeners that position contextual toolbars etc. to react first.
319
+ for (const editor of TooltipManager._editors) {
320
+ this.listenTo(editor.ui, 'update', this._updateTooltipPosition.bind(this), { priority: 'low' });
321
+ }
322
+ this._currentElementWithTooltip = targetDomElement;
323
+ this._currentTooltipPosition = position;
324
+ }
325
+ /**
326
+ * Unpins the tooltip and cancels all queued pinning.
327
+ *
328
+ * @private
329
+ */
330
+ _unpinTooltip() {
331
+ this._pinTooltipDebounced.cancel();
332
+ this.balloonPanelView.unpin();
333
+ for (const editor of TooltipManager._editors) {
334
+ this.stopListening(editor.ui, 'update');
335
+ }
336
+ this._currentElementWithTooltip = null;
337
+ this._currentTooltipPosition = null;
338
+ if (this._resizeObserver) {
339
+ this._resizeObserver.destroy();
340
+ }
341
+ }
342
+ /**
343
+ * Updates the position of the tooltip so it stays in sync with the element it is pinned to.
344
+ *
345
+ * Hides the tooltip when the element is no longer visible in DOM.
346
+ *
347
+ * @private
348
+ */
349
+ _updateTooltipPosition() {
350
+ // This could happen if the tooltip was attached somewhere in a contextual content toolbar and the toolbar
351
+ // disappeared (e.g. removed an image).
352
+ if (!isVisible(this._currentElementWithTooltip)) {
353
+ this._unpinTooltip();
354
+ return;
355
+ }
356
+ this.balloonPanelView.pin({
357
+ target: this._currentElementWithTooltip,
358
+ positions: TooltipManager.getPositioningFunctions(this._currentTooltipPosition)
359
+ });
360
+ }
382
361
  }
383
-
384
- mix( TooltipManager, DomEmitterMixin );
385
-
386
362
  /**
387
363
  * A set of default {@link module:utils/dom/position~PositioningFunction positioning functions} used by the `TooltipManager`
388
364
  * to pin tooltips in different positions.
@@ -390,41 +366,36 @@ mix( TooltipManager, DomEmitterMixin );
390
366
  * @member {Object.<String,module:utils/dom/position~PositioningFunction>}
391
367
  * module:ui/tooltipmanager~TooltipManager.defaultBalloonPositions
392
368
  */
393
- TooltipManager.defaultBalloonPositions = generatePositions( {
394
- heightOffset: 5,
395
- sideOffset: 13
396
- } );
397
-
369
+ TooltipManager.defaultBalloonPositions = generatePositions({
370
+ heightOffset: 5,
371
+ sideOffset: 13
372
+ });
398
373
  /**
399
- * A reference to the `TooltipManager` instance. The class is a singleton and as such,
400
- * successive attempts at creating instances should return this instance.
374
+ * A set of editors the single tooltip manager instance must listen to.
375
+ * This is mostly to handle `EditorUI#update` listeners from individual editors.
401
376
  *
402
377
  * @private
403
- * @member {module:ui/tooltipmanager~TooltipManager} module:ui/tooltipmanager~TooltipManager._instance
378
+ * @member {Set.<module:core/editor/editor~Editor>} module:ui/tooltipmanager~TooltipManager._editors
404
379
  */
405
- TooltipManager._instance = null;
406
-
380
+ TooltipManager._editors = new Set();
407
381
  /**
408
- * An array of editors the single tooltip manager instance must listen to.
409
- * This is mostly to handle `EditorUI#update` listeners from individual editors.
382
+ * A reference to the `TooltipManager` instance. The class is a singleton and as such,
383
+ * successive attempts at creating instances should return this instance.
410
384
  *
411
385
  * @private
412
- * @member {Set.<module:core/editor/editor~Editor>} module:ui/tooltipmanager~TooltipManager._editors
386
+ * @member {module:ui/tooltipmanager~TooltipManager} module:ui/tooltipmanager~TooltipManager._instance
413
387
  */
414
- TooltipManager._editors = new Set();
415
-
416
- function getDescendantWithTooltip( element ) {
417
- if ( !isElement( element ) ) {
418
- return null;
419
- }
420
-
421
- return element.closest( '[data-cke-tooltip-text]:not([data-cke-tooltip-disabled])' );
388
+ TooltipManager._instance = null;
389
+ function getDescendantWithTooltip(element) {
390
+ if (!isElement(element)) {
391
+ return null;
392
+ }
393
+ return element.closest('[data-cke-tooltip-text]:not([data-cke-tooltip-disabled])');
422
394
  }
423
-
424
- function getTooltipData( element ) {
425
- return {
426
- text: element.dataset.ckeTooltipText,
427
- position: element.dataset.ckeTooltipPosition || 's',
428
- cssClass: element.dataset.ckeTooltipClass || ''
429
- };
395
+ function getTooltipData(element) {
396
+ return {
397
+ text: element.dataset.ckeTooltipText,
398
+ position: (element.dataset.ckeTooltipPosition || 's'),
399
+ cssClass: element.dataset.ckeTooltipClass || ''
400
+ };
430
401
  }