Преглед изворни кода

Merge pull request #28 from ckeditor/t/ckeditor5-engine/1207

Other: Use post-fixer API.
Piotr Jasiun пре 8 година
родитељ
комит
d4dac1a249

+ 14 - 49
packages/ckeditor5-paragraph/src/paragraph.js

@@ -8,10 +8,8 @@
  */
 
 import ParagraphCommand from './paragraphcommand';
-import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
-
-import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
 import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
 
@@ -35,7 +33,6 @@ export default class Paragraph extends Plugin {
 	init() {
 		const editor = this.editor;
 		const model = editor.model;
-		const doc = model.document;
 		const data = editor.data;
 		const editing = editor.editing;
 
@@ -87,64 +84,32 @@ export default class Paragraph extends Plugin {
 		// Post-fixer which takes care of adding empty paragraph elements to empty roots.
 		// Besides fixing content on #changesDone we also need to handle #dataReady because
 		// if initial data is empty or setData() wasn't even called there will be no #change fired.
-		doc.on( 'change', ( evt, type, changes, batch ) => {
-			if ( batch.type == 'transparent' ) {
-				return;
-			}
+		model.document.registerPostFixer( writer => this._autoparagraphEmptyRoots( writer ) );
 
-			this._findEmptyRoots( batch );
-		} );
-		doc.on( 'changesDone', () => this._autoparagraphEmptyRoots(), { priority: 'lowest' } );
 		editor.on( 'dataReady', () => {
-			this._findEmptyRoots( new Batch( 'transparent' ) );
-			this._autoparagraphEmptyRoots();
+			model.enqueueChange( 'transparent', writer => this._autoparagraphEmptyRoots( writer ) );
 		}, { priority: 'lowest' } );
 	}
 
-	/**
-	 * Looks through all roots created in document and marks every empty root, saving which batch made it empty.
-	 *
-	 * @private
-	 * @param {module:engine/model/batch~Batch} batch Batch of current change.
-	 */
-	_findEmptyRoots( batch ) {
-		const doc = this.editor.model.document;
-
-		for ( const rootName of doc.getRootNames() ) {
-			const root = doc.getRoot( rootName );
-
-			if ( root.isEmpty ) {
-				if ( !this._rootsToFix.has( root ) ) {
-					this._rootsToFix.set( root, batch );
-				}
-			} else {
-				this._rootsToFix.delete( root );
-			}
-		}
-	}
-
 	/**
 	 * Fixes all empty roots.
 	 *
 	 * @private
+	 * @returns {Boolean} `true` if any change has been applied, `false` otherwise.
 	 */
-	_autoparagraphEmptyRoots() {
+	_autoparagraphEmptyRoots( writer ) {
 		const model = this.editor.model;
 
-		for ( const [ root, batch ] of this._rootsToFix ) {
-			// Only empty roots are in `#_rootsToFix`. Even if root got content during `changesDone` event (because of, for example
-			// other feature), this will fire `findEmptyRoots` and remove that root from `#_rootsToFix`. So we are guaranteed
-			// to have only empty roots here.
-			//
-			// If paragraph element is allowed in the root, create paragraph element.
-			if ( model.schema.checkChild( root, 'paragraph' ) ) {
-				model.enqueueChange( batch, writer => {
-					// Remove root from `rootsToFix` here, before executing batch, to prevent infinite loops.
-					this._rootsToFix.delete( root );
-
-					// Fix empty root.
+		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;
+				}
 			}
 		}
 	}

+ 0 - 36
packages/ckeditor5-paragraph/tests/paragraph.js

@@ -407,32 +407,6 @@ describe( 'Paragraph feature', () => {
 			expect( doc.getRoot().getChild( 0 ).is( 'paragraph' ) ).to.be.true;
 		} );
 
-		it( 'should not fix root if it got content during changesDone event', () => {
-			// "Autoheading feature".
-			model.schema.register( 'heading', { inheritAllFrom: '$block' } );
-
-			buildModelConverter().for( editor.editing.modelToView, editor.data.modelToView )
-				.fromElement( 'heading' )
-				.toElement( 'h1' );
-
-			doc.on( 'changesDone', () => {
-				const root = doc.getRoot();
-
-				if ( root.isEmpty ) {
-					model.change( writer => {
-						writer.insertElement( 'heading', root );
-					} );
-				}
-			} );
-
-			model.change( writer => {
-				writer.remove( ModelRange.createIn( root ) );
-			} );
-
-			expect( doc.getRoot().childCount ).to.equal( 1 );
-			expect( doc.getRoot().getChild( 0 ).name ).to.equal( 'heading' );
-		} );
-
 		it( 'should not fix root which does not allow paragraph', () => {
 			model.schema.on( 'checkChild', ( evt, args ) => {
 				const def = model.schema.getDefinition( args[ 1 ] );
@@ -450,16 +424,6 @@ describe( 'Paragraph feature', () => {
 			expect( editor.getData() ).to.equal( '' );
 		} );
 
-		it( 'should not fix root if change was in transparent batch', () => {
-			editor.setData( '<p>Foobar</p>' );
-
-			model.enqueueChange( 'transparent', writer => {
-				writer.remove( ModelRange.createIn( root ) );
-			} );
-
-			expect( editor.getData() ).to.equal( '' );
-		} );
-
 		it( 'should fix empty roots in the right batch', () => {
 			let removeBatch, attributeBatch;