Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Merge pull request #98 from ckeditor/t/97
Browse files Browse the repository at this point in the history
Other: Use transparent batches in undo & redo commands. Closes #97.
  • Loading branch information
Reinmar committed Jun 27, 2019
2 parents 8ac683f + 9b1426e commit ef8b640
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/redocommand.js
Expand Up @@ -30,7 +30,7 @@ export default class RedoCommand extends BaseCommand {
*/
execute() {
const item = this._stack.pop();
const redoingBatch = this.editor.model.createBatch();
const redoingBatch = this.editor.model.createBatch( 'transparent' );

// All changes have to be done in one `enqueueChange` callback so other listeners will not step between consecutive
// operations, or won't do changes to the document before selection is properly restored.
Expand Down
2 changes: 1 addition & 1 deletion src/undocommand.js
Expand Up @@ -33,7 +33,7 @@ export default class UndoCommand extends BaseCommand {
const batchIndex = batch ? this._stack.findIndex( a => a.batch == batch ) : this._stack.length - 1;

const item = this._stack.splice( batchIndex, 1 )[ 0 ];
const undoingBatch = this.editor.model.createBatch();
const undoingBatch = this.editor.model.createBatch( 'transparent' );

// All changes has to be done in one `enqueueChange` callback so other listeners will not
// step between consecutive operations, or won't do changes to the document before selection is properly restored.
Expand Down
10 changes: 7 additions & 3 deletions src/undoediting.js
Expand Up @@ -78,14 +78,18 @@ export default class UndoEditing extends Plugin {

const batch = operation.batch;

const isRedoBatch = this._redoCommand._createdBatches.has( batch );
const isUndoBatch = this._undoCommand._createdBatches.has( batch );
const isRegisteredBatch = this._batchRegistry.has( batch );

// If changes are not a part of a batch or this is not a new batch, omit those changes.
if ( this._batchRegistry.has( batch ) || batch.type == 'transparent' ) {
if ( isRegisteredBatch || ( batch.type == 'transparent' && !isRedoBatch && !isUndoBatch ) ) {
return;
} else {
if ( this._redoCommand._createdBatches.has( batch ) ) {
if ( isRedoBatch ) {
// If this batch comes from `redoCommand`, add it to `undoCommand` stack.
this._undoCommand.addBatch( batch );
} else if ( !this._undoCommand._createdBatches.has( batch ) ) {
} else if ( !isUndoBatch ) {
// 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 );
Expand Down
31 changes: 31 additions & 0 deletions tests/undoediting.js
Expand Up @@ -82,6 +82,27 @@ describe( 'UndoEditing', () => {
expect( undo._redoCommand.clearStack.called ).to.be.false;
} );

it( 'should add redo batch to undo', () => {
sinon.spy( undo._undoCommand, 'addBatch' );

model.change( writer => {
writer.insertText( 'foobar', root );
} );

model.change( writer => {
writer.insertText( 'baz', root );
} );

editor.execute( 'undo' );
editor.execute( 'undo' );

editor.execute( 'redo' );
sinon.assert.calledThrice( undo._undoCommand.addBatch );

editor.execute( 'redo' );
sinon.assert.callCount( undo._undoCommand.addBatch, 4 );
} );

it( 'should not add a batch that has only non-document operations', () => {
sinon.spy( undo._undoCommand, 'addBatch' );

Expand All @@ -95,6 +116,16 @@ describe( 'UndoEditing', () => {
expect( undo._undoCommand.addBatch.called ).to.be.false;
} );

it( 'should not add a transparent batch', () => {
sinon.spy( undo._undoCommand, 'addBatch' );

model.enqueueChange( 'transparent', writer => {
writer.insertText( 'foobar', root );
} );

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' );

Expand Down

0 comments on commit ef8b640

Please sign in to comment.