Ver código fonte

Changed the order of params and made the batch optional.

Piotrek Koszuliński 9 anos atrás
pai
commit
046d932a42

+ 8 - 7
packages/ckeditor5-engine/src/controller/datacontroller.js

@@ -111,7 +111,7 @@ export default class DataController {
 		this.viewToModel.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
 		this.viewToModel.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
 
-		this.on( 'insert', ( evt, data ) => insert( this, data.batch, data.selection, data.content ) );
+		this.on( 'insert', ( evt, data ) => insert( this, data.content, data.selection, data.batch ) );
 	}
 
 	/**
@@ -217,12 +217,13 @@ export default class DataController {
 	 * See {@link engine.controller.insert}.
 	 *
 	 * @fires engine.controller.DataController#insert
-	 * @param {engine.model.Batch} batch Batch to which deltas will be added.
-	 * @param {engine.model.Selection} selection Selection into which the content should be inserted.
 	 * @param {engine.view.DocumentFragment} content The content to insert.
+	 * @param {engine.model.Selection} selection Selection into which the content should be inserted.
+	 * @param {engine.model.Batch} [batch] Batch to which deltas will be added. If not specified, then
+	 * changes will be added to a new batch.
 	 */
-	insert( batch, selection, content ) {
-		this.fire( 'insert', { batch, selection, content } );
+	insert( content, selection, batch ) {
+		this.fire( 'insert', { content, selection, batch } );
 	}
 }
 
@@ -235,8 +236,8 @@ mix( DataController, EmitterMixin );
  *
  * @event engine.controller.DataController#insert
  * @param {Object} data
- * @param {engine.model.Batch} data.batch Batch to which deltas will be added.
- * @param {engine.model.Selection} data.selection Selection into which the content should be inserted.
  * @param {engine.view.DocumentFragment} data.content The content to insert.
+ * @param {engine.model.Selection} data.selection Selection into which the content should be inserted.
+ * @param {engine.model.Batch} [data.batch] Batch to which deltas will be added.
  */
 

+ 8 - 4
packages/ckeditor5-engine/src/controller/insert.js

@@ -23,12 +23,16 @@ import log from '../../utils/log.js';
  * @method engine.controller.insert
  * @param {engine.controller.DataController} dataController The data controller in context of which the insertion
  * should be performed.
- * @param {engine.model.Batch} batch Batch to which deltas will be added.
- * @param {engine.model.Selection} selection Selection into which the content should be inserted.
- * The selection should be collapsed.
  * @param {engine.view.DocumentFragment} content The content to insert.
+ * @param {engine.model.Selection} selection Selection into which the content should be inserted.
+ * @param {engine.model.Batch} [batch] Batch to which deltas will be added. If not specified, then
+ * changes will be added to a new batch.
  */
