@ckeditor/ckeditor5-utils 47.6.1 → 48.0.0-alpha.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 (154) hide show
  1. package/LICENSE.md +1 -1
  2. package/{src → dist}/dom/global.d.ts +1 -1
  3. package/{src → dist}/dom/rect.d.ts +8 -0
  4. package/dist/index.css +3 -0
  5. package/dist/index.css.map +1 -0
  6. package/dist/index.js +24 -5
  7. package/dist/index.js.map +1 -1
  8. package/{src → dist}/version.d.ts +1 -1
  9. package/package.json +20 -38
  10. package/src/abortabledebounce.js +0 -22
  11. package/src/areconnectedthroughproperties.js +0 -71
  12. package/src/ckeditorerror.js +0 -187
  13. package/src/collection.js +0 -620
  14. package/src/collectstylesheets.js +0 -52
  15. package/src/comparearrays.js +0 -47
  16. package/src/config.js +0 -167
  17. package/src/count.js +0 -25
  18. package/src/crc32.js +0 -61
  19. package/src/decodelicensekey.js +0 -30
  20. package/src/delay.js +0 -25
  21. package/src/diff.js +0 -115
  22. package/src/difftochanges.js +0 -79
  23. package/src/dom/createelement.js +0 -40
  24. package/src/dom/emittermixin.js +0 -237
  25. package/src/dom/findclosestscrollableancestor.js +0 -31
  26. package/src/dom/getancestors.js +0 -26
  27. package/src/dom/getborderwidths.js +0 -24
  28. package/src/dom/getcommonancestor.js +0 -25
  29. package/src/dom/getdatafromelement.js +0 -19
  30. package/src/dom/getpositionedancestor.js +0 -22
  31. package/src/dom/getrangefrommouseevent.js +0 -33
  32. package/src/dom/getvisualviewportoffset.js +0 -21
  33. package/src/dom/global.js +0 -35
  34. package/src/dom/indexof.js +0 -21
  35. package/src/dom/insertat.js +0 -17
  36. package/src/dom/iscomment.js +0 -13
  37. package/src/dom/isnode.js +0 -21
  38. package/src/dom/isrange.js +0 -13
  39. package/src/dom/istext.js +0 -13
  40. package/src/dom/isvalidattributename.js +0 -22
  41. package/src/dom/isvisible.js +0 -30
  42. package/src/dom/iswindow.js +0 -22
  43. package/src/dom/position.js +0 -322
  44. package/src/dom/rect.js +0 -498
  45. package/src/dom/remove.js +0 -18
  46. package/src/dom/resizeobserver.js +0 -134
  47. package/src/dom/scroll.js +0 -406
  48. package/src/dom/setdatainelement.js +0 -19
  49. package/src/dom/tounit.js +0 -16
  50. package/src/elementreplacer.js +0 -47
  51. package/src/emittermixin.js +0 -443
  52. package/src/env.js +0 -158
  53. package/src/eventinfo.js +0 -63
  54. package/src/fastdiff.js +0 -248
  55. package/src/first.js +0 -17
  56. package/src/focustracker.js +0 -329
  57. package/src/formathtml.js +0 -164
  58. package/src/index.js +0 -78
  59. package/src/inserttopriorityarray.js +0 -28
  60. package/src/isfeatureblockedbylicensekey.js +0 -17
  61. package/src/isiterable.js +0 -16
  62. package/src/keyboard.js +0 -256
  63. package/src/keystrokehandler.js +0 -126
  64. package/src/language.js +0 -21
  65. package/src/legacyerrors.js +0 -18
  66. package/src/locale.js +0 -135
  67. package/src/mapsequal.js +0 -27
  68. package/src/mix.js +0 -5
  69. package/src/nth.js +0 -24
  70. package/src/objecttomap.js +0 -27
  71. package/src/observablemixin.js +0 -575
  72. package/src/parsebase64encodedobject.js +0 -22
  73. package/src/priorities.js +0 -22
  74. package/src/retry.js +0 -48
  75. package/src/splicearray.js +0 -37
  76. package/src/spy.js +0 -21
  77. package/src/toarray.js +0 -7
  78. package/src/tomap.js +0 -29
  79. package/src/translation-service.js +0 -209
  80. package/src/uid.js +0 -45
  81. package/src/unicode.js +0 -85
  82. package/src/version.js +0 -177
  83. package/src/wait.js +0 -29
  84. /package/{src → dist}/abortabledebounce.d.ts +0 -0
  85. /package/{src → dist}/areconnectedthroughproperties.d.ts +0 -0
  86. /package/{src → dist}/ckeditorerror.d.ts +0 -0
  87. /package/{src → dist}/collection.d.ts +0 -0
  88. /package/{src → dist}/collectstylesheets.d.ts +0 -0
  89. /package/{src → dist}/comparearrays.d.ts +0 -0
  90. /package/{src → dist}/config.d.ts +0 -0
  91. /package/{src → dist}/count.d.ts +0 -0
  92. /package/{src → dist}/crc32.d.ts +0 -0
  93. /package/{src → dist}/decodelicensekey.d.ts +0 -0
  94. /package/{src → dist}/delay.d.ts +0 -0
  95. /package/{src → dist}/diff.d.ts +0 -0
  96. /package/{src → dist}/difftochanges.d.ts +0 -0
  97. /package/{src → dist}/dom/createelement.d.ts +0 -0
  98. /package/{src → dist}/dom/emittermixin.d.ts +0 -0
  99. /package/{src → dist}/dom/findclosestscrollableancestor.d.ts +0 -0
  100. /package/{src → dist}/dom/getancestors.d.ts +0 -0
  101. /package/{src → dist}/dom/getborderwidths.d.ts +0 -0
  102. /package/{src → dist}/dom/getcommonancestor.d.ts +0 -0
  103. /package/{src → dist}/dom/getdatafromelement.d.ts +0 -0
  104. /package/{src → dist}/dom/getpositionedancestor.d.ts +0 -0
  105. /package/{src → dist}/dom/getrangefrommouseevent.d.ts +0 -0
  106. /package/{src → dist}/dom/getvisualviewportoffset.d.ts +0 -0
  107. /package/{src → dist}/dom/indexof.d.ts +0 -0
  108. /package/{src → dist}/dom/insertat.d.ts +0 -0
  109. /package/{src → dist}/dom/iscomment.d.ts +0 -0
  110. /package/{src → dist}/dom/isnode.d.ts +0 -0
  111. /package/{src → dist}/dom/isrange.d.ts +0 -0
  112. /package/{src → dist}/dom/istext.d.ts +0 -0
  113. /package/{src → dist}/dom/isvalidattributename.d.ts +0 -0
  114. /package/{src → dist}/dom/isvisible.d.ts +0 -0
  115. /package/{src → dist}/dom/iswindow.d.ts +0 -0
  116. /package/{src → dist}/dom/position.d.ts +0 -0
  117. /package/{src → dist}/dom/remove.d.ts +0 -0
  118. /package/{src → dist}/dom/resizeobserver.d.ts +0 -0
  119. /package/{src → dist}/dom/scroll.d.ts +0 -0
  120. /package/{src → dist}/dom/setdatainelement.d.ts +0 -0
  121. /package/{src → dist}/dom/tounit.d.ts +0 -0
  122. /package/{src → dist}/elementreplacer.d.ts +0 -0
  123. /package/{src → dist}/emittermixin.d.ts +0 -0
  124. /package/{src → dist}/env.d.ts +0 -0
  125. /package/{src → dist}/eventinfo.d.ts +0 -0
  126. /package/{src → dist}/fastdiff.d.ts +0 -0
  127. /package/{src → dist}/first.d.ts +0 -0
  128. /package/{src → dist}/focustracker.d.ts +0 -0
  129. /package/{src → dist}/formathtml.d.ts +0 -0
  130. /package/{src → dist}/index.d.ts +0 -0
  131. /package/{src → dist}/inserttopriorityarray.d.ts +0 -0
  132. /package/{src → dist}/isfeatureblockedbylicensekey.d.ts +0 -0
  133. /package/{src → dist}/isiterable.d.ts +0 -0
  134. /package/{src → dist}/keyboard.d.ts +0 -0
  135. /package/{src → dist}/keystrokehandler.d.ts +0 -0
  136. /package/{src → dist}/language.d.ts +0 -0
  137. /package/{src → dist}/legacyerrors.d.ts +0 -0
  138. /package/{src → dist}/locale.d.ts +0 -0
  139. /package/{src → dist}/mapsequal.d.ts +0 -0
  140. /package/{src → dist}/mix.d.ts +0 -0
  141. /package/{src → dist}/nth.d.ts +0 -0
  142. /package/{src → dist}/objecttomap.d.ts +0 -0
  143. /package/{src → dist}/observablemixin.d.ts +0 -0
  144. /package/{src → dist}/parsebase64encodedobject.d.ts +0 -0
  145. /package/{src → dist}/priorities.d.ts +0 -0
  146. /package/{src → dist}/retry.d.ts +0 -0
  147. /package/{src → dist}/splicearray.d.ts +0 -0
  148. /package/{src → dist}/spy.d.ts +0 -0
  149. /package/{src → dist}/toarray.d.ts +0 -0
  150. /package/{src → dist}/tomap.d.ts +0 -0
  151. /package/{src → dist}/translation-service.d.ts +0 -0
  152. /package/{src → dist}/uid.d.ts +0 -0
  153. /package/{src → dist}/unicode.d.ts +0 -0
  154. /package/{src → dist}/wait.d.ts +0 -0
