8
0
Quellcode durchsuchen

Auto-paragraphing empty roots moved from the paragraph package to the engine.

Kuba Niegowski vor 5 Jahren
Ursprung
Commit
24d6147670

+ 6 - 0
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -140,6 +140,12 @@ export default class DataController {
 		this.on( 'init', () => {
 			this.fire( 'ready' );
 		}, { priority: 'lowest' } );
+
+		// 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 ) );
+		}, { priority: 'lowest' } );
 	}
 
 	/**

+ 28 - 0
packages/ckeditor5-engine/src/model/model.js

@@ -122,6 +122,9 @@ 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 ) );
+
 		// @if CK_DEBUG_ENGINE // this.on( 'applyOperation', () => {
 		// @if CK_DEBUG_ENGINE // 	dumpTrees( this.document, this.document.version );
 		// @if CK_DEBUG_ENGINE // }, { priority: 'lowest' } );
@@ -806,6 +809,31 @@ export default class Model {
 	}
 
 	/**
+	 * Fixes all empty roots.
+	 *
+	 * @protected
+	 * @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 );
+
+					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.
 	 *

+ 2 - 2
packages/ckeditor5-engine/tests/model/utils/deletecontent.js

@@ -943,8 +943,8 @@ describe( 'DataController utils', () => {
 
 				deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
 
-				expect( getData( model, { rootName: 'bodyRoot' } ) )
-					.to.equal( '[]' );
+				// Note that auto-paragraphing post-fixer injected a paragraph into the empty root.
+				expect( getData( model, { rootName: 'bodyRoot' } ) ).to.equal( '<paragraph>[]</paragraph>' );
 			} );
 		} );
 

+ 2 - 1
packages/ckeditor5-engine/tests/model/utils/selection-post-fixer.js

@@ -75,7 +75,8 @@ describe( 'Selection post-fixer', () => {
 		it( 'should not crash if there is no correct position for model selection', () => {
 			setModelData( model, '' );
 
-			expect( getModelData( model ) ).to.equal( '[]' );
+			// Note that auto-paragraphing post-fixer injected a paragraph into the empty root.
+			expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
 		} );
 
 		it( 'should react to structure changes', () => {

+ 0 - 34
packages/ckeditor5-paragraph/src/paragraph.js

@@ -66,40 +66,6 @@ export default class Paragraph extends Plugin {
 			view: /.+/,
 			converterPriority: 'low'
 		} );
-
-		// Empty roots autoparagraphing. -----------------------------------------------
-
-		// Post-fixer which takes care of adding empty paragraph elements to empty roots.
-		// Besides fixing content on #changesDone we also need to handle editor.data#ready event because
-		// if initial data is empty or setData() wasn't even called there will be no #change fired.
-		model.document.registerPostFixer( writer => this._autoparagraphEmptyRoots( writer ) );
-
-		editor.data.on( 'ready', () => {
-			model.enqueueChange( 'transparent', writer => this._autoparagraphEmptyRoots( writer ) );
-		}, { priority: 'lowest' } );
-	}
-
-	/**
-	 * Fixes all empty roots.
-	 *
-	 * @private
-	 * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
-	 */
-	_autoparagraphEmptyRoots( writer ) {
-		const model = this.editor.model;
-
-		for ( const rootName of model.document.getRootNames() ) {
-			const root = model.document.getRoot( rootName );
-
-			if ( root.isEmpty && root.rootName != '$graveyard' ) {
-				// If paragraph element is allowed in the root, create paragraph element.
-				if ( model.schema.checkChild( root, 'paragraph' ) ) {
-					writer.insertElement( 'paragraph', root );
-
-					return true;
-				}
-			}
-		}
 	}
 }