@ckeditor/ckeditor5-utils 47.6.1 → 48.0.0-alpha.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 (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/collection.js DELETED
@@ -1,620 +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/collection
7
- */
8
- import { EmitterMixin } from './emittermixin.js';
9
- import { CKEditorError } from './ckeditorerror.js';
10
- import { uid } from './uid.js';
11
- import { isIterable } from './isiterable.js';
12
- /**
13
- * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
14
- * in the collection (like in an array) or by their ids.
15
- *
16
- * If an object without an `id` property is being added to the collection, the `id` property will be generated
17
- * automatically. Note that the automatically generated id is unique only within this single collection instance.
18
- *
19
- * By default an item in the collection is identified by its `id` property. The name of the identifier can be
20
- * configured through the constructor of the collection.
21
- *
22
- * @typeParam T The type of the collection element.
23
- */
24
- export class Collection extends /* #__PURE__ */ EmitterMixin() {
25
- /**
26
- * The internal list of items in the collection.
27
- */
28
- _items;
29
- /**
30
- * The internal map of items in the collection.
31
- */
32
- _itemMap;
33
- /**
34
- * The name of the property which is considered to identify an item.
35
- */
36
- _idProperty;
37
- /**
38
- * A collection instance this collection is bound to as a result
39
- * of calling {@link #bindTo} method.
40
- */
41
- _bindToCollection;
42
- /**
43
- * A helper mapping external items of a bound collection ({@link #bindTo})
44
- * and actual items of this collection. It provides information
45
- * necessary to properly remove items bound to another collection.
46
- *
47
- * See {@link #_bindToInternalToExternalMap}.
48
- */
49
- _bindToExternalToInternalMap;
50
- /**
51
- * A helper mapping items of this collection to external items of a bound collection
52
- * ({@link #bindTo}). It provides information necessary to manage the bindings, e.g.
53
- * to avoid loops in two–way bindings.
54
- *
55
- * See {@link #_bindToExternalToInternalMap}.
56
- */
57
- _bindToInternalToExternalMap;
58
- /**
59
- * Stores indexes of skipped items from bound external collection.
60
- */
61
- _skippedIndexesFromExternal;
62
- constructor(initialItemsOrOptions = {}, options = {}) {
63
- super();
64
- const hasInitialItems = isIterable(initialItemsOrOptions);
65
- if (!hasInitialItems) {
66
- options = initialItemsOrOptions;
67
- }
68
- this._items = [];
69
- this._itemMap = new Map();
70
- this._idProperty = options.idProperty || 'id';
71
- this._bindToExternalToInternalMap = new WeakMap();
72
- this._bindToInternalToExternalMap = new WeakMap();
73
- this._skippedIndexesFromExternal = [];
74
- // Set the initial content of the collection (if provided in the constructor).
75
- if (hasInitialItems) {
76
- for (const item of initialItemsOrOptions) {
77
- this._items.push(item);
78
- this._itemMap.set(this._getItemIdBeforeAdding(item), item);
79
- }
80
- }
81
- }
82
- /**
83
- * The number of items available in the collection.
84
- */
85
- get length() {
86
- return this._items.length;
87
- }
88
- /**
89
- * Returns the first item from the collection or null when collection is empty.
90
- */
91
- get first() {
92
- return this._items[0] || null;
93
- }
94
- /**
95
- * Returns the last item from the collection or null when collection is empty.
96
- */
97
- get last() {
98
- return this._items[this.length - 1] || null;
99
- }
100
- /**
101
- * Adds an item into the collection.
102
- *
103
- * If the item does not have an id, then it will be automatically generated and set on the item.
104
- *
105
- * @param item
106
- * @param index The position of the item in the collection. The item
107
- * is pushed to the collection when `index` not specified.
108
- * @fires add
109
- * @fires change
110
- */
111
- add(item, index) {
112
- return this.addMany([item], index);
113
- }
114
- /**
115
- * Adds multiple items into the collection.
116
- *
117
- * Any item not containing an id will get an automatically generated one.
118
- *
119
- * @param items
120
- * @param index The position of the insertion. Items will be appended if no `index` is specified.
121
- * @fires add
122
- * @fires change
123
- */
124
- addMany(items, index) {
125
- if (index === undefined) {
126
- index = this._items.length;
127
- }
128
- else if (index > this._items.length || index < 0) {
129
- /**
130
- * The `index` passed to {@link module:utils/collection~Collection#addMany `Collection#addMany()`}
131
- * is invalid. It must be a number between 0 and the collection's length.
132
- *
133
- * @error collection-add-item-invalid-index
134
- */
135
- throw new CKEditorError('collection-add-item-invalid-index', this);
136
- }
137
- let offset = 0;
138
- for (const item of items) {
139
- const itemId = this._getItemIdBeforeAdding(item);
140
- const currentItemIndex = index + offset;
141
- this._items.splice(currentItemIndex, 0, item);
142
- this._itemMap.set(itemId, item);
143
- this.fire('add', item, currentItemIndex);
144
- offset++;
145
- }
146
- this.fire('change', {
147
- added: items,
148
- removed: [],
149
- index
150
- });
151
- return this;
152
- }
153
- /**
154
- * Gets an item by its ID or index.
155
- *
156
- * @param idOrIndex The item ID or index in the collection.
157
- * @returns The requested item or `null` if such item does not exist.
158
- */
159
- get(idOrIndex) {
160
- let item;
161
- if (typeof idOrIndex == 'string') {
162
- item = this._itemMap.get(idOrIndex);
163
- }
164
- else if (typeof idOrIndex == 'number') {
165
- item = this._items[idOrIndex];
166
- }
167
- else {
168
- /**
169
- * An index or ID must be given.
170
- *
171
- * @error collection-get-invalid-arg
172
- */
173
- throw new CKEditorError('collection-get-invalid-arg', this);
174
- }
175
- return item || null;
176
- }
177
- /**
178
- * Returns a Boolean indicating whether the collection contains an item.
179
- *
180
- * @param itemOrId The item or its ID in the collection.
181
- * @returns `true` if the collection contains the item, `false` otherwise.
182
- */
183
- has(itemOrId) {
184
- if (typeof itemOrId == 'string') {
185
- return this._itemMap.has(itemOrId);
186
- }
187
- else { // Object
188
- const idProperty = this._idProperty;
189
- const id = itemOrId[idProperty];
190
- return id && this._itemMap.has(id);
191
- }
192
- }
193
- /**
194
- * Gets an index of an item in the collection.
195
- * When an item is not defined in the collection, the index will equal -1.
196
- *
197
- * @param itemOrId The item or its ID in the collection.
198
- * @returns The index of a given item.
199
- */
200
- getIndex(itemOrId) {
201
- let item;
202
- if (typeof itemOrId == 'string') {
203
- item = this._itemMap.get(itemOrId);
204
- }
205
- else {
206
- item = itemOrId;
207
- }
208
- return item ? this._items.indexOf(item) : -1;
209
- }
210
- /**
211
- * Removes an item from the collection.
212
- *
213
- * @param subject The item to remove, its ID or index in the collection.
214
- * @returns The removed item.
215
- * @fires remove
216
- * @fires change
217
- */
218
- remove(subject) {
219
- const [item, index] = this._remove(subject);
220
- this.fire('change', {
221
- added: [],
222
- removed: [item],
223
- index
224
- });
225
- return item;
226
- }
227
- /**
228
- * Executes the callback for each item in the collection and composes an array or values returned by this callback.
229
- *
230
- * @typeParam U The result type of the callback.
231
- * @param callback
232
- * @param ctx Context in which the `callback` will be called.
233
- * @returns The result of mapping.
234
- */
235
- map(callback, ctx) {
236
- return this._items.map(callback, ctx);
237
- }
238
- /**
239
- * Performs the specified action for each item in the collection.
240
- *
241
- * @param ctx Context in which the `callback` will be called.
242
- */
243
- forEach(callback, ctx) {
244
- this._items.forEach(callback, ctx);
245
- }
246
- /**
247
- * Finds the first item in the collection for which the `callback` returns a true value.
248
- *
249
- * @param callback
250
- * @param ctx Context in which the `callback` will be called.
251
- * @returns The item for which `callback` returned a true value.
252
- */
253
- find(callback, ctx) {
254
- return this._items.find(callback, ctx);
255
- }
256
- /**
257
- * Returns an array with items for which the `callback` returned a true value.
258
- *
259
- * @param callback
260
- * @param ctx Context in which the `callback` will be called.
261
- * @returns The array with matching items.
262
- */
263
- filter(callback, ctx) {
264
- return this._items.filter(callback, ctx);
265
- }
266
- /**
267
- * Removes all items from the collection and destroys the binding created using
268
- * {@link #bindTo}.
269
- *
270
- * @fires remove
271
- * @fires change
272
- */
273
- clear() {
274
- if (this._bindToCollection) {
275
- this.stopListening(this._bindToCollection);
276
- this._bindToCollection = null;
277
- }
278
- const removedItems = Array.from(this._items);
279
- while (this.length) {
280
- this._remove(0);
281
- }
282
- this.fire('change', {
283
- added: [],
284
- removed: removedItems,
285
- index: 0
286
- });
287
- }
288
- /**
289
- * Binds and synchronizes the collection with another one.
290
- *
291
- * The binding can be a simple factory:
292
- *
293
- * ```ts
294
- * class FactoryClass {
295
- * public label: string;
296
- *
297
- * constructor( data: { label: string } ) {
298
- * this.label = data.label;
299
- * }
300
- * }
301
- *
302
- * const source = new Collection<{ label: string }>( { idProperty: 'label' } );
303
- * const target = new Collection<FactoryClass>();
304
- *
305
- * target.bindTo( source ).as( FactoryClass );
306
- *
307
- * source.add( { label: 'foo' } );
308
- * source.add( { label: 'bar' } );
309
- *
310
- * console.log( target.length ); // 2
311
- * console.log( target.get( 1 ).label ); // 'bar'
312
- *
313
- * source.remove( 0 );
314
- * console.log( target.length ); // 1
315
- * console.log( target.get( 0 ).label ); // 'bar'
316
- * ```
317
- *
318
- * or the factory driven by a custom callback:
319
- *
320
- * ```ts
321
- * class FooClass {
322
- * public label: string;
323
- *
324
- * constructor( data: { label: string } ) {
325
- * this.label = data.label;
326
- * }
327
- * }
328
- *
329
- * class BarClass {
330
- * public label: string;
331
- *
332
- * constructor( data: { label: string } ) {
333
- * this.label = data.label;
334
- * }
335
- * }
336
- *
337
- * const source = new Collection<{ label: string }>( { idProperty: 'label' } );
338
- * const target = new Collection<FooClass | BarClass>();
339
- *
340
- * target.bindTo( source ).using( ( item ) => {
341
- * if ( item.label == 'foo' ) {
342
- * return new FooClass( item );
343
- * } else {
344
- * return new BarClass( item );
345
- * }
346
- * } );
347
- *
348
- * source.add( { label: 'foo' } );
349
- * source.add( { label: 'bar' } );
350
- *
351
- * console.log( target.length ); // 2
352
- * console.log( target.get( 0 ) instanceof FooClass ); // true
353
- * console.log( target.get( 1 ) instanceof BarClass ); // true
354
- * ```
355
- *
356
- * or the factory out of property name:
357
- *
358
- * ```ts
359
- * const source = new Collection<{ nested: { value: string } }>();
360
- * const target = new Collection<{ value: string }>();
361
- *
362
- * target.bindTo( source ).using( 'nested' );
363
- *
364
- * source.add( { nested: { value: 'foo' } } );
365
- * source.add( { nested: { value: 'bar' } } );
366
- *
367
- * console.log( target.length ); // 2
368
- * console.log( target.get( 0 ).value ); // 'foo'
369
- * console.log( target.get( 1 ).value ); // 'bar'
370
- * ```
371
- *
372
- * It's possible to skip specified items by returning null value:
373
- *
374
- * ```ts
375
- * const source = new Collection<{ hidden: boolean }>();
376
- * const target = new Collection<{ hidden: boolean }>();
377
- *
378
- * target.bindTo( source ).using( item => {
379
- * if ( item.hidden ) {
380
- * return null;
381
- * }
382
- *
383
- * return item;
384
- * } );
385
- *
386
- * source.add( { hidden: true } );
387
- * source.add( { hidden: false } );
388
- *
389
- * console.log( source.length ); // 2
390
- * console.log( target.length ); // 1
391
- * ```
392
- *
393
- * **Note**: {@link #clear} can be used to break the binding.
394
- *
395
- * @typeParam S The type of `externalCollection` element.
396
- * @param externalCollection A collection to be bound.
397
- * @returns The binding chain object.
398
- */
399
- bindTo(externalCollection) {
400
- if (this._bindToCollection) {
401
- /**
402
- * The collection cannot be bound more than once.
403
- *
404
- * @error collection-bind-to-rebind
405
- */
406
- throw new CKEditorError('collection-bind-to-rebind', this);
407
- }
408
- this._bindToCollection = externalCollection;
409
- return {
410
- as: Class => {
411
- this._setUpBindToBinding(item => new Class(item));
412
- },
413
- using: callbackOrProperty => {
414
- if (typeof callbackOrProperty == 'function') {
415
- this._setUpBindToBinding(callbackOrProperty);
416
- }
417
- else {
418
- this._setUpBindToBinding(item => item[callbackOrProperty]);
419
- }
420
- }
421
- };
422
- }
423
- /**
424
- * Finalizes and activates a binding initiated by {@link #bindTo}.
425
- *
426
- * @param factory A function which produces collection items.
427
- */
428
- _setUpBindToBinding(factory) {
429
- const externalCollection = this._bindToCollection;
430
- // Adds the item to the collection once a change has been done to the external collection.
431
- const addItem = (evt, externalItem, index) => {
432
- const isExternalBoundToThis = externalCollection._bindToCollection == this;
433
- const externalItemBound = externalCollection._bindToInternalToExternalMap.get(externalItem);
434
- // If an external collection is bound to this collection, which makes it a 2–way binding,
435
- // and the particular external collection item is already bound, don't add it here.
436
- // The external item has been created **out of this collection's item** and (re)adding it will
437
- // cause a loop.
438
- if (isExternalBoundToThis && externalItemBound) {
439
- this._bindToExternalToInternalMap.set(externalItem, externalItemBound);
440
- this._bindToInternalToExternalMap.set(externalItemBound, externalItem);
441
- }
442
- else {
443
- const item = factory(externalItem);
444
- // When there is no item we need to remember skipped index first and then we can skip this item.
445
- if (!item) {
446
- this._skippedIndexesFromExternal.push(index);
447
- return;
448
- }
449
- // Lets try to put item at the same index as index in external collection
450
- // but when there are a skipped items in one or both collections we need to recalculate this index.
451
- let finalIndex = index;
452
- // When we try to insert item after some skipped items from external collection we need
453
- // to include this skipped items and decrease index.
454
- //
455
- // For the following example:
456
- // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal' ]
457
- // internal -> [ A ]
458
- //
459
- // Another item is been added at the end of external collection:
460
- // external.add( 'D' )
461
- // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal', 'D' ]
462
- //
463
- // We can't just add 'D' to internal at the same index as index in external because
464
- // this will produce empty indexes what is invalid:
465
- // internal -> [ 'A', empty, empty, 'D' ]
466
- //
467
- // So we need to include skipped items and decrease index
468
- // internal -> [ 'A', 'D' ]
469
- for (const skipped of this._skippedIndexesFromExternal) {
470
- if (index > skipped) {
471
- finalIndex--;
472
- }
473
- }
474
- // We need to take into consideration that external collection could skip some items from
475
- // internal collection.
476
- //
477
- // For the following example:
478
- // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external' ]
479
- // external -> [ A ]
480
- //
481
- // Another item is been added at the end of external collection:
482
- // external.add( 'D' )
483
- // external -> [ 'A', 'D' ]
484
- //
485
- // We need to include skipped items and place new item after them:
486
- // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external', 'D' ]
487
- for (const skipped of externalCollection._skippedIndexesFromExternal) {
488
- if (finalIndex >= skipped) {
489
- finalIndex++;
490
- }
491
- }
492
- this._bindToExternalToInternalMap.set(externalItem, item);
493
- this._bindToInternalToExternalMap.set(item, externalItem);
494
- this.add(item, finalIndex);
495
- // After adding new element to internal collection we need update indexes
496
- // of skipped items in external collection.
497
- for (let i = 0; i < externalCollection._skippedIndexesFromExternal.length; i++) {
498
- if (finalIndex <= externalCollection._skippedIndexesFromExternal[i]) {
499
- externalCollection._skippedIndexesFromExternal[i]++;
500
- }
501
- }
502
- }
503
- };
504
- // Load the initial content of the collection.
505
- for (const externalItem of externalCollection) {
506
- addItem(null, externalItem, externalCollection.getIndex(externalItem));
507
- }
508
- // Synchronize the with collection as new items are added.
509
- this.listenTo(externalCollection, 'add', addItem);
510
- // Synchronize the with collection as new items are removed.
511
- this.listenTo(externalCollection, 'remove', (evt, externalItem, index) => {
512
- const item = this._bindToExternalToInternalMap.get(externalItem);
513
- if (item) {
514
- this.remove(item);
515
- }
516
- // After removing element from external collection we need update/remove indexes
517
- // of skipped items in internal collection.
518
- this._skippedIndexesFromExternal = this._skippedIndexesFromExternal.reduce((result, skipped) => {
519
- if (index < skipped) {
520
- result.push(skipped - 1);
521
- }
522
- if (index > skipped) {
523
- result.push(skipped);
524
- }
525
- return result;
526
- }, []);
527
- });
528
- }
529
- /**
530
- * Returns an unique id property for a given `item`.
531
- *
532
- * The method will generate new id and assign it to the `item` if it doesn't have any.
533
- *
534
- * @param item Item to be added.
535
- */
536
- _getItemIdBeforeAdding(item) {
537
- const idProperty = this._idProperty;
538
- let itemId;
539
- if ((idProperty in item)) {
540
- itemId = item[idProperty];
541
- if (typeof itemId != 'string') {
542
- /**
543
- * This item's ID should be a string.
544
- *
545
- * @error collection-add-invalid-id
546
- */
547
- throw new CKEditorError('collection-add-invalid-id', this);
548
- }
549
- if (this.get(itemId)) {
550
- /**
551
- * This item already exists in the collection.
552
- *
553
- * @error collection-add-item-already-exists
554
- */
555
- throw new CKEditorError('collection-add-item-already-exists', this);
556
- }
557
- }
558
- else {
559
- item[idProperty] = itemId = uid();
560
- }
561
- return itemId;
562
- }
563
- /**
564
- * Core {@link #remove} method implementation shared in other functions.
565
- *
566
- * In contrast this method **does not** fire the {@link #event:change} event.
567
- *
568
- * @param subject The item to remove, its id or index in the collection.
569
- * @returns Returns an array with the removed item and its index.
570
- * @fires remove
571
- */
572
- _remove(subject) {
573
- let index, id, item;
574
- let itemDoesNotExist = false;
575
- const idProperty = this._idProperty;
576
- if (typeof subject == 'string') {
577
- id = subject;
578
- item = this._itemMap.get(id);
579
- itemDoesNotExist = !item;
580
- if (item) {
581
- index = this._items.indexOf(item);
582
- }
583
- }
584
- else if (typeof subject == 'number') {
585
- index = subject;
586
- item = this._items[index];
587
- itemDoesNotExist = !item;
588
- if (item) {
589
- id = item[idProperty];
590
- }
591
- }
592
- else {
593
- item = subject;
594
- id = item[idProperty];
595
- index = this._items.indexOf(item);
596
- itemDoesNotExist = (index == -1 || !this._itemMap.get(id));
597
- }
598
- if (itemDoesNotExist) {
599
- /**
600
- * Item not found.
601
- *
602
- * @error collection-remove-404
603
- */
604
- throw new CKEditorError('collection-remove-404', this);
605
- }
606
- this._items.splice(index, 1);
607
- this._itemMap.delete(id);
608
- const externalItem = this._bindToInternalToExternalMap.get(item);
609
- this._bindToInternalToExternalMap.delete(item);
610
- this._bindToExternalToInternalMap.delete(externalItem);
611
- this.fire('remove', item, index);
612
- return [item, index];
613
- }
614
- /**
615
- * Iterable interface.
616
- */
617
- [Symbol.iterator]() {
618
- return this._items[Symbol.iterator]();
619
- }
620
- }
@@ -1,52 +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/collectstylesheets
7
- */
8
- /**
9
- * A helper function for getting concatenated CSS rules from external stylesheets.
10
- *
11
- * @param stylesheets An array of stylesheet paths delivered by the user through the plugin configuration.
12
- */
13
- export async function collectStylesheets(stylesheets) {
14
- if (!stylesheets) {
15
- return '';
16
- }
17
- const results = await Promise.all(stylesheets.map(async (stylesheet) => {
18
- if (stylesheet === 'EDITOR_STYLES') {
19
- return getEditorStyles();
20
- }
21
- const response = await window.fetch(stylesheet);
22
- return response.text();
23
- }));
24
- return results.join(' ').trim();
25
- }
26
- /**
27
- * A helper function for getting the basic editor content styles for the `.ck-content` class
28
- * and all CSS variables defined in the document.
29
- */
30
- function getEditorStyles() {
31
- const editorStyles = [];
32
- const editorCSSVariables = [];
33
- for (const styleSheet of Array.from(document.styleSheets)) {
34
- const ownerNode = styleSheet.ownerNode;
35
- if (ownerNode.hasAttribute('data-cke')) {
36
- for (const rule of Array.from(styleSheet.cssRules)) {
37
- if (rule.cssText.indexOf('.ck-content') !== -1) {
38
- editorStyles.push(rule.cssText);
39
- }
40
- else if (rule.cssText.indexOf(':root') !== -1) {
41
- editorCSSVariables.push(rule.cssText);
42
- }
43
- }
44
- }
45
- }
46
- if (!editorStyles.length) {
47
- console.warn('The editor stylesheet could not be found in the document. ' +
48
- 'Check your webpack config - style-loader should use data-cke=true attribute for the editor stylesheet.');
49
- }
50
- // We want to trim the returned value in case of `[ "", "", ... ]`.
51
- return [...editorCSSVariables, ...editorStyles].join(' ').trim();
52
- }