浏览代码

Moved replayer to separate file.

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

+ 87 - 0
packages/ckeditor5-engine/src/dev-utils/deltareplayer.js

@@ -0,0 +1,87 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/**
+ * @module engine/dev-utils/deltareplayer
+ */
+
+/* global setTimeout, console */
+
+import DeltaFactory from '../model/delta/deltafactory';
+
+/**
+ * DeltaReplayer is a dev-tool created for easily replaying operations on the document from stringified deltas
+ */
+export default class DeltaReplayer {
+    /**
+     * @param {module:engine/model/document~Document} document 
+     * @param {String} logSeparator Separator between deltas.
+     * @param {String} stringifiedDeltas Deltas to replay.
+     */
+    constructor( document, logSeparator, stringifiedDeltas ) {
+        this._document = document;
+        this._deltaToReplay = [];
+        this._logSeparator = logSeparator;
+        this.setStringifiedDeltas( stringifiedDeltas );
+    }
+
+    /**
+     * @param {String} stringifiedDeltas Deltas to replay.
+     */
+    setStringifiedDeltas( stringifiedDeltas ) {
+        this._deltaToReplay = stringifiedDeltas
+			.split( this._logSeparator )
+			.map( stringifiedDelta => JSON.parse( stringifiedDelta ) )
+			.map( jsonDelta => DeltaFactory.fromJSON( jsonDelta, this._document ) );
+    }
+
+    /**
+     * @param {Number} timeInterval
+     */
+    play( timeInterval = 1000 ) {
+        if ( this._deltaToReplay.length === 0 ) {
+            return;
+        }
+
+        this.applyNextDelta().then( () => {
+            setTimeout( () => this.play(), timeInterval );
+        } );
+    }
+
+    /**
+     * @returns {Promise}
+     */
+    applyNextDelta() {
+        const document = this._document;
+
+        return new Promise( ( res, rej ) => {
+            document.enqueueChanges( () => {
+                const delta = this._deltaToReplay.shift();
+
+                if ( !delta ) {
+                    return rej( new Error('No deltas to replay') );
+                }
+
+                const batch = document.batch();
+                batch.addDelta( delta );
+
+                for ( const operation of delta.operations ) {
+                    document.applyOperation( operation );
+                }
+
+                res();
+            } );
+        } );
+    }
+
+    /**
+     * @returns {Promise}
+     */
+    applyAllDeltas() {
+        return this.applyNextDelta()
+            .then( () => this.applyAllDeltas() )
+            .catch( err => console.warn( err ) );
+    }
+}

+ 7 - 43
packages/ckeditor5-engine/src/dev-utils/enableenginedebug.js

@@ -46,7 +46,7 @@ import ViewDocumentFragment from '../view/documentfragment';
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
 
-import DeltaFactory from '../model/delta/deltafactory';
+import DeltaReplayer from './deltareplayer';
 
 const treeDump = Symbol( '_treeDump' );
 
@@ -476,56 +476,20 @@ function enableDocumentTools() {
 		logDocument( this, version );
 	};
 
+	ModelDocument.prototype.initializeDebugging = function() {
+		this._appliedDeltas = [];
+	};
 
-	ModelDocument.prototype.logDeltas = function() {
-		return ( this._deltaLogs || [] ).map( JSON.stringify ).join( LOG_SEPARATOR );
+	ModelDocument.prototype.addAppliedDelta = function( delta ) {
+		this._appliedDeltas.push( delta.toJSON() );
 	};
 
 	ModelDocument.prototype.logAppliedDeltas = function() {
 		return ( this._appliedDeltas || [] ).map( JSON.stringify ).join( LOG_SEPARATOR );
 	};
 
-	ModelDocument.prototype.applyStringifiedDeltas = function( stringifiedDeltas ) {
-		this.enqueueChanges( () => {
-			for ( const stringifiedDelta of stringifiedDeltas.split( LOG_SEPARATOR ) ) {
-				const jsonDelta = JSON.parse( stringifiedDelta );
-				const delta = DeltaFactory.fromJSON( jsonDelta, this );
-
-				const batch = this.batch();
-
-				batch.addDelta( delta );
-
-				for ( const operation of delta.operations ) {
-					this.applyOperation( operation );
-				}
-			}
-		} );
-	};
-
 	ModelDocument.prototype.createReplayer = function( stringifiedDeltas ) {
-		this._deltaToReplay = stringifiedDeltas
-			.split( LOG_SEPARATOR )
-			.map( stringifiedDelta => JSON.parse( stringifiedDelta ) )
-			.map( jsonDelta => DeltaFactory.fromJSON( jsonDelta, this ) );
-	};
-
-	ModelDocument.prototype.nextDelta = function() {
-		this.enqueueChanges( () => {
-			const delta = this._deltaToReplay.shift();
-
-			if ( !delta ) {
-				console.warn( 'No deltas to replay' );
-
-				return;
-			}
-
-			const batch = this.batch();
-			batch.addDelta( delta );
-
-			for ( const operation of delta.operations ) {
-				this.applyOperation( operation );
-			}
-		} );
+		return new DeltaReplayer( this, LOG_SEPARATOR, stringifiedDeltas );
 	};
 
 	ViewDocument.prototype.log = function( version ) {