Parcourir la source

Improved autoparagraphing.

Piotrek Koszuliński il y a 9 ans
Parent
commit
28dae24a7c

+ 25 - 22
packages/ckeditor5-engine/src/datacontroller/insertcontent.js

@@ -81,28 +81,29 @@ class Insertion {
 		context.isObject = this._checkIsObject( node );
 
 		if ( context.isObject ) {
-			this._handleObject( node );
+			this._handleObject( node, context );
 
 			return;
 		}
 
-		// if ( this._checkShouldReplaceParent( node, context ) ) {
-		// 	const parentNode = this.position.parent;
-
-		// 	this.position = Position.createBefore( parentNode );
-		// 	this.batch.remove( parentNode );
-		// }
-
 		const isAllowed = this._splitToAllowedPosition( node, context );
 
 		if ( !isAllowed ) {
 			if ( context.isElement ) {
 				this.handleNodes( node.getChildren(), context );
-			} else {
-				const container = new Element( 'paragraph' );
-				container.appendChildren( node );
+			}
+			// Try autoparagraphing.
+			else {
+				const paragraph = new Element( 'paragraph' );
+
+				// Do not autoparagraph if the paragraph won't be allowed there,
+				// cause that would lead to an infinite loop. The paragraph would be rejected in
+				// the next handleNode() call and we'd be here again.
+				if ( this._getAllowedIn( paragraph, this.position.parent ) ) {
+					paragraph.appendChildren( node );
 
-				this.handleNode( container, context );
+					this.handleNode( paragraph, context );
+				}
 			}
 
 			return;
@@ -167,9 +168,20 @@ class Insertion {
 		}
 	}
 
-	_handleObject( node ) {
+	_handleObject( node, context ) {
 		if ( this._splitToAllowedPosition( node ) ) {
 			this._insert( node );
+		} else {
+			const paragraph = new Element( 'paragraph' );
+
+			// Do not autoparagraph if the paragraph won't be allowed there,
+			// cause that would lead to an infinite loop. The paragraph would be rejected in
+			// the next handleNode() call and we'd be here again.
+			if ( this._getAllowedIn( paragraph, this.position.parent ) && this._checkIsAllowed( node, [ paragraph ] ) ) {
+				paragraph.appendChildren( node );
+
+				this.handleNode( paragraph, context );
+			}
 		}
 	}
 
@@ -265,15 +277,6 @@ class Insertion {
 		return this.schema.objects.has( this._getNodeSchemaName( node ) );
 	}
 
-	// _checkShouldReplaceParent( node, context ) {
-	// 	const parentNode = this.position.parent;
-
-	// 	return context.isElement &&
-	// 		( parentNode instanceof Element ) &&
-	// 		parentNode.isEmpty &&
-	// 		!this._checkIsAllowed( node, [ parentNode ] );
-	// }
-
 	_getNodeSchemaName( node ) {
 		if ( node instanceof Text ) {
 			return '$text';

+ 14 - 0
packages/ckeditor5-engine/tests/datacontroller/insertcontent.js

@@ -265,6 +265,13 @@ describe( 'DataController', () => {
 					'<paragraph>fxxx</paragraph><paragraph>yyy[]oo</paragraph>'
 				);
 
+				test(
+					'inserts text + inlineWidget + text + paragraph',
+					'xxx<inlineWidget></inlineWidget>yyy<paragraph>zzz</paragraph>',
+					'<paragraph>f[]oo</paragraph>',
+					'<paragraph>fxxx<inlineWidget></inlineWidget>yyy</paragraph><paragraph>zzz[]oo</paragraph>'
+				);
+
 				test(
 					'inserts text + paragraph (at the beginning)',
 					'xxx<paragraph>yyy</paragraph>',
@@ -293,6 +300,13 @@ describe( 'DataController', () => {
 					'<paragraph>fyyy</paragraph><paragraph>xxx<inlineWidget></inlineWidget>zzz[]oo</paragraph>'
 				);
 
+				test(
+					'inserts paragraph + text + paragraph',
+					'<paragraph>yyy</paragraph>xxx<paragraph>zzz</paragraph>',
+					'<paragraph>f[]oo</paragraph>',
+					'<paragraph>fyyy</paragraph><paragraph>xxx</paragraph><paragraph>zzz[]oo</paragraph>'
+				);
+
 				test(
 					'inserts paragraph + text (at the beginning)',
 					'<paragraph>yyy</paragraph>xxx',