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: commandStack.changed event provides context #528

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 13 additions & 2 deletions lib/command/CommandStack.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export default function CommandStack(eventBus, injector) {
* Current active commandStack execution
*
* @type {Object}
* @property {string|undefined} trigger Indicates whether this was an execution, undo, redo, or clear
*/
this._currentExecution = {
actions: [],
Expand Down Expand Up @@ -141,6 +142,8 @@ CommandStack.prototype.execute = function(command, context) {
throw new Error('command required');
}

var execution = this._currentExecution;
execution.trigger || (execution.trigger = 'execute');
PabloDons marked this conversation as resolved.
Show resolved Hide resolved
var action = { command: command, context: context };

this._pushAction(action);
Expand Down Expand Up @@ -199,9 +202,10 @@ CommandStack.prototype.canExecute = function(command, context) {
CommandStack.prototype.clear = function(emit) {
this._stack.length = 0;
this._stackIdx = -1;
delete this._currentExecution.trigger;
PabloDons marked this conversation as resolved.
Show resolved Hide resolved

if (emit !== false) {
this._fire('changed');
this._fire('changed', { trigger: 'clear' });
}
};

Expand All @@ -211,8 +215,10 @@ CommandStack.prototype.clear = function(emit) {
*/
CommandStack.prototype.undo = function() {
var action = this._getUndoAction(),
execution = this._currentExecution,
next;

execution.trigger = 'undo';
if (action) {
this._pushAction(action);

Expand All @@ -237,11 +243,14 @@ CommandStack.prototype.undo = function() {
*/
CommandStack.prototype.redo = function() {
var action = this._getRedoAction(),
execution = this._currentExecution,
next;

if (action) {
this._pushAction(action);

execution.trigger = 'redo';

while (action) {
this._internalExecute(action, true);
next = this._getRedoAction();
Expand Down Expand Up @@ -446,6 +455,7 @@ CommandStack.prototype._pushAction = function(action) {

CommandStack.prototype._popAction = function() {
var execution = this._currentExecution,
trigger = execution.trigger,
actions = execution.actions,
dirty = execution.dirty;

Expand All @@ -456,7 +466,8 @@ CommandStack.prototype._popAction = function() {

dirty.length = 0;

this._fire('changed');
this._fire('changed', { trigger: trigger });
delete execution.trigger;
}
};

Expand Down
84 changes: 84 additions & 0 deletions test/spec/command/CommandStackSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,4 +876,88 @@ describe('command/CommandStack', function() {

});

describe('change-event', function() {

var testSetup = function(eventBus, commandStack) {
var eventData = {};

commandStack.registerHandler('complex-command', ComplexCommand);
commandStack.registerHandler('pre-command', PreCommand);
commandStack.registerHandler('post-command', PostCommand);
eventBus.on('commandStack.changed', function(event) {
eventData.changeEvent = event.trigger;
});

return eventData;
};

describe('should indicate details about a change', function() {

it('with trigger <execute>', inject(function(eventBus, commandStack) {

// given
var eventData = testSetup(eventBus, commandStack);

// when
commandStack.execute('complex-command', { element: { trace: [] } });

// then
var changeEvent = eventData.changeEvent;

expect(changeEvent).to.equal('execute');

}));

it('with trigger <undo>', inject(function(eventBus, commandStack) {

// given
var eventData = testSetup(eventBus, commandStack);

// when
commandStack.execute('complex-command', { element: { trace: [] } });
commandStack.undo();

// then
var changeEvent = eventData.changeEvent;

expect(changeEvent).to.equal('undo');

}));

it('with trigger <redo>', inject(function(eventBus, commandStack) {

// given
var eventData = testSetup(eventBus, commandStack);

// when
commandStack.execute('complex-command', { element: { trace: [] } });
commandStack.undo();
commandStack.redo();

// then
var changeEvent = eventData.changeEvent;

expect(changeEvent).to.equal('redo');

}));

it('with trigger <clear>', inject(function(eventBus, commandStack) {

// given
var eventData = testSetup(eventBus, commandStack);

// when
commandStack.clear();

// then
var changeEvent = eventData.changeEvent;

expect(changeEvent).to.equal('clear');

}));

});

});

});