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

Commit ef8b640

Browse files
authored
Merge pull request #98 from ckeditor/t/97
Other: Use transparent batches in undo & redo commands. Closes #97.
2 parents 8ac683f + 9b1426e commit ef8b640

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

src/redocommand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class RedoCommand extends BaseCommand {
3030
*/
3131
execute() {
3232
const item = this._stack.pop();
33-
const redoingBatch = this.editor.model.createBatch();
33+
const redoingBatch = this.editor.model.createBatch( 'transparent' );
3434

3535
// All changes have to be done in one `enqueueChange` callback so other listeners will not step between consecutive
3636
// operations, or won't do changes to the document before selection is properly restored.

src/undocommand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export default class UndoCommand extends BaseCommand {
3333
const batchIndex = batch ? this._stack.findIndex( a => a.batch == batch ) : this._stack.length - 1;
3434

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

3838
// All changes has to be done in one `enqueueChange` callback so other listeners will not
3939
// step between consecutive operations, or won't do changes to the document before selection is properly restored.

src/undoediting.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ export default class UndoEditing extends Plugin {
7878

7979
const batch = operation.batch;
8080

81+
const isRedoBatch = this._redoCommand._createdBatches.has( batch );
82+
const isUndoBatch = this._undoCommand._createdBatches.has( batch );
83+
const isRegisteredBatch = this._batchRegistry.has( batch );
84+
8185
// If changes are not a part of a batch or this is not a new batch, omit those changes.
82-
if ( this._batchRegistry.has( batch ) || batch.type == 'transparent' ) {
86+
if ( isRegisteredBatch || ( batch.type == 'transparent' && !isRedoBatch && !isUndoBatch ) ) {
8387
return;
8488
} else {
85-
if ( this._redoCommand._createdBatches.has( batch ) ) {
89+
if ( isRedoBatch ) {
8690
// If this batch comes from `redoCommand`, add it to `undoCommand` stack.
8791
this._undoCommand.addBatch( batch );
88-
} else if ( !this._undoCommand._createdBatches.has( batch ) ) {
92+
} else if ( !isUndoBatch ) {
8993
// A default batch - these are new changes in the document, not introduced by undo feature.
9094
// Add them to `undoCommand` stack and clear `redoCommand` stack.
9195
this._undoCommand.addBatch( batch );

tests/undoediting.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ describe( 'UndoEditing', () => {
8282
expect( undo._redoCommand.clearStack.called ).to.be.false;
8383
} );
8484

85+
it( 'should add redo batch to undo', () => {
86+
sinon.spy( undo._undoCommand, 'addBatch' );
87+
88+
model.change( writer => {
89+
writer.insertText( 'foobar', root );
90+
} );
91+
92+
model.change( writer => {
93+
writer.insertText( 'baz', root );
94+
} );
95+
96+
editor.execute( 'undo' );
97+
editor.execute( 'undo' );
98+
99+
editor.execute( 'redo' );
100+
sinon.assert.calledThrice( undo._undoCommand.addBatch );
101+
102+
editor.execute( 'redo' );
103+
sinon.assert.callCount( undo._undoCommand.addBatch, 4 );
104+
} );
105+
85106
it( 'should not add a batch that has only non-document operations', () => {
86107
sinon.spy( undo._undoCommand, 'addBatch' );
87108

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

119+
it( 'should not add a transparent batch', () => {
120+
sinon.spy( undo._undoCommand, 'addBatch' );
121+
122+
model.enqueueChange( 'transparent', writer => {
123+
writer.insertText( 'foobar', root );
124+
} );
125+
126+
expect( undo._undoCommand.addBatch.called ).to.be.false;
127+
} );
128+
98129
it( 'should add a batch that has both document and non-document operations', () => {
99130
sinon.spy( undo._undoCommand, 'addBatch' );
100131

0 commit comments

Comments
 (0)