-export default function insert( dataController, batch, selection, content ) {
+export default function insert( dataController, content, selection, batch ) {
+	if ( !batch ) {
+		batch = dataController.model.batch();
+	}
+
 	if ( !selection.isCollapsed ) {
 		dataController.model.composer.deleteContents( batch, selection, {
 			merge: true

+ 7 - 6
packages/ckeditor5-engine/tests/controller/datacontroller.js

@@ -49,9 +49,10 @@ describe( 'DataController', () => {
 
 			setData( modelDocument, '<paragraph>a[]b</paragraph>' );
 
-			data.fire( 'insertContent', { batch, content, selection: modelDocument.selection } );
+			data.fire( 'insert', { content, selection: modelDocument.selection, batch } );
 
 			expect( getData( modelDocument ) ).to.equal( '<paragraph>ax[]b</paragraph>' );
+			expect( batch.deltas.length ).to.be.above( 0 );
 		} );
 	} );
 
@@ -269,15 +270,15 @@ describe( 'DataController', () => {
 		} );
 	} );
 
-	describe( 'insertContent', () => {
-		it( 'should fire the insertContent event', () => {
+	describe( 'insert', () => {
+		it( 'should fire the insert event', () => {
 			const spy = sinon.spy();
-			const batch = modelDocument.batch();
 			const content = new ViewDocumentFragment( [ new ViewText( 'x' ) ] );
+			const batch = modelDocument.batch();
 
-			data.on( 'insertContent', spy );
+			data.on( 'insert', spy );
 
-			data.insertContent( batch, modelDocument.selection, content );
+			data.insert( content, modelDocument.selection, batch );
 
 			expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( {
 				batch: batch,

+ 13 - 3
packages/ckeditor5-engine/tests/controller/editingcontroller.js

@@ -6,6 +6,8 @@
 /* globals setTimeout, Range, document */
 /* bender-tags: view */
 
+import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
+
 import EditingController from '/ckeditor5/engine/controller/editingcontroller.js';
 
 import ViewDocument from '/ckeditor5/engine/view/document.js';
@@ -103,15 +105,18 @@ describe( 'EditingController', () => {
 	} );
 
 	describe( 'conversion', () => {
-		let model, modelRoot, viewRoot, domRoot, editing;
+		let model, modelRoot, viewRoot, domRoot, editing, listener;
 
 		before( () => {
+			listener = Object.create( EmitterMixin );
+
 			model = new ModelDocument();
 			modelRoot = model.createRoot();
 
 			editing = new EditingController( model );
 
-			domRoot = document.getElementById( 'editor' );
+			domRoot = document.createElement( 'div' );
+			document.body.appendChild( domRoot );
 			viewRoot = editing.createRoot( domRoot );
 
 			model.schema.registerItem( 'paragraph', '$block' );
@@ -120,6 +125,11 @@ describe( 'EditingController', () => {
 			buildModelConverter().for( editing.modelToView ).fromElement( 'div' ).toElement( 'div' );
 		} );
 
+		after( () => {
+			document.body.removeChild( domRoot );
+			listener.stopListening();
+		} );
+
 		beforeEach( () => {
 			// Note: The below code is highly overcomplicated due to #455.
 			model.selection.removeAllRanges();
@@ -182,7 +192,7 @@ describe( 'EditingController', () => {
 		} );
 
 		it( 'should convert selection from view to model', ( done ) => {
-			editing.view.on( 'selectionChange', () => {
+			listener.listenTo( editing.view, 'selectionChange', () => {
 				setTimeout( () => {
 					expect( getModelData( model ) ).to.equal(
 						'<paragraph>foo</paragraph>' +

+ 19 - 2
packages/ckeditor5-engine/tests/controller/insert.js

@@ -7,9 +7,10 @@
 
 import Document from '/ckeditor5/engine/model/document.js';
 import DataController from '/ckeditor5/engine/controller/datacontroller.js';
-import insert from '/ckeditor5/engine/datacontroller/insert.js';
+import insert from '/ckeditor5/engine/controller/insert.js';
 
 import ViewDocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
+import ViewText from '/ckeditor5/engine/view/text.js';
 import ModelDocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
 import Text from '/ckeditor5/engine/model/text.js';
 
@@ -19,6 +20,22 @@ describe( 'DataController', () => {
 	let doc, dataController;
 
 	describe( 'insert', () => {
+		it( 'uses the passed batch', () => {
+			doc = new Document();
+			doc.createRoot();
+			doc.schema.allow( { name: '$text', inside: '$root' } );
+
+			dataController = new DataController( doc );
+
+			const batch = doc.batch();
+
+			setData( doc, 'x[]x' );
+
+			insert( dataController, new ViewDocumentFragment( [ new ViewText( 'a' ) ] ), doc.selection, batch );
+
+			expect( batch.deltas.length ).to.be.above( 0 );
+		} );
+
 		describe( 'in simple scenarios', () => {
 			beforeEach( () => {
 				doc = new Document();
@@ -598,7 +615,7 @@ describe( 'DataController', () => {
 				return content;
 			};
 
-			insert( dataController, doc.batch(), doc.selection, new ViewDocumentFragment() );
+			insert( dataController, new ViewDocumentFragment(), doc.selection );
 
 			expect( getData( doc ) ).to.equal( expectedData );
 		} );