diff --git a/lib/features/keyboard/KeyboardBindings.js b/lib/features/keyboard/KeyboardBindings.js index 18db9b3dc..ade77140e 100644 --- a/lib/features/keyboard/KeyboardBindings.js +++ b/lib/features/keyboard/KeyboardBindings.js @@ -4,18 +4,62 @@ import { isShift } from './KeyboardUtil'; +var LOW_PRIORITY = 500; + + +/** + * Adds default keyboard bindings. + * + * This does not pull in any features will bind only actions that + * have previously been registered against the editorActions component. + * + * @param {EventBus} eventBus + * @param {Keyboard} keyboard + */ +export default function KeyboardBindings(eventBus, keyboard) { + + var self = this; + + eventBus.on('editorActions.init', LOW_PRIORITY, function(event) { + + var editorActions = event.editorActions; + + self.registerBindings(keyboard, editorActions); + }); +} + +KeyboardBindings.$inject = [ + 'eventBus', + 'keyboard' +]; + /** - * Adds default KeyboardEvent listeners + * Register available keyboard bindings. * * @param {Keyboard} keyboard * @param {EditorActions} editorActions */ -export default function KeyboardBindings(keyboard, editorActions) { +KeyboardBindings.prototype.registerBindings = function(keyboard, editorActions) { + + /** + * Add keyboard binding if respective editor action + * is registered. + * + * @param {String} action name + * @param {Function} fn that implements the key binding + */ + function addListener(action, fn) { + + if (editorActions.isRegistered(action)) { + keyboard.addListener(fn); + } + } + // undo // (CTRL|CMD) + Z - function undo(context) { + addListener('undo', function(context) { var event = context.event; @@ -24,12 +68,12 @@ export default function KeyboardBindings(keyboard, editorActions) { return true; } - } + }); // redo // CTRL + Y // CMD + SHIFT + Z - function redo(context) { + addListener('redo', function(context) { var event = context.event; @@ -38,11 +82,11 @@ export default function KeyboardBindings(keyboard, editorActions) { return true; } - } + }); // copy // CTRL/CMD + C - function copy(context) { + addListener('copy', function(context) { var event = context.event; @@ -51,11 +95,11 @@ export default function KeyboardBindings(keyboard, editorActions) { return true; } - } + }); // paste // CTRL/CMD + V - function paste(context) { + addListener('paste', function(context) { var event = context.event; @@ -64,50 +108,37 @@ export default function KeyboardBindings(keyboard, editorActions) { return true; } - } + }); - /** - * zoom in one step - * CTRL + + - * - * `=` is included because of possible combination with SHIFT - */ - function zoomIn(context) { + // zoom in one step + // CTRL/CMD + + + addListener('stepZoom', function(context) { var event = context.event; - if (isKey([ '+', 'Add', '=' ], event) && isCmd(event)) { + if (isKey([ '+', 'Add' ], event) && isCmd(event)) { editorActions.trigger('stepZoom', { value: 1 }); return true; } - } + }); - /** - * zoom out one step - * CTRL + - - * - * `_` is included because of possible combination with SHIFT - */ - function zoomOut(context) { + // zoom out one step + // CTRL + - + addListener('stepZoom', function(context) { var event = context.event; - if (isKey([ '-', '_', 'Subtract' ], event) && isCmd(event)) { + if (isKey([ '-', 'Subtract' ], event) && isCmd(event)) { editorActions.trigger('stepZoom', { value: -1 }); return true; } - } + }); - /** - * zoom to the default level - * CTRL + 0 - * - * 96 = numpad zero - * 48 = regular zero - */ - function zoomDefault(context) { + // zoom to the default level + // CTRL + 0 + addListener('zoom', function(context) { var event = context.event; @@ -116,11 +147,11 @@ export default function KeyboardBindings(keyboard, editorActions) { return true; } - } + }); // delete selected element // DEL - function removeSelection(context) { + addListener('removeSelection', function(context) { var event = context.event; @@ -129,23 +160,5 @@ export default function KeyboardBindings(keyboard, editorActions) { return true; } - } - - - keyboard.addListener(copy); - keyboard.addListener(paste); - - keyboard.addListener(undo); - keyboard.addListener(redo); - - keyboard.addListener(removeSelection); - - keyboard.addListener(zoomDefault); - keyboard.addListener(zoomIn); - keyboard.addListener(zoomOut); -} - -KeyboardBindings.$inject = [ - 'keyboard', - 'editorActions' -]; \ No newline at end of file + }); +}; \ No newline at end of file diff --git a/lib/features/keyboard/index.js b/lib/features/keyboard/index.js index 852b52932..7b9fb0d2f 100644 --- a/lib/features/keyboard/index.js +++ b/lib/features/keyboard/index.js @@ -1,10 +1,7 @@ -import EditorActionsModule from '../editor-actions'; - import Keyboard from './Keyboard'; import KeyboardBindings from './KeyboardBindings'; export default { - __depends__: [ EditorActionsModule ], __init__: [ 'keyboard', 'keyboardBindings' ], keyboard: [ 'type', Keyboard ], keyboardBindings: [ 'type', KeyboardBindings ] diff --git a/test/spec/features/keyboard/CopySpec.js b/test/spec/features/keyboard/CopySpec.js index 4396f3838..dbd6bedee 100644 --- a/test/spec/features/keyboard/CopySpec.js +++ b/test/spec/features/keyboard/CopySpec.js @@ -10,9 +10,9 @@ import { } from 'min-dash'; import copyPasteModule from 'lib/features/copy-paste'; -import editorActionsModule from 'lib/features/editor-actions'; -import keyboardModule from 'lib/features/keyboard'; import modelingModule from 'lib/features/modeling'; +import keyboardModule from 'lib/features/keyboard'; +import editorActionsModule from 'lib/features/editor-actions'; import { createKeyEvent } from 'test/util/KeyEvents'; @@ -28,6 +28,7 @@ describe('features/keyboard - copy', function() { copyPasteModule, modelingModule, keyboardModule, + modelingModule, editorActionsModule ], canvas: { @@ -35,17 +36,19 @@ describe('features/keyboard - copy', function() { } }; - var decisionTable = [{ - desc: 'should call copy', - keys: KEYS, - ctrlKey: true, - called: true - }, { - desc: 'should not call copy', - keys: KEYS, - ctrlKey: false, - called: false - }]; + var decisionTable = [ + { + desc: 'should call copy', + keys: KEYS, + ctrlKey: true, + called: true + }, { + desc: 'should not call copy', + keys: KEYS, + ctrlKey: false, + called: false + } + ]; beforeEach(bootstrapDiagram(defaultDiagramConfig)); diff --git a/test/spec/features/keyboard/KeyboardSpec.js b/test/spec/features/keyboard/KeyboardSpec.js index 1cae2c676..cfb0c7fc0 100755 --- a/test/spec/features/keyboard/KeyboardSpec.js +++ b/test/spec/features/keyboard/KeyboardSpec.js @@ -248,6 +248,7 @@ describe('features/keyboard', function() { }); + // helpers ////////// function dispatchKeyboardEvent(target, type) { diff --git a/test/spec/features/keyboard/PasteSpec.js b/test/spec/features/keyboard/PasteSpec.js index e5a5968ad..c82f69eb4 100644 --- a/test/spec/features/keyboard/PasteSpec.js +++ b/test/spec/features/keyboard/PasteSpec.js @@ -10,9 +10,9 @@ import { } from 'min-dash'; import copyPasteModule from 'lib/features/copy-paste'; -import editorActionsModule from 'lib/features/editor-actions'; -import keyboardModule from 'lib/features/keyboard'; import modelingModule from 'lib/features/modeling'; +import keyboardModule from 'lib/features/keyboard'; +import editorActionsModule from 'lib/features/editor-actions'; import { createKeyEvent } from 'test/util/KeyEvents'; @@ -27,8 +27,9 @@ describe('features/keyboard - paste', function() { modules: [ copyPasteModule, modelingModule, + editorActionsModule, keyboardModule, - editorActionsModule + modelingModule ], canvas: { deferUpdate: false diff --git a/test/spec/features/keyboard/ZoomSpec.js b/test/spec/features/keyboard/ZoomSpec.js index 422670e19..ca2694b2a 100644 --- a/test/spec/features/keyboard/ZoomSpec.js +++ b/test/spec/features/keyboard/ZoomSpec.js @@ -7,16 +7,15 @@ import { forEach } from 'min-dash'; -import modelingModule from 'lib/features/modeling'; import editorActionsModule from 'lib/features/editor-actions'; -import keyboardModule from 'lib/features/keyboard'; import zoomScrollModule from 'lib/navigation/zoomscroll'; +import keyboardModule from 'lib/features/keyboard'; import { createKeyEvent } from 'test/util/KeyEvents'; var KEYS = { - ZOOM_IN: [ '+', 'Add', '=' ], - ZOOM_OUT: [ '-', 'Subtract', '_' ], + ZOOM_IN: [ '+', 'Add' ], + ZOOM_OUT: [ '-', 'Subtract' ], ZOOM_DEFAULT: [ '0' ], }; @@ -25,7 +24,6 @@ describe('features/keyboard - zoom', function() { var defaultDiagramConfig = { modules: [ - modelingModule, keyboardModule, editorActionsModule, zoomScrollModule