Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 7bde6f7

Browse files
authored
Merge pull request #1165 from ckeditor/t/1164
Other: The `removeHighlight()` function now accepts descriptor id instead of a `HighlightDescriptor` object. Closes #1164.
2 parents 0dd29d4 + e8156a7 commit 7bde6f7

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/conversion/buildmodelconverter.js

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

src/conversion/model-to-view-converters.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -444,15 +444,15 @@ export function highlightText( highlightDescriptor ) {
444444
}
445445

446446
/**
447-
* Function factory, creates converter that converts all elements inside marker's range. Converter checks if element has
448-
* functions stored under `addHighlight` and `removeHighlight` custom properties and calls them passing
449-
* {@link module:engine/conversion/model-to-view-converters~HighlightDescriptor}. In such case converter will consume
450-
* all element's children, assuming that they were handled by element itself. If highlight descriptor will not provide
451-
* priority, priority `10` will be used as default, to be compliant with
447+
* Converter function factory. Creates a function which applies the marker's highlight to all elements inside a marker's range.
448+
* The converter checks if an element has the addHighlight and removeHighlight functions stored as
449+
* {@link TODO custom properties} and if so use them to apply the highlight. In such case converter will consume all
450+
* element's children, assuming that they were handled by element itself.
451+
* If the highlight descriptor will not provide priority, priority `10` will be used as default, to be compliant with
452452
* {@link module:engine/conversion/model-to-view-converters~highlightText} method which uses default priority of
453453
* {@link module:engine/view/attributeelement~AttributeElement}.
454454
*
455-
* If highlight descriptor will not provide `id` property, name of the marker will be used.
455+
* If the highlight descriptor will not provide `id` property, name of the marker will be used.
456456
* When `addHighlight` and `removeHighlight` custom properties are not present, element is not converted
457457
* in any special way. This means that converters will proceed to convert element's child nodes.
458458
*
@@ -496,7 +496,7 @@ export function highlightElement( highlightDescriptor ) {
496496
consumable.consume( value.item, evt.name );
497497
}
498498

499-
viewElement.getCustomProperty( highlightHandlingMethod )( viewElement, descriptor );
499+
viewElement.getCustomProperty( highlightHandlingMethod )( viewElement, addMarker ? descriptor : descriptor.id );
500500
}
501501
};
502502
}
@@ -634,14 +634,17 @@ class HighlightAttributeElement extends ViewAttributeElement {
634634
}
635635

636636
/**
637-
* Object describing how content highlight should be created in the view.
637+
* Object describing how the content highlight should be created in the view.
638638
*
639-
* Each text node contained in highlight will be wrapped with `span` element with CSS class(es), attributes and priority
639+
* Each text node contained in the highlight will be wrapped with `span` element with CSS class(es), attributes and priority
640640
* described by this object.
641641
*
642-
* Each element can handle displaying highlight separately by providing `addHighlight` and `removeHighlight` custom
643-
* properties. Those properties are passed `HighlightDescriptor` object upon conversion and should use it to
644-
* change the element.
642+
* Each element can handle displaying the highlight separately by providing `addHighlight` and `removeHighlight` custom
643+
* properties:
644+
* * `HighlightDescriptor` is passed to the `addHighlight` function upon conversion and should be used to apply the highlight to
645+
* the element,
646+
* * descriptor id is passed to the `removeHighlight` function upon conversion and should be used to remove the highlight of given
647+
* id from the element.
645648
*
646649
* @typedef {Object} module:engine/conversion/model-to-view-converters~HighlightDescriptor
647650
*

tests/conversion/model-to-view-converters.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ describe( 'model-to-view-converters', () => {
211211
expect( descriptor ).to.equal( highlightDescriptor );
212212
} );
213213

214-
viewContainer.setCustomProperty( 'removeHighlight', ( element, descriptor ) => {
214+
viewContainer.setCustomProperty( 'removeHighlight', ( element, id ) => {
215215
element.removeClass( 'highlight-own-class' );
216216

217-
expect( descriptor ).to.equal( highlightDescriptor );
217+
expect( id ).to.equal( highlightDescriptor.id );
218218
} );
219219

220220
return viewContainer;
@@ -251,8 +251,13 @@ describe( 'model-to-view-converters', () => {
251251

252252
dispatcher.on( 'insert:paragraph', insertElement( () => {
253253
const element = new ViewContainerElement( 'p' );
254-
element.setCustomProperty( 'addHighlight', ( element, data ) => element.addClass( data.class ) );
255-
element.setCustomProperty( 'removeHighlight', ( element, data ) => element.removeClass( data.class ) );
254+
const descriptors = new Map();
255+
256+
element.setCustomProperty( 'addHighlight', ( element, data ) => {
257+
descriptors.set( data.id, data );
258+
element.addClass( data.class );
259+
} );
260+
element.setCustomProperty( 'removeHighlight', ( element, id ) => element.removeClass( descriptors.get( id ).class ) );
256261

257262
return element;
258263
} ), { priority: 'high' } );

0 commit comments

Comments
 (0)