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

Merge branch t/engine/1186

Internal: Update API usage after merge t/1186 on engine.
Piotr Jasiun пре 8 година
родитељ
комит
2a57818205

+ 15 - 16
packages/ckeditor5-enter/src/entercommand.js

@@ -19,13 +19,12 @@ export default class EnterCommand extends Command {
 	 * @inheritDoc
 	 */
 	execute() {
-		const doc = this.editor.document;
-		const batch = doc.batch();
+		const model = this.editor.model;
+		const doc = model.document;
 
-		doc.enqueueChanges( () => {
-			enterBlock( this.editor.data, batch, doc.selection, doc.schema );
-
-			this.fire( 'afterExecute', { batch } );
+		model.change( writer => {
+			enterBlock( this.editor.data, writer, doc.selection, model.schema );
+			this.fire( 'afterExecute', { writer } );
 		} );
 	}
 }
@@ -33,10 +32,10 @@ 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/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, batch, selection, schema ) {
+function enterBlock( dataController, writer, selection, schema ) {
 	const isSelectionEmpty = selection.isCollapsed;
 	const range = selection.getFirstRange();
 	const startElement = range.start.parent;
@@ -49,26 +48,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( writer, 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( writer, selection, selection.focus );
 			}
 			// Selection over multiple elements.
 			//
@@ -80,7 +79,7 @@ function enterBlock( dataController, batch, selection, schema ) {
 	}
 }
 
-function splitBlock( batch, selection, splitPos ) {
+function splitBlock( writer, selection, splitPos ) {
 	const oldElement = splitPos.parent;
 	const newElement = new oldElement.constructor( oldElement.name, oldElement.getAttributes() );
 
@@ -88,14 +87,14 @@ function splitBlock( batch, selection, splitPos ) {
 		// 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' );
+		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).
-		batch.insert( newElement, splitPos.parent, 'before' );
+		writer.insert( newElement, splitPos.parent, 'before' );
 	} else {
-		batch.split( splitPos );
+		writer.split( splitPos );
 	}
 
 	selection.setCollapsedAt( splitPos.parent.nextSibling );

+ 16 - 20
packages/ckeditor5-enter/tests/entercommand.js

@@ -9,18 +9,19 @@ import InsertDelta from '@ckeditor/ckeditor5-engine/src/model/delta/insertdelta'
 import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 
 describe( 'EnterCommand', () => {
-	let editor, doc, schema, command;
+	let editor, model, doc, schema, command;
 
 	beforeEach( () => {
 		return ModelTestEditor.create()
 			.then( newEditor => {
 				editor = newEditor;
-				doc = editor.document;
+				model = editor.model;
+				doc = model.document;
 
 				command = new EnterCommand( editor );
 				editor.commands.add( 'enter', command );
 
-				schema = doc.schema;
+				schema = model.schema;
 
 				// Note: We could use real names like 'paragraph', but that would make test patterns too long.
 				// Plus, this is actually a good test that the algorithm can be used for any model.
@@ -42,23 +43,18 @@ describe( 'EnterCommand', () => {
 	} );
 
 	describe( 'EnterCommand', () => {
-		it( 'enters a block using enqueueChanges', () => {
-			setData( doc, '<p>foo[]</p>' );
+		it( 'enters a block using parent batch', () => {
+			setData( model, '<p>foo[]</p>' );
 
-			doc.enqueueChanges( () => {
+			model.change( writer => {
+				expect( writer.batch.deltas ).to.length( 0 );
 				editor.execute( 'enter' );
-
-				// We expect that command is executed in enqueue changes block. Since we are already in
-				// an enqueued block, the command execution will be postponed. Hence, no changes.
-				expect( getData( doc, { withoutSelection: true } ) ).to.equal( '<p>foo</p>' );
+				expect( writer.batch.deltas ).to.length.above( 0 );
 			} );
-
-			// After all enqueued changes are done, the command execution is reflected.
-			expect( getData( doc, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p></p>' );
 		} );
 
 		it( 'creates InsertDelta if enter is at the beginning of block', () => {
-			setData( doc, '<p>[]foo</p>' );
+			setData( model, '<p>[]foo</p>' );
 
 			editor.execute( 'enter' );
 
@@ -68,7 +64,7 @@ describe( 'EnterCommand', () => {
 		} );
 
 		it( 'creates InsertDelta if enter is at the end of block', () => {
-			setData( doc, '<p>foo[]</p>' );
+			setData( model, '<p>foo[]</p>' );
 
 			editor.execute( 'enter' );
 
@@ -167,13 +163,13 @@ describe( 'EnterCommand', () => {
 			);
 
 			it( 'leaves one empty element after two were fully selected (backward)', () => {
-				setData( doc, '<p>[abc</p><p>def]</p>' );
+				setData( model, '<p>[abc</p><p>def]</p>' );
 				// @TODO: Add option for setting selection direction to model utils.
 				doc.selection._lastRangeBackward = true;
 
 				command.execute();
 
-				expect( getData( doc ) ).to.equal( '<p>[]</p>' );
+				expect( getData( model ) ).to.equal( '<p>[]</p>' );
 			} );
 
 			it( 'uses DataController.deleteContent', () => {
@@ -181,7 +177,7 @@ describe( 'EnterCommand', () => {
 
 				editor.data.on( 'deleteContent', spy );
 
-				setData( doc, '<p>[x]</p>' );
+				setData( model, '<p>[x]</p>' );
 
 				command.execute();
 
@@ -191,11 +187,11 @@ describe( 'EnterCommand', () => {
 
 		function test( title, input, output ) {
 			it( title, () => {
-				setData( doc, input );
+				setData( model, input );
 
 				command.execute();
 
-				expect( getData( doc ) ).to.equal( output );
+				expect( getData( model ) ).to.equal( output );
 			} );
 		}
 	} );