Răsfoiți Sursa

Update API of slot binding.

Maciej Gołaszewski 5 ani în urmă
părinte
comite
e7f161cf75

+ 1 - 1
packages/ckeditor5-engine/src/conversion/downcastdispatcher.js

@@ -619,7 +619,7 @@ export default class DowncastDispatcher {
 
 			// At this stage we get the update view element, so any mapped model item might be a potential "slot".
 			if ( modelItem ) {
-				const currentViewItem = this.conversionApi.mapper._temporalModelToView.get( modelItem );
+				const currentViewItem = this.conversionApi.mapper.getExistingViewForSlot( modelItem );
 
 				// This of course needs better API, but for now it works.
 				// Mapper.bindSlot() creates mappings as mapper.bindElements() but also binds view element

+ 33 - 9
packages/ckeditor5-engine/src/conversion/mapper.js

@@ -49,8 +49,13 @@ export default class Mapper {
 		 */
 		this._modelToViewMapping = new WeakMap();
 
-		// @todo POC
-		this._temporalModelToView = new WeakMap();
+		/**
+		 * Model element to existing view slot element mapping.
+		 *
+		 * @private
+		 * @member {WeakMap}
+		 */
+		this._slotToViewMapping = new WeakMap();
 
 		/**
 		 * View element to model element mapping.
@@ -138,13 +143,6 @@ export default class Mapper {
 		this._viewToModelMapping.set( viewElement, modelElement );
 	}
 
-	bindSlotElements( modelElement, viewElement ) {
-		const oldView = this.toViewElement( modelElement );
-
-		this._temporalModelToView.set( modelElement, oldView );
-		this.bindElements( modelElement, viewElement );
-	}
-
 	/**
 	 * Unbinds given {@link module:engine/view/element~Element view element} from the map.
 	 *
@@ -262,6 +260,7 @@ export default class Mapper {
 		this._markerNameToElements = new Map();
 		this._elementToMarkerNames = new Map();
 		this._unboundMarkerNames = new Set();
+		this._slotToViewMapping = new WeakMap();
 	}
 
 	/**
@@ -346,6 +345,31 @@ export default class Mapper {
 		return data.viewPosition;
 	}
 
+	/**
+	 * Marks model and view elements as corresponding "slot". Similar to {@link #bindElements} but it memoizes existing view element
+	 * during re-conversion of complex elements with slots.
+	 *
+	 * @param {module:engine/model/element~Element} modelElement Model element.
+	 * @param {module:engine/view/element~Element} viewElement View element.
+	 */
+	bindSlotElements( modelElement, viewElement ) {
+		const existingView = this.toViewElement( modelElement );
+
+		this._slotToViewMapping.set( modelElement, existingView );
+
+		this.bindElements( modelElement, viewElement );
+	}
+
+	/**
+	 * Gets the previously converted view element.
+	 *
+	 * @param {module:engine/model/element~Element} modelElement Model element.
+	 * @returns {module:engine/view/element~Element|undefined} Corresponding view element or `undefined` if not found.
+	 */
+	getExistingViewForSlot( modelElement ) {
+		return this._slotToViewMapping.get( modelElement );
+	}
+
 	/**
 	 * Gets all view elements bound to the given marker name.
 	 *