Răsfoiți Sursa

Added custom proerties to view.Element.

Szymon Kupś 9 ani în urmă
părinte
comite
98b0542412
1 a modificat fișierele cu 51 adăugiri și 0 ștergeri
  1. 51 0
      packages/ckeditor5-engine/src/view/element.js

+ 51 - 0
packages/ckeditor5-engine/src/view/element.js

@@ -105,6 +105,15 @@ export default class Element extends Node {
 			parseInlineStyles( this._styles, this._attrs.get( 'style' ) );
 			this._attrs.delete( 'style' );
 		}
+
+		/**
+		 * Map of custom properties.
+		 * Custom properties can be added to element instance, will be cloned but not rendered into DOM.
+		 *
+		 * @protected
+		 * @memeber {Map} engine.view.Element#_customProperties.
+		 */
+		this._customProperties = new Map();
 	}
 
 	/**
@@ -151,6 +160,9 @@ export default class Element extends Node {
 		cloned._classes = new Set( this._classes );
 		cloned._styles = new Map( this._styles );
 
+		// Clone custom properties.
+		cloned._customProperties = new Map( this._customProperties );
+
 		return cloned;
 	}
 
@@ -597,6 +609,45 @@ export default class Element extends Node {
 
 		return null;
 	}
+
+	/**
+	 * Sets custom property.
+	 *
+	 * @param {String|Symbol} key
+	 * @param {*} value
+	 */
+	setCustomProperty( key, value ) {
+		this._customProperties.set( key, value );
+	}
+
+	/**
+	 * Returns custom property value for given key.
+	 *
+	 * @param {String|Symbol} key
+	 * @returns {*}
+	 */
+	getCustomProperty( key ) {
+		return this._customProperties.get( key );
+	}
+
+	/**
+	 * Removes custom property stored under given key.
+	 *
+	 * @param {String|Symbol} key
+	 * @returns {Boolean} Returns true if property was removed.
+	 */
+	removeCustomProperty( key ) {
+		return this._customProperties.delete( key );
+	}
+
+	/**
+	 * Returns iterator that iterates over this element's custom properties.
+	 *
+	 * @returns {Iterable.<*>}
+	 */
+	*getCustomProperties( ) {
+		yield* this._customProperties.entries();
+	}
 }
 
 // Parses inline styles and puts property - value pairs into styles map.