Browse Source

The autoParagraphEmptyRoots extracted to util function.

Kuba Niegowski 5 years ago
parent
commit
8aceb3429b

+ 2 - 1
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -24,6 +24,7 @@ import ViewDocument from '../view/document';
 import ViewDowncastWriter from '../view/downcastwriter';
 
 import ModelRange from '../model/range';
+import { autoParagraphEmptyRoots } from '../model/utils/autoparagraphing';
 
 /**
  * Controller for the data pipeline. The data pipeline controls how data is retrieved from the document
@@ -144,7 +145,7 @@ export default class DataController {
 		// Fix empty roots after DataController is 'ready' (note that init method could be decorated and stopped).
 		// We need to handle this event because initial data could be empty and post-fixer would not get triggered.
 		this.on( 'ready', () => {
-			this.model.enqueueChange( 'transparent', writer => this.model._autoparagraphEmptyRoots( writer ) );
+			this.model.enqueueChange( 'transparent', writer => autoParagraphEmptyRoots( writer ) );
 		}, { priority: 'lowest' } );
 	}
 

+ 2 - 30
packages/ckeditor5-engine/src/model/model.js

@@ -25,6 +25,7 @@ import deleteContent from './utils/deletecontent';
 import modifySelection from './utils/modifyselection';
 import getSelectedContent from './utils/getselectedcontent';
 import { injectSelectionPostFixer } from './utils/selection-post-fixer';
+import { autoParagraphEmptyRoots } from './utils/autoparagraphing';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 // @if CK_DEBUG_ENGINE // const { dumpTrees } = require( '../dev-utils/utils' );
@@ -123,7 +124,7 @@ export default class Model {
 		injectSelectionPostFixer( this );
 
 		// Post-fixer which takes care of adding empty paragraph elements to the empty roots.
-		this.document.registerPostFixer( writer => this._autoparagraphEmptyRoots( writer ) );
+		this.document.registerPostFixer( writer => autoParagraphEmptyRoots( writer ) );
 
 		// @if CK_DEBUG_ENGINE // this.on( 'applyOperation', () => {
 		// @if CK_DEBUG_ENGINE // 	dumpTrees( this.document, this.document.version );
@@ -809,35 +810,6 @@ export default class Model {
 	}
 
 	/**
-	 * Fixes all empty roots.
-	 *
-	 * @protected
-	 * @param {module:engine/model/writer~Writer} writer The model writer.
-	 * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
-	 */
-	_autoparagraphEmptyRoots( writer ) {
-		const { schema, document } = writer.model;
-
-		for ( const rootName of document.getRootNames() ) {
-			const root = document.getRoot( rootName );
-
-			if ( root.isEmpty && !schema.checkChild( root, '$text' ) ) {
-				// If paragraph element is allowed in the root, create paragraph element.
-				if ( schema.checkChild( root, 'paragraph' ) ) {
-					writer.insertElement( 'paragraph', root );
-
-					// Other roots will get fixed in the next post-fixer round. Those will be triggered
-					// in the same batch no matter if this method was triggered by the post-fixing or not
-					// (the above insertElement call will trigger the post-fixers).
-					return true;
-				}
-			}
-		}
-
-		return false;
-	}
-
-	/**
 	 * Common part of {@link module:engine/model/model~Model#change} and {@link module:engine/model/model~Model#enqueueChange}
 	 * which calls callbacks and returns array of values returned by these callbacks.
 	 *

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

@@ -0,0 +1,37 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module engine/model/utils/autoparagraphing
+ */
+
+/**
+ * Fixes all empty roots.
+ *
+ * @protected
+ * @param {module:engine/model/writer~Writer} writer The model writer.
+ * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
+ */
+export function autoParagraphEmptyRoots( writer ) {
+	const { schema, document } = writer.model;
+
+	for ( const rootName of document.getRootNames() ) {
+		const root = document.getRoot( rootName );
+
+		if ( root.isEmpty && !schema.checkChild( root, '$text' ) ) {
+			// If paragraph element is allowed in the root, create paragraph element.
+			if ( schema.checkChild( root, 'paragraph' ) ) {
+				writer.insertElement( 'paragraph', root );
+
+				// Other roots will get fixed in the next post-fixer round. Those will be triggered
+				// in the same batch no matter if this method was triggered by the post-fixing or not
+				// (the above insertElement call will trigger the post-fixers).
+				return true;
+			}
+		}
+	}
+
+	return false;
+}