@ckeditor/ckeditor5-ui 35.2.1 → 35.3.1

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,15 +2,13 @@
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/toolbar/balloon/balloontoolbar
8
7
  */
9
-
10
8
  import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
11
9
  import ContextualBalloon from '../../panel/balloon/contextualballoon';
12
10
  import ToolbarView from '../toolbarview';
13
- import BalloonPanelView, { generatePositions } from '../../panel/balloon/balloonpanelview.js';
11
+ import BalloonPanelView, { generatePositions } from '../../panel/balloon/balloonpanelview';
14
12
  import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
15
13
  import Rect from '@ckeditor/ckeditor5-utils/src/dom/rect';
16
14
  import normalizeToolbarConfig from '../normalizetoolbarconfig';
@@ -18,9 +16,7 @@ import { debounce } from 'lodash-es';
18
16
  import ResizeObserver from '@ckeditor/ckeditor5-utils/src/dom/resizeobserver';
19
17
  import toUnit from '@ckeditor/ckeditor5-utils/src/dom/tounit';
20
18
  import { env, global } from '@ckeditor/ckeditor5-utils';
21
-
22
- const toPx = toUnit( 'px' );
23
-
19
+ const toPx = toUnit('px');
24
20
  /**
25
21
  * The contextual toolbar.
26
22
  *
@@ -29,382 +25,333 @@ const toPx = toUnit( 'px' );
29
25
  * @extends module:core/plugin~Plugin
30
26
  */
