@ckeditor/ckeditor5-utils 34.2.0 → 35.0.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 (61) hide show
  1. package/CHANGELOG.md +324 -0
  2. package/LICENSE.md +1 -1
  3. package/package.json +19 -8
  4. package/src/areconnectedthroughproperties.js +0 -92
  5. package/src/ckeditorerror.js +0 -217
  6. package/src/collection.js +0 -785
  7. package/src/comparearrays.js +0 -51
  8. package/src/config.js +0 -246
  9. package/src/count.js +0 -26
  10. package/src/diff.js +0 -138
  11. package/src/difftochanges.js +0 -86
  12. package/src/dom/createelement.js +0 -49
  13. package/src/dom/emittermixin.js +0 -341
  14. package/src/dom/getancestors.js +0 -31
  15. package/src/dom/getborderwidths.js +0 -27
  16. package/src/dom/getcommonancestor.js +0 -31
  17. package/src/dom/getdatafromelement.js +0 -24
  18. package/src/dom/getpositionedancestor.js +0 -28
  19. package/src/dom/global.js +0 -26
  20. package/src/dom/indexof.js +0 -25
  21. package/src/dom/insertat.js +0 -19
  22. package/src/dom/iscomment.js +0 -20
  23. package/src/dom/isnode.js +0 -26
  24. package/src/dom/isrange.js +0 -18
  25. package/src/dom/istext.js +0 -18
  26. package/src/dom/isvisible.js +0 -25
  27. package/src/dom/iswindow.js +0 -30
  28. package/src/dom/position.js +0 -518
  29. package/src/dom/rect.js +0 -443
  30. package/src/dom/remove.js +0 -21
  31. package/src/dom/resizeobserver.js +0 -378
  32. package/src/dom/scroll.js +0 -302
  33. package/src/dom/setdatainelement.js +0 -24
  34. package/src/dom/tounit.js +0 -27
  35. package/src/elementreplacer.js +0 -57
  36. package/src/emittermixin.js +0 -719
  37. package/src/env.js +0 -190
  38. package/src/eventinfo.js +0 -79
  39. package/src/fastdiff.js +0 -261
  40. package/src/first.js +0 -24
  41. package/src/focustracker.js +0 -157
  42. package/src/index.js +0 -45
  43. package/src/inserttopriorityarray.js +0 -42
  44. package/src/isiterable.js +0 -18
  45. package/src/keyboard.js +0 -301
  46. package/src/keystrokehandler.js +0 -130
  47. package/src/language.js +0 -26
  48. package/src/locale.js +0 -176
  49. package/src/mapsequal.js +0 -32
  50. package/src/mix.js +0 -47
  51. package/src/nth.js +0 -31
  52. package/src/objecttomap.js +0 -29
  53. package/src/observablemixin.js +0 -908
  54. package/src/priorities.js +0 -44
  55. package/src/spy.js +0 -25
  56. package/src/toarray.js +0 -18
  57. package/src/tomap.js +0 -29
  58. package/src/translation-service.js +0 -216
  59. package/src/uid.js +0 -59
  60. package/src/unicode.js +0 -106
  61. package/src/version.js +0 -157
