Kaynağa Gözat

The isParagraphable and wrapInParagraph extracted to util functions.

Kuba Niegowski 5 yıl önce
ebeveyn
işleme
b0e1ae9292

+ 6 - 21
packages/ckeditor5-engine/src/conversion/upcastdispatcher.js

@@ -11,6 +11,7 @@ import ViewConsumable from './viewconsumable';
 import ModelRange from '../model/range';
 import ModelPosition from '../model/position';
 import { SchemaContext } from '../model/schema';
+import { isParagraphable, wrapInParagraph } from '../model/utils/autoparagraphing';
 
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
@@ -368,10 +369,12 @@ export default class UpcastDispatcher {
 
 		if ( !allowedParent ) {
 			// Check if the node wrapped with a paragraph would be accepted by the schema.
-			const paragraph = wrapWithParagraphIfPossible( node, modelCursor, writer, schema );
+			if ( !isParagraphable( modelCursor, node, schema ) ) {
+				return null;
+			}
 
-			return paragraph && {
-				position: writer.createPositionAt( paragraph, 0 )
+			return {
+				position: wrapInParagraph( modelCursor, writer )
 			};
 		}
 
@@ -578,24 +581,6 @@ function createContextTree( contextDefinition, writer ) {
 	return position;
 }
 
-// Auto-paragraphing
-function wrapWithParagraphIfPossible( node, position, writer, schema ) {
-	// Check if the node wrapped with a paragraph would be accepted by the schema.
-	const context = schema.createContext( position );
-
-	// If paragraph is not acceptable in the current position or the model node is not accepted in that context
-	// there is nothing more that can be done with it.
-	if ( !schema.checkChild( context, 'paragraph' ) || !schema.checkChild( context.push( 'paragraph' ), node ) ) {
-		return null;
-	}
-
-	const paragraph = writer.createElement( 'paragraph' );
-
-	writer.insert( paragraph, position );
-
-	return paragraph;
-}
-
 /**
  * A set of conversion utils available as the third parameter of
  * {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher upcast dispatcher}'s events.

+ 3 - 8
packages/ckeditor5-engine/src/conversion/upcasthelpers.js

@@ -10,6 +10,7 @@ import { cloneDeep } from 'lodash-es';
 import { attachLinkToDocumentation } from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 import priorities from '@ckeditor/ckeditor5-utils/src/priorities';
+import { isParagraphable, wrapInParagraph } from '../model/utils/autoparagraphing';
 
 /* global console */
 
@@ -468,17 +469,11 @@ export function convertText() {
 		}
 
 		if ( !schema.checkChild( position, '$text' ) ) {
-			// Check if this text node would be allowed if wrapped with a paragraph.
-			const context = schema.createContext( position );
-
-			if ( !schema.checkChild( context, 'paragraph' ) || !schema.checkChild( context.push( 'paragraph' ), '$text' ) ) {
+			if ( !isParagraphable( position, '$text', schema ) ) {
 				return;
 			}
 
-			const paragraph = writer.createElement( 'paragraph' );
-
-			writer.insert( paragraph, position );
-			position = writer.createPositionAt( paragraph, 0 );
+			position = wrapInParagraph( position, writer );
 		}
 
 		const text = writer.createText( data.viewItem.data );

+ 40 - 0
packages/ckeditor5-engine/src/model/utils/autoparagraphing.js

@@ -35,3 +35,43 @@ export function autoParagraphEmptyRoots( writer ) {
 
 	return false;
 }
+
+/**
+ * Checks if the given node wrapped with a paragraph would be accepted by the schema in the given position.
+ *
+ * @protected
+ * @param {module:engine/model/position~Position} position The position at which to check.
+ * @param {module:engine/model/node~Node|String} nodeOrType The child node or child type to check.
+ * @param {module:engine/model/schema~Schema} schema A schema instance used for element validation.
+ */
+export function isParagraphable( position, nodeOrType, schema ) {
+	const context = schema.createContext( position );
+
+	// When paragraph is allowed in this context...
+	if ( !schema.checkChild( context, 'paragraph' ) ) {
+		return false;
+	}
+
+	// And a node would be allowed in this paragraph...
+	if ( !schema.checkChild( context.push( 'paragraph' ), nodeOrType ) ) {
+		return false;
+	}
+
+	return true;
+}
+
+/**
+ * Inserts a new paragraph at the given position and returns a position inside that paragraph.
+ *
+ * @protected
+ * @param {module:engine/model/position~Position} position The position where a paragraph should be inserted.
+ * @param {module:engine/model/writer~Writer} writer The model writer.
+ * @returns {module:engine/model/position~Position} Position inside the created paragraph.
+ */
+export function wrapInParagraph( position, writer ) {
+	const paragraph = writer.createElement( 'paragraph' );
+
+	writer.insert( paragraph, position );
+
+	return writer.createPositionAt( paragraph, 0 );
+}