Browse Source

Custom highlight handling removeHighlight method now accepts only highlight id.

Szymon Kupś 8 years ago
parent
commit
dfbe4d40dd

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

@@ -284,7 +284,7 @@ class ModelConverterBuilder {
 	 * custom properties:
 	 *
 	 *		viewElement.setCustomProperty( 'addHighlight', ( element, descriptor ) => {} );
-	 *		viewElement.setCustomProperty( 'removeHighlight', ( element, descriptor ) => {} );
+	 *		viewElement.setCustomProperty( 'removeHighlight', ( element, descriptorId ) => {} );
 	 *
 	 * {@link module:engine/conversion/model-to-view-converters~HighlightDescriptor} will be used to create
 	 * spans over text nodes and also will be provided to `addHighlight` and `removeHighlight` methods

+ 9 - 7
packages/ckeditor5-engine/src/conversion/model-to-view-converters.js

@@ -445,10 +445,9 @@ export function highlightText( highlightDescriptor ) {
 
 /**
  * Function factory, creates converter that converts all elements inside marker's range. Converter checks if element has
- * functions stored under `addHighlight` and `removeHighlight` custom properties and calls them passing
- * {@link module:engine/conversion/model-to-view-converters~HighlightDescriptor}. In such case converter will consume
- * all element's children, assuming that they were handled by element itself. If highlight descriptor will not provide
- * priority, priority `10` will be used as default, to be compliant with
+ * functions stored under `addHighlight` and `removeHighlight` custom properties and calls them when highlight should be added
+ * or removed. In such case converter will consume all element's children, assuming that they were handled by element itself.
+ * If highlight descriptor will not provide priority, priority `10` will be used as default, to be compliant with
  * {@link module:engine/conversion/model-to-view-converters~highlightText} method which uses default priority of
  * {@link module:engine/view/attributeelement~AttributeElement}.
  *
@@ -496,7 +495,7 @@ export function highlightElement( highlightDescriptor ) {
 				consumable.consume( value.item, evt.name );
 			}
 
-			viewElement.getCustomProperty( highlightHandlingMethod )( viewElement, descriptor );
+			viewElement.getCustomProperty( highlightHandlingMethod )( viewElement, addMarker ? descriptor : descriptor.id );
 		}
 	};
 }
@@ -640,8 +639,11 @@ class HighlightAttributeElement extends ViewAttributeElement {
  * described by this object.
  *
  * Each element can handle displaying highlight separately by providing `addHighlight` and `removeHighlight` custom
- * properties. Those properties are passed `HighlightDescriptor` object upon conversion and should use it to
- * change the element.
+ * properties:
+ *  * `HighlightDescriptor` is passed to `addHighlight` function upon conversion and should be used to apply highlight to
+ *  the element,
+ *  * descriptor id is passed to `removeHighlight` function upon conversion and should be used to remove highlight of given
+ *  id from the element.
  *
  * @typedef {Object} module:engine/conversion/model-to-view-converters~HighlightDescriptor
  *

+ 9 - 4
packages/ckeditor5-engine/tests/conversion/model-to-view-converters.js

@@ -211,10 +211,10 @@ describe( 'model-to-view-converters', () => {
 					expect( descriptor ).to.equal( highlightDescriptor );
 				} );
 
-				viewContainer.setCustomProperty( 'removeHighlight', ( element, descriptor ) => {
+				viewContainer.setCustomProperty( 'removeHighlight', ( element, id ) => {
 					element.removeClass( 'highlight-own-class' );
 
-					expect( descriptor ).to.equal( highlightDescriptor );
+					expect( id ).to.equal( highlightDescriptor.id );
 				} );
 
 				return viewContainer;
@@ -251,8 +251,13 @@ describe( 'model-to-view-converters', () => {
 
 			dispatcher.on( 'insert:paragraph', insertElement( () => {
 				const element = new ViewContainerElement( 'p' );
-				element.setCustomProperty( 'addHighlight', ( element, data ) => element.addClass( data.class ) );
-				element.setCustomProperty( 'removeHighlight', ( element, data ) => element.removeClass( data.class ) );
+				const descriptors = new Map();
+
+				element.setCustomProperty( 'addHighlight', ( element, data ) => {
+					descriptors.set( data.id, data );
+					element.addClass( data.class );
+				} );
+				element.setCustomProperty( 'removeHighlight', ( element, id ) => element.removeClass( descriptors.get( id ).class ) );
 
 				return element;
 			} ), { priority: 'high' } );