Procházet zdrojové kódy

Add id to HighlightDescriptor.

Szymon Kupś před 8 roky
rodič
revize
c4d76da49a

+ 2 - 0
packages/ckeditor5-engine/src/conversion/buildmodelconverter.js

@@ -441,6 +441,8 @@ export default function buildModelConverter() {
  *
  * @property {String|Array.<String>} class CSS class or array of classes that will be added to `span`
  * {@link module:engine/view/attributeelement~AttributeElement} wrapping each text node in the highlighted content.
+ * @property {String} [id] Descriptor identifier. If not provided, marker's name from which given highlight is created
+ * will be used.
  * @property {Number} [priority] {@link module:engine/view/attributeelement~AttributeElement#priority} of the `span`
  * wrapping each text node in the highlighted content. If not provided, default 10 priority will be used.
  * @property {Object} [attributes] Attributes that will be added to `span`

+ 5 - 0
packages/ckeditor5-engine/src/conversion/model-to-view-converters.js

@@ -447,6 +447,7 @@ export function highlightText( highlightDescriptor ) {
  * 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}.
+ * If highlight descriptor will not provide id property, name of the marker will be used.
  * When `addHighlight` and `removeHighlight` custom properties are not present, element is not converted
  * in any special way. This means that converters will proceed to convert element's child nodes.
  *
@@ -473,6 +474,10 @@ export function highlightElement( highlightDescriptor ) {
 			descriptor.priority = 10;
 		}
 
+		if ( !descriptor.id ) {
+			descriptor.id = data.markerName;
+		}
+
 		const viewElement = conversionApi.mapper.toViewElement( modelItem );
 		const addMarker = evt.name.split( ':' )[ 0 ] == 'addMarker';
 		const highlightHandlingMethod = addMarker ? 'addHighlight' : 'removeHighlight';

+ 33 - 1
packages/ckeditor5-engine/tests/conversion/model-to-view-converters.js

@@ -176,7 +176,8 @@ describe( 'model-to-view-converters', () => {
 		const highlightDescriptor = {
 			class: 'highlight-class',
 			priority: 7,
-			attributes: { title: 'title' }
+			attributes: { title: 'title' },
+			id: 'customId'
 		};
 
 		beforeEach( () => {
@@ -276,6 +277,37 @@ describe( 'model-to-view-converters', () => {
 			expect( viewToString( viewRoot ) ).to.equal( '<div><p>foo</p><p>bar</p></div>' );
 		} );
 
+		it( 'should use provide default priority and id if not provided', () => {
+			const highlightDescriptor = { class: 'highlight-class' };
+
+			dispatcher.on( 'addMarker:marker', highlightElement( highlightDescriptor ) );
+			dispatcher.on( 'removeMarker:marker', highlightElement( highlightDescriptor ) );
+			dispatcher.on( 'insert:paragraph', insertElement( data => {
+				// Use special converter only for first paragraph.
+				if ( data.item == modelElement2 ) {
+					return;
+				}
+
+				const viewContainer = new ViewContainerElement( 'p' );
+
+				viewContainer.setCustomProperty( 'addHighlight', ( element, descriptor ) => {
+					expect( descriptor.priority ).to.equal( 10 );
+					expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
+				} );
+
+				viewContainer.setCustomProperty( 'removeHighlight', ( element, descriptor ) => {
+					expect( descriptor.priority ).to.equal( 10 );
+					expect( descriptor.id ).to.equal( 'marker:foo-bar-baz' );
+				} );
+
+				return viewContainer;
+			} ), { priority: 'high' } );
+
+			dispatcher.convertInsertion( markerRange );
+			modelDoc.markers.set( 'marker', markerRange );
+			dispatcher.convertMarker( 'addMarker', 'marker:foo-bar-baz', markerRange );
+		} );
+
 		it( 'should do nothing if descriptor is not provided', () => {
 			dispatcher.on( 'addMarker:marker', highlightElement( () => null ) );
 			dispatcher.on( 'removeMarker:marker', highlightElement( () => null ) );