Skip to content

Commit

Permalink
feat(keyboard): only bind provided shortcuts
Browse files Browse the repository at this point in the history
Building upon the EditorAction refactoring this ensures that
keyboard shortcuts are only bound if the required editor
action exists.

This makes it possible to use the keyboard independent from
editor action or action implementing services.
  • Loading branch information
nikku authored and merge-me[bot] committed Oct 29, 2018
1 parent a9089ad commit aa308fd
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 82 deletions.
129 changes: 71 additions & 58 deletions lib/features/keyboard/KeyboardBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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'
];
});
};
3 changes: 0 additions & 3 deletions lib/features/keyboard/index.js
Original file line number Diff line number Diff line change
@@ -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 ]
Expand Down
29 changes: 16 additions & 13 deletions test/spec/features/keyboard/CopySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -28,24 +28,27 @@ describe('features/keyboard - copy', function() {
copyPasteModule,
modelingModule,
keyboardModule,
modelingModule,
editorActionsModule
],
canvas: {
deferUpdate: false
}
};

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));

Expand Down
1 change: 1 addition & 0 deletions test/spec/features/keyboard/KeyboardSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ describe('features/keyboard', function() {

});


// helpers //////////

function dispatchKeyboardEvent(target, type) {
Expand Down
7 changes: 4 additions & 3 deletions test/spec/features/keyboard/PasteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -27,8 +27,9 @@ describe('features/keyboard - paste', function() {
modules: [
copyPasteModule,
modelingModule,
editorActionsModule,
keyboardModule,
editorActionsModule
modelingModule
],
canvas: {
deferUpdate: false
Expand Down
8 changes: 3 additions & 5 deletions test/spec/features/keyboard/ZoomSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ],
};

Expand All @@ -25,7 +24,6 @@ describe('features/keyboard - zoom', function() {

var defaultDiagramConfig = {
modules: [
modelingModule,
keyboardModule,
editorActionsModule,
zoomScrollModule
Expand Down

0 comments on commit aa308fd

Please sign in to comment.