31
27
  export default class BalloonToolbar extends Plugin {
32
- /**
33
- * @inheritDoc
34
- */
35
- static get pluginName() {
36
- return 'BalloonToolbar';
37
- }
38
-
39
- /**
40
- * @inheritDoc
41
- */
42
- static get requires() {
43
- return [ ContextualBalloon ];
44
- }
45
-
46
- /**
47
- * @inheritDoc
48
- */
49
- constructor( editor ) {
50
- super( editor );
51
-
52
- /**
53
- * A cached and normalized `config.balloonToolbar` object.
54
- *
55
- * @type {module:core/editor/editorconfig~EditorConfig#balloonToolbar}
56
- * @private
57
- */
58
- this._balloonConfig = normalizeToolbarConfig( editor.config.get( 'balloonToolbar' ) );
59
-
60
- /**
61
- * The toolbar view displayed in the balloon.
62
- *
63
- * @type {module:ui/toolbar/toolbarview~ToolbarView}
64
- */
65
- this.toolbarView = this._createToolbarView();
66
-
67
- /**
68
- * Tracks the focus of the {@link module:core/editor/editorui~EditorUI#getEditableElement editable element}
69
- * and the {@link #toolbarView}. When both are blurred then the toolbar should hide.
70
- *
71
- * @readonly
72
- * @type {module:utils:focustracker~FocusTracker}
73
- */
74
- this.focusTracker = new FocusTracker();
75
-
76
- // Wait for the EditorUI#init. EditableElement is not available before.
77
- editor.ui.once( 'ready', () => {
78
- this.focusTracker.add( editor.ui.getEditableElement() );
79
- this.focusTracker.add( this.toolbarView.element );
80
- } );
81
-
82
- // Register the toolbar so it becomes available for Alt+F10 and Esc navigation.
83
- editor.ui.addToolbar( this.toolbarView, {
84
- beforeFocus: () => this.show( true ),
85
- afterBlur: () => this.hide(),
86
- isContextual: true
87
- } );
88
-
89
- /**
90
- * An instance of the resize observer that allows to respond to changes in editable's geometry
91
- * so the toolbar can stay within its boundaries (and group toolbar items that do not fit).
92
- *
93
- * **Note**: Used only when `shouldNotGroupWhenFull` was **not** set in the
94
- * {@link module:core/editor/editorconfig~EditorConfig#balloonToolbar configuration}.
95
- *
96
- * **Note:** Created in {@link #init}.
97
- *
98
- * @protected
99
- * @member {module:utils/dom/resizeobserver~ResizeObserver}
100
- */
101
- this._resizeObserver = null;
102
-
103
- /**
104
- * The contextual balloon plugin instance.
105
- *
106
- * @private
107
- * @type {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
108
- */
109
- this._balloon = editor.plugins.get( ContextualBalloon );
110
-
111
- /**
112
- * Fires {@link #event:_selectionChangeDebounced} event using `lodash#debounce`.
113
- *
114
- * This function is stored as a plugin property to make possible to cancel
115
- * trailing debounced invocation on destroy.
116
- *
117
- * @private
118
- * @type {Function}
119
- */
120
- this._fireSelectionChangeDebounced = debounce( () => this.fire( '_selectionChangeDebounced' ), 200 );
121
-
122
- // The appearance of the BalloonToolbar method is event–driven.
123
- // It is possible to stop the #show event and this prevent the toolbar from showing up.
124
- this.decorate( 'show' );
125
- }
126
-
127
- /**
128
- * @inheritDoc
129
- */
130
- init() {
131
- const editor = this.editor;
132
- const selection = editor.model.document.selection;
133
-
134
- // Show/hide the toolbar on editable focus/blur.
135
- this.listenTo( this.focusTracker, 'change:isFocused', ( evt, name, isFocused ) => {
136
- const isToolbarVisible = this._balloon.visibleView === this.toolbarView;
137
-
138
- if ( !isFocused && isToolbarVisible ) {
139
- this.hide();
140
- } else if ( isFocused ) {
141
- this.show();
142
- }
143
- } );
144
-
145
- // Hide the toolbar when the selection is changed by a direct change or has changed to collapsed.
146
- this.listenTo( selection, 'change:range', ( evt, data ) => {
147
- if ( data.directChange || selection.isCollapsed ) {
148
- this.hide();
149
- }
150
-
151
- // Fire internal `_selectionChangeDebounced` event to use it for showing
152
- // the toolbar after the selection stops changing.
153
- this._fireSelectionChangeDebounced();
154
- } );
155
-
156
- // Show the toolbar when the selection stops changing.
157
- this.listenTo( this, '_selectionChangeDebounced', () => {
158
- if ( this.editor.editing.view.document.isFocused ) {
159
- this.show();
160
- }
161
- } );
162
-
163
- if ( !this._balloonConfig.shouldNotGroupWhenFull ) {
164
- this.listenTo( editor, 'ready', () => {
165
- const editableElement = editor.ui.view.editable.element;
166
-
167
- // Set #toolbarView's max-width on the initialization and update it on the editable resize.
168
- this._resizeObserver = new ResizeObserver( editableElement, () => {
169
- // The max-width equals 90% of the editable's width for the best user experience.
170
- // The value keeps the balloon very close to the boundaries of the editable and limits the cases
171
- // when the balloon juts out from the editable element it belongs to.
172
- this.toolbarView.maxWidth = toPx( new Rect( editableElement ).width * .9 );
173
- } );
174
- } );
175
- }
176
-
177
- // Listen to the toolbar view and whenever it changes its geometry due to some items being
178
- // grouped or ungrouped, update the position of the balloon because a shorter/longer toolbar
179
- // means the balloon could be pointing at the wrong place. Once updated, the balloon will point
180
- // at the right selection in the content again.
181
- // https://github.com/ckeditor/ckeditor5/issues/6444
182
- this.listenTo( this.toolbarView, 'groupedItemsUpdate', () => {
183
- this._updatePosition();
184
- } );
185
- }
186
-
187
- /**
188
- * Creates toolbar components based on given configuration.
189
- * This needs to be done when all plugins are ready.
190
- *
191
- * @inheritDoc
192
- */
193
- afterInit() {
194
- const factory = this.editor.ui.componentFactory;
195
-
196
- this.toolbarView.fillFromConfig( this._balloonConfig, factory );
197
- }
198
-
199
- /**
200
- * Creates the toolbar view instance.
201
- *
202
- * @private
203
- * @returns {module:ui/toolbar/toolbarview~ToolbarView}
204
- */
205
- _createToolbarView() {
206
- const t = this.editor.locale.t;
207
- const shouldGroupWhenFull = !this._balloonConfig.shouldNotGroupWhenFull;
208
- const toolbarView = new ToolbarView( this.editor.locale, {
209
- shouldGroupWhenFull,
210
- isFloating: true
211
- } );
212
-
213
- toolbarView.ariaLabel = t( 'Editor contextual toolbar' );
214
- toolbarView.render();
215
-
216
- return toolbarView;
217
- }
218
-
219
- /**
220
- * Shows the toolbar and attaches it to the selection.
221
- *
222
- * Fires {@link #event:show} event which can be stopped to prevent the toolbar from showing up.
223
- *
224
- * @param {Boolean} [showForCollapsedSelection=false] When set `true`, the toolbar will show despite collapsed selection in the
225
- * editing view.
226
- */
227
- show( showForCollapsedSelection = false ) {
228
- const editor = this.editor;
229
- const selection = editor.model.document.selection;
230
- const schema = editor.model.schema;
231
-
232
- // Do not add the toolbar to the balloon stack twice.
233
- if ( this._balloon.hasView( this.toolbarView ) ) {
234
- return;
235
- }
236
-
237
- // Do not show the toolbar when the selection is collapsed.
238
- if ( selection.isCollapsed && !showForCollapsedSelection ) {
239
- return;
240
- }
241
-
242
- // Do not show the toolbar when there is more than one range in the selection and they fully contain selectable elements.
243
- // See https://github.com/ckeditor/ckeditor5/issues/6443.
244
- if ( selectionContainsOnlyMultipleSelectables( selection, schema ) ) {
245
- return;
246
- }
247
-
248
- // Don not show the toolbar when all components inside are disabled
249
- // see https://github.com/ckeditor/ckeditor5-ui/issues/269.
250
- if ( Array.from( this.toolbarView.items ).every( item => item.isEnabled !== undefined && !item.isEnabled ) ) {
251
- return;
252
- }
253
-
254
- // Update the toolbar position when the editor ui should be refreshed.
255
- this.listenTo( this.editor.ui, 'update', () => {
256
- this._updatePosition();
257
- } );
258
-
259
- // Add the toolbar to the common editor contextual balloon.
260
- this._balloon.add( {
261
- view: this.toolbarView,
262
- position: this._getBalloonPositionData(),
263
- balloonClassName: 'ck-toolbar-container'
264
- } );
265
- }
266
-
267
- /**
268
- * Hides the toolbar.
269
- */
270
- hide() {
271
- if ( this._balloon.hasView( this.toolbarView ) ) {
272
- this.stopListening( this.editor.ui, 'update' );
273
- this._balloon.remove( this.toolbarView );
274
- }
275
- }
276
-
277
- /**
278
- * Returns positioning options for the {@link #_balloon}. They control the way balloon is attached
279
- * to the selection.
280
- *
281
- * @private
282
- * @returns {module:utils/dom/position~Options}
283
- */
284
- _getBalloonPositionData() {
285
- const editor = this.editor;
286
- const view = editor.editing.view;
287
- const viewDocument = view.document;
288
- const viewSelection = viewDocument.selection;
289
-
290
- // Get direction of the selection.
291
- const isBackward = viewDocument.selection.isBackward;
292
-
293
- return {
294
- // Because the target for BalloonPanelView is a Rect (not DOMRange), it's geometry will stay fixed
295
- // as the window scrolls. To let the BalloonPanelView follow such Rect, is must be continuously
296
- // computed and hence, the target is defined as a function instead of a static value.
297
- // https://github.com/ckeditor/ckeditor5-ui/issues/195
298
- target: () => {
299
- const range = isBackward ? viewSelection.getFirstRange() : viewSelection.getLastRange();
300
- const rangeRects = Rect.getDomRangeRects( view.domConverter.viewRangeToDom( range ) );
301
-
302
- // Select the proper range rect depending on the direction of the selection.
303
- if ( isBackward ) {
304
- return rangeRects[ 0 ];
305
- } else {
306
- // Ditch the zero-width "orphan" rect in the next line for the forward selection if there's
307
- // another one preceding it. It is not rendered as a selection by the web browser anyway.
308
- // https://github.com/ckeditor/ckeditor5-ui/issues/308
309
- if ( rangeRects.length > 1 && rangeRects[ rangeRects.length - 1 ].width === 0 ) {
310
- rangeRects.pop();
311
- }
312
-
313
- return rangeRects[ rangeRects.length - 1 ];
314
- }
315
- },
316
- positions: this._getBalloonPositions( isBackward )
317
- };
318
- }
319
-
320
- /**
321
- * Updates the position of the {@link #_balloon} to make up for changes:
322
- *
323
- * * in the geometry of the selection it is attached to (e.g. the selection moved in the viewport or expanded or shrunk),
324
- * * or the geometry of the balloon toolbar itself (e.g. the toolbar has grouped or ungrouped some items and it is shorter or longer).
325
- *
326
- * @private
327
- */
328
- _updatePosition() {
329
- this._balloon.updatePosition( this._getBalloonPositionData() );
330
- }
331
-
332
- /**
333
- * @inheritDoc
334
- */
335
- destroy() {
336
- super.destroy();
337
-
338
- this.stopListening();
339
- this._fireSelectionChangeDebounced.cancel();
340
- this.toolbarView.destroy();
341
- this.focusTracker.destroy();
342
-
343
- if ( this._resizeObserver ) {
344
- this._resizeObserver.destroy();
345
- }
346
- }
347
-
348
- /**
349
- * This event is fired just before the toolbar shows up. Stopping this event will prevent this.
350
- *
351
- * @event show
352
- */
353
-
354
- /**
355
- * This is internal plugin event which is fired 200 ms after model selection last change.
356
- * This is to makes easy test debounced action without need to use `setTimeout`.
357
- *
358
- * @protected
359
- * @event _selectionChangeDebounced
360
- */
361
-
362
- /**
363
- * Returns toolbar positions for the given direction of the selection.
364
- *
365
- * @private
366
- * @param {Boolean} isBackward
367
- * @returns {Array.<module:utils/dom/position~Position>}
368
- */
369
- _getBalloonPositions( isBackward ) {
370
- const isSafariIniOS = env.isSafari && env.isiOS;
371
-
372
- // https://github.com/ckeditor/ckeditor5/issues/7707
373
- const positions = isSafariIniOS ? generatePositions( {
374
- // 20px when zoomed out. Less then 20px when zoomed in; the "radius" of the native selection handle gets
375
- // smaller as the user zooms in. No less than the default v-offset, though.
376
- heightOffset: Math.max(
377
- BalloonPanelView.arrowHeightOffset,
378
- Math.round( 20 / global.window.visualViewport.scale )
379
- )
380
- } ) : BalloonPanelView.defaultPositions;
381
-
382
- return isBackward ? [
383
- positions.northWestArrowSouth,
384
- positions.northWestArrowSouthWest,
385
- positions.northWestArrowSouthEast,
386
- positions.northWestArrowSouthMiddleEast,
387
- positions.northWestArrowSouthMiddleWest,
388
- positions.southWestArrowNorth,
389
- positions.southWestArrowNorthWest,
390
- positions.southWestArrowNorthEast,
391
- positions.southWestArrowNorthMiddleWest,
392
- positions.southWestArrowNorthMiddleEast
393
- ] : [
394
- positions.southEastArrowNorth,
395
- positions.southEastArrowNorthEast,
396
- positions.southEastArrowNorthWest,
397
- positions.southEastArrowNorthMiddleEast,
398
- positions.southEastArrowNorthMiddleWest,
399
- positions.northEastArrowSouth,
400
- positions.northEastArrowSouthEast,
401
- positions.northEastArrowSouthWest,
402
- positions.northEastArrowSouthMiddleEast,
403
- positions.northEastArrowSouthMiddleWest
404
- ];
405
- }
28
+ /**
29
+ * @inheritDoc
30
+ */
31
+ constructor(editor) {
32
+ super(editor);
33
+ /**
34
+ * A cached and normalized `config.balloonToolbar` object.
35
+ *
36
+ * @type {module:core/editor/editorconfig~EditorConfig#balloonToolbar}
37
+ * @private
38
+ */
39
+ this._balloonConfig = normalizeToolbarConfig(editor.config.get('balloonToolbar'));
40
+ /**
41
+ * The toolbar view displayed in the balloon.
42
+ *
43
+ * @type {module:ui/toolbar/toolbarview~ToolbarView}
44
+ */
45
+ this.toolbarView = this._createToolbarView();
46
+ /**
47
+ * Tracks the focus of the {@link module:core/editor/editorui~EditorUI#getEditableElement editable element}
48
+ * and the {@link #toolbarView}. When both are blurred then the toolbar should hide.
49
+ *
50
+ * @readonly
51
+ * @type {module:utils:focustracker~FocusTracker}
52
+ */
53
+ this.focusTracker = new FocusTracker();
54
+ // Wait for the EditorUI#init. EditableElement is not available before.
55
+ editor.ui.once('ready', () => {
56
+ this.focusTracker.add(editor.ui.getEditableElement());
57
+ this.focusTracker.add(this.toolbarView.element);
58
+ });
59
+ // Register the toolbar so it becomes available for Alt+F10 and Esc navigation.
60
+ editor.ui.addToolbar(this.toolbarView, {
61
+ beforeFocus: () => this.show(true),
62
+ afterBlur: () => this.hide(),
63
+ isContextual: true
64
+ });
65
+ /**
66
+ * An instance of the resize observer that allows to respond to changes in editable's geometry
67
+ * so the toolbar can stay within its boundaries (and group toolbar items that do not fit).
68
+ *
69
+ * **Note**: Used only when `shouldNotGroupWhenFull` was **not** set in the
70
+ * {@link module:core/editor/editorconfig~EditorConfig#balloonToolbar configuration}.
71
+ *
72
+ * **Note:** Created in {@link #init}.
73
+ *
74
+ * @protected
75
+ * @member {module:utils/dom/resizeobserver~ResizeObserver}
76
+ */
77
+ this._resizeObserver = null;
78
+ /**
79
+ * The contextual balloon plugin instance.
80
+ *
81
+ * @private
82
+ * @type {module:ui/panel/balloon/contextualballoon~ContextualBalloon}
83
+ */
84
+ this._balloon = editor.plugins.get(ContextualBalloon);
85
+ /**
86
+ * Fires {@link #event:_selectionChangeDebounced} event using `lodash#debounce`.
87
+ *
88
+ * This function is stored as a plugin property to make possible to cancel
89
+ * trailing debounced invocation on destroy.
90
+ *
91
+ * @private
92
+ * @type {Function}
93
+ */
94
+ this._fireSelectionChangeDebounced = debounce(() => this.fire('_selectionChangeDebounced'), 200);
95
+ // The appearance of the BalloonToolbar method is event–driven.
96
+ // It is possible to stop the #show event and this prevent the toolbar from showing up.
97
+ this.decorate('show');
98
+ }
99
+ /**
100
+ * @inheritDoc
101
+ */
102
+ static get pluginName() {
103
+ return 'BalloonToolbar';
104
+ }
105
+ /**
106
+ * @inheritDoc
107
+ */
108
+ static get requires() {
109
+ return [ContextualBalloon];
110
+ }
111
+ /**
112
+ * @inheritDoc
113
+ */
114
+ init() {
115
+ const editor = this.editor;
116
+ const selection = editor.model.document.selection;
117
+ // Show/hide the toolbar on editable focus/blur.
118
+ this.listenTo(this.focusTracker, 'change:isFocused', (evt, name, isFocused) => {
119
+ const isToolbarVisible = this._balloon.visibleView === this.toolbarView;
120
+ if (!isFocused && isToolbarVisible) {
121
+ this.hide();
122
+ }
123
+ else if (isFocused) {
124
+ this.show();
125
+ }
126
+ });
127
+ // Hide the toolbar when the selection is changed by a direct change or has changed to collapsed.
128
+ this.listenTo(selection, 'change:range', (evt, data) => {
129
+ if (data.directChange || selection.isCollapsed) {
130
+ this.hide();
131
+ }
132
+ // Fire internal `_selectionChangeDebounced` event to use it for showing
133
+ // the toolbar after the selection stops changing.
134
+ this._fireSelectionChangeDebounced();
135
+ });
136
+ // Show the toolbar when the selection stops changing.
137
+ this.listenTo(this, '_selectionChangeDebounced', () => {
138
+ if (this.editor.editing.view.document.isFocused) {
139
+ this.show();
140
+ }
141
+ });
142
+ if (!this._balloonConfig.shouldNotGroupWhenFull) {
143
+ this.listenTo(editor, 'ready', () => {
144
+ const editableElement = editor.ui.view.editable.element;
145
+ // Set #toolbarView's max-width on the initialization and update it on the editable resize.
146
+ this._resizeObserver = new ResizeObserver(editableElement, () => {
147
+ // The max-width equals 90% of the editable's width for the best user experience.
148
+ // The value keeps the balloon very close to the boundaries of the editable and limits the cases
149
+ // when the balloon juts out from the editable element it belongs to.
150
+ this.toolbarView.maxWidth = toPx(new Rect(editableElement).width * .9);
151
+ });
152
+ });
153
+ }
154
+ // Listen to the toolbar view and whenever it changes its geometry due to some items being
155
+ // grouped or ungrouped, update the position of the balloon because a shorter/longer toolbar
156
+ // means the balloon could be pointing at the wrong place. Once updated, the balloon will point
157
+ // at the right selection in the content again.
158
+ // https://github.com/ckeditor/ckeditor5/issues/6444
159
+ this.listenTo(this.toolbarView, 'groupedItemsUpdate', () => {
160
+ this._updatePosition();
161
+ });
162
+ }
163
+ /**
164
+ * Creates toolbar components based on given configuration.
165
+ * This needs to be done when all plugins are ready.
166
+ *
167
+ * @inheritDoc
168
+ */
169
+ afterInit() {
170
+ const factory = this.editor.ui.componentFactory;
171
+ this.toolbarView.fillFromConfig(this._balloonConfig, factory);
172
+ }
173
+ /**
174
+ * Creates the toolbar view instance.
175
+ *
176
+ * @private
177
+ * @returns {module:ui/toolbar/toolbarview~ToolbarView}
178
+ */
179
+ _createToolbarView() {
180
+ const t = this.editor.locale.t;
181
+ const shouldGroupWhenFull = !this._balloonConfig.shouldNotGroupWhenFull;
182
+ const toolbarView = new ToolbarView(this.editor.locale, {
183
+ shouldGroupWhenFull,
184
+ isFloating: true
185
+ });
186
+ toolbarView.ariaLabel = t('Editor contextual toolbar');
187
+ toolbarView.render();
188
+ return toolbarView;
189
+ }
190
+ /**
191
+ * Shows the toolbar and attaches it to the selection.
192
+ *
193
+ * Fires {@link #event:show} event which can be stopped to prevent the toolbar from showing up.
194
+ *
195
+ * @param {Boolean} [showForCollapsedSelection=false] When set `true`, the toolbar will show despite collapsed selection in the
196
+ * editing view.
197
+ */
198
+ show(showForCollapsedSelection = false) {
199
+ const editor = this.editor;
200
+ const selection = editor.model.document.selection;
201
+ const schema = editor.model.schema;
202
+ // Do not add the toolbar to the balloon stack twice.
203
+ if (this._balloon.hasView(this.toolbarView)) {
204
+ return;
205
+ }
206
+ // Do not show the toolbar when the selection is collapsed.
207
+ if (selection.isCollapsed && !showForCollapsedSelection) {
208
+ return;
209
+ }
210
+ // Do not show the toolbar when there is more than one range in the selection and they fully contain selectable elements.
211
+ // See https://github.com/ckeditor/ckeditor5/issues/6443.
212
+ if (selectionContainsOnlyMultipleSelectables(selection, schema)) {
213
+ return;
214
+ }
215
+ // Don not show the toolbar when all components inside are disabled
216
+ // see https://github.com/ckeditor/ckeditor5-ui/issues/269.
217
+ if (Array.from(this.toolbarView.items).every((item) => item.isEnabled !== undefined && !item.isEnabled)) {
218
+ return;
219
+ }
220
+ // Update the toolbar position when the editor ui should be refreshed.
221
+ this.listenTo(this.editor.ui, 'update', () => {
222
+ this._updatePosition();
223
+ });
224
+ // Add the toolbar to the common editor contextual balloon.
225
+ this._balloon.add({
226
+ view: this.toolbarView,
227
+ position: this._getBalloonPositionData(),
228
+ balloonClassName: 'ck-toolbar-container'
229
+ });
230
+ }
231
+ /**
232
+ * Hides the toolbar.
233
+ */
234
+ hide() {
235
+ if (this._balloon.hasView(this.toolbarView)) {
236
+ this.stopListening(this.editor.ui, 'update');
237
+ this._balloon.remove(this.toolbarView);
238
+ }
239
+ }
240
+ /**
241
+ * Returns positioning options for the {@link #_balloon}. They control the way balloon is attached
242
+ * to the selection.
243
+ *
244
+ * @private
245
+ * @returns {module:utils/dom/position~Options}
246
+ */
247
+ _getBalloonPositionData() {
248
+ const editor = this.editor;
249
+ const view = editor.editing.view;
250
+ const viewDocument = view.document;
251
+ const viewSelection = viewDocument.selection;
252
+ // Get direction of the selection.
253
+ const isBackward = viewDocument.selection.isBackward;
254
+ return {
255
+ // Because the target for BalloonPanelView is a Rect (not DOMRange), it's geometry will stay fixed
256
+ // as the window scrolls. To let the BalloonPanelView follow such Rect, is must be continuously
257
+ // computed and hence, the target is defined as a function instead of a static value.
258
+ // https://github.com/ckeditor/ckeditor5-ui/issues/195
259
+ target: () => {
260
+ const range = isBackward ? viewSelection.getFirstRange() : viewSelection.getLastRange();
261
+ const rangeRects = Rect.getDomRangeRects(view.domConverter.viewRangeToDom(range));
262
+ // Select the proper range rect depending on the direction of the selection.
263
+ if (isBackward) {
264
+ return rangeRects[0];
265
+ }
266
+ else {
267
+ // Ditch the zero-width "orphan" rect in the next line for the forward selection if there's
268
+ // another one preceding it. It is not rendered as a selection by the web browser anyway.
269
+ // https://github.com/ckeditor/ckeditor5-ui/issues/308
270
+ if (rangeRects.length > 1 && rangeRects[rangeRects.length - 1].width === 0) {
271
+ rangeRects.pop();
272
+ }
273
+ return rangeRects[rangeRects.length - 1];
274
+ }
275
+ },
276
+ positions: this._getBalloonPositions(isBackward)
277
+ };
278
+ }
279
+ /**
280
+ * Updates the position of the {@link #_balloon} to make up for changes:
281
+ *
282
+ * * in the geometry of the selection it is attached to (e.g. the selection moved in the viewport or expanded or shrunk),
283
+ * * or the geometry of the balloon toolbar itself (e.g. the toolbar has grouped or ungrouped some items and it is shorter or longer).
284
+ *
285
+ * @private
286
+ */
287
+ _updatePosition() {
288
+ this._balloon.updatePosition(this._getBalloonPositionData());
289
+ }
290
+ /**
291
+ * @inheritDoc
292
+ */
293
+ destroy() {
294
+ super.destroy();
295
+ this.stopListening();
296
+ this._fireSelectionChangeDebounced.cancel();
297
+ this.toolbarView.destroy();
298
+ this.focusTracker.destroy();
299
+ if (this._resizeObserver) {
300
+ this._resizeObserver.destroy();
301
+ }
302
+ }
303
+ /**
304
+ * This event is fired just before the toolbar shows up. Stopping this event will prevent this.
305
+ *
306
+ * @event show
307
+ */
308
+ /**
309
+ * This is internal plugin event which is fired 200 ms after model selection last change.
310
+ * This is to makes easy test debounced action without need to use `setTimeout`.
311
+ *
312
+ * @protected
313
+ * @event _selectionChangeDebounced
314
+ */
315
+ /**
316
+ * Returns toolbar positions for the given direction of the selection.
317
+ *
318
+ * @private
319
+ * @param {Boolean} isBackward
320
+ * @returns {Array.<module:utils/dom/position~Position>}
321
+ */
322
+ _getBalloonPositions(isBackward) {
323
+ const isSafariIniOS = env.isSafari && env.isiOS;
324
+ // https://github.com/ckeditor/ckeditor5/issues/7707
325
+ const positions = isSafariIniOS ? generatePositions({
326
+ // 20px when zoomed out. Less then 20px when zoomed in; the "radius" of the native selection handle gets
327
+ // smaller as the user zooms in. No less than the default v-offset, though.
328
+ heightOffset: Math.max(BalloonPanelView.arrowHeightOffset, Math.round(20 / global.window.visualViewport.scale))
329
+ }) : BalloonPanelView.defaultPositions;
330
+ return isBackward ? [
331
+ positions.northWestArrowSouth,
332
+ positions.northWestArrowSouthWest,
333
+ positions.northWestArrowSouthEast,
334
+ positions.northWestArrowSouthMiddleEast,
335
+ positions.northWestArrowSouthMiddleWest,
336
+ positions.southWestArrowNorth,
337
+ positions.southWestArrowNorthWest,
338
+ positions.southWestArrowNorthEast,
339
+ positions.southWestArrowNorthMiddleWest,
340
+ positions.southWestArrowNorthMiddleEast
341
+ ] : [
342
+ positions.southEastArrowNorth,
343
+ positions.southEastArrowNorthEast,
344
+ positions.southEastArrowNorthWest,
345
+ positions.southEastArrowNorthMiddleEast,
346
+ positions.southEastArrowNorthMiddleWest,
347
+ positions.northEastArrowSouth,
348
+ positions.northEastArrowSouthEast,
349
+ positions.northEastArrowSouthWest,
350
+ positions.northEastArrowSouthMiddleEast,
351
+ positions.northEastArrowSouthMiddleWest
352
+ ];
353
+ }
406
354
  }
