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

Undo Feature. #1

Merged
merged 13 commits into from
May 13, 2016
4 changes: 2 additions & 2 deletions src/undo.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ export default class Undo extends Feature {
} );

// Whenever batch is reverted by undo command, add it to redo history.
this._undoCommand.listenTo( this._redoCommand, 'revert', ( evt, batch ) => {
this.listenTo( this._redoCommand, 'revert', ( evt, batch ) => {
this._undoCommand.addBatch( batch );
} );

// Whenever batch is reverted by redo command, add it to undo history.
this._redoCommand.listenTo( this._undoCommand, 'revert', ( evt, batch ) => {
this.listenTo( this._undoCommand, 'revert', ( evt, batch ) => {
this._redoCommand.addBatch( batch );
} );
}
Expand Down
31 changes: 25 additions & 6 deletions src/undocommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,20 @@ export default class UndoCommand extends Command {
};

this._batchStack.push( { batch, selection } );
this.refreshState();
}

/**
* Removes all batches from the stack.
*/
clearStack() {
this._batchStack = [];
this.refreshState();
}

/**
* @inheritDoc
*/
_checkEnabled() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misses docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, it's inherited from Command. Hmm... so maybe, to avoid confusion, we could use @inheritDoc in such cases. Then it will be clear that the doc comes from the parent.

return this._batchStack.length > 0;
}
Expand All @@ -54,13 +59,26 @@ export default class UndoCommand extends Command {
* Executes the command: reverts a {@link engine.treeModel.Batch batch} added to the command's stack,
* applies it on the document and removes the batch from the stack.
*
* @private
* @protected
* @fires undo.undoCommand#event:revert
* @param {Number} [batchIndex] If set, batch under the given index on the stack will be reverted and removed.
* If not set, or invalid, the last added batch will be reverted and removed.
* @param {engine.treeModel.Batch} [batch] If set, batch that should be undone. If that batch is not on undo stack,
* the command execution won't do anything. If not set, the last added batch will be undone.
*/
_doExecute( batchIndex ) {
batchIndex = this._batchStack[ batchIndex ] ? batchIndex : this._batchStack.length - 1;
_doExecute( batch = null ) {
let batchIndex;

// If batch is not given, set `batchIndex` to the last index in command stack.
// If it is given, find it on the stack.
if ( !batch ) {
batchIndex = this._batchStack.length - 1;
} else {
for ( let i = 0; i < this._batchStack.length; i++ ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ( this._batchStack[ i ].batch == batch ) {
batchIndex = i;
break;
}
}
}

const undoItem = this._batchStack.splice( batchIndex, 1 )[ 0 ];

Expand Down Expand Up @@ -90,7 +108,7 @@ export default class UndoCommand extends Command {

// The ranges will be transformed by deltas from history that took place
// after the selection got stored.
const deltas = this.editor.document.history.getDeltas( undoBatch.deltas[ 0 ].baseVersion ) || [];
const deltas = this.editor.document.history.getDeltas( undoBatch.deltas[ 0 ].baseVersion );

// This will keep the transformed ranges.
const transformedRanges = [];
Expand Down Expand Up @@ -183,6 +201,7 @@ export default class UndoCommand extends Command {
this.editor.document.selection.setRanges( transformedRanges, selectionState.isBackward );
}

this.refreshState();
this.fire( 'revert', undoBatch );
}
}
Expand Down
Loading