From d15751af4c24d946b9a0ee7ae648ea7f7cda0265 Mon Sep 17 00:00:00 2001 From: Beatriz Mendes Date: Tue, 24 Jan 2023 15:01:25 +0100 Subject: [PATCH] feat(create-append-anything): add create shortcut Trigger create with 'n' and 'a' if no selection Closes #1814 --- .../CreateAppendEditorActions.js | 10 ++++ .../CreateAppendKeyboardBindings.js | 27 ++++++++- test/spec/ModelerSpec.js | 1 + .../CreateAppendEditorActionsSpec.js | 29 ++++++++++ .../CreateAppendKeyboardBindingsSpec.js | 58 ++++++++++++++++++- 5 files changed, 121 insertions(+), 4 deletions(-) diff --git a/lib/features/create-append-anything/CreateAppendEditorActions.js b/lib/features/create-append-anything/CreateAppendEditorActions.js index 2145fb2a05..6cffded855 100644 --- a/lib/features/create-append-anything/CreateAppendEditorActions.js +++ b/lib/features/create-append-anything/CreateAppendEditorActions.js @@ -24,6 +24,7 @@ CreateAppendEditorActions.prototype.registerActions = function() { var editorActions = this._injector.get('editorActions', false); var selection = this._injector.get('selection', false); var contextPad = this._injector.get('contextPad', false); + var palette = this._injector.get('palette', false); const actions = {}; @@ -36,6 +37,15 @@ CreateAppendEditorActions.prototype.registerActions = function() { ); } + // create + if (palette) { + assign(actions, { + 'createElement': function(event) { + palette.triggerEntry('create', 'click', event); + } } + ); + } + editorActions && editorActions.register(actions); }; diff --git a/lib/features/create-append-anything/CreateAppendKeyboardBindings.js b/lib/features/create-append-anything/CreateAppendKeyboardBindings.js index 6c1282fb95..a09c7d3734 100644 --- a/lib/features/create-append-anything/CreateAppendKeyboardBindings.js +++ b/lib/features/create-append-anything/CreateAppendKeyboardBindings.js @@ -13,6 +13,7 @@ export default function CreateAppendKeyboardBindings(injector) { this._injector = injector; this._keyboard = this._injector.get('keyboard', false); this._editorActions = this._injector.get('editorActions', false); + this._selection = this._injector.get('selection', false); if (this._keyboard) { this._injector.invoke(KeyboardBindings, this); @@ -36,6 +37,7 @@ CreateAppendKeyboardBindings.prototype.registerBindings = function() { var keyboard = this._keyboard; var editorActions = this._editorActions; + var selection = this._selection; // inherit default bindings KeyboardBindings.prototype.registerBindings.call(this, keyboard, editorActions); @@ -54,7 +56,7 @@ CreateAppendKeyboardBindings.prototype.registerBindings = function() { } } - // activate append element + // activate append/create element // A addListener('appendElement', function(context) { @@ -65,7 +67,28 @@ CreateAppendKeyboardBindings.prototype.registerBindings = function() { } if (keyboard && keyboard.isKey([ 'a', 'A' ], event)) { - editorActions && editorActions.trigger('appendElement', event); + + if (selection && selection.get().length == 1) { + editorActions && editorActions.trigger('appendElement', event); + } else { + editorActions && editorActions.trigger('createElement', event); + } + + return true; + } + }); + + // N + addListener('createElement', function(context) { + + var event = context.keyEvent; + + if (keyboard && keyboard.hasModifier(event)) { + return; + } + + if (keyboard && keyboard.isKey([ 'n', 'N' ], event)) { + editorActions && editorActions.trigger('createElement', event); return true; } diff --git a/test/spec/ModelerSpec.js b/test/spec/ModelerSpec.js index b5f327a035..10dfb5e93b 100644 --- a/test/spec/ModelerSpec.js +++ b/test/spec/ModelerSpec.js @@ -265,6 +265,7 @@ describe('Modeler', function() { // given var expectedActions = [ 'appendElement', + 'createElement', 'undo', 'redo', 'copy', diff --git a/test/spec/features/create-append-anything/CreateAppendEditorActionsSpec.js b/test/spec/features/create-append-anything/CreateAppendEditorActionsSpec.js index c6026fea02..051ddb06de 100644 --- a/test/spec/features/create-append-anything/CreateAppendEditorActionsSpec.js +++ b/test/spec/features/create-append-anything/CreateAppendEditorActionsSpec.js @@ -82,4 +82,33 @@ describe('features/create-append-anything - editor actions', function() { }); + + describe('#createElement', function() { + + beforeEach(bootstrapModeler(basicXML, { + modules: [ + selectionModule, + modelingModule, + coreModule, + editorActionsModule, + createAppendAnything + ] + })); + + + it('should open create element', inject(function(editorActions, eventBus) { + + // given + var changedSpy = sinon.spy(); + eventBus.once('popupMenu.open', changedSpy); + + // when + editorActions.trigger('createElement', {}); + + // then + expect(changedSpy).to.have.been.called; + })); + + }); + }); diff --git a/test/spec/features/create-append-anything/CreateAppendKeyboardBindingsSpec.js b/test/spec/features/create-append-anything/CreateAppendKeyboardBindingsSpec.js index 94b77ab449..c69131807b 100644 --- a/test/spec/features/create-append-anything/CreateAppendKeyboardBindingsSpec.js +++ b/test/spec/features/create-append-anything/CreateAppendKeyboardBindingsSpec.js @@ -1,6 +1,7 @@ import { bootstrapViewer, - inject + inject, + getBpmnJS } from 'test/TestHelper'; import { forEach } from 'min-dash'; @@ -15,6 +16,10 @@ import { createKeyEvent } from 'test/util/KeyEvents'; +import { + query as domQuery +} from 'min-dom'; + describe('features/create-append-anything - keyboard bindings', function() { @@ -37,7 +42,8 @@ describe('features/create-append-anything - keyboard bindings', function() { // given var expectedActions = [ - 'appendElement' + 'appendElement', + 'createElement' ]; var actualActions = editorActions.getActions(); @@ -67,6 +73,45 @@ describe('features/create-append-anything - keyboard bindings', function() { // then expect(popupMenu.open).to.have.been.calledOnce; + expect(isMenu('bpmn-append')).to.be.true; + })); + + + it('should trigger create menu', + inject(function(keyboard, popupMenu) { + + sinon.spy(popupMenu, 'open'); + + // given + var e = createKeyEvent(key); + + // when + keyboard._keyHandler(e); + + // then + expect(popupMenu.open).to.have.been.calledOnce; + expect(isMenu('bpmn-create')).to.be.true; + })); + + }); + + + forEach([ 'n', 'N' ], function(key) { + + it('should trigger create menu', + inject(function(keyboard, popupMenu) { + + sinon.spy(popupMenu, 'open'); + + // given + var e = createKeyEvent(key); + + // when + keyboard._keyHandler(e); + + // then + expect(popupMenu.open).to.have.been.calledOnce; + expect(isMenu('bpmn-create')).to.be.true; })); }); @@ -74,3 +119,12 @@ describe('features/create-append-anything - keyboard bindings', function() { }); }); + + +// helpers ////////////////////// +function isMenu(menuId) { + const popup = getBpmnJS().get('popupMenu'); + const popupElement = popup._current && domQuery('.djs-popup', popup._current.container); + + return popupElement.classList.contains(menuId); +} \ No newline at end of file