Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(command): attach command to previous undo/redo context #535

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/command/CommandStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,20 @@ CommandStack.$inject = [ 'eventBus', 'injector' ];
*
* @param {string} command the command to execute
* @param {Object} context the environment to execute the command in
* @param {boolean} [append=false] if true, append this execution to the existing undo step
*/
CommandStack.prototype.execute = function(command, context) {
CommandStack.prototype.execute = function(command, context, compact) {
if (!command) {
throw new Error('command required');
}

this._currentExecution.trigger = 'execute';

var action = { command: command, context: context };
var action = {
id: compact && this.canUndo() ? this._getUndoAction().id : null,
command: command,
context: context,
};

this._pushAction(action);
this._internalExecute(action);
Expand Down
43 changes: 43 additions & 0 deletions test/spec/command/CommandStackSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,49 @@ describe('command/CommandStack', function() {
});


describe('#execute compact', function() {

it('should undo in single step', inject(function(commandStack) {

// given
commandStack.registerHandler('simple-command', SimpleCommand);

var context = { element: { trace: [] } };

commandStack.execute('simple-command', context, true);
commandStack.execute('simple-command', context, true);

// when
commandStack.undo();

// then
expect(commandStack._stack.length).to.equal(2);
expect(commandStack._stackIdx).to.equal(-1);
}));


it('should redo in single step', inject(function(commandStack) {

// given
commandStack.registerHandler('simple-command', SimpleCommand);

var context = { element: { trace: [] } };

commandStack.execute('simple-command', context, true);
commandStack.execute('simple-command', context, true);

// when
commandStack.undo();
commandStack.redo();

// then
expect(commandStack._stack.length).to.equal(2);
expect(commandStack._stackIdx).to.equal(1);
}));

});


describe('command context', function() {

it('should pass command context to handler', inject(function(commandStack) {
Expand Down