@ckeditor/ckeditor5-ui 40.0.0 → 40.1.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 (165) hide show
  1. package/LICENSE.md +3 -3
  2. package/lang/translations/pt-br.po +1 -1
  3. package/lang/translations/ug.po +26 -26
  4. package/package.json +3 -3
  5. package/src/arialiveannouncer.d.ts +94 -0
  6. package/src/arialiveannouncer.js +113 -0
  7. package/src/augmentation.d.ts +86 -86
  8. package/src/augmentation.js +5 -5
  9. package/src/autocomplete/autocompleteview.d.ts +81 -81
  10. package/src/autocomplete/autocompleteview.js +153 -146
  11. package/src/bindings/addkeyboardhandlingforgrid.d.ts +27 -27
  12. package/src/bindings/addkeyboardhandlingforgrid.js +107 -107
  13. package/src/bindings/clickoutsidehandler.d.ts +28 -28
  14. package/src/bindings/clickoutsidehandler.js +36 -36
  15. package/src/bindings/csstransitiondisablermixin.d.ts +40 -40
  16. package/src/bindings/csstransitiondisablermixin.js +55 -55
  17. package/src/bindings/injectcsstransitiondisabler.d.ts +59 -59
  18. package/src/bindings/injectcsstransitiondisabler.js +71 -71
  19. package/src/bindings/preventdefault.d.ts +33 -33
  20. package/src/bindings/preventdefault.js +34 -34
  21. package/src/bindings/submithandler.d.ts +57 -57
  22. package/src/bindings/submithandler.js +47 -47
  23. package/src/button/button.d.ts +172 -178
  24. package/src/button/button.js +5 -5
  25. package/src/button/buttonlabel.d.ts +34 -34
  26. package/src/button/buttonlabel.js +5 -5
  27. package/src/button/buttonlabelview.d.ts +31 -31
  28. package/src/button/buttonlabelview.js +42 -42
  29. package/src/button/buttonview.d.ts +181 -185
  30. package/src/button/buttonview.js +217 -219
  31. package/src/button/switchbuttonview.d.ts +45 -45
  32. package/src/button/switchbuttonview.js +75 -75
  33. package/src/colorgrid/colorgridview.d.ts +132 -132
  34. package/src/colorgrid/colorgridview.js +124 -124
  35. package/src/colorgrid/colortileview.d.ts +28 -28
  36. package/src/colorgrid/colortileview.js +40 -40
  37. package/src/colorgrid/utils.d.ts +47 -47
  38. package/src/colorgrid/utils.js +84 -84
  39. package/src/colorpicker/colorpickerview.d.ts +137 -137
  40. package/src/colorpicker/colorpickerview.js +270 -270
  41. package/src/colorpicker/utils.d.ts +43 -43
  42. package/src/colorpicker/utils.js +99 -99
  43. package/src/colorselector/colorgridsfragmentview.d.ts +194 -194
  44. package/src/colorselector/colorgridsfragmentview.js +289 -289
  45. package/src/colorselector/colorpickerfragmentview.d.ts +128 -128
  46. package/src/colorselector/colorpickerfragmentview.js +205 -205
  47. package/src/colorselector/colorselectorview.d.ts +242 -242
  48. package/src/colorselector/colorselectorview.js +256 -256
  49. package/src/colorselector/documentcolorcollection.d.ts +70 -70
  50. package/src/colorselector/documentcolorcollection.js +42 -42
  51. package/src/componentfactory.d.ts +81 -81
  52. package/src/componentfactory.js +104 -104
  53. package/src/dropdown/button/dropdownbutton.d.ts +25 -25
  54. package/src/dropdown/button/dropdownbutton.js +5 -5
  55. package/src/dropdown/button/dropdownbuttonview.d.ts +48 -48
  56. package/src/dropdown/button/dropdownbuttonview.js +66 -66
  57. package/src/dropdown/button/splitbuttonview.d.ts +161 -161
  58. package/src/dropdown/button/splitbuttonview.js +152 -152
  59. package/src/dropdown/dropdownpanelfocusable.d.ts +21 -21
  60. package/src/dropdown/dropdownpanelfocusable.js +5 -5
  61. package/src/dropdown/dropdownpanelview.d.ts +62 -62
  62. package/src/dropdown/dropdownpanelview.js +97 -97
  63. package/src/dropdown/dropdownview.d.ts +315 -315
  64. package/src/dropdown/dropdownview.js +379 -379
  65. package/src/dropdown/utils.d.ts +235 -235
  66. package/src/dropdown/utils.js +463 -458
  67. package/src/editableui/editableuiview.d.ts +72 -72
  68. package/src/editableui/editableuiview.js +112 -112
  69. package/src/editableui/inline/inlineeditableuiview.d.ts +40 -40
  70. package/src/editableui/inline/inlineeditableuiview.js +48 -48
  71. package/src/editorui/bodycollection.d.ts +55 -55
  72. package/src/editorui/bodycollection.js +84 -84
  73. package/src/editorui/boxed/boxededitoruiview.d.ts +40 -40
  74. package/src/editorui/boxed/boxededitoruiview.js +81 -81
  75. package/src/editorui/editorui.d.ts +288 -282
  76. package/src/editorui/editorui.js +412 -410
  77. package/src/editorui/editoruiview.d.ts +39 -39
  78. package/src/editorui/editoruiview.js +38 -38
  79. package/src/editorui/poweredby.d.ts +71 -71
  80. package/src/editorui/poweredby.js +276 -276
  81. package/src/focuscycler.d.ts +226 -226
  82. package/src/focuscycler.js +245 -245
  83. package/src/formheader/formheaderview.d.ts +59 -59
  84. package/src/formheader/formheaderview.js +69 -69
  85. package/src/highlightedtext/highlightedtextview.d.ts +38 -38
  86. package/src/highlightedtext/highlightedtextview.js +102 -102
  87. package/src/icon/iconview.d.ts +85 -85
  88. package/src/icon/iconview.js +114 -114
  89. package/src/iframe/iframeview.d.ts +50 -50
  90. package/src/iframe/iframeview.js +63 -63
  91. package/src/index.d.ts +73 -73
  92. package/src/index.js +70 -70
  93. package/src/input/inputbase.d.ts +107 -107
  94. package/src/input/inputbase.js +110 -110
  95. package/src/input/inputview.d.ts +36 -36
  96. package/src/input/inputview.js +24 -24
  97. package/src/inputnumber/inputnumberview.d.ts +49 -49
  98. package/src/inputnumber/inputnumberview.js +40 -40
  99. package/src/inputtext/inputtextview.d.ts +18 -18
  100. package/src/inputtext/inputtextview.js +27 -27
  101. package/src/label/labelview.d.ts +36 -36
  102. package/src/label/labelview.js +41 -41
  103. package/src/labeledfield/labeledfieldview.d.ts +187 -187
  104. package/src/labeledfield/labeledfieldview.js +157 -157
  105. package/src/labeledfield/utils.d.ts +123 -123
  106. package/src/labeledfield/utils.js +176 -176
  107. package/src/labeledinput/labeledinputview.d.ts +125 -125
  108. package/src/labeledinput/labeledinputview.js +125 -125
  109. package/src/list/listitemgroupview.d.ts +59 -51
  110. package/src/list/listitemgroupview.js +63 -75
  111. package/src/list/listitemview.d.ts +36 -36
  112. package/src/list/listitemview.js +42 -42
  113. package/src/list/listseparatorview.d.ts +18 -18
  114. package/src/list/listseparatorview.js +28 -28
  115. package/src/list/listview.d.ts +122 -122
  116. package/src/list/listview.js +187 -187
  117. package/src/model.d.ts +22 -22
  118. package/src/model.js +31 -31
  119. package/src/notification/notification.d.ts +211 -211
  120. package/src/notification/notification.js +187 -187
  121. package/src/panel/balloon/balloonpanelview.d.ts +685 -685
  122. package/src/panel/balloon/balloonpanelview.js +1010 -1010
  123. package/src/panel/balloon/contextualballoon.d.ts +299 -299
  124. package/src/panel/balloon/contextualballoon.js +572 -572
  125. package/src/panel/sticky/stickypanelview.d.ts +156 -156
  126. package/src/panel/sticky/stickypanelview.js +234 -234
  127. package/src/search/filteredview.d.ts +31 -31
  128. package/src/search/filteredview.js +5 -5
  129. package/src/search/searchinfoview.d.ts +45 -45
  130. package/src/search/searchinfoview.js +59 -59
  131. package/src/search/searchresultsview.d.ts +54 -54
  132. package/src/search/searchresultsview.js +65 -65
  133. package/src/search/text/searchtextqueryview.d.ts +76 -76
  134. package/src/search/text/searchtextqueryview.js +75 -75
  135. package/src/search/text/searchtextview.d.ts +219 -219
  136. package/src/search/text/searchtextview.js +201 -201
  137. package/src/spinner/spinnerview.d.ts +25 -25
  138. package/src/spinner/spinnerview.js +38 -38
  139. package/src/template.d.ts +942 -942
  140. package/src/template.js +1294 -1294
  141. package/src/textarea/textareaview.d.ts +88 -88
  142. package/src/textarea/textareaview.js +142 -140
  143. package/src/toolbar/balloon/balloontoolbar.d.ts +122 -122
  144. package/src/toolbar/balloon/balloontoolbar.js +300 -300
  145. package/src/toolbar/block/blockbuttonview.d.ts +35 -35
  146. package/src/toolbar/block/blockbuttonview.js +41 -41
  147. package/src/toolbar/block/blocktoolbar.d.ts +161 -161
  148. package/src/toolbar/block/blocktoolbar.js +395 -395
  149. package/src/toolbar/normalizetoolbarconfig.d.ts +40 -40
  150. package/src/toolbar/normalizetoolbarconfig.js +52 -51
  151. package/src/toolbar/toolbarlinebreakview.d.ts +18 -18
  152. package/src/toolbar/toolbarlinebreakview.js +28 -28
  153. package/src/toolbar/toolbarseparatorview.d.ts +18 -18
  154. package/src/toolbar/toolbarseparatorview.js +28 -28
  155. package/src/toolbar/toolbarview.d.ts +266 -266
  156. package/src/toolbar/toolbarview.js +719 -719
  157. package/src/tooltipmanager.d.ts +180 -180
  158. package/src/tooltipmanager.js +353 -353
  159. package/src/view.d.ts +422 -422
  160. package/src/view.js +396 -396
  161. package/src/viewcollection.d.ts +139 -139
  162. package/src/viewcollection.js +206 -206
  163. package/theme/components/arialiveannouncer/arialiveannouncer.css +10 -0
  164. package/theme/components/button/button.css +9 -1
  165. package/theme/components/formheader/formheader.css +0 -4
