@ckeditor/ckeditor5-ui 38.1.0 → 38.1.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 (123) hide show
  1. package/package.json +3 -3
  2. package/src/augmentation.d.ts +86 -86
  3. package/src/augmentation.js +5 -5
  4. package/src/bindings/addkeyboardhandlingforgrid.d.ts +27 -27
  5. package/src/bindings/addkeyboardhandlingforgrid.js +107 -107
  6. package/src/bindings/clickoutsidehandler.d.ts +28 -28
  7. package/src/bindings/clickoutsidehandler.js +36 -36
  8. package/src/bindings/csstransitiondisablermixin.d.ts +40 -40
  9. package/src/bindings/csstransitiondisablermixin.js +55 -55
  10. package/src/bindings/injectcsstransitiondisabler.d.ts +59 -59
  11. package/src/bindings/injectcsstransitiondisabler.js +71 -71
  12. package/src/bindings/preventdefault.d.ts +33 -33
  13. package/src/bindings/preventdefault.js +34 -34
  14. package/src/bindings/submithandler.d.ts +57 -57
  15. package/src/bindings/submithandler.js +47 -47
  16. package/src/button/button.d.ts +178 -178
  17. package/src/button/button.js +5 -5
  18. package/src/button/buttonview.d.ts +177 -177
  19. package/src/button/buttonview.js +231 -231
  20. package/src/button/switchbuttonview.d.ts +45 -45
  21. package/src/button/switchbuttonview.js +75 -75
  22. package/src/colorgrid/colorgridview.d.ts +132 -132
  23. package/src/colorgrid/colorgridview.js +124 -124
  24. package/src/colorgrid/colortileview.d.ts +28 -28
  25. package/src/colorgrid/colortileview.js +40 -40
  26. package/src/colorgrid/utils.d.ts +47 -47
  27. package/src/colorgrid/utils.js +84 -84
  28. package/src/colorpicker/colorpickerview.d.ts +110 -110
  29. package/src/colorpicker/colorpickerview.js +253 -253
  30. package/src/colorpicker/utils.d.ts +35 -35
  31. package/src/colorpicker/utils.js +99 -99
  32. package/src/componentfactory.d.ts +81 -81
  33. package/src/componentfactory.js +104 -104
  34. package/src/dropdown/button/dropdownbutton.d.ts +25 -25
  35. package/src/dropdown/button/dropdownbutton.js +5 -5
  36. package/src/dropdown/button/dropdownbuttonview.d.ts +48 -48
  37. package/src/dropdown/button/dropdownbuttonview.js +66 -66
  38. package/src/dropdown/button/splitbuttonview.d.ts +161 -161
  39. package/src/dropdown/button/splitbuttonview.js +152 -152
  40. package/src/dropdown/dropdownpanelfocusable.d.ts +21 -21
  41. package/src/dropdown/dropdownpanelfocusable.js +5 -5
  42. package/src/dropdown/dropdownpanelview.d.ts +62 -62
  43. package/src/dropdown/dropdownpanelview.js +96 -96
  44. package/src/dropdown/dropdownview.d.ts +315 -315
  45. package/src/dropdown/dropdownview.js +378 -378
  46. package/src/dropdown/utils.d.ts +221 -221
  47. package/src/dropdown/utils.js +434 -434
  48. package/src/editableui/editableuiview.d.ts +72 -72
  49. package/src/editableui/editableuiview.js +112 -112
  50. package/src/editableui/inline/inlineeditableuiview.d.ts +40 -40
  51. package/src/editableui/inline/inlineeditableuiview.js +48 -48
  52. package/src/editorui/bodycollection.d.ts +55 -55
  53. package/src/editorui/bodycollection.js +84 -84
  54. package/src/editorui/boxed/boxededitoruiview.d.ts +40 -40
  55. package/src/editorui/boxed/boxededitoruiview.js +81 -81
  56. package/src/editorui/editorui.d.ts +282 -282
  57. package/src/editorui/editorui.js +410 -410
  58. package/src/editorui/editoruiview.d.ts +39 -39
  59. package/src/editorui/editoruiview.js +38 -38
  60. package/src/editorui/poweredby.d.ts +71 -71
  61. package/src/editorui/poweredby.js +294 -294
  62. package/src/focuscycler.d.ts +183 -183
  63. package/src/focuscycler.js +220 -220
  64. package/src/formheader/formheaderview.d.ts +53 -53
  65. package/src/formheader/formheaderview.js +63 -63
  66. package/src/icon/iconview.d.ts +78 -78
  67. package/src/icon/iconview.js +112 -112
  68. package/src/iframe/iframeview.d.ts +50 -50
  69. package/src/iframe/iframeview.js +63 -63
  70. package/src/index.d.ts +62 -62
  71. package/src/index.js +61 -61
  72. package/src/input/inputview.d.ts +121 -121
  73. package/src/input/inputview.js +106 -106
  74. package/src/inputnumber/inputnumberview.d.ts +49 -49
  75. package/src/inputnumber/inputnumberview.js +40 -40
  76. package/src/inputtext/inputtextview.d.ts +18 -18
  77. package/src/inputtext/inputtextview.js +27 -27
  78. package/src/label/labelview.d.ts +36 -36
  79. package/src/label/labelview.js +41 -41
  80. package/src/labeledfield/labeledfieldview.d.ts +182 -182
  81. package/src/labeledfield/labeledfieldview.js +157 -157
  82. package/src/labeledfield/utils.d.ts +93 -93
  83. package/src/labeledfield/utils.js +131 -131
  84. package/src/labeledinput/labeledinputview.d.ts +125 -125
  85. package/src/labeledinput/labeledinputview.js +125 -125
  86. package/src/list/listitemview.d.ts +35 -35
  87. package/src/list/listitemview.js +40 -40
  88. package/src/list/listseparatorview.d.ts +18 -18
  89. package/src/list/listseparatorview.js +28 -28
  90. package/src/list/listview.d.ts +65 -65
  91. package/src/list/listview.js +90 -90
  92. package/src/model.d.ts +22 -22
  93. package/src/model.js +31 -31
  94. package/src/notification/notification.d.ts +211 -211
  95. package/src/notification/notification.js +187 -187
  96. package/src/panel/balloon/balloonpanelview.d.ts +685 -685
  97. package/src/panel/balloon/balloonpanelview.js +988 -988
  98. package/src/panel/balloon/contextualballoon.d.ts +299 -299
  99. package/src/panel/balloon/contextualballoon.js +572 -572
  100. package/src/panel/sticky/stickypanelview.d.ts +132 -132
  101. package/src/panel/sticky/stickypanelview.js +139 -139
  102. package/src/template.d.ts +942 -942
  103. package/src/template.js +1294 -1294
  104. package/src/toolbar/balloon/balloontoolbar.d.ts +122 -122
  105. package/src/toolbar/balloon/balloontoolbar.js +300 -300
  106. package/src/toolbar/block/blockbuttonview.d.ts +35 -35
  107. package/src/toolbar/block/blockbuttonview.js +41 -41
  108. package/src/toolbar/block/blocktoolbar.d.ts +161 -161
  109. package/src/toolbar/block/blocktoolbar.js +391 -391
  110. package/src/toolbar/normalizetoolbarconfig.d.ts +39 -39
  111. package/src/toolbar/normalizetoolbarconfig.js +51 -51
  112. package/src/toolbar/toolbarlinebreakview.d.ts +18 -18
  113. package/src/toolbar/toolbarlinebreakview.js +28 -28
  114. package/src/toolbar/toolbarseparatorview.d.ts +18 -18
  115. package/src/toolbar/toolbarseparatorview.js +28 -28
  116. package/src/toolbar/toolbarview.d.ts +265 -265
  117. package/src/toolbar/toolbarview.js +717 -717
  118. package/src/tooltipmanager.d.ts +180 -180
  119. package/src/tooltipmanager.js +353 -353
  120. package/src/view.d.ts +422 -422
  121. package/src/view.js +396 -396
  122. package/src/viewcollection.d.ts +139 -139
  123. package/src/viewcollection.js +206 -206
