Browse Source

Introduces utils.mix.

Piotrek Koszuliński 10 năm trước cách đây
mục cha
commit
6c305d5225
1 tập tin đã thay đổi với 24 bổ sung0 xóa
  1. 24 0
      packages/ckeditor5-ui/src/utils.js

+ 24 - 0
packages/ckeditor5-ui/src/utils.js

@@ -107,6 +107,30 @@ const utils = {
 		}
 
 		return null;
+	},
+
+	/**
+	 * Copies enumerable properties from the objects given as 2nd+ parameters to the
+	 * prototype of the base class.
+	 *
+	 *		class SpecificEditor extends utils.mix( Editor, SomeMixin1, SomeMixin2 ) {
+	 *			...
+	 *		}
+	 *
+	 * @param {Function} [baseClass] Class which prototype will be extended.
+	 * @param {Object} [...mixins] Objects from which to get properties.
+	 */
+	mix( baseClass, ...mixins ) {
+		mixins.forEach( ( mixin ) => {
+			Object.keys( mixin ).forEach( ( key ) => {
+				Object.defineProperty( baseClass.prototype, key, {
+					enumerable: false,
+					configurable: true,
+					writable: true,
+					value: mixin[ key ]
+				} );
+			} );
+		} );
 	}
 };