From b0883d973a8efe7ce0602dc00b72faccd6b5a731 Mon Sep 17 00:00:00 2001 From: Omar Saad Date: Thu, 21 Jan 2021 18:30:02 +0100 Subject: [PATCH] feat: commandStack.changed event provides context --- lib/command/CommandStack.js | 15 ++++- test/spec/command/CommandStackSpec.js | 84 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/lib/command/CommandStack.js b/lib/command/CommandStack.js index 6243be945..ee7729876 100644 --- a/lib/command/CommandStack.js +++ b/lib/command/CommandStack.js @@ -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: [], @@ -141,6 +142,8 @@ CommandStack.prototype.execute = function(command, context) { throw new Error('command required'); } + var execution = this._currentExecution; + execution.trigger || (execution.trigger = 'execute'); var action = { command: command, context: context }; this._pushAction(action); @@ -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; if (emit !== false) { - this._fire('changed'); + this._fire('changed', { trigger: 'clear' }); } }; @@ -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); @@ -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(); @@ -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; @@ -456,7 +466,8 @@ CommandStack.prototype._popAction = function() { dirty.length = 0; - this._fire('changed'); + this._fire('changed', { trigger: trigger }); + delete execution.trigger; } }; diff --git a/test/spec/command/CommandStackSpec.js b/test/spec/command/CommandStackSpec.js index 00654f972..e2546ad1f 100755 --- a/test/spec/command/CommandStackSpec.js +++ b/test/spec/command/CommandStackSpec.js @@ -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 ', 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 ', 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 ', 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 ', inject(function(eventBus, commandStack) { + + // given + var eventData = testSetup(eventBus, commandStack); + + // when + commandStack.clear(); + + // then + var changeEvent = eventData.changeEvent; + + expect(changeEvent).to.equal('clear'); + + })); + + }); + + }); + });