浏览代码

Internal: Reused _getItemIdBeforeAdding() method for the add().

Restored `CKEditorError` old interface, no big reason to change the API here.
Marek Lewandowski 5 年之前
父节点
当前提交
f8f80ada88
共有 1 个文件被更改,包括 3 次插入38 次删除
  1. 3 38
      packages/ckeditor5-utils/src/collection.js

+ 3 - 38
packages/ckeditor5-utils/src/collection.js

@@ -180,32 +180,7 @@ export default class Collection {
 	 * @fires add
 	 */
 	add( item, index ) {
-		let itemId;
-		const idProperty = this._idProperty;
-
-		if ( ( idProperty in item ) ) {
-			itemId = item[ idProperty ];
-
-			if ( typeof itemId != 'string' ) {
-				/**
-				 * This item's id should be a string.
-				 *
-				 * @error collection-add-invalid-id
-				 */
-				throw new CKEditorError( 'collection-add-invalid-id', this );
-			}
-
-			if ( this.get( itemId ) ) {
-				/**
-				 * This item already exists in the collection.
-				 *
-				 * @error collection-add-item-already-exists
-				 */
-				throw new CKEditorError( 'collection-add-item-already-exists', this );
-			}
-		} else {
-			item[ idProperty ] = itemId = uid();
-		}
+		const itemId = this._getItemIdBeforeAdding( item );
 
 		// TODO: Use ES6 default function argument.
 		if ( index === undefined ) {
@@ -669,13 +644,8 @@ export default class Collection {
 				 * This item's id should be a string.
 				 *
 				 * @error collection-add-invalid-id
-				 * @param {Object} item The item being added to the collection.
-				 * @param {module:utils/collection~Collection} collection The collection the item is added to.
 				 */
-				throw new CKEditorError( 'collection-add-invalid-id', {
-					item,
-					collection: this,
-				} );
+				throw new CKEditorError( 'collection-add-invalid-id', this );
 			}
 
 			if ( this.get( itemId ) ) {
@@ -683,13 +653,8 @@ export default class Collection {
 				 * This item already exists in the collection.
 				 *
 				 * @error collection-add-item-already-exists
-				 * @param {Object} item The item being added to the collection.
-				 * @param {module:utils/collection~Collection} collection The collection the item is added to.
 				 */
-				throw new CKEditorError( 'collection-add-item-already-exists', {
-					item,
-					collection: this
-				} );
+				throw new CKEditorError( 'collection-add-item-already-exists', this );
 			}
 		} else {
 			item[ idProperty ] = itemId = uid();