瀏覽代碼

Expose `insertUIElement()` conversion helper as a protected export.

Maciej Gołaszewski 7 年之前
父節點
當前提交
d87b93445e

+ 15 - 13
packages/ckeditor5-engine/src/conversion/downcasthelpers.js

@@ -535,19 +535,21 @@ export function insertElement( elementCreator ) {
 	};
 }
 
-// Function factory that creates a converter which converts marker adding change to the
-// {@link module:engine/view/uielement~UIElement view UI element}.
-//
-// The view UI element that will be added to the view depends on the passed parameter. See {@link ~insertElement}.
-// In case of a non-collapsed range, the UI element will not wrap nodes but separate elements will be placed at the beginning
-// and at the end of the range.
-//
-// This converter binds created UI elements with the marker name using {@link module:engine/conversion/mapper~Mapper#bindElementToMarker}.
-//
-// @param {module:engine/view/uielement~UIElement|Function} elementCreator A view UI element or a function returning the view element
-// that will be inserted.
-// @returns {Function} Insert element event converter.
-function insertUIElement( elementCreator ) {
+/**
+ * Function factory that creates a converter which converts marker adding change to the
+ * {@link module:engine/view/uielement~UIElement view UI element}.
+ *
+ * The view UI element that will be added to the view depends on the passed parameter. See {@link ~insertElement}.
+ * In case of a non-collapsed range, the UI element will not wrap nodes but separate elements will be placed at the beginning
+ * and at the end of the range.
+ *
+ * This converter binds created UI elements with the marker name using {@link module:engine/conversion/mapper~Mapper#bindElementToMarker}.
+ *
+ * @param {module:engine/view/uielement~UIElement|Function} elementCreator A view UI element or a function returning the view element
+ * that will be inserted.
+ * @returns {Function} Insert element event converter.
+ */
+export function insertUIElement( elementCreator ) {
 	return ( evt, data, conversionApi ) => {
 		// Create two view elements. One will be inserted at the beginning of marker, one at the end.
 		// If marker is collapsed, only "opening" element will be inserted.

+ 5 - 37
packages/ckeditor5-engine/src/dev-utils/model.js

@@ -32,7 +32,7 @@ import {
 	convertRangeSelection,
 	convertCollapsedSelection,
 } from '../conversion/downcast-selection-converters';
-import { insertElement, insertText, wrap } from '../conversion/downcasthelpers';
+import { insertElement, insertText, insertUIElement, wrap } from '../conversion/downcasthelpers';
 
 import { isPlainObject } from 'lodash-es';
 import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
@@ -234,43 +234,11 @@ export function stringify( node, selectionOrPositionOrRange = null, markers = nu
 
 	downcastDispatcher.on( 'selection', convertRangeSelection() );
 	downcastDispatcher.on( 'selection', convertCollapsedSelection() );
-	downcastDispatcher.on( 'addMarker', ( evt, data, conversionApi ) => {
-		const elementCreator = ( data, writer ) => {
-			const name = data.markerName + ':' + ( data.isOpening ? 'start' : 'end' );
+	downcastDispatcher.on( 'addMarker', insertUIElement( ( data, writer ) => {
+		const name = data.markerName + ':' + ( data.isOpening ? 'start' : 'end' );
 
-			return writer.createUIElement( name );
-		};
-
-		// Create two view elements. One will be inserted at the beginning of marker, one at the end.
-		// If marker is collapsed, only "opening" element will be inserted.
-		data.isOpening = true;
-		const viewStartElement = elementCreator( data, conversionApi.writer );
-
-		data.isOpening = false;
-		const viewEndElement = elementCreator( data, conversionApi.writer );
-
-		const markerRange = data.markerRange;
-
-		// If marker's range is not collapsed - consume all items inside.
-		for ( const value of markerRange ) {
-			conversionApi.consumable.consume( value.item, evt.name );
-		}
-
-		const mapper = conversionApi.mapper;
-		const viewWriter = conversionApi.writer;
-
-		// Add "opening" element.
-		viewWriter.insert( mapper.toViewPosition( markerRange.start ), viewStartElement );
-		conversionApi.mapper.bindElementToMarker( viewStartElement, data.markerName );
-
-		// Add "closing" element only if range is not collapsed.
-		if ( !markerRange.isCollapsed ) {
-			viewWriter.insert( mapper.toViewPosition( markerRange.end ), viewEndElement );
-			conversionApi.mapper.bindElementToMarker( viewEndElement, data.markerName );
-		}
-
-		evt.stop();
-	} );
+		return writer.createUIElement( name );
+	} ) );
 
 	// Convert model to view.
 	const writer = view._writer;