Explorar o código

Changed: Stopped using 'undo' and 'redo' batch type.

Szymon Cofalik %!s(int64=9) %!d(string=hai) anos
pai
achega
c3229d1665

+ 8 - 0
packages/ckeditor5-undo/src/basecommand.js

@@ -29,6 +29,14 @@ export default class BaseCommand extends Command {
 		 */
 		 */
 		this._items = [];
 		this._items = [];
 
 
+		/**
+		 * Stores all batches that were created by this command.
+		 *
+		 * @protected
+		 * @member {WeakSet.<engine.model.Batch>} undo.BaseCommand#_createdBatches
+		 */
+		this._createdBatches = new WeakSet();
+
 		// Refresh state, so command is inactive just after initialization.
 		// Refresh state, so command is inactive just after initialization.
 		this.refreshState();
 		this.refreshState();
 	}
 	}

+ 1 - 1
packages/ckeditor5-undo/src/redocommand.js

@@ -75,7 +75,7 @@ export default class RedoCommand extends BaseCommand {
 			// Omit deltas from "redo" batches, because reversed delta already bases on them. Transforming by them
 			// Omit deltas from "redo" batches, because reversed delta already bases on them. Transforming by them
 			// again will result in incorrect deltas.
 			// again will result in incorrect deltas.
 			for ( let historyDelta of document.history.getDeltas( nextBaseVersion ) ) {
 			for ( let historyDelta of document.history.getDeltas( nextBaseVersion ) ) {
-				if ( historyDelta.batch.type != 'redo' ) {
+				if ( !this._createdBatches.has( historyDelta.batch ) ) {
 					reversedDelta = transformDelta( reversedDelta, [ historyDelta ], true );
 					reversedDelta = transformDelta( reversedDelta, [ historyDelta ], true );
 				}
 				}
 			}
 			}

+ 13 - 13
packages/ckeditor5-undo/src/undoengine.js

@@ -64,21 +64,21 @@ export default class UndoEngine extends Feature {
 
 
 		this.listenTo( this.editor.document, 'change', ( evt, type, changes, batch ) => {
 		this.listenTo( this.editor.document, 'change', ( evt, type, changes, batch ) => {
 			// If changes are not a part of a batch or this is not a new batch, omit those changes.
 			// If changes are not a part of a batch or this is not a new batch, omit those changes.
-			if ( !batch || this._batchRegistry.has( batch ) || batch.type == 'ignore' ) {
+			if ( this._batchRegistry.has( batch ) || batch.type == 'transparent' ) {
 				return;
 				return;
-			}
-
-			if ( batch.type == 'undo' ) {
-				// If this batch comes from `undoCommand`, add it to `redoCommand` stack.
-				this._redoCommand.addBatch( batch );
-			} else if ( batch.type == 'redo' ) {
-				// If this batch comes from `redoCommand`, add it to `undoCommand` stack.
-				this._undoCommand.addBatch( batch );
 			} else {
 			} else {
-				// Any other batch - these are new changes in the document.
-				// Add them to `undoCommand` stack and clear `redoCommand` stack.
-				this._undoCommand.addBatch( batch );
-				this._redoCommand.clearStack();
+				if ( this._undoCommand._createdBatches.has( batch ) ) {
+					// If this batch comes from `undoCommand`, add it to `redoCommand` stack.
+					this._redoCommand.addBatch( batch );
+				} else if ( this._redoCommand._createdBatches.has( batch ) ) {
+					// If this batch comes from `redoCommand`, add it to `undoCommand` stack.
+					this._undoCommand.addBatch( batch );
+				} else {
+					// A default batch - these are new changes in the document, not introduced by undo feature.
+					// Add them to `undoCommand` stack and clear `redoCommand` stack.
+					this._undoCommand.addBatch( batch );
+					this._redoCommand.clearStack();
+				}
 			}
 			}
 
 
 			// Add the batch to the registry so it will not be processed again.
 			// Add the batch to the registry so it will not be processed again.

+ 1 - 1
packages/ckeditor5-undo/tests/redocommand.js

@@ -39,7 +39,7 @@ describe( 'RedoCommand', () => {
 
 
 			// Simple integration with undo.
 			// Simple integration with undo.
 			doc.on( 'change', ( evt, type, data, batch ) => {
 			doc.on( 'change', ( evt, type, data, batch ) => {
-				if ( batch.type == 'undo' && !batches.has( batch ) ) {
+				if ( undo._createdBatches.has( batch ) && !batches.has( batch ) ) {
 					redo.addBatch( batch );
 					redo.addBatch( batch );
 					batches.add( batch );
 					batches.add( batch );
 				}
 				}

+ 16 - 10
packages/ckeditor5-undo/tests/undoengine.js

@@ -32,7 +32,7 @@ describe( 'UndoEngine', () => {
 		expect( editor.commands.get( 'redo' ) ).to.equal( undo._redoCommand );
 		expect( editor.commands.get( 'redo' ) ).to.equal( undo._redoCommand );
 	} );
 	} );
 
 
-	it( 'should add a batch to undo command and clear redo stack, if it\'s type is different than "undo" and "redo"', () => {
+	it( 'should add a batch to undo command and clear redo stack, if it\'s type is "default"', () => {
 		sinon.spy( undo._undoCommand, 'addBatch' );
 		sinon.spy( undo._undoCommand, 'addBatch' );
 		sinon.spy( undo._redoCommand, 'clearStack' );
 		sinon.spy( undo._redoCommand, 'clearStack' );
 
 
@@ -45,29 +45,35 @@ describe( 'UndoEngine', () => {
 		expect( undo._redoCommand.clearStack.calledOnce ).to.be.true;
 		expect( undo._redoCommand.clearStack.calledOnce ).to.be.true;
 	} );
 	} );
 
 
-	it( 'should add a batch to undo command, if it\'s type is redo and not clear redo stack', () => {
+	it( 'should add each batch only once', () => {
 		sinon.spy( undo._undoCommand, 'addBatch' );
 		sinon.spy( undo._undoCommand, 'addBatch' );
-		sinon.spy( undo._redoCommand, 'clearStack' );
 
 
-		batch.type = 'redo';
+		batch.insert( new Position( root, [ 0 ] ), 'foobar' ).insert( new Position( root, [ 0 ] ), 'foobar' );
 
 
-		expect( undo._undoCommand.addBatch.called ).to.be.false;
-		expect( undo._redoCommand.clearStack.called ).to.be.false;
+		expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
+	} );
+
+	it( 'should add a batch to undo command, if it\'s type is undo and it comes from redo command', () => {
+		sinon.spy( undo._undoCommand, 'addBatch' );
+		sinon.spy( undo._redoCommand, 'clearStack' );
+
+		undo._redoCommand._createdBatches.add( batch );
 
 
 		batch.insert( new Position( root, [ 0 ] ), 'foobar' );
 		batch.insert( new Position( root, [ 0 ] ), 'foobar' );
 
 
 		expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
 		expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
-		expect( undo._redoCommand.clearStack.calledOnce ).to.be.false;
+		expect( undo._redoCommand.clearStack.called ).to.be.false;
 	} );
 	} );
 
 
 	it( 'should add a batch to redo command, if it\'s type is undo', () => {
 	it( 'should add a batch to redo command, if it\'s type is undo', () => {
-		batch.type = 'undo';
-
 		sinon.spy( undo._redoCommand, 'addBatch' );
 		sinon.spy( undo._redoCommand, 'addBatch' );
+		sinon.spy( undo._redoCommand, 'clearStack' );
+
+		undo._undoCommand._createdBatches.add( batch );
 
 
 		batch.insert( new Position( root, [ 0 ] ), 'foobar' );
 		batch.insert( new Position( root, [ 0 ] ), 'foobar' );
 
 
 		expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
 		expect( undo._redoCommand.addBatch.calledOnce ).to.be.true;
-		expect( undo._redoCommand.addBatch.calledWith( batch ) ).to.be.true;
+		expect( undo._redoCommand.clearStack.called ).to.be.false;
 	} );
 	} );
 } );
 } );