Explorar el Código

Internal: Changed figure view->model converter.

Oskar Wróbel hace 8 años
padre
commit
ae12d32532

+ 22 - 12
packages/ckeditor5-image/src/image/converters.js

@@ -8,7 +8,6 @@
  */
 
 import ModelPosition from '@ckeditor/ckeditor5-engine/src/model/position';
-import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
 import first from '@ckeditor/ckeditor5-utils/src/first';
 
 /**
@@ -32,11 +31,6 @@ export function viewFigureToModel() {
 			return;
 		}
 
-		// Do not convert if image cannot be placed in model at current position.
-		if ( !conversionApi.schema.checkChild( data.modelCursor, 'image' ) ) {
-			return;
-		}
-
 		// Find an image element inside the figure element.
 		const viewImage = Array.from( data.viewItem.getChildren() ).find( viewChild => viewChild.is( 'img' ) );
 
@@ -45,18 +39,34 @@ export function viewFigureToModel() {
 			return;
 		}
 
-		// Convert view image to model image.
-		const { modelRange } = conversionApi.convertItem( viewImage, data.modelCursor );
+		// Find allowed ancestor in model for image that is gong to be inserted.
+		const splitResult = conversionApi.splitToAllowedParent( 'image', data.modelCursor );
+
+		// Do not convert if image cannot be placed in model at current position and in any of ancestors.
+		if ( !splitResult ) {
+			return;
+		}
+
+		// Convert view image to model image and save result as a conversion result.
+		// Image is an object, so we are sure that it won't be split so at this stage of conversion
+		// modelCursor and modelRange ends after converted image.
+		data = Object.assign( data, conversionApi.convertItem( viewImage, data.modelCursor ) );
+
+		// Consume item from consumable list.
+		conversionApi.consumable.consume( viewImage, { name: true, attribute: 'src' } );
+		conversionApi.consumable.consume( data.viewItem, { name: true } );
 
 		// Get image element from conversion result.
-		const modelImage = first( modelRange.getItems() );
+		const modelImage = first( data.modelRange.getItems() );
 
 		// Convert rest of the figure element's children as an image children.
 		conversionApi.convertChildren( data.viewItem, ModelPosition.createAt( modelImage ) );
 
-		// Set model image as conversion result.
-		data.modelRange = ModelRange.createOn( modelImage );
-		data.modelCursor = data.modelRange.end;
+		// When parent of current modelCursor had to be split to insert image let's
+		// continue conversion inside split element.
+		if ( splitResult.cursorParent ) {
+			data.modelCursor = ModelPosition.createAt( splitResult.cursorParent );
+		}
 	};
 }
 

+ 1 - 1
packages/ckeditor5-image/tests/image/converters.js

@@ -81,7 +81,7 @@ describe( 'Image converters', () => {
 			expect( imgConverterCalled ).to.be.true;
 		} );
 
-		it( 'should convert non-img children in image context and append them to model image element', () => {
+		it( 'should convert children allowed by schema and omit disallowed', () => {
 			buildViewConverter().for( editor.data.viewToModel ).fromElement( 'foo' ).toElement( 'foo' );
 			buildViewConverter().for( editor.data.viewToModel ).fromElement( 'bar' ).toElement( 'bar' );
 

+ 0 - 11
packages/ckeditor5-image/tests/image/imageengine.js

@@ -248,17 +248,6 @@ describe( 'ImageEngine', () => {
 				expect( getModelData( model, { withoutSelection: true } ) ).to.equal( '<div></div>' );
 			} );
 
-			it( 'should handle figure with two images', () => {
-				model.schema.extend( '$text', { allowIn: 'image' } );
-
-				editor.setData( '<figure class="image"><img src="foo.jpg" /><img src="bar.jpg" />abc</figure>' );
-
-				// The foo.jpg image is properly converted using figure converter. The other image was tried to
-				// be added as a child of foo.jpg and then was autohoisted.
-				expect( getModelData( model, { withoutSelection: true } ) )
-					.to.equal( '<image src="bar.jpg"></image><image src="foo.jpg">abc</image>' );
-			} );
-
 			it( 'should convert image with srcset attribute', () => {
 				editor.setData(
 					'<figure class="image">' +