浏览代码

Added hook for the `Document#applyOperation`.

Maciej Bukowski 8 年之前
父节点
当前提交
6bb281b556

+ 27 - 16
packages/ckeditor5-engine/src/dev-utils/enableenginedebug.js

@@ -111,6 +111,7 @@ export default function enableEngineDebug( logger ) {
 
 		enableLoggingTools();
 		enableDocumentTools();
+		enableReplayerTools();
 	}
 
 	return DebugPlugin;
@@ -455,6 +456,32 @@ function enableLoggingTools() {
 	};
 }
 
+function enableReplayerTools() {
+	const _modelDocumentApplyOperation = ModelDocument.prototype.applyOperation;
+
+	ModelDocument.prototype.applyOperation = function( operation ) {
+		if ( !this._lastDelta ) {
+			this._appliedDeltas = [];
+			this._lastDelta = operation.delta;
+		} else if ( this._lastDelta !== operation.delta ) {
+			this._appliedDeltas.push( this._lastDelta.toJSON() );
+			this._lastDelta = operation.delta;
+		}
+
+		_modelDocumentApplyOperation.call( this, operation );
+	};
+
+	ModelDocument.prototype.getAppliedDeltas = function() {
+		const appliedDeltas = this._appliedDeltas.concat( this._lastDelta.toJSON() );
+
+		return appliedDeltas.map( JSON.stringify ).join( LOG_SEPARATOR );
+	};
+
+	ModelDocument.prototype.createReplayer = function( stringifiedDeltas ) {
+		return new DeltaReplayer( this, LOG_SEPARATOR, stringifiedDeltas );
+	};
+}
+
 function enableDocumentTools() {
 	const _modelDocumentApplyOperation = ModelDocument.prototype.applyOperation;
 
@@ -476,22 +503,6 @@ function enableDocumentTools() {
 		logDocument( this, version );
 	};
 
-	ModelDocument.prototype.initializeDebugging = function() {
-		this._appliedDeltas = [];
-	};
-
-	ModelDocument.prototype.addAppliedDelta = function( delta ) {
-		this._appliedDeltas.push( delta.toJSON() );
-	};
-
-	ModelDocument.prototype.getAppliedDeltas = function() {
-		return this._appliedDeltas.map( JSON.stringify ).join( LOG_SEPARATOR );
-	};
-
-	ModelDocument.prototype.createReplayer = function( stringifiedDeltas ) {
-		return new DeltaReplayer( this, LOG_SEPARATOR, stringifiedDeltas );
-	};
-
 	ViewDocument.prototype.log = function( version ) {
 		logDocument( this, version );
 	};

+ 2 - 2
packages/ckeditor5-engine/tests/dev-utils/enableenginedebug.js

@@ -730,8 +730,8 @@ describe( 'debug tools', () => {
 			delta.addOperation( move );
 			delta.addOperation( remove );
 
-			modelDoc.initializeDebugging();
-			modelDoc.addAppliedDelta( delta );
+			modelDoc.applyOperation( move );
+			modelDoc.applyOperation( remove );
 
 			const stringifiedDeltas = modelDoc.getAppliedDeltas();