8
0
Просмотр исходного кода

Replaced model parameter by writer.

Oskar Wróbel 8 лет назад
Родитель
Сommit
7ee3536a51
1 измененных файлов с 22 добавлено и 25 удалено
  1. 22 25
      packages/ckeditor5-enter/src/entercommand.js

+ 22 - 25
packages/ckeditor5-enter/src/entercommand.js

@@ -8,7 +8,6 @@
  */
 
 import Command from '@ckeditor/ckeditor5-core/src/command';
-import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
 
 /**
  * Enter command. It is used by the {@link module:enter/enter~Enter Enter feature} to handle the <kbd>Enter</kbd> key.
@@ -22,11 +21,11 @@ export default class EnterCommand extends Command {
 	execute() {
 		const model = this.editor.model;
 		const doc = model.document;
-		const batch = new Batch();
 
-		model.enqueueChange( () => {
-			enterBlock( this.editor.data, doc.selection, model.schema );
-			this.fire( 'afterExecute', { batch } );
+		model.change( writer => {
+			enterBlock( this.editor.data, writer, doc.selection, model.schema );
+			// @TODO This event is fired mainly for HeadingFeature this should be replace by heading post-fixer in the future.
+			this.fire( 'afterExecute', { writer } );
 		} );
 	}
 }
@@ -34,14 +33,14 @@ export default class EnterCommand extends Command {
 // Creates a new block in the way that the <kbd>Enter</kbd> key is expected to work.
 //
 // @param {engine.controller.DataController} dataController
+// @param {module:engine.model/writer~Writer} writer
 // @param {module:engine/model/selection~Selection} selection Selection on which the action should be performed.
 // @param {module:engine/model/schema~Schema} schema
-function enterBlock( dataController, selection, schema ) {
+function enterBlock( dataController, writer, selection, schema ) {
 	const isSelectionEmpty = selection.isCollapsed;
 	const range = selection.getFirstRange();
 	const startElement = range.start.parent;
 	const endElement = range.end.parent;
-	const model = dataController.model;
 
 	// Don't touch the roots and other limit elements.
 	if ( schema.limits.has( startElement.name ) || schema.limits.has( endElement.name ) ) {
@@ -57,7 +56,7 @@ function enterBlock( dataController, selection, schema ) {
 	}
 
 	if ( isSelectionEmpty ) {
-		splitBlock( model, selection, range.start );
+		splitBlock( writer, selection, range.start );
 	} else {
 		const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
 		const isContainedWithinOneElement = ( startElement == endElement );
@@ -69,7 +68,7 @@ function enterBlock( dataController, selection, schema ) {
 			//
 			// <h>x[xx]x</h>		-> <h>x^x</h>			-> <h>x</h><h>^x</h>
 			if ( isContainedWithinOneElement ) {
-				splitBlock( model, selection, selection.focus );
+				splitBlock( writer, selection, selection.focus );
 			}
 			// Selection over multiple elements.
 			//
@@ -81,25 +80,23 @@ function enterBlock( dataController, selection, schema ) {
 	}
 }
 
-function splitBlock( model, selection, splitPos ) {
+function splitBlock( writer, selection, splitPos ) {
 	const oldElement = splitPos.parent;
 	const newElement = new oldElement.constructor( oldElement.name, oldElement.getAttributes() );
 
-	model.change( writer => {
-		if ( splitPos.isAtEnd ) {
-			// If the split is at the end of element, instead of splitting, just create a clone of position's parent
-			// element and insert it after split element. The result is the same but less operations are done
-			// and it's more semantically correct (when it comes to operational transformation).
-			writer.insert( newElement, splitPos.parent, 'after' );
-		} else if ( splitPos.isAtStart ) {
-			// If the split is at the start of element, instead of splitting, just create a clone of position's parent
-			// element and insert it before split element. The result is the same but less operations are done
-			// and it's more semantically correct (when it comes to operational transformation).
-			writer.insert( newElement, splitPos.parent, 'before' );
-		} else {
-			writer.split( splitPos );
-		}
-	} );
+	if ( splitPos.isAtEnd ) {
+		// If the split is at the end of element, instead of splitting, just create a clone of position's parent
+		// element and insert it after split element. The result is the same but less operations are done
+		// and it's more semantically correct (when it comes to operational transformation).
+		writer.insert( newElement, splitPos.parent, 'after' );
+	} else if ( splitPos.isAtStart ) {
+		// If the split is at the start of element, instead of splitting, just create a clone of position's parent
+		// element and insert it before split element. The result is the same but less operations are done
+		// and it's more semantically correct (when it comes to operational transformation).
+		writer.insert( newElement, splitPos.parent, 'before' );
+	} else {
+		writer.split( splitPos );
+	}
 
 	selection.setCollapsedAt( splitPos.parent.nextSibling );
 }