407
-
408
355
  // Returns "true" when the selection has multiple ranges and each range contains a selectable element
409
356
  // and nothing else.
410
357
  //
@@ -412,47 +359,13 @@ export default class BalloonToolbar extends Plugin {
412
359
  // @param {module:engine/model/selection~Selection} selection
413
360
  // @param {module:engine/model/schema~Schema} schema
414
361
  // @returns {Boolean}
415
- function selectionContainsOnlyMultipleSelectables( selection, schema ) {
416
- // It doesn't contain multiple objects if there is only one range.
417
- if ( selection.rangeCount === 1 ) {
418
- return false;
419
- }
420
-
421
- return [ ...selection.getRanges() ].every( range => {
422
- const element = range.getContainedElement();
423
-
424
- return element && schema.isSelectable( element );
425
- } );
362
+ function selectionContainsOnlyMultipleSelectables(selection, schema) {
363
+ // It doesn't contain multiple objects if there is only one range.
364
+ if (selection.rangeCount === 1) {
365
+ return false;
366
+ }
367
+ return [...selection.getRanges()].every(range => {
368
+ const element = range.getContainedElement();
369
+ return element && schema.isSelectable(element);
370
+ });
426
371
  }
427
-
428
- /**
429
- * Contextual toolbar configuration. Used by the {@link module:ui/toolbar/balloon/balloontoolbar~BalloonToolbar}
430
- * feature.
431
- *
432
- * ## Configuring toolbar items
433
- *
434
- * const config = {
435
- * balloonToolbar: [ 'bold', 'italic', 'undo', 'redo' ]
436
- * };
437
- *
438
- * You can also use `'|'` to create a separator between groups of items:
439
- *
440
- * const config = {
441
- * balloonToolbar: [ 'bold', 'italic', '|', 'undo', 'redo' ]
442
- * };
443
- *
444
- * Read also about configuring the main editor toolbar in {@link module:core/editor/editorconfig~EditorConfig#toolbar}.
445
- *
446
- * ## Configuring items grouping
447
- *
448
- * You can prevent automatic items grouping by setting the `shouldNotGroupWhenFull` option:
449
- *
450
- * const config = {
451
- * balloonToolbar: {
452
- * items: [ 'bold', 'italic', 'undo', 'redo' ],
453
- * shouldNotGroupWhenFull: true
454
- * },
455
- * };
456
- *
457
- * @member {Array.<String>|Object} module:core/editor/editorconfig~EditorConfig#balloonToolbar
458
- */