瀏覽代碼

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

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

+ 27 - 24
packages/ckeditor5-engine/src/conversion/downcasthelpers.js

@@ -490,30 +490,33 @@ export function wrap( elementCreator ) {
 	};
 }
 
-// Function factory that creates a converter which converts node insertion changes from the model to the view.
-// The function passed will be provided with all the parameters of the dispatcher's
-// {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert `insert` event}.
-// It is expected that the function returns an {@link module:engine/view/element~Element}.
-// The result of the function will be inserted into the view.
-//
-// The converter automatically consumes the corresponding value from the consumables list, stops the event (see
-// {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}) and binds the model and view elements.
-//
-//		downcastDispatcher.on(
-//			'insert:myElem',
-//			insertElement( ( modelItem, viewWriter ) => {
-//				const text = viewWriter.createText( 'myText' );
-//				const myElem = viewWriter.createElement( 'myElem', { myAttr: 'my-' + modelItem.getAttribute( 'myAttr' ) }, text );
-//
-//				// Do something fancy with `myElem` using `modelItem` or other parameters.
-//
-//				return myElem;
-//			}
-//		) );
-//
-// @param {Function} elementCreator Function returning a view element, which will be inserted.
-// @returns {Function} Insert element event converter.
-function insertElement( elementCreator ) {
+/**
+ * Function factory that creates a converter which converts node insertion changes from the model to the view.
+ * The function passed will be provided with all the parameters of the dispatcher's
+ * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher#event:insert `insert` event}.
+ * It is expected that the function returns an {@link module:engine/view/element~Element}.
+ * The result of the function will be inserted into the view.
+ *
+ * The converter automatically consumes the corresponding value from the consumables list, stops the event (see
+ * {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}) and binds the model and view elements.
+ *
+ *		downcastDispatcher.on(
+ *			'insert:myElem',
+ *			insertElement( ( modelItem, viewWriter ) => {
+ *				const text = viewWriter.createText( 'myText' );
+ *				const myElem = viewWriter.createElement( 'myElem', { myAttr: 'my-' + modelItem.getAttribute( 'myAttr' ) }, text );
+ *
+ *				// Do something fancy with `myElem` using `modelItem` or other parameters.
+ *
+ *				return myElem;
+ *			}
+ *		) );
+ *
+ * @protected
+ * @param {Function} elementCreator Function returning a view element, which will be inserted.
+ * @returns {Function} Insert element event converter.
+ */
+export function insertElement( elementCreator ) {
 	return ( evt, data, conversionApi ) => {
 		const viewElement = elementCreator( data.item, conversionApi.writer );
 

+ 6 - 13
packages/ckeditor5-engine/src/dev-utils/model.js

@@ -32,7 +32,7 @@ import {
 	convertRangeSelection,
 	convertCollapsedSelection,
 } from '../conversion/downcast-selection-converters';
-import { insertText, wrap } from '../conversion/downcasthelpers';
+import { insertElement, insertText, wrap } from '../conversion/downcasthelpers';
 
 import { isPlainObject } from 'lodash-es';
 import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
@@ -225,19 +225,12 @@ export function stringify( node, selectionOrPositionOrRange = null, markers = nu
 			converter( evt, data, conversionApi );
 		}
 	} );
-	downcastDispatcher.on( 'insert', ( evt, data, conversionApi ) => {
-		const attributes = convertAttributes( data.item.getAttributes(), stringifyAttributeValue );
-		const viewElement = new ViewContainerElement( data.item.name, attributes );
+	downcastDispatcher.on( 'insert', insertElement( modelItem => {
+		// Stringify object types values for properly display as an output string.
+		const attributes = convertAttributes( modelItem.getAttributes(), stringifyAttributeValue );
 
-		if ( !conversionApi.consumable.consume( data.item, 'insert' ) ) {
-			return;
-		}
-
-		const viewPosition = conversionApi.mapper.toViewPosition( data.range.start );
-
-		conversionApi.mapper.bindElements( data.item, viewElement );
-		conversionApi.writer.insert( viewPosition, viewElement );
-	} );
+		return new ViewContainerElement( modelItem.name, attributes );
+	} ) );
 
 	downcastDispatcher.on( 'selection', convertRangeSelection() );
 	downcastDispatcher.on( 'selection', convertCollapsedSelection() );