package/src/fastdiff.js DELETED
@@ -1,248 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module utils/fastdiff
7
- */
8
- /**
9
- * Finds positions of the first and last change in the given string/array and generates a set of changes:
10
- *
11
- * ```ts
12
- * fastDiff( '12a', '12xyza' );
13
- * // [ { index: 2, type: 'insert', values: [ 'x', 'y', 'z' ] } ]
14
- *
15
- * fastDiff( '12a', '12aa' );
16
- * // [ { index: 3, type: 'insert', values: [ 'a' ] } ]
17
- *
18
- * fastDiff( '12xyza', '12a' );
19
- * // [ { index: 2, type: 'delete', howMany: 3 } ]
20
- *
21
- * fastDiff( [ '1', '2', 'a', 'a' ], [ '1', '2', 'a' ] );
22
- * // [ { index: 3, type: 'delete', howMany: 1 } ]
23
- *
24
- * fastDiff( [ '1', '2', 'a', 'b', 'c', '3' ], [ '2', 'a', 'b' ] );
25
- * // [ { index: 0, type: 'insert', values: [ '2', 'a', 'b' ] }, { index: 3, type: 'delete', howMany: 6 } ]
26
- * ```
27
- *
28
- * Passed arrays can contain any type of data, however to compare them correctly custom comparator function
29
- * should be passed as a third parameter:
30
- *
31
- * ```ts
32
- * fastDiff( [ { value: 1 }, { value: 2 } ], [ { value: 1 }, { value: 3 } ], ( a, b ) => {
33
- * return a.value === b.value;
34
- * } );
35
- * // [ { index: 1, type: 'insert', values: [ { value: 3 } ] }, { index: 2, type: 'delete', howMany: 1 } ]
36
- * ```
37
- *
38
- * The resulted set of changes can be applied to the input in order to transform it into the output, for example:
39
- *
40
- * ```ts
41
- * let input = '12abc3';
42
- * const output = '2ab';
43
- * const changes = fastDiff( input, output );
44
- *
45
- * changes.forEach( change => {
46
- * if ( change.type == 'insert' ) {
47
- * input = input.substring( 0, change.index ) + change.values.join( '' ) + input.substring( change.index );
48
- * } else if ( change.type == 'delete' ) {
49
- * input = input.substring( 0, change.index ) + input.substring( change.index + change.howMany );
50
- * }
51
- * } );
52
- *
53
- * // input equals output now
54
- * ```
55
- *
56
- * or in case of arrays:
57
- *
58
- * ```ts
59
- * let input = [ '1', '2', 'a', 'b', 'c', '3' ];
60
- * const output = [ '2', 'a', 'b' ];
61
- * const changes = fastDiff( input, output );
62
- *
63
- * changes.forEach( change => {
64
- * if ( change.type == 'insert' ) {
65
- * input = input.slice( 0, change.index ).concat( change.values, input.slice( change.index ) );
66
- * } else if ( change.type == 'delete' ) {
67
- * input = input.slice( 0, change.index ).concat( input.slice( change.index + change.howMany ) );
68
- * }
69
- * } );
70
- *
71
- * // input equals output now
72
- * ```
73
- *
74
- * By passing `true` as the fourth parameter (`atomicChanges`) the output of this function will become compatible with
75
- * the {@link module:utils/diff~diff `diff()`} function:
76
- *
77
- * ```ts
78
- * fastDiff( '12a', '12xyza', undefined, true );
79
- * // [ 'equal', 'equal', 'insert', 'insert', 'insert', 'equal' ]
80
- * ```
81
- *
82
- * The default output format of this function is compatible with the output format of
83
- * {@link module:utils/difftochanges~diffToChanges `diffToChanges()`}. The `diffToChanges()` input format is, in turn,
84
- * compatible with the output of {@link module:utils/diff~diff `diff()`}:
85
- *
86
- * ```ts
87
- * const a = '1234';
88
- * const b = '12xyz34';
89
- *
90
- * // Both calls will return the same results (grouped changes format).
91
- * fastDiff( a, b );
92
- * diffToChanges( diff( a, b ) );
93
- *
94
- * // Again, both calls will return the same results (atomic changes format).
95
- * fastDiff( a, b, undefined, true );
96
- * diff( a, b );
97
- * ```
98
- *
99
- * @typeParam T The type of array elements.
100
- * @typeParam AtomicChanges The type of `atomicChanges` parameter (selects the result type).
101
- * @param a Input array or string.
102
- * @param b Input array or string.
103
- * @param cmp Optional function used to compare array values, by default `===` (strict equal operator) is used.
104
- * @param atomicChanges Whether an array of `inset|delete|equal` operations should
105
- * be returned instead of changes set. This makes this function compatible with {@link module:utils/diff~diff `diff()`}.
106
- * Defaults to `false`.
107
- * @returns Array of changes. The elements are either {@link module:utils/diff~DiffResult} or {@link module:utils/difftochanges~Change},
108
- * depending on `atomicChanges` parameter.
109
- */
110
- export function fastDiff(a, b, cmp, atomicChanges) {
111
- // Set the comparator function.
112
- cmp = cmp || function (a, b) {
113
- return a === b;
114
- };
115
- // Convert the string (or any array-like object - eg. NodeList) to an array by using the slice() method because,
116
- // unlike Array.from(), it returns array of UTF-16 code units instead of the code points of a string.
117
- // One code point might be a surrogate pair of two code units. All text offsets are expected to be in code units.
118
- // See ckeditor/ckeditor5#3147.
119
- //
120
- // We need to make sure here that fastDiff() works identical to diff().
121
- const arrayA = Array.isArray(a) ? a : Array.prototype.slice.call(a);
122
- const arrayB = Array.isArray(b) ? b : Array.prototype.slice.call(b);
123
- // Find first and last change.
124
- const changeIndexes = findChangeBoundaryIndexes(arrayA, arrayB, cmp);
125
- // Transform into changes array.
126
- const result = atomicChanges ?
127
- changeIndexesToAtomicChanges(changeIndexes, arrayB.length) :
128
- changeIndexesToChanges(arrayB, changeIndexes);
129
- return result;
130
- }
131
- /**
132
- * Finds position of the first and last change in the given arrays. For example:
133
- *
134
- * ```ts
135
- * const indexes = findChangeBoundaryIndexes( [ '1', '2', '3', '4' ], [ '1', '3', '4', '2', '4' ] );
136
- * console.log( indexes ); // { firstIndex: 1, lastIndexOld: 3, lastIndexNew: 4 }
137
- * ```
138
- *
139
- * The above indexes means that in the first array the modified part is `1[23]4` and in the second array it is `1[342]4`.
140
- * Based on such indexes, array with `insert`/`delete` operations which allows transforming first value into the second one
141
- * can be generated.
142
- */
143
- function findChangeBoundaryIndexes(arr1, arr2, cmp) {
144
- // Find the first difference between passed values.
145
- const firstIndex = findFirstDifferenceIndex(arr1, arr2, cmp);
146
- // If arrays are equal return -1 indexes object.
147
- if (firstIndex === -1) {
148
- return { firstIndex: -1, lastIndexOld: -1, lastIndexNew: -1 };
149
- }
150
- // Remove the common part of each value and reverse them to make it simpler to find the last difference between them.
151
- const oldArrayReversed = cutAndReverse(arr1, firstIndex);
152
- const newArrayReversed = cutAndReverse(arr2, firstIndex);
153
- // Find the first difference between reversed values.
154
- // It should be treated as "how many elements from the end the last difference occurred".
155
- //
156
- // For example:
157
- //
158
- // initial -> after cut -> reversed:
159
- // oldValue: '321ba' -> '21ba' -> 'ab12'
160
- // newValue: '31xba' -> '1xba' -> 'abx1'
161
- // lastIndex: -> 2
162
- //
163
- // So the last change occurred two characters from the end of the arrays.
164
- const lastIndex = findFirstDifferenceIndex(oldArrayReversed, newArrayReversed, cmp);
165
- // Use `lastIndex` to calculate proper offset, starting from the beginning (`lastIndex` kind of starts from the end).
166
- const lastIndexOld = arr1.length - lastIndex;
167
- const lastIndexNew = arr2.length - lastIndex;
168
- return { firstIndex, lastIndexOld, lastIndexNew };
169
- }
170
- /**
171
- * Returns a first index on which given arrays differ. If both arrays are the same, -1 is returned.
172
- */
173
- function findFirstDifferenceIndex(arr1, arr2, cmp) {
174
- for (let i = 0; i < Math.max(arr1.length, arr2.length); i++) {
175
- if (arr1[i] === undefined || arr2[i] === undefined || !cmp(arr1[i], arr2[i])) {
176
- return i;
177
- }
178
- }
179
- return -1; // Return -1 if arrays are equal.
180
- }
181
- /**
182
- * Returns a copy of the given array with `howMany` elements removed starting from the beginning and in reversed order.
183
- *
184
- * @param arr Array to be processed.
185
- * @param howMany How many elements from array beginning to remove.
186
- * @returns Shortened and reversed array.
187
- */
188
- function cutAndReverse(arr, howMany) {
189
- return arr.slice(howMany).reverse();
190
- }
191
- /**
192
- * Generates changes array based on change indexes from `findChangeBoundaryIndexes` function. This function will
193
- * generate array with 0 (no changes), 1 (deletion or insertion) or 2 records (insertion and deletion).
194
- *
195
- * @param newArray New array for which change indexes were calculated.
196
- * @param changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
197
- * @returns Array of changes compatible with {@link module:utils/difftochanges~diffToChanges} format.
198
- */
199
- function changeIndexesToChanges(newArray, changeIndexes) {
200
- const result = [];
201
- const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
202
- // Order operations as 'insert', 'delete' array to keep compatibility with {@link module:utils/difftochanges~diffToChanges}
203
- // in most cases. However, 'diffToChanges' does not stick to any order so in some cases
204
- // (for example replacing '12345' with 'abcd') it will generate 'delete', 'insert' order.
205
- if (lastIndexNew - firstIndex > 0) {
206
- result.push({
207
- index: firstIndex,
208
- type: 'insert',
209
- values: newArray.slice(firstIndex, lastIndexNew)
210
- });
211
- }
212
- if (lastIndexOld - firstIndex > 0) {
213
- result.push({
214
- index: firstIndex + (lastIndexNew - firstIndex), // Increase index of what was inserted.
215
- type: 'delete',
216
- howMany: lastIndexOld - firstIndex
217
- });
218
- }
219
- return result;
220
- }
221
- /**
222
- * Generates array with set `equal|insert|delete` operations based on change indexes from `findChangeBoundaryIndexes` function.
223
- *
224
- * @param changeIndexes Change indexes object from `findChangeBoundaryIndexes` function.
225
- * @param newLength Length of the new array on which `findChangeBoundaryIndexes` calculated change indexes.
226
- * @returns Array of changes compatible with {@link module:utils/diff~diff} format.
227
- */
228
- function changeIndexesToAtomicChanges(changeIndexes, newLength) {
229
- const { firstIndex, lastIndexOld, lastIndexNew } = changeIndexes;
230
- // No changes.
231
- if (firstIndex === -1) {
232
- return Array(newLength).fill('equal');
233
- }
234
- let result = [];
235
- if (firstIndex > 0) {
236
- result = result.concat(Array(firstIndex).fill('equal'));
237
- }
238
- if (lastIndexNew - firstIndex > 0) {
239
- result = result.concat(Array(lastIndexNew - firstIndex).fill('insert'));
240
- }
241
- if (lastIndexOld - firstIndex > 0) {
242
- result = result.concat(Array(lastIndexOld - firstIndex).fill('delete'));
243
- }
244
- if (lastIndexNew < newLength) {
245
- result = result.concat(Array(newLength - lastIndexNew).fill('equal'));
246
- }
247
- return result;
248
- }
package/src/first.js DELETED
@@ -1,17 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module utils/first
7
- */
8
- /**
9
- * Returns first item of the given `iterator`.
10
- */
11
- export function first(iterator) {
12
- const iteratorItem = iterator.next();
13
- if (iteratorItem.done) {
14
- return null;
15
- }
16
- return iteratorItem.value;
17
- }
@@ -1,329 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module utils/focustracker
7
- */
8
- import { DomEmitterMixin } from './dom/emittermixin.js';
9
- import { ObservableMixin } from './observablemixin.js';
10
- import { CKEditorError } from './ckeditorerror.js';
11
- import { isElement as _isElement } from 'es-toolkit/compat';
12
- /**
13
- * Allows observing a group of DOM `Element`s or {@link module:ui/view~View view instances} whether at least one of them (or their child)
14
- * is focused.
15
- *
16
- * Used by the {@link module:core/editor/editor~Editor} in order to track whether the focus is still within the application,
17
- * or were used outside of its UI.
18
- *
19
- * **Note** `focus` and `blur` listeners use event capturing, so it is only needed to register wrapper `Element`
20
- * which contain other `focusable` elements. But note that this wrapper element has to be focusable too
21
- * (have e.g. `tabindex="-1"`).
22
- *
23
- * Check out the {@glink framework/deep-dive/ui/focus-tracking "Deep dive into focus tracking"} guide to learn more.
24
- */
25
- export class FocusTracker extends /* #__PURE__ */ DomEmitterMixin(/* #__PURE__ */ ObservableMixin()) {
26
- /**
27
- * List of registered DOM elements.
28
- *
29
- * @internal
30
- */
31
- _elements = new Set();
32
- /**
33
- * List of views with external focus trackers that contribute to the state of this focus tracker.
34
- *
35
- * @internal
36
- */
37
- _externalViews = new Set();
38
- /**
39
- * Asynchronous blur event timeout.
40
- */
41
- _blurTimeout = null;
42
- // @if CK_DEBUG_FOCUSTRACKER // public _label?: string;
43
- constructor() {
44
- super();
45
- this.set('isFocused', false);
46
- this.set('focusedElement', null);
47
- // @if CK_DEBUG_FOCUSTRACKER // FocusTracker._instances.push( this );
48
- }
49
- /**
50
- * List of registered DOM elements.
51
- *
52
- * **Note**: The list does do not include elements from {@link #externalViews}.
53
- */
54
- get elements() {
55
- return Array.from(this._elements.values());
56
- }
57
- /**
58
- * List of external focusable views that contribute to the state of this focus tracker. See {@link #add} to learn more.
59
- */
60
- get externalViews() {
61
- return Array.from(this._externalViews.values());
62
- }
63
- /**
64
- * Starts tracking a specified DOM element or a {@link module:ui/view~View} instance.
65
- *
66
- * * If a DOM element is passed, the focus tracker listens to the `focus` and `blur` events on this element.
67
- * Tracked elements are listed in {@link #elements}.
68
- * * If a {@link module:ui/view~View} instance is passed that has a `FocusTracker` instance ({@link ~ViewWithFocusTracker}),
69
- * the external focus tracker's state ({@link #isFocused}, {@link #focusedElement}) starts contributing to the current tracker instance.
70
- * This allows for increasing the "reach" of a focus tracker instance, by connecting two or more focus trackers together when DOM
71
- * elements they track are located in different subtrees in DOM. External focus trackers are listed in {@link #externalViews}.
72
- * * If a {@link module:ui/view~View} instance is passed that has no `FocusTracker` (**not** a {@link ~ViewWithFocusTracker}),
73
- * its {@link module:ui/view~View#element} is used to track focus like any other DOM element.
74
- */
75
- add(elementOrView) {
76
- if (isElement(elementOrView)) {
77
- this._addElement(elementOrView);
78
- }
79
- else {
80
- if (isViewWithFocusTracker(elementOrView)) {
81
- this._addView(elementOrView);
82
- }
83
- else {
84
- if (!elementOrView.element) {
85
- /**
86
- * The {@link module:ui/view~View} added to the {@link module:utils/focustracker~FocusTracker} does not have an
87
- * {@link module:ui/view~View#element}. Make sure the view is {@link module:ui/view~View#render} before adding
88
- * it to the focus tracker.
89
- *
90
- * @error focustracker-add-view-missing-element
91
- */
92
- throw new CKEditorError('focustracker-add-view-missing-element', {
93
- focusTracker: this,
94
- view: elementOrView
95
- });
96
- }
97
- this._addElement(elementOrView.element);
98
- }
99
- }
100
- }
101
- /**
102
- * Stops tracking focus in the specified DOM element or a {@link module:ui/view~View view instance}. See {@link #add} to learn more.
103
- */
104
- remove(elementOrView) {
105
- if (isElement(elementOrView)) {
106
- this._removeElement(elementOrView);
107
- }
108
- else {
109
- if (isViewWithFocusTracker(elementOrView)) {
110
- this._removeView(elementOrView);
111
- }
112
- else {
113
- // Assuming that if the view was successfully added, it must have come with an existing #element.
114
- this._removeElement(elementOrView.element);
115
- }
116
- }
117
- }
118
- /**
119
- * Adds a DOM element to the focus tracker and starts listening to the `focus` and `blur` events on it.
120
- */
121
- _addElement(element) {
122
- if (this._elements.has(element)) {
123
- /**
124
- * This element is already tracked by {@link module:utils/focustracker~FocusTracker}.
125
- *
126
- * @error focustracker-add-element-already-exist
127
- */
128
- throw new CKEditorError('focustracker-add-element-already-exist', this);
129
- }
130
- this.listenTo(element, 'focus', () => {
131
- // @if CK_DEBUG_FOCUSTRACKER // console.log( `"${ getName( this ) }": Focus with useCapture on DOM element` );
132
- const externalFocusedViewInSubtree = this.externalViews.find(view => isExternalViewSubtreeFocused(element, view));
133
- if (externalFocusedViewInSubtree) {
134
- this._focus(externalFocusedViewInSubtree.element);
135
- }
136
- else {
137
- this._focus(element);
138
- }
139
- }, { useCapture: true });
140
- this.listenTo(element, 'blur', () => {
141
- // @if CK_DEBUG_FOCUSTRACKER // console.log( `"${ getName( this ) }": Blur with useCapture on DOM element` );
142
- this._blur();
143
- }, { useCapture: true });
144
- this._elements.add(element);
145
- }
146
- /**
147
- * Removes a DOM element from the focus tracker.
148
- */
149
- _removeElement(element) {
150
- if (this._elements.has(element)) {
151
- this.stopListening(element);
152
- this._elements.delete(element);
153
- }
154
- if (element === this.focusedElement) {
155
- this._blur();
156
- }
157
- }
158
- /**
159
- * Adds an external {@link module:ui/view~View view instance} to this focus tracker and makes it contribute to this focus tracker's
160
- * state either by its `View#element` or by its `View#focusTracker` instance.
161
- */
162
- _addView(view) {
163
- if (view.element) {
164
- this._addElement(view.element);
165
- }
166
- this.listenTo(view.focusTracker, 'change:focusedElement', () => {
167
- // @if CK_DEBUG_FOCUSTRACKER // console.log(
168
- // @if CK_DEBUG_FOCUSTRACKER // `"${ getName( this ) }": Related "${ getName( view.focusTracker ) }"#focusedElement = `,
169
- // @if CK_DEBUG_FOCUSTRACKER // view.focusTracker.focusedElement
170
- // @if CK_DEBUG_FOCUSTRACKER // );
171
- if (view.focusTracker.focusedElement) {
172
- if (view.element) {
173
- this._focus(view.element);
174
- }
175
- }
176
- else {
177
- this._blur();
178
- }
179
- });
180
- this._externalViews.add(view);
181
- }
182
- /**
183
- * Removes an external {@link module:ui/view~View view instance} from this focus tracker.
184
- */
185
- _removeView(view) {
186
- if (view.element) {
187
- this._removeElement(view.element);
188
- }
189
- this.stopListening(view.focusTracker);
190
- this._externalViews.delete(view);
191
- }
192
- /**
193
- * Destroys the focus tracker by:
194
- * - Disabling all event listeners attached to tracked elements or external views.
195
- * - Removing all tracked elements and views that were previously added.
196
- */
197
- destroy() {
198
- this.stopListening();
199
- this._elements.clear();
200
- this._externalViews.clear();
201
- this.isFocused = false;
202
- this.focusedElement = null;
203
- }
204
- /**
205
- * Stores currently focused element as {@link #focusedElement} and sets {@link #isFocused} `true`.
206
- */
207
- _focus(element) {
208
- // @if CK_DEBUG_FOCUSTRACKER // console.log( `"${ getName( this ) }": _focus() on element`, element );
209
- this._clearBlurTimeout();
210
- this.focusedElement = element;
211
- this.isFocused = true;
212
- }
213
- /**
214
- * Clears currently {@link #focusedElement} and sets {@link #isFocused} `false`.
215
- *
216
- * This method uses `setTimeout()` to change order of `blur` and `focus` events calls, ensuring that moving focus between
217
- * two elements within a single focus tracker's scope, will not cause `[ blurA, focusB ]` sequence but just `[ focusB ]`.
218
- * The former would cause a momentary change of `#isFocused` to `false` which is not desired because any logic listening to
219
- * a focus tracker state would experience UI flashes and glitches as the user focus travels across the UI.
220
- */
221
- _blur() {
222
- const isAnyElementFocused = this.elements.find(element => element.contains(document.activeElement));
223
- // Avoid blurs originating from external FTs when the focus still remains in one of the #elements.
224
- if (isAnyElementFocused) {
225
- return;
226
- }
227
- const isAnyExternalViewFocused = this.externalViews.find(view => {
228
- // Do not consider external views's focus trackers as focused if there's a blur timeout pending.
229
- return view.focusTracker.isFocused && !view.focusTracker._blurTimeout;
230
- });
231
- // Avoid unnecessary DOM blurs coming from #elements when the focus still remains in one of #externalViews.
232
- if (isAnyExternalViewFocused) {
233
- return;
234
- }
235
- this._clearBlurTimeout();
236
- this._blurTimeout = setTimeout(() => {
237
- // @if CK_DEBUG_FOCUSTRACKER // console.log( `"${ getName( this ) }": Blur.` );
238
- this.focusedElement = null;
239
- this.isFocused = false;
240
- }, 0);
241
- }
242
- /**
243
- * Clears the asynchronous blur event timeout on demand. See {@link #_blur} to learn more.
244
- */
245
- _clearBlurTimeout() {
246
- clearTimeout(this._blurTimeout);
247
- this._blurTimeout = null;
248
- }
249
- }
250
- /**
251
- * Checks whether a view is an instance of {@link ~ViewWithFocusTracker}.
252
- */
253
- export function isViewWithFocusTracker(view) {
254
- return 'focusTracker' in view && view.focusTracker instanceof FocusTracker;
255
- }
256
- function isElement(value) {
257
- return _isElement(value);
258
- }
259
- function isExternalViewSubtreeFocused(subTreeRoot, view) {
260
- if (isFocusedView(subTreeRoot, view)) {
261
- return true;
262
- }
263
- return !!view.focusTracker.externalViews.find(view => isFocusedView(subTreeRoot, view));
264
- }
265
- function isFocusedView(subTreeRoot, view) {
266
- // Note: You cannot depend on externalView.focusTracker.focusedElement because blurs are asynchronous and the value may
267
- // be outdated when moving focus between two elements. Using document.activeElement instead.
268
- return !!view.element && view.element.contains(document.activeElement) && subTreeRoot.contains(view.element);
269
- }
270
- // @if CK_DEBUG_FOCUSTRACKER // declare global {
271
- // @if CK_DEBUG_FOCUSTRACKER // interface Window {
272
- // @if CK_DEBUG_FOCUSTRACKER // logFocusTrackers: Function;
273
- // @if CK_DEBUG_FOCUSTRACKER // }
274
- // @if CK_DEBUG_FOCUSTRACKER // }
275
- // @if CK_DEBUG_FOCUSTRACKER //
276
- // @if CK_DEBUG_FOCUSTRACKER // function getName( focusTracker: FocusTracker ): string {
277
- // @if CK_DEBUG_FOCUSTRACKER // return focusTracker._label || 'Unknown';
278
- // @if CK_DEBUG_FOCUSTRACKER // }
279
- // @if CK_DEBUG_FOCUSTRACKER //
280
- // @if CK_DEBUG_FOCUSTRACKER // function logState(
281
- // @if CK_DEBUG_FOCUSTRACKER // focusTracker: FocusTracker,
282
- // @if CK_DEBUG_FOCUSTRACKER // keysToLog: Array<string> = [ 'isFocused', 'focusedElement' ]
283
- // @if CK_DEBUG_FOCUSTRACKER // ): string {
284
- // @if CK_DEBUG_FOCUSTRACKER // keysToLog.forEach( key => { console.log( `${ key }=`, focusTracker[ key ] ) } );
285
- // @if CK_DEBUG_FOCUSTRACKER // console.log( 'elements', focusTracker.elements );
286
- // @if CK_DEBUG_FOCUSTRACKER // console.log( 'externalViews', focusTracker.externalViews );
287
- // @if CK_DEBUG_FOCUSTRACKER // }
288
- // @if CK_DEBUG_FOCUSTRACKER //
289
- // @if CK_DEBUG_FOCUSTRACKER // window.logFocusTrackers = (
290
- // @if CK_DEBUG_FOCUSTRACKER // filter = () => true,
291
- // @if CK_DEBUG_FOCUSTRACKER // keysToLog: Array<string>
292
- // @if CK_DEBUG_FOCUSTRACKER // ): void => {
293
- // @if CK_DEBUG_FOCUSTRACKER // console.group( 'FocusTrackers' );
294
- // @if CK_DEBUG_FOCUSTRACKER //
295
- // @if CK_DEBUG_FOCUSTRACKER // for ( const focusTracker of FocusTracker._instances ) {
296
- // @if CK_DEBUG_FOCUSTRACKER // if ( filter( focusTracker ) ) {
297
- // @if CK_DEBUG_FOCUSTRACKER // console.group( `"${ getName( focusTracker ) }"` );
298
- // @if CK_DEBUG_FOCUSTRACKER // logState( focusTracker, keysToLog );
299
- // @if CK_DEBUG_FOCUSTRACKER // console.groupEnd();
300
- // @if CK_DEBUG_FOCUSTRACKER // }
301
- // @if CK_DEBUG_FOCUSTRACKER // }
302
- // @if CK_DEBUG_FOCUSTRACKER //
303
- // @if CK_DEBUG_FOCUSTRACKER // console.groupEnd();
304
- // @if CK_DEBUG_FOCUSTRACKER // };
305
- // @if CK_DEBUG_FOCUSTRACKER //
306
- // @if CK_DEBUG_FOCUSTRACKER // window.logFocusTrackerTree = (
307
- // @if CK_DEBUG_FOCUSTRACKER // rootFocusTracker: FocusTracker,
308
- // @if CK_DEBUG_FOCUSTRACKER // filter = () => true,
309
- // @if CK_DEBUG_FOCUSTRACKER // keysToLog: Array<string>
310
- // @if CK_DEBUG_FOCUSTRACKER // ): void => {
311
- // @if CK_DEBUG_FOCUSTRACKER // console.group( 'FocusTrackers tree' );
312
- // @if CK_DEBUG_FOCUSTRACKER //
313
- // @if CK_DEBUG_FOCUSTRACKER // logBranch( rootFocusTracker, filter );
314
- // @if CK_DEBUG_FOCUSTRACKER //
315
- // @if CK_DEBUG_FOCUSTRACKER // function logBranch( focusTracker, filter ) {
316
- // @if CK_DEBUG_FOCUSTRACKER // console.group( `"${ getName( focusTracker ) }"` );
317
- // @if CK_DEBUG_FOCUSTRACKER // logState( focusTracker, keysToLog );
318
- // @if CK_DEBUG_FOCUSTRACKER //
319
- // @if CK_DEBUG_FOCUSTRACKER // for ( const externalView of focusTracker.externalViews ) {
320
- // @if CK_DEBUG_FOCUSTRACKER // if ( filter( externalView.focusTracker ) ) {
321
- // @if CK_DEBUG_FOCUSTRACKER // logBranch( externalView.focusTracker, filter );
322
- // @if CK_DEBUG_FOCUSTRACKER // }
323
- // @if CK_DEBUG_FOCUSTRACKER // }
324
- // @if CK_DEBUG_FOCUSTRACKER //
325
- // @if CK_DEBUG_FOCUSTRACKER // console.groupEnd();
326
- // @if CK_DEBUG_FOCUSTRACKER // }
327
- // @if CK_DEBUG_FOCUSTRACKER //
328
- // @if CK_DEBUG_FOCUSTRACKER // console.groupEnd();
329
- // @if CK_DEBUG_FOCUSTRACKER // };