浏览代码

Refactoring: Deduped the code.

Piotrek Koszuliński 9 年之前
父节点
当前提交
0ec5e0bd93
共有 1 个文件被更改,包括 29 次插入21 次删除
  1. 29 21
      packages/ckeditor5-engine/src/datacontroller/insertcontent.js

+ 29 - 21
packages/ckeditor5-engine/src/datacontroller/insertcontent.js

@@ -186,20 +186,18 @@ class Insertion {
 		this._mergeSiblingsOf( node, context );
 	}
 
+	/**
+	 * @param {engine.model.Element} node The object element.
+	 * @param {Object} context
+	 */
 	_handleObject( node, context ) {
+		// Try finding it a place in the tree.
 		if ( this._checkAndSplitToAllowedPosition( 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 );
-			}
+		}
+		// Try autoparagraphing.
+		else {
+			this._tryAutoparagraphing( node, context );
 		}
 	}
 
@@ -214,16 +212,7 @@ class Insertion {
 		}
 		// 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( paragraph, context );
-			}
+			this._tryAutoparagraphing( node, context );
 		}
 	}
 
@@ -310,6 +299,25 @@ class Insertion {
 		mergePosRight.detach();
 	}
 
+	/**
+	 * Tries wrapping the node in a new paragraph and inserting it this way.
+	 *
+	 * @param {engine.model.Node} node The node which needs to be autoparagraphed.
+	 * @param {Object} context
+	 */
+	_tryAutoparagraphing( node, context ) {
+		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 );
+		}
+	}
+
 	/**
 	 * @param {engine.model.Node} node
 	 */