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

Aligned code with the engine changes.

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

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

@@ -8,6 +8,7 @@
  */
 
 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.
@@ -19,12 +20,12 @@ export default class EnterCommand extends Command {
 	 * @inheritDoc
 	 */
 	execute() {
-		const doc = this.editor.document;
-		const batch = doc.batch();
-
-		doc.enqueueChanges( () => {
-			enterBlock( this.editor.data, batch, doc.selection, doc.schema );
+		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 } );
 		} );
 	}
@@ -33,14 +34,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/batch~Batch} batch A batch to which the deltas will be added.
 // @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, batch, selection, schema ) {
+function enterBlock( dataController, 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 ) ) {
@@ -49,26 +50,26 @@ function enterBlock( dataController, batch, selection, schema ) {
 		// This is an edge case and it's hard to tell what should actually happen because such a selection
 		// is not entirely valid.
 		if ( !isSelectionEmpty && startElement == endElement ) {
-			dataController.deleteContent( selection, batch );
+			dataController.deleteContent( selection );
 		}
 
 		return;
 	}
 
 	if ( isSelectionEmpty ) {
-		splitBlock( batch, selection, range.start );
+		splitBlock( model, selection, range.start );
 	} else {
 		const leaveUnmerged = !( range.start.isAtStart && range.end.isAtEnd );
 		const isContainedWithinOneElement = ( startElement == endElement );
 
-		dataController.deleteContent( selection, batch, { leaveUnmerged } );
+		dataController.deleteContent( selection, { leaveUnmerged } );
 
 		if ( leaveUnmerged ) {
 			// Partially selected elements.
 			//
 			// <h>x[xx]x</h>		-> <h>x^x</h>			-> <h>x</h><h>^x</h>
 			if ( isContainedWithinOneElement ) {
-				splitBlock( batch, selection, selection.focus );
+				splitBlock( model, selection, selection.focus );
 			}
 			// Selection over multiple elements.
 			//
@@ -80,23 +81,25 @@ function enterBlock( dataController, batch, selection, schema ) {
 	}
 }
 
-function splitBlock( batch, selection, splitPos ) {
+function splitBlock( model, selection, splitPos ) {
 	const oldElement = splitPos.parent;
 	const newElement = new oldElement.constructor( oldElement.name, oldElement.getAttributes() );
 
-	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).
-		batch.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).
-		batch.insert( newElement, splitPos.parent, 'before' );
-	} else {
-		batch.split( splitPos );
-	}
+	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 );
+		}
+	} );
 
 	selection.setCollapsedAt( splitPos.parent.nextSibling );
 }