@@ -1,410 +1,412 @@
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/editorui/editorui
7
- */
8
- /* globals console */
9
- import ComponentFactory from '../componentfactory';
10
- import TooltipManager from '../tooltipmanager';
11
- import PoweredBy from './poweredby';
12
- import { ObservableMixin, isVisible, FocusTracker } from '@ckeditor/ckeditor5-utils';
13
- /**
14
- * A class providing the minimal interface that is required to successfully bootstrap any editor UI.
15
- */
16
- export default class EditorUI extends ObservableMixin() {
17
- /**
18
- * Creates an instance of the editor UI class.
19
- *
20
- * @param editor The editor instance.
21
- */
22
- constructor(editor) {
23
- super();
24
- /**
25
- * Indicates the UI is ready. Set `true` after {@link #event:ready} event is fired.
26
- *
27
- * @readonly
28
- * @default false
29
- */
30
- this.isReady = false;
31
- /**
32
- * Stores all editable elements used by the editor instance.
33
- */
34
- this._editableElementsMap = new Map();
35
- /**
36
- * All available & focusable toolbars.
37
- */
38
- this._focusableToolbarDefinitions = [];
39
- const editingView = editor.editing.view;
40
- this.editor = editor;
41
- this.componentFactory = new ComponentFactory(editor);
42
- this.focusTracker = new FocusTracker();
43
- this.tooltipManager = new TooltipManager(editor);
44
- this.poweredBy = new PoweredBy(editor);
45
- this.set('viewportOffset', this._readViewportOffsetFromConfig());
46
- this.once('ready', () => {
47
- this.isReady = true;
48
- });
49
- // Informs UI components that should be refreshed after layout change.
50
- this.listenTo(editingView.document, 'layoutChanged', this.update.bind(this));
51
- this.listenTo(editingView, 'scrollToTheSelection', this._handleScrollToTheSelection.bind(this));
52
- this._initFocusTracking();
53
- }
54
- /**
55
- * The main (outermost) DOM element of the editor UI.
56
- *
57
- * For example, in {@link module:editor-classic/classiceditor~ClassicEditor} it is a `<div>` which
58
- * wraps the editable element and the toolbar. In {@link module:editor-inline/inlineeditor~InlineEditor}
59
- * it is the editable element itself (as there is no other wrapper). However, in
60
- * {@link module:editor-decoupled/decouplededitor~DecoupledEditor} it is set to `null` because this editor does not
61
- * come with a single "main" HTML element (its editable element and toolbar are separate).
62
- *
63
- * This property can be understood as a shorthand for retrieving the element that a specific editor integration
64
- * considers to be its main DOM element.
65
- */
66
- get element() {
67
- return null;
68
- }
69
- /**
70
- * Fires the {@link module:ui/editorui/editorui~EditorUI#event:update `update`} event.
71
- *
72
- * This method should be called when the editor UI (e.g. positions of its balloons) needs to be updated due to
73
- * some environmental change which CKEditor 5 is not aware of (e.g. resize of a container in which it is used).
74
- */
75
- update() {
76
- this.fire('update');
77
- }
78
- /**
79
- * Destroys the UI.
80
- */
81
- destroy() {
82
- this.stopListening();
83
- this.focusTracker.destroy();
84
- this.tooltipManager.destroy(this.editor);
85
- this.poweredBy.destroy();
86
- // Clean–up the references to the CKEditor instance stored in the native editable DOM elements.
87
- for (const domElement of this._editableElementsMap.values()) {
88
- domElement.ckeditorInstance = null;
89
- this.editor.keystrokes.stopListening(domElement);
90
- }
91
- this._editableElementsMap = new Map();
92
- this._focusableToolbarDefinitions = [];
93
- }
94
- /**
95
- * Stores the native DOM editable element used by the editor under a unique name.
96
- *
97
- * Also, registers the element in the editor to maintain the accessibility of the UI. When the user is editing text in a focusable
98
- * editable area, they can use the <kbd>Alt</kbd> + <kbd>F10</kbd> keystroke to navigate over editor toolbars. See {@link #addToolbar}.
99
- *
100
- * @param rootName The unique name of the editable element.
101
- * @param domElement The native DOM editable element.
102
- */
103
- setEditableElement(rootName, domElement) {
104
- this._editableElementsMap.set(rootName, domElement);
105
- // Put a reference to the CKEditor instance in the editable native DOM element.
106
- // It helps 3rd–party software (browser extensions, other libraries) access and recognize
107
- // CKEditor 5 instances (editing roots) and use their API (there is no global editor
108
- // instance registry).
109
- if (!domElement.ckeditorInstance) {
110
- domElement.ckeditorInstance = this.editor;
111
- }
112
- // Register the element, so it becomes available for Alt+F10 and Esc navigation.
113
- this.focusTracker.add(domElement);
114
- const setUpKeystrokeHandler = () => {
115
- // The editing view of the editor is already listening to keystrokes from DOM roots (see: KeyObserver).
116
- // Do not duplicate listeners.
117
- if (this.editor.editing.view.getDomRoot(rootName)) {
118
- return;
119
- }
120
- this.editor.keystrokes.listenTo(domElement);
121
- };
122
- // For editable elements set by features after EditorUI is ready (e.g. source editing).
123
- if (this.isReady) {
124
- setUpKeystrokeHandler();
125
- }
126
- // For editable elements set while the editor is being created (e.g. DOM roots).
127
- else {
128
- this.once('ready', setUpKeystrokeHandler);
129
- }
130
- }
131
- /**
132
- * Removes the editable from the editor UI. Removes all handlers added by {@link #setEditableElement}.
133
- *
134
- * @param rootName The name of the editable element to remove.
135
- */
136
- removeEditableElement(rootName) {
137
- const domElement = this._editableElementsMap.get(rootName);
138
- if (!domElement) {
139
- return;
140
- }
141
- this._editableElementsMap.delete(rootName);
142
- this.editor.keystrokes.stopListening(domElement);
143
- this.focusTracker.remove(domElement);
144
- domElement.ckeditorInstance = null;
145
- }
146
- /**
147
- * Returns the editable editor element with the given name or null if editable does not exist.
148
- *
149
- * @param rootName The editable name.
150
- */
151
- getEditableElement(rootName = 'main') {
152
- return this._editableElementsMap.get(rootName);
153
- }
154
- /**
155
- * Returns array of names of all editor editable elements.
156
- */
157
- getEditableElementsNames() {
158
- return this._editableElementsMap.keys();
159
- }
160
- /**
161
- * Adds a toolbar to the editor UI. Used primarily to maintain the accessibility of the UI.
162
- *
163
- * Focusable toolbars can be accessed (focused) by users by pressing the <kbd>Alt</kbd> + <kbd>F10</kbd> keystroke.
164
- * Successive keystroke presses navigate over available toolbars.
165
- *
166
- * @param toolbarView A instance of the toolbar to be registered.
167
- */
168
- addToolbar(toolbarView, options = {}) {
169
- if (toolbarView.isRendered) {
170
- this.focusTracker.add(toolbarView.element);
171
- this.editor.keystrokes.listenTo(toolbarView.element);
172
- }
173
- else {
174
- toolbarView.once('render', () => {
175
- this.focusTracker.add(toolbarView.element);
176
- this.editor.keystrokes.listenTo(toolbarView.element);
177
- });
178
- }
179
- this._focusableToolbarDefinitions.push({ toolbarView, options });
180
- }
181
- /**
182
- * Stores all editable elements used by the editor instance.
183
- *
184
- * @deprecated
185
- */
186
- get _editableElements() {
187
- /**
188
- * The {@link module:ui/editorui/editorui~EditorUI#_editableElements `EditorUI#_editableElements`} property has been
189
- * deprecated and will be removed in the near future. Please use
190
- * {@link module:ui/editorui/editorui~EditorUI#setEditableElement `setEditableElement()`} and
191
- * {@link module:ui/editorui/editorui~EditorUI#getEditableElement `getEditableElement()`} methods instead.
192
- *
193
- * @error editor-ui-deprecated-editable-elements
194
- * @param editorUI Editor UI instance the deprecated property belongs to.
195
- */
196
- console.warn('editor-ui-deprecated-editable-elements: ' +
197
- 'The EditorUI#_editableElements property has been deprecated and will be removed in the near future.', { editorUI: this });
198
- return this._editableElementsMap;
199
- }
200
- /**
201
- * Returns viewport offsets object:
202
- *
203
- * ```js
204
- * {
205
- * top: Number,
206
- * right: Number,
207
- * bottom: Number,
208
- * left: Number
209
- * }
210
- * ```
211
- *
212
- * Only top property is currently supported.
213
- */
214
- _readViewportOffsetFromConfig() {
215
- const editor = this.editor;
216
- const viewportOffsetConfig = editor.config.get('ui.viewportOffset');
217
- if (viewportOffsetConfig) {
218
- return viewportOffsetConfig;
219
- }
220
- // Not present in EditorConfig type, because it's legacy. Hence the `as` expression.
221
- const legacyOffsetConfig = editor.config.get('toolbar.viewportTopOffset');
222
- // Fall back to deprecated toolbar config.
223
- if (legacyOffsetConfig) {
224
- /**
225
- * The {@link module:core/editor/editorconfig~EditorConfig#toolbar `EditorConfig#toolbar.viewportTopOffset`}
226
- * property has been deprecated and will be removed in the near future. Please use
227
- * {@link module:core/editor/editorconfig~EditorConfig#ui `EditorConfig#ui.viewportOffset`} instead.
228
- *
229
- * @error editor-ui-deprecated-viewport-offset-config
230
- */
231
- console.warn('editor-ui-deprecated-viewport-offset-config: ' +
232
- 'The `toolbar.vieportTopOffset` configuration option is deprecated. ' +
233
- 'It will be removed from future CKEditor versions. Use `ui.viewportOffset.top` instead.');
234
- return { top: legacyOffsetConfig };
235
- }
236
- // More keys to come in the future.
237
- return { top: 0 };
238
- }
239
- /**
240
- * Starts listening for <kbd>Alt</kbd> + <kbd>F10</kbd> and <kbd>Esc</kbd> keystrokes in the context of focusable
241
- * {@link #setEditableElement editable elements} and {@link #addToolbar toolbars}
242
- * to allow users navigate across the UI.
243
- */
244
- _initFocusTracking() {
245
- const editor = this.editor;
246
- const editingView = editor.editing.view;
247
- let lastFocusedForeignElement;
248
- let candidateDefinitions;
249
- // Focus the next focusable toolbar on <kbd>Alt</kbd> + <kbd>F10</kbd>.
250
- editor.keystrokes.set('Alt+F10', (data, cancel) => {
251
- const focusedElement = this.focusTracker.focusedElement;
252
- // Focus moved out of a DOM element that
253
- // * is not a toolbar,
254
- // * does not belong to the editing view (e.g. source editing).
255
- if (Array.from(this._editableElementsMap.values()).includes(focusedElement) &&
256
- !Array.from(editingView.domRoots.values()).includes(focusedElement)) {
257
- lastFocusedForeignElement = focusedElement;
258
- }
259
- const currentFocusedToolbarDefinition = this._getCurrentFocusedToolbarDefinition();
260
- // * When focusing a toolbar for the first time, set the array of definitions for successive presses of Alt+F10.
261
- // This ensures, the navigation works always the same and no pair of toolbars takes over
262
- // (e.g. image and table toolbars when a selected image is inside a cell).
263
- // * It could be that the focus went to the toolbar by clicking a toolbar item (e.g. a dropdown). In this case,
264
- // there were no candidates so they must be obtained (#12339).
265
- if (!currentFocusedToolbarDefinition || !candidateDefinitions) {
266
- candidateDefinitions = this._getFocusableCandidateToolbarDefinitions();
267
- }
268
- // In a single Alt+F10 press, check all candidates but if none were focused, don't go any further.
269
- // This prevents an infinite loop.
270
- for (let i = 0; i < candidateDefinitions.length; i++) {
271
- const candidateDefinition = candidateDefinitions.shift();
272
- // Put the first definition to the back of the array. This allows circular navigation over all toolbars
273
- // on successive presses of Alt+F10.
274
- candidateDefinitions.push(candidateDefinition);
275
- // Don't focus the same toolbar again. If you did, this would move focus from the nth focused toolbar item back to the
276
- // first item as per ToolbarView#focus() if the user navigated inside the toolbar.
277
- if (candidateDefinition !== currentFocusedToolbarDefinition &&
278
- this._focusFocusableCandidateToolbar(candidateDefinition)) {
279
- // Clean up after a current visible toolbar when switching to the next one.
280
- if (currentFocusedToolbarDefinition && currentFocusedToolbarDefinition.options.afterBlur) {
281
- currentFocusedToolbarDefinition.options.afterBlur();
282
- }
283
- break;
284
- }
285
- }
286
- cancel();
287
- });
288
- // Blur the focused toolbar on <kbd>Esc</kbd> and bring the focus back to its origin.
289
- editor.keystrokes.set('Esc', (data, cancel) => {
290
- const focusedToolbarDef = this._getCurrentFocusedToolbarDefinition();
291
- if (!focusedToolbarDef) {
292
- return;
293
- }
294
- // Bring focus back to where it came from before focusing the toolbar:
295
- // 1. If it came from outside the engine view (e.g. source editing), move it there.
296
- if (lastFocusedForeignElement) {
297
- lastFocusedForeignElement.focus();
298
- lastFocusedForeignElement = null;
299
- }
300
- // 2. There are two possibilities left:
301
- // 2.1. It could be that the focus went from an editable element in the view (root or nested).
302
- // 2.2. It could be the focus went straight to the toolbar before even focusing the editing area.
303
- // In either case, just focus the view editing. The focus will land where it belongs.
304
- else {
305
- editor.editing.view.focus();
306
- }
307
- // Clean up after the toolbar if there is anything to do there.
308
- if (focusedToolbarDef.options.afterBlur) {
309
- focusedToolbarDef.options.afterBlur();
310
- }
311
- cancel();
312
- });
313
- }
314
- /**
315
- * Returns definitions of toolbars that could potentially be focused, sorted by their importance for the user.
316
- *
317
- * Focusable toolbars candidates are either:
318
- * * already visible,
319
- * * have `beforeFocus()` set in their {@link module:ui/editorui/editorui~FocusableToolbarDefinition definition} that suggests that
320
- * they might show up when called. Keep in mind that determining whether a toolbar will show up (and become focusable) is impossible
321
- * at this stage because it depends on its implementation, that in turn depends on the editing context (selection).
322
- *
323
- * **Note**: Contextual toolbars take precedence over regular toolbars.
324
- */
325
- _getFocusableCandidateToolbarDefinitions() {
326
- const definitions = [];
327
- for (const toolbarDef of this._focusableToolbarDefinitions) {
328
- const { toolbarView, options } = toolbarDef;
329
- if (isVisible(toolbarView.element) || options.beforeFocus) {
330
- definitions.push(toolbarDef);
331
- }
332
- }
333
- // Contextual and already visible toolbars have higher priority. If both are true, the toolbar will always focus first.
334
- // For instance, a selected widget toolbar vs inline editor toolbar: both are visible but the widget toolbar is contextual.
335
- definitions.sort((defA, defB) => getToolbarDefinitionWeight(defA) - getToolbarDefinitionWeight(defB));
336
- return definitions;
337
- }
338
- /**
339
- * Returns a definition of the toolbar that is currently visible and focused (one of its children has focus).
340
- *
341
- * `null` is returned when no toolbar is currently focused.
342
- */
343
- _getCurrentFocusedToolbarDefinition() {
344
- for (const definition of this._focusableToolbarDefinitions) {
345
- if (definition.toolbarView.element && definition.toolbarView.element.contains(this.focusTracker.focusedElement)) {
346
- return definition;
347
- }
348
- }
349
- return null;
350
- }
351
- /**
352
- * Focuses a focusable toolbar candidate using its definition.
353
- *
354
- * @param candidateToolbarDefinition A definition of the toolbar to focus.
355
- * @returns `true` when the toolbar candidate was focused. `false` otherwise.
356
- */
357
- _focusFocusableCandidateToolbar(candidateToolbarDefinition) {
358
- const { toolbarView, options: { beforeFocus } } = candidateToolbarDefinition;
359
- if (beforeFocus) {
360
- beforeFocus();
361
- }
362
- // If it didn't show up after beforeFocus(), it's not focusable at all.
363
- if (!isVisible(toolbarView.element)) {
364
- return false;
365
- }
366
- toolbarView.focus();
367
- return true;
368
- }
369
- /**
370
- * Provides an integration between {@link #viewportOffset} and {@link module:utils/dom/scroll~scrollViewportToShowTarget}.
371
- * It allows the UI-agnostic engine method to consider user-configured viewport offsets specific for the integration.
372
- *
373
- * @param evt The `scrollToTheSelection` event info.
374
- * @param data The payload carried by the `scrollToTheSelection` event.
375
- */
376
- _handleScrollToTheSelection(evt, data) {
377
- const configuredViewportOffset = {
378
- top: 0,
379
- bottom: 0,
380
- left: 0,
381
- right: 0,
382
- ...this.viewportOffset
383
- };
384
- data.viewportOffset.top += configuredViewportOffset.top;
385
- data.viewportOffset.bottom += configuredViewportOffset.bottom;
386
- data.viewportOffset.left += configuredViewportOffset.left;
387
- data.viewportOffset.right += configuredViewportOffset.right;
388
- }
389
- }
390
- /**
391
- * Returns a number (weight) for a toolbar definition. Visible toolbars have a higher priority and so do
392
- * contextual toolbars (displayed in the context of a content, for instance, an image toolbar).
393
- *
394
- * A standard invisible toolbar is the heaviest. A visible contextual toolbar is the lightest.
395
- *
396
- * @param toolbarDef A toolbar definition to be weighted.
397
- */
398
- function getToolbarDefinitionWeight(toolbarDef) {
399
- const { toolbarView, options } = toolbarDef;
400
- let weight = 10;
401
- // Prioritize already visible toolbars. They should get focused first.
402
- if (isVisible(toolbarView.element)) {
403
- weight--;
404
- }
405
- // Prioritize contextual toolbars. They are displayed at the selection.
406
- if (options.isContextual) {
407
- weight--;
408
- }
409
- return weight;
410
- }
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/editorui/editorui
7
+ */
8
+ /* globals console */
9
+ import ComponentFactory from '../componentfactory';
10
+ import TooltipManager from '../tooltipmanager';
11
+ import PoweredBy from './poweredby';
12
+ import AriaLiveAnnouncer from '../arialiveannouncer';
13
+ import { ObservableMixin, isVisible, FocusTracker } from '@ckeditor/ckeditor5-utils';
14
+ /**
15
+ * A class providing the minimal interface that is required to successfully bootstrap any editor UI.
16
+ */
17
+ export default class EditorUI extends ObservableMixin() {
18
+ /**
19
+ * Creates an instance of the editor UI class.
20
+ *
21
+ * @param editor The editor instance.
22
+ */
23
+ constructor(editor) {
24
+ super();
25
+ /**
26
+ * Indicates the UI is ready. Set `true` after {@link #event:ready} event is fired.
27
+ *
28
+ * @readonly
29
+ * @default false
30
+ */
31
+ this.isReady = false;
32
+ /**
33
+ * Stores all editable elements used by the editor instance.
34
+ */
35
+ this._editableElementsMap = new Map();
36
+ /**
37
+ * All available & focusable toolbars.
38
+ */
39
+ this._focusableToolbarDefinitions = [];
40
+ const editingView = editor.editing.view;
41
+ this.editor = editor;
42
+ this.componentFactory = new ComponentFactory(editor);
43
+ this.focusTracker = new FocusTracker();
44
+ this.tooltipManager = new TooltipManager(editor);
45
+ this.poweredBy = new PoweredBy(editor);
46
+ this.ariaLiveAnnouncer = new AriaLiveAnnouncer(editor);
47
+ this.set('viewportOffset', this._readViewportOffsetFromConfig());
48
+ this.once('ready', () => {
49
+ this.isReady = true;
50
+ });
51
+ // Informs UI components that should be refreshed after layout change.
52
+ this.listenTo(editingView.document, 'layoutChanged', this.update.bind(this));
53
+ this.listenTo(editingView, 'scrollToTheSelection', this._handleScrollToTheSelection.bind(this));
54
+ this._initFocusTracking();
55
+ }
56
+ /**
57
+ * The main (outermost) DOM element of the editor UI.
58
+ *
59
+ * For example, in {@link module:editor-classic/classiceditor~ClassicEditor} it is a `<div>` which
60
+ * wraps the editable element and the toolbar. In {@link module:editor-inline/inlineeditor~InlineEditor}
61
+ * it is the editable element itself (as there is no other wrapper). However, in
62
+ * {@link module:editor-decoupled/decouplededitor~DecoupledEditor} it is set to `null` because this editor does not
63
+ * come with a single "main" HTML element (its editable element and toolbar are separate).
64
+ *
65
+ * This property can be understood as a shorthand for retrieving the element that a specific editor integration
66
+ * considers to be its main DOM element.
67
+ */
68
+ get element() {
69
+ return null;
70
+ }
71
+ /**
72
+ * Fires the {@link module:ui/editorui/editorui~EditorUI#event:update `update`} event.
73
+ *
74
+ * This method should be called when the editor UI (e.g. positions of its balloons) needs to be updated due to
75
+ * some environmental change which CKEditor 5 is not aware of (e.g. resize of a container in which it is used).
76
+ */
77
+ update() {
78
+ this.fire('update');
79
+ }
80
+ /**
81
+ * Destroys the UI.
82
+ */
83
+ destroy() {
84
+ this.stopListening();
85
+ this.focusTracker.destroy();
86
+ this.tooltipManager.destroy(this.editor);
87
+ this.poweredBy.destroy();
88
+ // Clean–up the references to the CKEditor instance stored in the native editable DOM elements.
89
+ for (const domElement of this._editableElementsMap.values()) {
90
+ domElement.ckeditorInstance = null;
91
+ this.editor.keystrokes.stopListening(domElement);
92
+ }
93
+ this._editableElementsMap = new Map();
94
+ this._focusableToolbarDefinitions = [];
95
+ }
96
+ /**
97
+ * Stores the native DOM editable element used by the editor under a unique name.
98
+ *
99
+ * Also, registers the element in the editor to maintain the accessibility of the UI. When the user is editing text in a focusable
100
+ * editable area, they can use the <kbd>Alt</kbd> + <kbd>F10</kbd> keystroke to navigate over editor toolbars. See {@link #addToolbar}.
101
+ *
102
+ * @param rootName The unique name of the editable element.
103
+ * @param domElement The native DOM editable element.
104
+ */
105
+ setEditableElement(rootName, domElement) {
106
+ this._editableElementsMap.set(rootName, domElement);
107
+ // Put a reference to the CKEditor instance in the editable native DOM element.
108
+ // It helps 3rd–party software (browser extensions, other libraries) access and recognize
109
+ // CKEditor 5 instances (editing roots) and use their API (there is no global editor
110
+ // instance registry).
111
+ if (!domElement.ckeditorInstance) {
112
+ domElement.ckeditorInstance = this.editor;
113
+ }
114
+ // Register the element, so it becomes available for Alt+F10 and Esc navigation.
115
+ this.focusTracker.add(domElement);
116
+ const setUpKeystrokeHandler = () => {
117
+ // The editing view of the editor is already listening to keystrokes from DOM roots (see: KeyObserver).
118
+ // Do not duplicate listeners.
119
+ if (this.editor.editing.view.getDomRoot(rootName)) {
120
+ return;
121
+ }
122
+ this.editor.keystrokes.listenTo(domElement);
123
+ };
124
+ // For editable elements set by features after EditorUI is ready (e.g. source editing).
125
+ if (this.isReady) {
126
+ setUpKeystrokeHandler();
127
+ }
128
+ // For editable elements set while the editor is being created (e.g. DOM roots).
129
+ else {
130
+ this.once('ready', setUpKeystrokeHandler);
131
+ }
132
+ }
133
+ /**
134
+ * Removes the editable from the editor UI. Removes all handlers added by {@link #setEditableElement}.
135
+ *
136
+ * @param rootName The name of the editable element to remove.
137
+ */
138
+ removeEditableElement(rootName) {
139
+ const domElement = this._editableElementsMap.get(rootName);
140
+ if (!domElement) {
141
+ return;
142
+ }
143
+ this._editableElementsMap.delete(rootName);
144
+ this.editor.keystrokes.stopListening(domElement);
145
+ this.focusTracker.remove(domElement);
146
+ domElement.ckeditorInstance = null;
147
+ }
148
+ /**
149
+ * Returns the editable editor element with the given name or null if editable does not exist.
150
+ *
151
+ * @param rootName The editable name.
152
+ */
153
+ getEditableElement(rootName = 'main') {
154
+ return this._editableElementsMap.get(rootName);
155
+ }
156
+ /**
157
+ * Returns array of names of all editor editable elements.
158
+ */
159
+ getEditableElementsNames() {
160
+ return this._editableElementsMap.keys();
161
+ }
162
+ /**
163
+ * Adds a toolbar to the editor UI. Used primarily to maintain the accessibility of the UI.
164
+ *
165
+ * Focusable toolbars can be accessed (focused) by users by pressing the <kbd>Alt</kbd> + <kbd>F10</kbd> keystroke.
166
+ * Successive keystroke presses navigate over available toolbars.
167
+ *
168
+ * @param toolbarView A instance of the toolbar to be registered.
169
+ */
170
+ addToolbar(toolbarView, options = {}) {
171
+ if (toolbarView.isRendered) {
172
+ this.focusTracker.add(toolbarView.element);
173
+ this.editor.keystrokes.listenTo(toolbarView.element);
174
+ }
175
+ else {
176
+ toolbarView.once('render', () => {
177
+ this.focusTracker.add(toolbarView.element);
178
+ this.editor.keystrokes.listenTo(toolbarView.element);
179
+ });
180
+ }
181
+ this._focusableToolbarDefinitions.push({ toolbarView, options });
182
+ }
183
+ /**
184
+ * Stores all editable elements used by the editor instance.
185
+ *
186
+ * @deprecated
187
+ */
188
+ get _editableElements() {
189
+ /**
190
+ * The {@link module:ui/editorui/editorui~EditorUI#_editableElements `EditorUI#_editableElements`} property has been
191
+ * deprecated and will be removed in the near future. Please use
192
+ * {@link module:ui/editorui/editorui~EditorUI#setEditableElement `setEditableElement()`} and
193
+ * {@link module:ui/editorui/editorui~EditorUI#getEditableElement `getEditableElement()`} methods instead.
194
+ *
195
+ * @error editor-ui-deprecated-editable-elements
196
+ * @param editorUI Editor UI instance the deprecated property belongs to.
197
+ */
198
+ console.warn('editor-ui-deprecated-editable-elements: ' +
199
+ 'The EditorUI#_editableElements property has been deprecated and will be removed in the near future.', { editorUI: this });
200
+ return this._editableElementsMap;
201
+ }
202
+ /**
203
+ * Returns viewport offsets object:
204
+ *
205
+ * ```js
206
+ * {
207
+ * top: Number,
208
+ * right: Number,
209
+ * bottom: Number,
210
+ * left: Number
211
+ * }
212
+ * ```
213
+ *
214
+ * Only top property is currently supported.
215
+ */
216
+ _readViewportOffsetFromConfig() {
217
+ const editor = this.editor;
218
+ const viewportOffsetConfig = editor.config.get('ui.viewportOffset');
219
+ if (viewportOffsetConfig) {
220
+ return viewportOffsetConfig;
221
+ }
222
+ // Not present in EditorConfig type, because it's legacy. Hence the `as` expression.
223
+ const legacyOffsetConfig = editor.config.get('toolbar.viewportTopOffset');
224
+ // Fall back to deprecated toolbar config.
225
+ if (legacyOffsetConfig) {
226
+ /**
227
+ * The {@link module:core/editor/editorconfig~EditorConfig#toolbar `EditorConfig#toolbar.viewportTopOffset`}
228
+ * property has been deprecated and will be removed in the near future. Please use
229
+ * {@link module:core/editor/editorconfig~EditorConfig#ui `EditorConfig#ui.viewportOffset`} instead.
230
+ *
231
+ * @error editor-ui-deprecated-viewport-offset-config
232
+ */
233
+ console.warn('editor-ui-deprecated-viewport-offset-config: ' +
234
+ 'The `toolbar.vieportTopOffset` configuration option is deprecated. ' +
235
+ 'It will be removed from future CKEditor versions. Use `ui.viewportOffset.top` instead.');
236
+ return { top: legacyOffsetConfig };
237
+ }
238
+ // More keys to come in the future.
239
+ return { top: 0 };
240
+ }
241
+ /**
242
+ * Starts listening for <kbd>Alt</kbd> + <kbd>F10</kbd> and <kbd>Esc</kbd> keystrokes in the context of focusable
243
+ * {@link #setEditableElement editable elements} and {@link #addToolbar toolbars}
244
+ * to allow users navigate across the UI.
245
+ */
246
+ _initFocusTracking() {
247
+ const editor = this.editor;
248
+ const editingView = editor.editing.view;
249
+ let lastFocusedForeignElement;
250
+ let candidateDefinitions;
251
+ // Focus the next focusable toolbar on <kbd>Alt</kbd> + <kbd>F10</kbd>.
252
+ editor.keystrokes.set('Alt+F10', (data, cancel) => {
253
+ const focusedElement = this.focusTracker.focusedElement;
254
+ // Focus moved out of a DOM element that
255
+ // * is not a toolbar,
256
+ // * does not belong to the editing view (e.g. source editing).
257
+ if (Array.from(this._editableElementsMap.values()).includes(focusedElement) &&
258
+ !Array.from(editingView.domRoots.values()).includes(focusedElement)) {
259
+ lastFocusedForeignElement = focusedElement;
260
+ }
261
+ const currentFocusedToolbarDefinition = this._getCurrentFocusedToolbarDefinition();
262
+ // * When focusing a toolbar for the first time, set the array of definitions for successive presses of Alt+F10.
263
+ // This ensures, the navigation works always the same and no pair of toolbars takes over
264
+ // (e.g. image and table toolbars when a selected image is inside a cell).
265
+ // * It could be that the focus went to the toolbar by clicking a toolbar item (e.g. a dropdown). In this case,
266
+ // there were no candidates so they must be obtained (#12339).
267
+ if (!currentFocusedToolbarDefinition || !candidateDefinitions) {
268
+ candidateDefinitions = this._getFocusableCandidateToolbarDefinitions();
269
+ }
270
+ // In a single Alt+F10 press, check all candidates but if none were focused, don't go any further.
271
+ // This prevents an infinite loop.
272
+ for (let i = 0; i < candidateDefinitions.length; i++) {
273
+ const candidateDefinition = candidateDefinitions.shift();
274
+ // Put the first definition to the back of the array. This allows circular navigation over all toolbars
275
+ // on successive presses of Alt+F10.
276
+ candidateDefinitions.push(candidateDefinition);
277
+ // Don't focus the same toolbar again. If you did, this would move focus from the nth focused toolbar item back to the
278
+ // first item as per ToolbarView#focus() if the user navigated inside the toolbar.
279
+ if (candidateDefinition !== currentFocusedToolbarDefinition &&
280
+ this._focusFocusableCandidateToolbar(candidateDefinition)) {
281
+ // Clean up after a current visible toolbar when switching to the next one.
282
+ if (currentFocusedToolbarDefinition && currentFocusedToolbarDefinition.options.afterBlur) {
283
+ currentFocusedToolbarDefinition.options.afterBlur();
284
+ }
285
+ break;
286
+ }
287
+ }
288
+ cancel();
289
+ });
290
+ // Blur the focused toolbar on <kbd>Esc</kbd> and bring the focus back to its origin.
291
+ editor.keystrokes.set('Esc', (data, cancel) => {
292
+ const focusedToolbarDef = this._getCurrentFocusedToolbarDefinition();
293
+ if (!focusedToolbarDef) {
294
+ return;
295
+ }
296
+ // Bring focus back to where it came from before focusing the toolbar:
297
+ // 1. If it came from outside the engine view (e.g. source editing), move it there.
298
+ if (lastFocusedForeignElement) {
299
+ lastFocusedForeignElement.focus();
300
+ lastFocusedForeignElement = null;
301
+ }
302
+ // 2. There are two possibilities left:
303
+ // 2.1. It could be that the focus went from an editable element in the view (root or nested).
304
+ // 2.2. It could be the focus went straight to the toolbar before even focusing the editing area.
305
+ // In either case, just focus the view editing. The focus will land where it belongs.
306
+ else {
307
+ editor.editing.view.focus();
308
+ }
309
+ // Clean up after the toolbar if there is anything to do there.
310
+ if (focusedToolbarDef.options.afterBlur) {
311
+ focusedToolbarDef.options.afterBlur();
312
+ }
313
+ cancel();
314
+ });
315
+ }
316
+ /**
317
+ * Returns definitions of toolbars that could potentially be focused, sorted by their importance for the user.
318
+ *
319
+ * Focusable toolbars candidates are either:
320
+ * * already visible,
321
+ * * have `beforeFocus()` set in their {@link module:ui/editorui/editorui~FocusableToolbarDefinition definition} that suggests that
322
+ * they might show up when called. Keep in mind that determining whether a toolbar will show up (and become focusable) is impossible
323
+ * at this stage because it depends on its implementation, that in turn depends on the editing context (selection).
324
+ *
325
+ * **Note**: Contextual toolbars take precedence over regular toolbars.
326
+ */
327
+ _getFocusableCandidateToolbarDefinitions() {
328
+ const definitions = [];
329
+ for (const toolbarDef of this._focusableToolbarDefinitions) {
330
+ const { toolbarView, options } = toolbarDef;
331
+ if (isVisible(toolbarView.element) || options.beforeFocus) {
332
+ definitions.push(toolbarDef);
333
+ }
334
+ }
335
+ // Contextual and already visible toolbars have higher priority. If both are true, the toolbar will always focus first.
336
+ // For instance, a selected widget toolbar vs inline editor toolbar: both are visible but the widget toolbar is contextual.
337
+ definitions.sort((defA, defB) => getToolbarDefinitionWeight(defA) - getToolbarDefinitionWeight(defB));
338
+ return definitions;
339
+ }
340
+ /**
341
+ * Returns a definition of the toolbar that is currently visible and focused (one of its children has focus).
342
+ *
343
+ * `null` is returned when no toolbar is currently focused.
344
+ */
345
+ _getCurrentFocusedToolbarDefinition() {
346
+ for (const definition of this._focusableToolbarDefinitions) {
347
+ if (definition.toolbarView.element && definition.toolbarView.element.contains(this.focusTracker.focusedElement)) {
348
+ return definition;
349
+ }
350
+ }
351
+ return null;
352
+ }
353
+ /**
354
+ * Focuses a focusable toolbar candidate using its definition.
355
+ *
356
+ * @param candidateToolbarDefinition A definition of the toolbar to focus.
357
+ * @returns `true` when the toolbar candidate was focused. `false` otherwise.
358
+ */
359
+ _focusFocusableCandidateToolbar(candidateToolbarDefinition) {
360
+ const { toolbarView, options: { beforeFocus } } = candidateToolbarDefinition;
361
+ if (beforeFocus) {
362
+ beforeFocus();
363
+ }
364
+ // If it didn't show up after beforeFocus(), it's not focusable at all.
365
+ if (!isVisible(toolbarView.element)) {
366
+ return false;
367
+ }
368
+ toolbarView.focus();
369
+ return true;
370
+ }
371
+ /**
372
+ * Provides an integration between {@link #viewportOffset} and {@link module:utils/dom/scroll~scrollViewportToShowTarget}.
373
+ * It allows the UI-agnostic engine method to consider user-configured viewport offsets specific for the integration.
374
+ *
375
+ * @param evt The `scrollToTheSelection` event info.
376
+ * @param data The payload carried by the `scrollToTheSelection` event.
377
+ */
378
+ _handleScrollToTheSelection(evt, data) {
379
+ const configuredViewportOffset = {
380
+ top: 0,
381
+ bottom: 0,
382
+ left: 0,
383
+ right: 0,
384
+ ...this.viewportOffset
385
+ };
386
+ data.viewportOffset.top += configuredViewportOffset.top;
387
+ data.viewportOffset.bottom += configuredViewportOffset.bottom;
388
+ data.viewportOffset.left += configuredViewportOffset.left;
389
+ data.viewportOffset.right += configuredViewportOffset.right;
390
+ }
391
+ }
392
+ /**
393
+ * Returns a number (weight) for a toolbar definition. Visible toolbars have a higher priority and so do
394
+ * contextual toolbars (displayed in the context of a content, for instance, an image toolbar).
395
+ *
396
+ * A standard invisible toolbar is the heaviest. A visible contextual toolbar is the lightest.
397
+ *
398
+ * @param toolbarDef A toolbar definition to be weighted.
399
+ */
400
+ function getToolbarDefinitionWeight(toolbarDef) {
401
+ const { toolbarView, options } = toolbarDef;
402
+ let weight = 10;
403
+ // Prioritize already visible toolbars. They should get focused first.
404
+ if (isVisible(toolbarView.element)) {
405
+ weight--;
406
+ }
407
+ // Prioritize contextual toolbars. They are displayed at the selection.
408
+ if (options.isContextual) {
409
+ weight--;
410
+ }
411
+ return weight;
412
+ }