package/src/collection.js DELETED
@@ -1,785 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2022, 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
- /**
7
- * @module utils/collection
8
- */
9
-
10
- import EmitterMixin from './emittermixin';
11
- import CKEditorError from './ckeditorerror';
12
- import uid from './uid';
13
- import isIterable from './isiterable';
14
- import mix from './mix';
15
-
16
- /**
17
- * Collections are ordered sets of objects. Items in the collection can be retrieved by their indexes
18
- * in the collection (like in an array) or by their ids.
19
- *
20
- * If an object without an `id` property is being added to the collection, the `id` property will be generated
21
- * automatically. Note that the automatically generated id is unique only within this single collection instance.
22
- *
23
- * By default an item in the collection is identified by its `id` property. The name of the identifier can be
24
- * configured through the constructor of the collection.
25
- *
26
- * @mixes module:utils/emittermixin~EmitterMixin
27
- */
28
- export default class Collection {
29
- /**
30
- * Creates a new Collection instance.
31
- *
32
- * You can provide an iterable of initial items the collection will be created with:
33
- *
34
- * const collection = new Collection( [ { id: 'John' }, { id: 'Mike' } ] );
35
- *
36
- * console.log( collection.get( 0 ) ); // -> { id: 'John' }
37
- * console.log( collection.get( 1 ) ); // -> { id: 'Mike' }
38
- * console.log( collection.get( 'Mike' ) ); // -> { id: 'Mike' }
39
- *
40
- * Or you can first create a collection and then add new items using the {@link #add} method:
41
- *
42
- * const collection = new Collection();
43
- *
44
- * collection.add( { id: 'John' } );
45
- * console.log( collection.get( 0 ) ); // -> { id: 'John' }
46
- *
47
- * Whatever option you choose, you can always pass a configuration object as the last argument
48
- * of the constructor:
49
- *
50
- * const emptyCollection = new Collection( { idProperty: 'name' } );
51
- * emptyCollection.add( { name: 'John' } );
52
- * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
53
- *
54
- * const nonEmptyCollection = new Collection( [ { name: 'John' } ], { idProperty: 'name' } );
55
- * nonEmptyCollection.add( { name: 'George' } );
56
- * console.log( collection.get( 'George' ) ); // -> { name: 'George' }
57
- * console.log( collection.get( 'John' ) ); // -> { name: 'John' }
58
- *
59
- * @param {Iterable.<Object>|Object} [initialItemsOrOptions] The initial items of the collection or
60
- * the options object.
61
- * @param {Object} [options={}] The options object, when the first argument is an array of initial items.
62
- * @param {String} [options.idProperty='id'] The name of the property which is used to identify an item.
63
- * Items that do not have such a property will be assigned one when added to the collection.
64
- */
65
- constructor( initialItemsOrOptions = {}, options = {} ) {
66
- const hasInitialItems = isIterable( initialItemsOrOptions );
67
-
68
- if ( !hasInitialItems ) {
69
- options = initialItemsOrOptions;
70
- }
71
-
72
- /**
73
- * The internal list of items in the collection.
74
- *
75
- * @private
76
- * @member {Object[]}
77
- */
78
- this._items = [];
79
-
80
- /**
81
- * The internal map of items in the collection.
82
- *
83
- * @private
84
- * @member {Map}
85
- */
86
- this._itemMap = new Map();
87
-
88
- /**
89
- * The name of the property which is considered to identify an item.
90
- *
91
- * @private
92
- * @member {String}
93
- */
94
- this._idProperty = options.idProperty || 'id';
95
-
96
- /**
97
- * A helper mapping external items of a bound collection ({@link #bindTo})
98
- * and actual items of this collection. It provides information
99
- * necessary to properly remove items bound to another collection.
100
- *
101
- * See {@link #_bindToInternalToExternalMap}.
102
- *
103
- * @protected
104
- * @member {WeakMap}
105
- */
106
- this._bindToExternalToInternalMap = new WeakMap();
107
-
108
- /**
109
- * A helper mapping items of this collection to external items of a bound collection
110
- * ({@link #bindTo}). It provides information necessary to manage the bindings, e.g.
111
- * to avoid loops in two–way bindings.
112
- *
113
- * See {@link #_bindToExternalToInternalMap}.
114
- *
115
- * @protected
116
- * @member {WeakMap}
117
- */
118
- this._bindToInternalToExternalMap = new WeakMap();
119
-
120
- /**
121
- * Stores indexes of skipped items from bound external collection.
122
- *
123
- * @private
124
- * @member {Array}
125
- */
126
- this._skippedIndexesFromExternal = [];
127
-
128
- // Set the initial content of the collection (if provided in the constructor).
129
- if ( hasInitialItems ) {
130
- for ( const item of initialItemsOrOptions ) {
131
- this._items.push( item );
132
- this._itemMap.set( this._getItemIdBeforeAdding( item ), item );
133
- }
134
- }
135
-
136
- /**
137
- * A collection instance this collection is bound to as a result
138
- * of calling {@link #bindTo} method.
139
- *
140
- * @protected
141
- * @member {module:utils/collection~Collection} #_bindToCollection
142
- */
143
- }
144
-
145
- /**
146
- * The number of items available in the collection.
147
- *
148
- * @member {Number} #length
149
- */
150
- get length() {
151
- return this._items.length;
152
- }
153
-
154
- /**
155
- * Returns the first item from the collection or null when collection is empty.
156
- *
157
- * @returns {Object|null} The first item or `null` if collection is empty.
158
- */
159
- get first() {
160
- return this._items[ 0 ] || null;
161
- }
162
-
163
- /**
164
- * Returns the last item from the collection or null when collection is empty.
165
- *
166
- * @returns {Object|null} The last item or `null` if collection is empty.
167
- */
168
- get last() {
169
- return this._items[ this.length - 1 ] || null;
170
- }
171
-
172
- /**
173
- * Adds an item into the collection.
174
- *
175
- * If the item does not have an id, then it will be automatically generated and set on the item.
176
- *
177
- * @chainable
178
- * @param {Object} item
179
- * @param {Number} [index] The position of the item in the collection. The item
180
- * is pushed to the collection when `index` not specified.
181
- * @fires add
182
- * @fires change
183
- */
184
- add( item, index ) {
185
- return this.addMany( [ item ], index );
186
- }
187
-
188
- /**
189
- * Adds multiple items into the collection.
190
- *
191
- * Any item not containing an id will get an automatically generated one.
192
- *
193
- * @chainable
194
- * @param {Iterable.<Object>} item
195
- * @param {Number} [index] The position of the insertion. Items will be appended if no `index` is specified.
196
- * @fires add
197
- * @fires change
198
- */
199
- addMany( items, index ) {
200
- if ( index === undefined ) {
201
- index = this._items.length;
202
- } else if ( index > this._items.length || index < 0 ) {
203
- /**
204
- * The `index` passed to {@link module:utils/collection~Collection#addMany `Collection#addMany()`}
205
- * is invalid. It must be a number between 0 and the collection's length.
206
- *
207
- * @error collection-add-item-invalid-index
208
- */
209
- throw new CKEditorError( 'collection-add-item-invalid-index', this );
210
- }
211
-
212
- for ( let offset = 0; offset < items.length; offset++ ) {
213
- const item = items[ offset ];
214
- const itemId = this._getItemIdBeforeAdding( item );
215
- const currentItemIndex = index + offset;
216
-
217
- this._items.splice( currentItemIndex, 0, item );
218
- this._itemMap.set( itemId, item );
219
-
220
- this.fire( 'add', item, currentItemIndex );
221
- }
222
-
223
- this.fire( 'change', {
224
- added: items,
225
- removed: [],
226
- index
227
- } );
228
-
229
- return this;
230
- }
231
-
232
- /**
233
- * Gets an item by its ID or index.
234
- *
235
- * @param {String|Number} idOrIndex The item ID or index in the collection.
236
- * @returns {Object|null} The requested item or `null` if such item does not exist.
237
- */
238
- get( idOrIndex ) {
239
- let item;
240
-
241
- if ( typeof idOrIndex == 'string' ) {
242
- item = this._itemMap.get( idOrIndex );
243
- } else if ( typeof idOrIndex == 'number' ) {
244
- item = this._items[ idOrIndex ];
245
- } else {
246
- /**
247
- * An index or ID must be given.
248
- *
249
- * @error collection-get-invalid-arg
250
- */
251
- throw new CKEditorError( 'collection-get-invalid-arg', this );
252
- }
253
-
254
- return item || null;
255
- }
256
-
257
- /**
258
- * Returns a Boolean indicating whether the collection contains an item.
259
- *
260
- * @param {Object|String} itemOrId The item or its ID in the collection.
261
- * @returns {Boolean} `true` if the collection contains the item, `false` otherwise.
262
- */
263
- has( itemOrId ) {
264
- if ( typeof itemOrId == 'string' ) {
265
- return this._itemMap.has( itemOrId );
266
- } else { // Object
267
- const idProperty = this._idProperty;
268
- const id = itemOrId[ idProperty ];
269
-
270
- return this._itemMap.has( id );
271
- }
272
- }
273
-
274
- /**
275
- * Gets an index of an item in the collection.
276
- * When an item is not defined in the collection, the index will equal -1.
277
- *
278
- * @param {Object|String} itemOrId The item or its ID in the collection.
279
- * @returns {Number} The index of a given item.
280
- */
281
- getIndex( itemOrId ) {
282
- let item;
283
-
284
- if ( typeof itemOrId == 'string' ) {
285
- item = this._itemMap.get( itemOrId );
286
- } else {
287
- item = itemOrId;
288
- }
289
-
290
- return this._items.indexOf( item );
291
- }
292
-
293
- /**
294
- * Removes an item from the collection.
295
- *
296
- * @param {Object|Number|String} subject The item to remove, its ID or index in the collection.
297
- * @returns {Object} The removed item.
298
- * @fires remove
299
- * @fires change
300
- */
301
- remove( subject ) {
302
- const [ item, index ] = this._remove( subject );
303
-
304
- this.fire( 'change', {
305
- added: [],
306
- removed: [ item ],
307
- index
308
- } );
309
-
310
- return item;
311
- }
312
-
313
- /**
314
- * Executes the callback for each item in the collection and composes an array or values returned by this callback.
315
- *
316
- * @param {Function} callback
317
- * @param {Object} callback.item
318
- * @param {Number} callback.index
319
- * @param {Object} ctx Context in which the `callback` will be called.
320
- * @returns {Array} The result of mapping.
321
- */
322
- map( callback, ctx ) {
323
- return this._items.map( callback, ctx );
324
- }
325
-
326
- /**
327
- * Finds the first item in the collection for which the `callback` returns a true value.
328
- *
329
- * @param {Function} callback
330
- * @param {Object} callback.item
331
- * @param {Number} callback.index
332
- * @param {Object} ctx Context in which the `callback` will be called.
333
- * @returns {Object} The item for which `callback` returned a true value.
334
- */
335
- find( callback, ctx ) {
336
- return this._items.find( callback, ctx );
337
- }
338
-
339
- /**
340
- * Returns an array with items for which the `callback` returned a true value.
341
- *
342
- * @param {Function} callback
343
- * @param {Object} callback.item
344
- * @param {Number} callback.index
345
- * @param {Object} ctx Context in which the `callback` will be called.
346
- * @returns {Object[]} The array with matching items.
347
- */
348
- filter( callback, ctx ) {
349
- return this._items.filter( callback, ctx );
350
- }
351
-
352
- /**
353
- * Removes all items from the collection and destroys the binding created using
354
- * {@link #bindTo}.
355
- *
356
- * @fires remove
357
- * @fires change
358
- */
359
- clear() {
360
- if ( this._bindToCollection ) {
361
- this.stopListening( this._bindToCollection );
362
- this._bindToCollection = null;
363
- }
364
-
365
- const removedItems = Array.from( this._items );
366
-
367
- while ( this.length ) {
368
- this._remove( 0 );
369
- }
370
-
371
- this.fire( 'change', {
372
- added: [],
373
- removed: removedItems,
374
- index: 0
375
- } );
376
- }
377
-
378
- /**
379
- * Binds and synchronizes the collection with another one.
380
- *
381
- * The binding can be a simple factory:
382
- *
383
- * class FactoryClass {
384
- * constructor( data ) {
385
- * this.label = data.label;
386
- * }
387
- * }
388
- *
389
- * const source = new Collection( { idProperty: 'label' } );
390
- * const target = new Collection();
391
- *
392
- * target.bindTo( source ).as( FactoryClass );
393
- *
394
- * source.add( { label: 'foo' } );
395
- * source.add( { label: 'bar' } );
396
- *
397
- * console.log( target.length ); // 2
398
- * console.log( target.get( 1 ).label ); // 'bar'
399
- *
400
- * source.remove( 0 );
401
- * console.log( target.length ); // 1
402
- * console.log( target.get( 0 ).label ); // 'bar'
403
- *
404
- * or the factory driven by a custom callback:
405
- *
406
- * class FooClass {
407
- * constructor( data ) {
408
- * this.label = data.label;
409
- * }
410
- * }
411
- *
412
- * class BarClass {
413
- * constructor( data ) {
414
- * this.label = data.label;
415
- * }
416
- * }
417
- *
418
- * const source = new Collection( { idProperty: 'label' } );
419
- * const target = new Collection();
420
- *
421
- * target.bindTo( source ).using( ( item ) => {
422
- * if ( item.label == 'foo' ) {
423
- * return new FooClass( item );
424
- * } else {
425
- * return new BarClass( item );
426
- * }
427
- * } );
428
- *
429
- * source.add( { label: 'foo' } );
430
- * source.add( { label: 'bar' } );
431
- *
432
- * console.log( target.length ); // 2
433
- * console.log( target.get( 0 ) instanceof FooClass ); // true
434
- * console.log( target.get( 1 ) instanceof BarClass ); // true
435
- *
436
- * or the factory out of property name:
437
- *
438
- * const source = new Collection( { idProperty: 'label' } );
439
- * const target = new Collection();
440
- *
441
- * target.bindTo( source ).using( 'label' );
442
- *
443
- * source.add( { label: { value: 'foo' } } );
444
- * source.add( { label: { value: 'bar' } } );
445
- *
446
- * console.log( target.length ); // 2
447
- * console.log( target.get( 0 ).value ); // 'foo'
448
- * console.log( target.get( 1 ).value ); // 'bar'
449
- *
450
- * It's possible to skip specified items by returning falsy value:
451
- *
452
- * const source = new Collection();
453
- * const target = new Collection();
454
- *
455
- * target.bindTo( source ).using( item => {
456
- * if ( item.hidden ) {
457
- * return null;
458
- * }
459
- *
460
- * return item;
461
- * } );
462
- *
463
- * source.add( { hidden: true } );
464
- * source.add( { hidden: false } );
465
- *
466
- * console.log( source.length ); // 2
467
- * console.log( target.length ); // 1
468
- *
469
- * **Note**: {@link #clear} can be used to break the binding.
470
- *
471
- * @param {module:utils/collection~Collection} externalCollection A collection to be bound.
472
- * @returns {Object}
473
- * @returns {module:utils/collection~CollectionBindToChain} The binding chain object.
474
- */
475
- bindTo( externalCollection ) {
476
- if ( this._bindToCollection ) {
477
- /**
478
- * The collection cannot be bound more than once.
479
- *
480
- * @error collection-bind-to-rebind
481
- */
482
- throw new CKEditorError( 'collection-bind-to-rebind', this );
483
- }
484
-
485
- this._bindToCollection = externalCollection;
486
-
487
- return {
488
- as: Class => {
489
- this._setUpBindToBinding( item => new Class( item ) );
490
- },
491
-
492
- using: callbackOrProperty => {
493
- if ( typeof callbackOrProperty == 'function' ) {
494
- this._setUpBindToBinding( item => callbackOrProperty( item ) );
495
- } else {
496
- this._setUpBindToBinding( item => item[ callbackOrProperty ] );
497
- }
498
- }
499
- };
500
- }
501
-
502
- /**
503
- * Finalizes and activates a binding initiated by {#bindTo}.
504
- *
505
- * @protected
506
- * @param {Function} factory A function which produces collection items.
507
- */
508
- _setUpBindToBinding( factory ) {
509
- const externalCollection = this._bindToCollection;
510
-
511
- // Adds the item to the collection once a change has been done to the external collection.
512
- //
513
- // @private
514
- const addItem = ( evt, externalItem, index ) => {
515
- const isExternalBoundToThis = externalCollection._bindToCollection == this;
516
- const externalItemBound = externalCollection._bindToInternalToExternalMap.get( externalItem );
517
-
518
- // If an external collection is bound to this collection, which makes it a 2–way binding,
519
- // and the particular external collection item is already bound, don't add it here.
520
- // The external item has been created **out of this collection's item** and (re)adding it will
521
- // cause a loop.
522
- if ( isExternalBoundToThis && externalItemBound ) {
523
- this._bindToExternalToInternalMap.set( externalItem, externalItemBound );
524
- this._bindToInternalToExternalMap.set( externalItemBound, externalItem );
525
- } else {
526
- const item = factory( externalItem );
527
-
528
- // When there is no item we need to remember skipped index first and then we can skip this item.
529
- if ( !item ) {
530
- this._skippedIndexesFromExternal.push( index );
531
-
532
- return;
533
- }
534
-
535
- // Lets try to put item at the same index as index in external collection
536
- // but when there are a skipped items in one or both collections we need to recalculate this index.
537
- let finalIndex = index;
538
-
539
- // When we try to insert item after some skipped items from external collection we need
540
- // to include this skipped items and decrease index.
541
- //
542
- // For the following example:
543
- // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal' ]
544
- // internal -> [ A ]
545
- //
546
- // Another item is been added at the end of external collection:
547
- // external.add( 'D' )
548
- // external -> [ 'A', 'B - skipped for internal', 'C - skipped for internal', 'D' ]
549
- //
550
- // We can't just add 'D' to internal at the same index as index in external because
551
- // this will produce empty indexes what is invalid:
552
- // internal -> [ 'A', empty, empty, 'D' ]
553
- //
554
- // So we need to include skipped items and decrease index
555
- // internal -> [ 'A', 'D' ]
556
- for ( const skipped of this._skippedIndexesFromExternal ) {
557
- if ( index > skipped ) {
558
- finalIndex--;
559
- }
560
- }
561
-
562
- // We need to take into consideration that external collection could skip some items from
563
- // internal collection.
564
- //
565
- // For the following example:
566
- // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external' ]
567
- // external -> [ A ]
568
- //
569
- // Another item is been added at the end of external collection:
570
- // external.add( 'D' )
571
- // external -> [ 'A', 'D' ]
572
- //
573
- // We need to include skipped items and place new item after them:
574
- // internal -> [ 'A', 'B - skipped for external', 'C - skipped for external', 'D' ]
575
- for ( const skipped of externalCollection._skippedIndexesFromExternal ) {
576
- if ( finalIndex >= skipped ) {
577
- finalIndex++;
578
- }
579
- }
580
-
581
- this._bindToExternalToInternalMap.set( externalItem, item );
582
- this._bindToInternalToExternalMap.set( item, externalItem );
583
- this.add( item, finalIndex );
584
-
585
- // After adding new element to internal collection we need update indexes
586
- // of skipped items in external collection.
587
- for ( let i = 0; i < externalCollection._skippedIndexesFromExternal.length; i++ ) {
588
- if ( finalIndex <= externalCollection._skippedIndexesFromExternal[ i ] ) {
589
- externalCollection._skippedIndexesFromExternal[ i ]++;
590
- }
591
- }
592
- }
593
- };
594
-
595
- // Load the initial content of the collection.
596
- for ( const externalItem of externalCollection ) {
597
- addItem( null, externalItem, externalCollection.getIndex( externalItem ) );
598
- }
599
-
600
- // Synchronize the with collection as new items are added.
601
- this.listenTo( externalCollection, 'add', addItem );
602
-
603
- // Synchronize the with collection as new items are removed.
604
- this.listenTo( externalCollection, 'remove', ( evt, externalItem, index ) => {
605
- const item = this._bindToExternalToInternalMap.get( externalItem );
606
-
607
- if ( item ) {
608
- this.remove( item );
609
- }
610
-
611
- // After removing element from external collection we need update/remove indexes
612
- // of skipped items in internal collection.
613
- this._skippedIndexesFromExternal = this._skippedIndexesFromExternal.reduce( ( result, skipped ) => {
614
- if ( index < skipped ) {
615
- result.push( skipped - 1 );
616
- }
617
-
618
- if ( index > skipped ) {
619
- result.push( skipped );
620
- }
621
-
622
- return result;
623
- }, [] );
624
- } );
625
- }
626
-
627
- /**
628
- * Returns an unique id property for a given `item`.
629
- *
630
- * The method will generate new id and assign it to the `item` if it doesn't have any.
631
- *
632
- * @private
633
- * @param {Object} item Item to be added.
634
- * @returns {String}
635
- */
636
- _getItemIdBeforeAdding( item ) {
637
- const idProperty = this._idProperty;
638
- let itemId;
639
-
640
- if ( ( idProperty in item ) ) {
641
- itemId = item[ idProperty ];
642
-
643
- if ( typeof itemId != 'string' ) {
644
- /**
645
- * This item's ID should be a string.
646
- *
647
- * @error collection-add-invalid-id
648
- */
649
- throw new CKEditorError( 'collection-add-invalid-id', this );
650
- }
651
-
652
- if ( this.get( itemId ) ) {
653
- /**
654
- * This item already exists in the collection.
655
- *
656
- * @error collection-add-item-already-exists
657
- */
658
- throw new CKEditorError( 'collection-add-item-already-exists', this );
659
- }
660
- } else {
661
- item[ idProperty ] = itemId = uid();
662
- }
663
-
664
- return itemId;
665
- }
666
-
667
- /**
668
- * Core {@link #remove} method implementation shared in other functions.
669
- *
670
- * In contrast this method **does not** fire the {@link #event:change} event.
671
- *
672
- * @private
673
- * @param {Object} subject The item to remove, its id or index in the collection.
674
- * @returns {Array} Returns an array with the removed item and its index.
675
- * @fires remove
676
- */
677
- _remove( subject ) {
678
- let index, id, item;
679
- let itemDoesNotExist = false;
680
- const idProperty = this._idProperty;
681
-
682
- if ( typeof subject == 'string' ) {
683
- id = subject;
684
- item = this._itemMap.get( id );
685
- itemDoesNotExist = !item;
686
-
687
- if ( item ) {
688
- index = this._items.indexOf( item );
689
- }
690
- } else if ( typeof subject == 'number' ) {
691
- index = subject;
692
- item = this._items[ index ];
693
- itemDoesNotExist = !item;
694
-
695
- if ( item ) {
696
- id = item[ idProperty ];
697
- }
698
- } else {
699
- item = subject;
700
- id = item[ idProperty ];
701
- index = this._items.indexOf( item );
702
- itemDoesNotExist = ( index == -1 || !this._itemMap.get( id ) );
703
- }
704
-
705
- if ( itemDoesNotExist ) {
706
- /**
707
- * Item not found.
708
- *
709
- * @error collection-remove-404
710
- */
711
- throw new CKEditorError( 'collection-remove-404', this );
712
- }
713
-
714
- this._items.splice( index, 1 );
715
- this._itemMap.delete( id );
716
-
717
- const externalItem = this._bindToInternalToExternalMap.get( item );
718
- this._bindToInternalToExternalMap.delete( item );
719
- this._bindToExternalToInternalMap.delete( externalItem );
720
-
721
- this.fire( 'remove', item, index );
722
-
723
- return [ item, index ];
724
- }
725
-
726
- /**
727
- * Iterable interface.
728
- *
729
- * @returns {Iterable.<*>}
730
- */
731
- [ Symbol.iterator ]() {
732
- return this._items[ Symbol.iterator ]();
733
- }
734
-
735
- /**
736
- * Fired when an item is added to the collection.
737
- *
738
- * @event add
739
- * @param {Object} item The added item.
740
- */
741
-
742
- /**
743
- * Fired when the collection was changed due to adding or removing items.
744
- *
745
- * @event change
746
- * @param {Iterable.<Object>} added A list of added items.
747
- * @param {Iterable.<Object>} removed A list of removed items.
748
- * @param {Number} index An index where the addition or removal occurred.
749
- */
750
-
751
- /**
752
- * Fired when an item is removed from the collection.
753
- *
754
- * @event remove
755
- * @param {Object} item The removed item.
756
- * @param {Number} index Index from which item was removed.
757
- */
758
- }
759
-
760
- mix( Collection, EmitterMixin );
761
-
762
- /**
763
- * An object returned by the {@link module:utils/collection~Collection#bindTo `bindTo()`} method
764
- * providing functions that specify the type of the binding.
765
- *
766
- * See the {@link module:utils/collection~Collection#bindTo `bindTo()`} documentation for examples.
767
- *
768
- * @interface module:utils/collection~CollectionBindToChain
769
- */
770
-
771
- /**
772
- * Creates a callback or a property binding.
773
- *
774
- * @method #using
775
- * @param {Function|String} callbackOrProperty When the function is passed, it should return
776
- * the collection items. When the string is provided, the property value is used to create the bound collection items.
777
- */
778
-
779
- /**
780
- * Creates the class factory binding in which items of the source collection are passed to
781
- * the constructor of the specified class.
782
- *
783
- * @method #as
784
- * @param {Function} Class The class constructor used to create instances in the factory.
785
- */