Browse Source

Fix: Do not register batches which have only non-document operations.

Szymon Cofalik 7 years ago
parent
commit
1377b1a4fe

+ 7 - 0
packages/ckeditor5-undo/src/undoengine.js

@@ -65,6 +65,13 @@ export default class UndoEngine extends Plugin {
 
 		this.listenTo( this.editor.model, 'applyOperation', ( evt, args ) => {
 			const operation = args[ 0 ];
+
+			// Do not register batch if the operation is not a document operation.
+			// This prevents from creating empty undo steps, where all operations where non-document operations.
+			if ( !operation.isDocumentOperation ) {
+				return;
+			}
+
 			const batch = operation.delta.batch;
 
 			// If changes are not a part of a batch or this is not a new batch, omit those changes.

+ 25 - 0
packages/ckeditor5-undo/tests/undoengine.js

@@ -56,6 +56,31 @@ describe( 'UndoEngine', () => {
 			expect( undo._undoCommand.addBatch.calledOnce ).to.be.true;
 		} );
 
+		it( 'should not add a batch that has only non-document operations', () => {
+			sinon.spy( undo._undoCommand, 'addBatch' );
+
+			model.change( writer => {
+				const docFrag = writer.createDocumentFragment();
+				const element = writer.createElement( 'paragraph' );
+				writer.insert( element, docFrag, 0 );
+				writer.insertText( 'foo', null, element, 0 );
+			} );
+
+			expect( undo._undoCommand.addBatch.called ).to.be.false;
+		} );
+
+		it( 'should add a batch that has both document and non-document operations', () => {
+			sinon.spy( undo._undoCommand, 'addBatch' );
+
+			model.change( writer => {
+				const element = writer.createElement( 'paragraph' );
+				writer.insertText( 'foo', null, element, 0 );
+				writer.insert( element, root, 0 );
+			} );
+
+			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' );