Przeglądaj źródła

Aligned DeltaReplayer with engine changes.

Oskar Wróbel 8 lat temu
rodzic
commit
9c5ef9a181

+ 8 - 9
packages/ckeditor5-engine/src/dev-utils/deltareplayer.js

@@ -16,12 +16,12 @@ import DeltaFactory from '../model/delta/deltafactory';
  */
 export default class DeltaReplayer {
 	/**
-	 * @param {module:engine/model/document~Document} document Document to replay deltas on.
+	 * @param {module:engine/model/model~Model} model Data model.
 	 * @param {String} logSeparator Separator between deltas.
 	 * @param {String} stringifiedDeltas Deltas to replay.
 	 */
-	constructor( document, logSeparator, stringifiedDeltas ) {
-		this._document = document;
+	constructor( model, logSeparator, stringifiedDeltas ) {
+		this._model = model;
 		this._logSeparator = logSeparator;
 		this.setStringifiedDeltas( stringifiedDeltas );
 	}
@@ -118,23 +118,22 @@ export default class DeltaReplayer {
 	 * @returns {Promise.<Boolean>}
 	 */
 	applyNextDelta() {
-		const document = this._document;
+		const model = this._model;
 
 		return new Promise( res => {
-			document.enqueueChanges( () => {
+			model.enqueueChange( writer => {
 				const jsonDelta = this._deltasToReplay.shift();
 
 				if ( !jsonDelta ) {
 					return res( true );
 				}
 
-				const delta = DeltaFactory.fromJSON( jsonDelta, this._document );
+				const delta = DeltaFactory.fromJSON( jsonDelta, model.document );
 
-				const batch = document.batch();
-				batch.addDelta( delta );
+				writer.batch.addDelta( delta );
 
 				for ( const operation of delta.operations ) {
-					document.applyOperation( operation );
+					model.applyOperation( operation );
 				}
 
 				res( false );

+ 32 - 31
packages/ckeditor5-engine/tests/dev-utils/deltareplayer.js

@@ -4,23 +4,23 @@
  */
 
 import DeltaReplayer from '../../src/dev-utils/deltareplayer';
-import Document from '../../src/model/document';
+import Model from '../../src/model/model';
 
 describe( 'DeltaReplayer', () => {
 	describe( 'constructor()', () => {
 		it( 'should be able to initialize replayer without deltas', () => {
-			const doc = getDocument();
+			const model = getModel();
 			const stringifiedDeltas = '';
-			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
+			const deltaReplayer = new DeltaReplayer( model, '---', stringifiedDeltas );
 
 			expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [] );
 		} );
 
 		it( 'should be able to initialize replayer with deltas', () => {
-			const doc = getDocument();
+			const model = getModel();
 			const delta = getFirstDelta();
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
+			const deltaReplayer = new DeltaReplayer( model, '---', JSON.stringify( delta ) );
 
 			expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [ delta ] );
 		} );
@@ -28,10 +28,10 @@ describe( 'DeltaReplayer', () => {
 
 	describe( 'applyNextDelta()', () => {
 		it( 'should remove first delta from stack', () => {
-			const doc = getDocument();
+			const model = getModel();
 			const delta = getFirstDelta();
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
+			const deltaReplayer = new DeltaReplayer( model, '---', JSON.stringify( delta ) );
 
 			return deltaReplayer.applyNextDelta().then( isFinished => {
 				expect( deltaReplayer.getDeltasToReplay() ).to.deep.equal( [] );
@@ -40,19 +40,19 @@ describe( 'DeltaReplayer', () => {
 		} );
 
 		it( 'should apply first delta on the document', () => {
-			const doc = getDocument();
+			const model = getModel();
 			const delta = getFirstDelta();
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', JSON.stringify( delta ) );
+			const deltaReplayer = new DeltaReplayer( model, '---', JSON.stringify( delta ) );
 
 			return deltaReplayer.applyNextDelta().then( () => {
-				expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 1 );
+				expect( Array.from( model.document.getRoot().getChildren() ).length ).to.equal( 1 );
 			} );
 		} );
 
 		it( 'should resolve with true if 0 deltas are provided', () => {
-			const doc = getDocument();
-			const deltaReplayer = new DeltaReplayer( doc, '---', '' );
+			const model = getModel();
+			const deltaReplayer = new DeltaReplayer( model, '---', '' );
 
 			return deltaReplayer.applyNextDelta().then( isFinished => {
 				expect( isFinished ).to.equal( true );
@@ -62,16 +62,16 @@ describe( 'DeltaReplayer', () => {
 
 	describe( 'applyAllDeltas()', () => {
 		it( 'should apply all deltas on the document', () => {
-			const doc = getDocument();
+			const model = getModel();
 
 			const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
 				.map( d => JSON.stringify( d ) )
 				.join( '---' );
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
+			const deltaReplayer = new DeltaReplayer( model, '---', stringifiedDeltas );
 
 			return deltaReplayer.applyAllDeltas().then( () => {
-				expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 2 );
+				expect( Array.from( model.document.getRoot().getChildren() ).length ).to.equal( 2 );
 				expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 0 );
 			} );
 		} );
@@ -79,31 +79,31 @@ describe( 'DeltaReplayer', () => {
 
 	describe( 'applyDeltas()', () => {
 		it( 'should apply certain number of deltas on the document', () => {
-			const doc = getDocument();
+			const model = getModel();
 
 			const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
 				.map( d => JSON.stringify( d ) )
 				.join( '---' );
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
+			const deltaReplayer = new DeltaReplayer( model, '---', stringifiedDeltas );
 
 			return deltaReplayer.applyDeltas( 1 ).then( () => {
-				expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 1 );
+				expect( Array.from( model.document.getRoot().getChildren() ).length ).to.equal( 1 );
 				expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 1 );
 			} );
 		} );
 
 		it( 'should not throw an error if the number of deltas is lower than number of expected deltas to replay', () => {
-			const doc = getDocument();
+			const model = getModel();
 
 			const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
 				.map( d => JSON.stringify( d ) )
 				.join( '---' );
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
+			const deltaReplayer = new DeltaReplayer( model, '---', stringifiedDeltas );
 
 			return deltaReplayer.applyDeltas( 3 ).then( () => {
-				expect( Array.from( doc.getRoot().getChildren() ).length ).to.equal( 2 );
+				expect( Array.from( model.document.getRoot().getChildren() ).length ).to.equal( 2 );
 				expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 0 );
 			} );
 		} );
@@ -111,13 +111,13 @@ describe( 'DeltaReplayer', () => {
 
 	describe( 'play()', () => {
 		it( 'should play deltas with time interval', () => {
-			const doc = getDocument();
+			const model = getModel();
 
 			const stringifiedDeltas = [ getFirstDelta(), getSecondDelta() ]
 				.map( d => JSON.stringify( d ) )
 				.join( '---' );
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
+			const deltaReplayer = new DeltaReplayer( model, '---', stringifiedDeltas );
 
 			return deltaReplayer.play( 0 ).then( () => {
 				expect( deltaReplayer.getDeltasToReplay().length ).to.equal( 0 );
@@ -125,15 +125,15 @@ describe( 'DeltaReplayer', () => {
 		} );
 
 		it( 'should work with default time interval', () => {
-			const doc = getDocument();
+			const model = getModel();
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', '' );
+			const deltaReplayer = new DeltaReplayer( model, '---', '' );
 
 			return deltaReplayer.play();
 		} );
 
 		it( 'should correctly handle errors coming from the engine', () => {
-			const doc = getDocument();
+			const model = getModel();
 
 			const invalidDelta = getSecondDelta();
 			invalidDelta.operations[ 0 ].baseVersion = 3;
@@ -142,7 +142,7 @@ describe( 'DeltaReplayer', () => {
 				.map( d => JSON.stringify( d ) )
 				.join( '---' );
 
-			const deltaReplayer = new DeltaReplayer( doc, '---', stringifiedDeltas );
+			const deltaReplayer = new DeltaReplayer( model, '---', stringifiedDeltas );
 
 			return deltaReplayer.play( 1 )
 				.then( () => {
@@ -154,11 +154,12 @@ describe( 'DeltaReplayer', () => {
 	} );
 } );
 
-function getDocument() {
-	const doc = new Document();
-	doc.createRoot( 'main' );
+function getModel() {
+	const model = new Model();
 
-	return doc;
+	model.document.createRoot();
+
+	return model;
 }
 
 function getFirstDelta() {