@@ -1,391 +1,391 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module ui/toolbar/block/blocktoolbar
7
- */
8
- /* global window */
9
- import { Plugin, icons } from '@ckeditor/ckeditor5-core';
10
- import { Rect, ResizeObserver, getOptimalPosition, toUnit } from '@ckeditor/ckeditor5-utils';
11
- import BlockButtonView from './blockbuttonview';
12
- import BalloonPanelView from '../../panel/balloon/balloonpanelview';
13
- import ToolbarView from '../toolbarview';
14
- import clickOutsideHandler from '../../bindings/clickoutsidehandler';
15
- import normalizeToolbarConfig from '../normalizetoolbarconfig';
16
- const toPx = toUnit('px');
17
- const { pilcrow } = icons;
18
- /**
19
- * The block toolbar plugin.
20
- *
21
- * This plugin provides a button positioned next to the block of content where the selection is anchored.
22
- * Upon clicking the button, a dropdown providing access to editor features shows up, as configured in
23
- * {@link module:core/editor/editorconfig~EditorConfig#blockToolbar}.
24
- *
25
- * By default, the button is displayed next to all elements marked in {@link module:engine/model/schema~Schema}
26
- * as `$block` for which the toolbar provides at least one option.
27
- *
28
- * By default, the button is attached so its right boundary is touching the
29
- * {@link module:engine/view/editableelement~EditableElement}:
30
- *
31
- * ```
32
- * __ |
33
- * | || This is a block of content that the
34
- * ¯¯ | button is attached to. This is a
35
- * | block of content that the button is
36
- * | attached to.
37
- * ```
38
- *
39
- * The position of the button can be adjusted using the CSS `transform` property:
40
- *
41
- * ```css
42
- * .ck-block-toolbar-button {
43
- * transform: translateX( -10px );
44
- * }
45
- * ```
46
- *
47
- * ```
48
- * __ |
49
- * | | | This is a block of content that the
50
- * ¯¯ | button is attached to. This is a
51
- * | block of content that the button is
52
- * | attached to.
53
- * ```
54
- *
55
- * **Note**: If you plan to run the editor in a right–to–left (RTL) language, keep in mind the button
56
- * will be attached to the **right** boundary of the editable area. In that case, make sure the
57
- * CSS position adjustment works properly by adding the following styles:
58
- *
59
- * ```css
60
- * .ck[dir="rtl"] .ck-block-toolbar-button {
61
- * transform: translateX( 10px );
62
- * }
63
- * ```
64
- */
65
- export default class BlockToolbar extends Plugin {
66
- /**
67
- * @inheritDoc
68
- */
69
- static get pluginName() {
70
- return 'BlockToolbar';
71
- }
72
- /**
73
- * @inheritDoc
74
- */
75
- constructor(editor) {
76
- super(editor);
77
- /**
78
- * An instance of the resize observer that allows to respond to changes in editable's geometry
79
- * so the toolbar can stay within its boundaries (and group toolbar items that do not fit).
80
- *
81
- * **Note**: Used only when `shouldNotGroupWhenFull` was **not** set in the
82
- * {@link module:core/editor/editorconfig~EditorConfig#blockToolbar configuration}.
83
- *
84
- * **Note:** Created in {@link #afterInit}.
85
- */
86
- this._resizeObserver = null;
87
- this._blockToolbarConfig = normalizeToolbarConfig(this.editor.config.get('blockToolbar'));
88
- this.toolbarView = this._createToolbarView();
89
- this.panelView = this._createPanelView();
90
- this.buttonView = this._createButtonView();
91
- // Close the #panelView upon clicking outside of the plugin UI.
92
- clickOutsideHandler({
93
- emitter: this.panelView,
94
- contextElements: [this.panelView.element, this.buttonView.element],
95
- activator: () => this.panelView.isVisible,
96
- callback: () => this._hidePanel()
97
- });
98
- }
99
- /**
100
- * @inheritDoc
101
- */
102
- init() {
103
- const editor = this.editor;
104
- // Hides panel on a direct selection change.
105
- this.listenTo(editor.model.document.selection, 'change:range', (evt, data) => {
106
- if (data.directChange) {
107
- this._hidePanel();
108
- }
109
- });
110
- this.listenTo(editor.ui, 'update', () => this._updateButton());
111
- // `low` priority is used because of https://github.com/ckeditor/ckeditor5-core/issues/133.
112
- this.listenTo(editor, 'change:isReadOnly', () => this._updateButton(), { priority: 'low' });
113
- this.listenTo(editor.ui.focusTracker, 'change:isFocused', () => this._updateButton());
114
- // Reposition button on resize.
115
- this.listenTo(this.buttonView, 'change:isVisible', (evt, name, isVisible) => {
116
- if (isVisible) {
117
- // Keep correct position of button and panel on window#resize.
118
- this.buttonView.listenTo(window, 'resize', () => this._updateButton());
119
- }
120
- else {
121
- // Stop repositioning button when is hidden.
122
- this.buttonView.stopListening(window, 'resize');
123
- // Hide the panel when the button disappears.
124
- this._hidePanel();
125
- }
126
- });
127
- // Register the toolbar so it becomes available for Alt+F10 and Esc navigation.
128
- editor.ui.addToolbar(this.toolbarView, {
129
- beforeFocus: () => this._showPanel(),
130
- afterBlur: () => this._hidePanel()
131
- });
132
- }
133
- /**
134
- * Fills the toolbar with its items based on the configuration.
135
- *
136
- * **Note:** This needs to be done after all plugins are ready.
137
- */
138
- afterInit() {
139
- this.toolbarView.fillFromConfig(this._blockToolbarConfig, this.editor.ui.componentFactory);
140
- // Hide panel before executing each button in the panel.
141
- for (const item of this.toolbarView.items) {
142
- item.on('execute', () => this._hidePanel(true), { priority: 'high' });
143
- }
144
- }
145
- /**
146
- * @inheritDoc
147
- */
148
- destroy() {
149
- super.destroy();
150
- // Destroy created UI components as they are not automatically destroyed (see ckeditor5#1341).
151
- this.panelView.destroy();
152
- this.buttonView.destroy();
153
- this.toolbarView.destroy();
154
- if (this._resizeObserver) {
155
- this._resizeObserver.destroy();
156
- }
157
- }
158
- /**
159
- * Creates the {@link #toolbarView}.
160
- */
161
- _createToolbarView() {
162
- const t = this.editor.locale.t;
163
- const shouldGroupWhenFull = !this._blockToolbarConfig.shouldNotGroupWhenFull;
164
- const toolbarView = new ToolbarView(this.editor.locale, {
165
- shouldGroupWhenFull,
166
- isFloating: true
167
- });
168
- toolbarView.ariaLabel = t('Editor block content toolbar');
169
- return toolbarView;
170
- }
171
- /**
172
- * Creates the {@link #panelView}.
173
- */
174
- _createPanelView() {
175
- const editor = this.editor;
176
- const panelView = new BalloonPanelView(editor.locale);
177
- panelView.content.add(this.toolbarView);
178
- panelView.class = 'ck-toolbar-container';
179
- editor.ui.view.body.add(panelView);
180
- editor.ui.focusTracker.add(panelView.element);
181
- // Close #panelView on `Esc` press.
182
- this.toolbarView.keystrokes.set('Esc', (evt, cancel) => {
183
- this._hidePanel(true);
184
- cancel();
185
- });
186
- return panelView;
187
- }
188
- /**
189
- * Creates the {@link #buttonView}.
190
- */
191
- _createButtonView() {
192
- const editor = this.editor;
193
- const t = editor.t;
194
- const buttonView = new BlockButtonView(editor.locale);
195
- buttonView.set({
196
- label: t('Edit block'),
197
- icon: pilcrow,
198
- withText: false
199
- });
200
- // Bind the panelView observable properties to the buttonView.
201
- buttonView.bind('isOn').to(this.panelView, 'isVisible');
202
- buttonView.bind('tooltip').to(this.panelView, 'isVisible', isVisible => !isVisible);
203
- // Toggle the panelView upon buttonView#execute.
204
- this.listenTo(buttonView, 'execute', () => {
205
- if (!this.panelView.isVisible) {
206
- this._showPanel();
207
- }
208
- else {
209
- this._hidePanel(true);
210
- }
211
- });
212
- editor.ui.view.body.add(buttonView);
213
- editor.ui.focusTracker.add(buttonView.element);
214
- return buttonView;
215
- }
216
- /**
217
- * Shows or hides the button.
218
- * When all the conditions for displaying the button are matched, it shows the button. Hides otherwise.
219
- */
220
- _updateButton() {
221
- const editor = this.editor;
222
- const model = editor.model;
223
- const view = editor.editing.view;
224
- // Hides the button when the editor is not focused.
225
- if (!editor.ui.focusTracker.isFocused) {
226
- this._hideButton();
227
- return;
228
- }
229
- // Hides the button when the selection is in non-editable place.
230
- if (!editor.model.canEditAt(editor.model.document.selection)) {
231
- this._hideButton();
232
- return;
233
- }
234
- // Get the first selected block, button will be attached to this element.
235
- const modelTarget = Array.from(model.document.selection.getSelectedBlocks())[0];
236
- // Hides the button when there is no enabled item in toolbar for the current block element.
237
- if (!modelTarget || Array.from(this.toolbarView.items).every((item) => !item.isEnabled)) {
238
- this._hideButton();
239
- return;
240
- }
241
- // Get DOM target element.
242
- const domTarget = view.domConverter.mapViewToDom(editor.editing.mapper.toViewElement(modelTarget));
243
- // Show block button.
244
- this.buttonView.isVisible = true;
245
- // Make sure that the block toolbar panel is resized properly.
246
- this._setupToolbarResize();
247
- // Attach block button to target DOM element.
248
- this._attachButtonToElement(domTarget);
249
- // When panel is opened then refresh it position to be properly aligned with block button.
250
- if (this.panelView.isVisible) {
251
- this._showPanel();
252
- }
253
- }
254
- /**
255
- * Hides the button.
256
- */
257
- _hideButton() {
258
- this.buttonView.isVisible = false;
259
- }
260
- /**
261
- * Shows the {@link #toolbarView} attached to the {@link #buttonView}.
262
- * If the toolbar is already visible, then it simply repositions it.
263
- */
264
- _showPanel() {
265
- // Usually, the only way to show the toolbar is by pressing the block button. It makes it impossible for
266
- // the toolbar to show up when the button is invisible (feature does not make sense for the selection then).
267
- // The toolbar navigation using Alt+F10 does not access the button but shows the panel directly using this method.
268
- // So we need to check whether this is possible first.
269
- if (!this.buttonView.isVisible) {
270
- return;
271
- }
272
- const wasVisible = this.panelView.isVisible;
273
- // So here's the thing: If there was no initial panelView#show() or these two were in different order, the toolbar
274
- // positioning will break in RTL editors. Weird, right? What you show know is that the toolbar
275
- // grouping works thanks to:
276
- //
277
- // * the ResizeObserver, which kicks in as soon as the toolbar shows up in DOM (becomes visible again).
278
- // * the observable ToolbarView#maxWidth, which triggers re-grouping when changed.
279
- //
280
- // Here are the possible scenarios:
281
- //
282
- // 1. (WRONG ❌) If the #maxWidth is set when the toolbar is invisible, it won't affect item grouping (no DOMRects, no grouping).
283
- // Then, when panelView.pin() is called, the position of the toolbar will be calculated for the old
284
- // items grouping state, and when finally ResizeObserver kicks in (hey, the toolbar is visible now, right?)
285
- // it will group/ungroup some items and the length of the toolbar will change. But since in RTL the toolbar
286
- // is attached on the right side and the positioning uses CSS "left", it will result in the toolbar shifting
287
- // to the left and being displayed in the wrong place.
288
- // 2. (WRONG ❌) If the panelView.pin() is called first and #maxWidth set next, then basically the story repeats. The balloon
289
- // calculates the position for the old toolbar grouping state, then the toolbar re-groups items and because
290
- // it is positioned using CSS "left" it will move.
291
- // 3. (RIGHT ✅) We show the panel first (the toolbar does re-grouping but it does not matter), then the #maxWidth
292
- // is set allowing the toolbar to re-group again and finally panelView.pin() does the positioning when the
293
- // items grouping state is stable and final.
294
- //
295
- // https://github.com/ckeditor/ckeditor5/issues/6449, https://github.com/ckeditor/ckeditor5/issues/6575
296
- this.panelView.show();
297
- const editableElement = this._getSelectedEditableElement();
298
- this.toolbarView.maxWidth = this._getToolbarMaxWidth(editableElement);
299
- this.panelView.pin({
300
- target: this.buttonView.element,
301
- limiter: editableElement
302
- });
303
- if (!wasVisible) {
304
- this.toolbarView.items.get(0).focus();
305
- }
306
- }
307
- /**
308
- * Returns currently selected editable, based on the model selection.
309
- */
310
- _getSelectedEditableElement() {
311
- const selectedModelRootName = this.editor.model.document.selection.getFirstRange().root.rootName;
312
- return this.editor.ui.getEditableElement(selectedModelRootName);
313
- }
314
- /**
315
- * Hides the {@link #toolbarView}.
316
- *
317
- * @param focusEditable When `true`, the editable will be focused after hiding the panel.
318
- */
319
- _hidePanel(focusEditable) {
320
- this.panelView.isVisible = false;
321
- if (focusEditable) {
322
- this.editor.editing.view.focus();
323
- }
324
- }
325
- /**
326
- * Attaches the {@link #buttonView} to the target block of content.
327
- *
328
- * @param targetElement Target element.
329
- */
330
- _attachButtonToElement(targetElement) {
331
- const contentStyles = window.getComputedStyle(targetElement);
332
- const editableRect = new Rect(this._getSelectedEditableElement());
333
- const contentPaddingTop = parseInt(contentStyles.paddingTop, 10);
334
- // When line height is not an integer then treat it as "normal".
335
- // MDN says that 'normal' == ~1.2 on desktop browsers.
336
- const contentLineHeight = parseInt(contentStyles.lineHeight, 10) || parseInt(contentStyles.fontSize, 10) * 1.2;
337
- const position = getOptimalPosition({
338
- element: this.buttonView.element,
339
- target: targetElement,
340
- positions: [
341
- (contentRect, buttonRect) => {
342
- let left;
343
- if (this.editor.locale.uiLanguageDirection === 'ltr') {
344
- left = editableRect.left - buttonRect.width;
345
- }
346
- else {
347
- left = editableRect.right;
348
- }
349
- return {
350
- top: contentRect.top + contentPaddingTop + (contentLineHeight - buttonRect.height) / 2,
351
- left
352
- };
353
- }
354
- ]
355
- });
356
- this.buttonView.top = position.top;
357
- this.buttonView.left = position.left;
358
- }
359
- /**
360
- * Creates a resize observer that observes selected editable and resizes the toolbar panel accordingly.
361
- */
362
- _setupToolbarResize() {
363
- const editableElement = this._getSelectedEditableElement();
364
- // Do this only if the automatic grouping is turned on.
365
- if (!this._blockToolbarConfig.shouldNotGroupWhenFull) {
366
- // If resize observer is attached to a different editable than currently selected editable, re-attach it.
367
- if (this._resizeObserver && this._resizeObserver.element !== editableElement) {
368
- this._resizeObserver.destroy();
369
- this._resizeObserver = null;
370
- }
371
- if (!this._resizeObserver) {
372
- this._resizeObserver = new ResizeObserver(editableElement, () => {
373
- this.toolbarView.maxWidth = this._getToolbarMaxWidth(editableElement);
374
- });
375
- }
376
- }
377
- }
378
- /**
379
- * Gets the {@link #toolbarView} max-width, based on given `editableElement` width plus the distance between the farthest
380
- * edge of the {@link #buttonView} and the editable.
381
- *
382
- * @returns A maximum width that toolbar can have, in pixels.
383
- */
384
- _getToolbarMaxWidth(editableElement) {
385
- const editableRect = new Rect(editableElement);
386
- const buttonRect = new Rect(this.buttonView.element);
387
- const isRTL = this.editor.locale.uiLanguageDirection === 'rtl';
388
- const offset = isRTL ? (buttonRect.left - editableRect.right) + buttonRect.width : editableRect.left - buttonRect.left;
389
- return toPx(editableRect.width + offset);
390
- }
391
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module ui/toolbar/block/blocktoolbar
7
+ */
8
+ /* global window */
9
+ import { Plugin, icons } from '@ckeditor/ckeditor5-core';
10
+ import { Rect, ResizeObserver, getOptimalPosition, toUnit } from '@ckeditor/ckeditor5-utils';
11
+ import BlockButtonView from './blockbuttonview';
12
+ import BalloonPanelView from '../../panel/balloon/balloonpanelview';
13
+ import ToolbarView from '../toolbarview';
14
+ import clickOutsideHandler from '../../bindings/clickoutsidehandler';
15
+ import normalizeToolbarConfig from '../normalizetoolbarconfig';
16
+ const toPx = toUnit('px');
17
+ const { pilcrow } = icons;
18
+ /**
19
+ * The block toolbar plugin.
20
+ *
21
+ * This plugin provides a button positioned next to the block of content where the selection is anchored.
22
+ * Upon clicking the button, a dropdown providing access to editor features shows up, as configured in
23
+ * {@link module:core/editor/editorconfig~EditorConfig#blockToolbar}.
24
+ *
25
+ * By default, the button is displayed next to all elements marked in {@link module:engine/model/schema~Schema}
26
+ * as `$block` for which the toolbar provides at least one option.
27
+ *
28
+ * By default, the button is attached so its right boundary is touching the
29
+ * {@link module:engine/view/editableelement~EditableElement}:
30
+ *
31
+ * ```
32
+ * __ |
33
+ * | || This is a block of content that the
34
+ * ¯¯ | button is attached to. This is a
35
+ * | block of content that the button is
36
+ * | attached to.
37
+ * ```
38
+ *
39
+ * The position of the button can be adjusted using the CSS `transform` property:
40
+ *
41
+ * ```css
42
+ * .ck-block-toolbar-button {
43
+ * transform: translateX( -10px );
44
+ * }
45
+ * ```
46
+ *
47
+ * ```
48
+ * __ |
49
+ * | | | This is a block of content that the
50
+ * ¯¯ | button is attached to. This is a
51
+ * | block of content that the button is
52
+ * | attached to.
53
+ * ```
54
+ *
55
+ * **Note**: If you plan to run the editor in a right–to–left (RTL) language, keep in mind the button
56
+ * will be attached to the **right** boundary of the editable area. In that case, make sure the
57
+ * CSS position adjustment works properly by adding the following styles:
58
+ *
59
+ * ```css
60
+ * .ck[dir="rtl"] .ck-block-toolbar-button {
61
+ * transform: translateX( 10px );
62
+ * }
63
+ * ```
64
+ */
65
+ export default class BlockToolbar extends Plugin {
66
+ /**
67
+ * @inheritDoc
68
+ */
69
+ static get pluginName() {
70
+ return 'BlockToolbar';
71
+ }
72
+ /**
73
+ * @inheritDoc
74
+ */
75
+ constructor(editor) {
76
+ super(editor);
77
+ /**
78
+ * An instance of the resize observer that allows to respond to changes in editable's geometry
79
+ * so the toolbar can stay within its boundaries (and group toolbar items that do not fit).
80
+ *
81
+ * **Note**: Used only when `shouldNotGroupWhenFull` was **not** set in the
82
+ * {@link module:core/editor/editorconfig~EditorConfig#blockToolbar configuration}.
83
+ *
84
+ * **Note:** Created in {@link #afterInit}.
85
+ */
86
+ this._resizeObserver = null;
87
+ this._blockToolbarConfig = normalizeToolbarConfig(this.editor.config.get('blockToolbar'));
88
+ this.toolbarView = this._createToolbarView();
89
+ this.panelView = this._createPanelView();
90
+ this.buttonView = this._createButtonView();
91
+ // Close the #panelView upon clicking outside of the plugin UI.
92
+ clickOutsideHandler({
93
+ emitter: this.panelView,
94
+ contextElements: [this.panelView.element, this.buttonView.element],
95
+ activator: () => this.panelView.isVisible,
96
+ callback: () => this._hidePanel()
97
+ });
98
+ }
99
+ /**
100
+ * @inheritDoc
101
+ */
102
+ init() {
103
+ const editor = this.editor;
104
+ // Hides panel on a direct selection change.
105
+ this.listenTo(editor.model.document.selection, 'change:range', (evt, data) => {
106
+ if (data.directChange) {
107
+ this._hidePanel();
108
+ }
109
+ });
110
+ this.listenTo(editor.ui, 'update', () => this._updateButton());
111
+ // `low` priority is used because of https://github.com/ckeditor/ckeditor5-core/issues/133.
112
+ this.listenTo(editor, 'change:isReadOnly', () => this._updateButton(), { priority: 'low' });
113
+ this.listenTo(editor.ui.focusTracker, 'change:isFocused', () => this._updateButton());
114
+ // Reposition button on resize.
115
+ this.listenTo(this.buttonView, 'change:isVisible', (evt, name, isVisible) => {
116
+ if (isVisible) {
117
+ // Keep correct position of button and panel on window#resize.
118
+ this.buttonView.listenTo(window, 'resize', () => this._updateButton());
119
+ }
120
+ else {
121
+ // Stop repositioning button when is hidden.
122
+ this.buttonView.stopListening(window, 'resize');
123
+ // Hide the panel when the button disappears.
124
+ this._hidePanel();
125
+ }
126
+ });
127
+ // Register the toolbar so it becomes available for Alt+F10 and Esc navigation.
128
+ editor.ui.addToolbar(this.toolbarView, {
129
+ beforeFocus: () => this._showPanel(),
130
+ afterBlur: () => this._hidePanel()
131
+ });
132
+ }
133
+ /**
134
+ * Fills the toolbar with its items based on the configuration.
135
+ *
136
+ * **Note:** This needs to be done after all plugins are ready.
137
+ */
138
+ afterInit() {
139
+ this.toolbarView.fillFromConfig(this._blockToolbarConfig, this.editor.ui.componentFactory);
140
+ // Hide panel before executing each button in the panel.
141
+ for (const item of this.toolbarView.items) {
142
+ item.on('execute', () => this._hidePanel(true), { priority: 'high' });
143
+ }
144
+ }
145
+ /**
146
+ * @inheritDoc
147
+ */
148
+ destroy() {
149
+ super.destroy();
150
+ // Destroy created UI components as they are not automatically destroyed (see ckeditor5#1341).
151
+ this.panelView.destroy();
152
+ this.buttonView.destroy();
153
+ this.toolbarView.destroy();
154
+ if (this._resizeObserver) {
155
+ this._resizeObserver.destroy();
156
+ }
157
+ }
158
+ /**
159
+ * Creates the {@link #toolbarView}.
160
+ */
161
+ _createToolbarView() {
162
+ const t = this.editor.locale.t;
163
+ const shouldGroupWhenFull = !this._blockToolbarConfig.shouldNotGroupWhenFull;
164
+ const toolbarView = new ToolbarView(this.editor.locale, {
165
+ shouldGroupWhenFull,
166
+ isFloating: true
167
+ });
168
+ toolbarView.ariaLabel = t('Editor block content toolbar');
169
+ return toolbarView;
170
+ }
171
+ /**
172
+ * Creates the {@link #panelView}.
173
+ */
174
+ _createPanelView() {
175
+ const editor = this.editor;
176
+ const panelView = new BalloonPanelView(editor.locale);
177
+ panelView.content.add(this.toolbarView);
178
+ panelView.class = 'ck-toolbar-container';
179
+ editor.ui.view.body.add(panelView);
180
+ editor.ui.focusTracker.add(panelView.element);
181
+ // Close #panelView on `Esc` press.
182
+ this.toolbarView.keystrokes.set('Esc', (evt, cancel) => {
183
+ this._hidePanel(true);
184
+ cancel();
185
+ });
186
+ return panelView;
187
+ }
188
+ /**
189
+ * Creates the {@link #buttonView}.
190
+ */
191
+ _createButtonView() {
192
+ const editor = this.editor;
193
+ const t = editor.t;
194
+ const buttonView = new BlockButtonView(editor.locale);
195
+ buttonView.set({
196
+ label: t('Edit block'),
197
+ icon: pilcrow,
198
+ withText: false
199
+ });
200
+ // Bind the panelView observable properties to the buttonView.
201
+ buttonView.bind('isOn').to(this.panelView, 'isVisible');
202
+ buttonView.bind('tooltip').to(this.panelView, 'isVisible', isVisible => !isVisible);
203
+ // Toggle the panelView upon buttonView#execute.
204
+ this.listenTo(buttonView, 'execute', () => {
205
+ if (!this.panelView.isVisible) {
206
+ this._showPanel();
207
+ }
208
+ else {
209
+ this._hidePanel(true);
210
+ }
211
+ });
212
+ editor.ui.view.body.add(buttonView);
213
+ editor.ui.focusTracker.add(buttonView.element);
214
+ return buttonView;
215
+ }
216
+ /**
217
+ * Shows or hides the button.
218
+ * When all the conditions for displaying the button are matched, it shows the button. Hides otherwise.
219
+ */
220
+ _updateButton() {
221
+ const editor = this.editor;
222
+ const model = editor.model;
223
+ const view = editor.editing.view;
224
+ // Hides the button when the editor is not focused.
225
+ if (!editor.ui.focusTracker.isFocused) {
226
+ this._hideButton();
227
+ return;
228
+ }
229
+ // Hides the button when the selection is in non-editable place.
230
+ if (!editor.model.canEditAt(editor.model.document.selection)) {
231
+ this._hideButton();
232
+ return;
233
+ }
234
+ // Get the first selected block, button will be attached to this element.
235
+ const modelTarget = Array.from(model.document.selection.getSelectedBlocks())[0];
236
+ // Hides the button when there is no enabled item in toolbar for the current block element.
237
+ if (!modelTarget || Array.from(this.toolbarView.items).every((item) => !item.isEnabled)) {
238
+ this._hideButton();
239
+ return;
240
+ }
241
+ // Get DOM target element.
242
+ const domTarget = view.domConverter.mapViewToDom(editor.editing.mapper.toViewElement(modelTarget));
243
+ // Show block button.
244
+ this.buttonView.isVisible = true;
245
+ // Make sure that the block toolbar panel is resized properly.
246
+ this._setupToolbarResize();
247
+ // Attach block button to target DOM element.
248
+ this._attachButtonToElement(domTarget);
249
+ // When panel is opened then refresh it position to be properly aligned with block button.
250
+ if (this.panelView.isVisible) {
251
+ this._showPanel();
252
+ }
253
+ }
254
+ /**
255
+ * Hides the button.
256
+ */
257
+ _hideButton() {
258
+ this.buttonView.isVisible = false;
259
+ }
260
+ /**
261
+ * Shows the {@link #toolbarView} attached to the {@link #buttonView}.
262
+ * If the toolbar is already visible, then it simply repositions it.
263
+ */
264
+ _showPanel() {
265
+ // Usually, the only way to show the toolbar is by pressing the block button. It makes it impossible for
266
+ // the toolbar to show up when the button is invisible (feature does not make sense for the selection then).
267
+ // The toolbar navigation using Alt+F10 does not access the button but shows the panel directly using this method.
268
+ // So we need to check whether this is possible first.
269
+ if (!this.buttonView.isVisible) {
270
+ return;
271
+ }
272
+ const wasVisible = this.panelView.isVisible;
273
+ // So here's the thing: If there was no initial panelView#show() or these two were in different order, the toolbar
274
+ // positioning will break in RTL editors. Weird, right? What you show know is that the toolbar
275
+ // grouping works thanks to:
276
+ //
277
+ // * the ResizeObserver, which kicks in as soon as the toolbar shows up in DOM (becomes visible again).
278
+ // * the observable ToolbarView#maxWidth, which triggers re-grouping when changed.
279
+ //
280
+ // Here are the possible scenarios:
281
+ //
282
+ // 1. (WRONG ❌) If the #maxWidth is set when the toolbar is invisible, it won't affect item grouping (no DOMRects, no grouping).
283
+ // Then, when panelView.pin() is called, the position of the toolbar will be calculated for the old
284
+ // items grouping state, and when finally ResizeObserver kicks in (hey, the toolbar is visible now, right?)
285
+ // it will group/ungroup some items and the length of the toolbar will change. But since in RTL the toolbar
286
+ // is attached on the right side and the positioning uses CSS "left", it will result in the toolbar shifting
287
+ // to the left and being displayed in the wrong place.
288
+ // 2. (WRONG ❌) If the panelView.pin() is called first and #maxWidth set next, then basically the story repeats. The balloon
289
+ // calculates the position for the old toolbar grouping state, then the toolbar re-groups items and because
290
+ // it is positioned using CSS "left" it will move.
291
+ // 3. (RIGHT ✅) We show the panel first (the toolbar does re-grouping but it does not matter), then the #maxWidth
292
+ // is set allowing the toolbar to re-group again and finally panelView.pin() does the positioning when the
293
+ // items grouping state is stable and final.
294
+ //
295
+ // https://github.com/ckeditor/ckeditor5/issues/6449, https://github.com/ckeditor/ckeditor5/issues/6575
296
+ this.panelView.show();
297
+ const editableElement = this._getSelectedEditableElement();
298
+ this.toolbarView.maxWidth = this._getToolbarMaxWidth(editableElement);
299
+ this.panelView.pin({
300
+ target: this.buttonView.element,
301
+ limiter: editableElement
302
+ });
303
+ if (!wasVisible) {
304
+ this.toolbarView.items.get(0).focus();
305
+ }
306
+ }
307
+ /**
308
+ * Returns currently selected editable, based on the model selection.
309
+ */
310
+ _getSelectedEditableElement() {
311
+ const selectedModelRootName = this.editor.model.document.selection.getFirstRange().root.rootName;
312
+ return this.editor.ui.getEditableElement(selectedModelRootName);
313
+ }
314
+ /**
315
+ * Hides the {@link #toolbarView}.
316
+ *
317
+ * @param focusEditable When `true`, the editable will be focused after hiding the panel.
318
+ */
319
+ _hidePanel(focusEditable) {
320
+ this.panelView.isVisible = false;
321
+ if (focusEditable) {
322
+ this.editor.editing.view.focus();
323
+ }
324
+ }
325
+ /**
326
+ * Attaches the {@link #buttonView} to the target block of content.
327
+ *
328
+ * @param targetElement Target element.
329
+ */
330
+ _attachButtonToElement(targetElement) {
331
+ const contentStyles = window.getComputedStyle(targetElement);
332
+ const editableRect = new Rect(this._getSelectedEditableElement());
333
+ const contentPaddingTop = parseInt(contentStyles.paddingTop, 10);
334
+ // When line height is not an integer then treat it as "normal".
335
+ // MDN says that 'normal' == ~1.2 on desktop browsers.
336
+ const contentLineHeight = parseInt(contentStyles.lineHeight, 10) || parseInt(contentStyles.fontSize, 10) * 1.2;
337
+ const position = getOptimalPosition({
338
+ element: this.buttonView.element,
339
+ target: targetElement,
340
+ positions: [
341
+ (contentRect, buttonRect) => {
342
+ let left;
343
+ if (this.editor.locale.uiLanguageDirection === 'ltr') {
344
+ left = editableRect.left - buttonRect.width;
345
+ }
346
+ else {
347
+ left = editableRect.right;
348
+ }
349
+ return {
350
+ top: contentRect.top + contentPaddingTop + (contentLineHeight - buttonRect.height) / 2,
351
+ left
352
+ };
353
+ }
354
+ ]
355
+ });
356
+ this.buttonView.top = position.top;
357
+ this.buttonView.left = position.left;
358
+ }
359
+ /**
360
+ * Creates a resize observer that observes selected editable and resizes the toolbar panel accordingly.
361
+ */
362
+ _setupToolbarResize() {
363
+ const editableElement = this._getSelectedEditableElement();
364
+ // Do this only if the automatic grouping is turned on.
365
+ if (!this._blockToolbarConfig.shouldNotGroupWhenFull) {
366
+ // If resize observer is attached to a different editable than currently selected editable, re-attach it.
367
+ if (this._resizeObserver && this._resizeObserver.element !== editableElement) {
368
+ this._resizeObserver.destroy();
369
+ this._resizeObserver = null;
370
+ }
371
+ if (!this._resizeObserver) {
372
+ this._resizeObserver = new ResizeObserver(editableElement, () => {
373
+ this.toolbarView.maxWidth = this._getToolbarMaxWidth(editableElement);
374
+ });
375
+ }
376
+ }
377
+ }
378
+ /**
379
+ * Gets the {@link #toolbarView} max-width, based on given `editableElement` width plus the distance between the farthest
380
+ * edge of the {@link #buttonView} and the editable.
381
+ *
382
+ * @returns A maximum width that toolbar can have, in pixels.
383
+ */
384
+ _getToolbarMaxWidth(editableElement) {
385
+ const editableRect = new Rect(editableElement);
386
+ const buttonRect = new Rect(this.buttonView.element);
387
+ const isRTL = this.editor.locale.uiLanguageDirection === 'rtl';
388
+ const offset = isRTL ? (buttonRect.left - editableRect.right) + buttonRect.width : editableRect.left - buttonRect.left;
389
+ return toPx(editableRect.width + offset);
390
+ }
391
+ }