Explorar o código

Refactor _insertMissingModelCaptionElement() method.

Maciej Gołaszewski %!s(int64=7) %!d(string=hai) anos
pai
achega
5630454e6c
Modificáronse 1 ficheiros con 17 adicións e 23 borrados
  1. 17 23
      packages/ckeditor5-image/src/imagecaption/imagecaptionediting.js

+ 17 - 23
packages/ckeditor5-image/src/imagecaption/imagecaptionediting.js

@@ -185,40 +185,34 @@ export default class ImageCaptionEditing extends Plugin {
 		let wasFixed = false;
 
 		for ( const entry of changes ) {
-			if ( entry.type == 'insert' ) {
-				const images = [];
+			const images = [];
 
+			if ( entry.type == 'insert' && entry.name != '$text' ) {
 				const item = entry.position.nodeAfter;
 
-				if ( item ) {
-					if ( entry.name == 'image' ) {
-						images.push( item );
-					} else {
-						if ( !item.is( 'image' ) && item.childCount ) {
-							const walker = model.createRangeOn( item ).getWalker();
+				if ( item.is( 'image' ) ) {
+					images.push( item );
+				} else if ( item.childCount ) {
+					const walker = model.createRangeOn( item ).getWalker();
 
-							for ( const walkerValue of walker ) {
-								if ( walkerValue.type === 'elementStart' ) {
-									const itemos = walkerValue.item;
+					for ( const walkerValue of walker ) {
+						if ( walkerValue.type === 'elementStart' ) {
+							const walkerItem = walkerValue.item;
 
-									if ( itemos.name === 'image' ) {
-										images.push( itemos );
-									}
-								}
+							if ( walkerItem.is( 'image' ) ) {
+								images.push( walkerItem );
 							}
 						}
 					}
 				}
+			}
 
-				for ( const image of images ) {
-					if ( !getCaptionFromImage( image ) ) {
-						writer.appendElement( 'caption', image );
-					}
-				}
-
-				// TODO: infinite loop :/
-				wasFixed = false;
+			for ( const image of images.filter( image => !getCaptionFromImage( image ) ) ) {
+				writer.appendElement( 'caption', image );
 			}
+
+			// TODO: infinite loop :/
+			wasFixed = false;
 		}
 
 		return wasFixed;