8
0
Prechádzať zdrojové kódy

Make Batch fire 'batch' event on Document when first delta is added to a batch.

Szymon Cofalik 9 rokov pred
rodič
commit
fbe87ce29c

+ 7 - 3
packages/ckeditor5-engine/src/treemodel/batch.js

@@ -65,6 +65,10 @@ export default class Batch {
 		delta.batch = this;
 		this.deltas.push( delta );
 
+		if ( this.deltas.length == 1 ) {
+			this.doc.fire( 'batch', this );
+		}
+
 		return delta;
 	}
 }
@@ -87,12 +91,12 @@ export default class Batch {
  *			// Create operations which should be components of this delta.
  *			const operation = new InsertOperation( position, nodes, this.doc.version );
  *
+ *			// Add operation to the delta. It is important to add operation before applying it.
+ *			delta.addOperation( operation );
+ *
  *			// Remember to apply every operation, no magic, you need to do it manually.
  *			this.doc.applyOperation( operation );
  *
- *			// Add operation to the delta.
- *			delta.addOperation( operation );
- *
  *			// Add delta to the Batch instance.
  *			this.addDelta( delta );
  *

+ 2 - 2
packages/ckeditor5-engine/src/treemodel/delta/attributedelta.js

@@ -154,8 +154,8 @@ function changeNode( doc, key, value, node ) {
 			operation = new AttributeOperation( range, key, previousValue, value, doc.version );
 		}
 
-		doc.applyOperation( operation );
 		delta.addOperation( operation );
+		doc.applyOperation( operation );
 	}
 
 	// It is expected that this method returns a delta.
@@ -206,8 +206,8 @@ function changeRange( doc, attributeKey, attributeValue, range ) {
 		let range = new Range( lastSplitPosition, position );
 		const operation = new AttributeOperation( range, attributeKey, attributeValueBefore, attributeValue, doc.version );
 
-		doc.applyOperation( operation );
 		delta.addOperation( operation );
+		doc.applyOperation( operation );
 	}
 
 	return delta;

+ 2 - 2
packages/ckeditor5-engine/src/treemodel/delta/mergedelta.js

@@ -93,12 +93,12 @@ register( 'merge', function( position ) {
 	const positionBefore = Position.createFromParentAndOffset( nodeBefore, nodeBefore.getChildCount() );
 
 	const move = new MoveOperation( positionAfter, nodeAfter.getChildCount(), positionBefore, this.doc.version );
-	this.doc.applyOperation( move );
 	delta.addOperation( move );
+	this.doc.applyOperation( move );
 
 	const remove = new RemoveOperation( position, 1, this.doc.version );
-	this.doc.applyOperation( remove );
 	delta.addOperation( remove );
+	this.doc.applyOperation( remove );
 
 	this.addDelta( delta );
 

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/movedelta.js

@@ -77,8 +77,8 @@ export default class MoveDelta extends Delta {
 
 function addMoveOperation( batch, delta, sourcePosition, howMany, targetPosition ) {
 	const operation = new MoveOperation( sourcePosition, howMany, targetPosition, batch.doc.version );
-	batch.doc.applyOperation( operation );
 	delta.addOperation( operation );
+	batch.doc.applyOperation( operation );
 }
 
 /**

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/removedelta.js

@@ -22,8 +22,8 @@ export default class RemoveDelta extends MoveDelta {}
 
 function addRemoveOperation( batch, delta, position, howMany ) {
 	const operation = new RemoveOperation( position, howMany, batch.doc.version );
-	batch.doc.applyOperation( operation );
 	delta.addOperation( operation );
+	batch.doc.applyOperation( operation );
 }
 
 /**

+ 2 - 2
packages/ckeditor5-engine/src/treemodel/delta/unwrapdelta.js

@@ -77,14 +77,14 @@ register( 'unwrap', function( element ) {
 	let sourcePosition = Position.createFromParentAndOffset( element, 0 );
 
 	const move = new MoveOperation( sourcePosition, element.getChildCount(), Position.createBefore( element ), this.doc.version );
-	this.doc.applyOperation( move );
 	delta.addOperation( move );
+	this.doc.applyOperation( move );
 
 	// Computing new position because we moved some nodes before `element`.
 	// If we would cache `Position.createBefore( element )` we remove wrong node.
 	const remove = new RemoveOperation( Position.createBefore( element ), 1, this.doc.version );
-	this.doc.applyOperation( remove );
 	delta.addOperation( remove );
+	this.doc.applyOperation( remove );
 
 	this.addDelta( delta );
 

+ 1 - 1
packages/ckeditor5-engine/src/treemodel/delta/weakinsertdelta.js

@@ -45,8 +45,8 @@ register( 'weakInsert', function( position, nodes ) {
 	}
 
 	const operation = new InsertOperation( position, nodes, this.doc.version );
-	this.doc.applyOperation( operation );
 	delta.addOperation( operation );
+	this.doc.applyOperation( operation );
 
 	this.addDelta( delta );
 

+ 2 - 2
packages/ckeditor5-engine/src/treemodel/delta/wrapdelta.js

@@ -100,13 +100,13 @@ register( 'wrap', function( range, elementOrString ) {
 	const delta = new WrapDelta();
 
 	let insert = new InsertOperation( range.end, element, this.doc.version );
-	this.doc.applyOperation( insert );
 	delta.addOperation( insert );
+	this.doc.applyOperation( insert );
 
 	let targetPosition = Position.createFromParentAndOffset( element, 0 );
 	let move = new MoveOperation( range.start, range.end.offset - range.start.offset, targetPosition, this.doc.version );
-	this.doc.applyOperation( move );
 	delta.addOperation( move );
+	this.doc.applyOperation( move );
 
 	this.addDelta( delta );