Skip to content

Commit

Permalink
chore(keyboard): coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme authored and merge-me[bot] committed Oct 25, 2018
1 parent d529496 commit b967046
Show file tree
Hide file tree
Showing 17 changed files with 336 additions and 247 deletions.
4 changes: 2 additions & 2 deletions lib/features/keyboard/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
isCmd,
isKey,
isShift
} from './helpers';

} from './KeyboardUtil';

var KEYDOWN_EVENT = 'keyboard.keydown';

Expand Down Expand Up @@ -100,6 +99,7 @@ Keyboard.prototype._keyHandler = function(event) {
};

Keyboard.prototype.bind = function(node) {

// make sure that the keyboard is only bound once to the DOM
this.unbind();

Expand Down
31 changes: 15 additions & 16 deletions lib/features/keyboard/KeyboardBindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
isCmd,
isKey,
isShift
} from './helpers';
} from './KeyboardUtil';


/**
Expand All @@ -15,21 +15,6 @@ export default function KeyboardBindings(keyboard, editorActions) {

var config = keyboard._config;

keyboard.addListener(copy);
keyboard.addListener(paste);

keyboard.addListener(undo);
keyboard.addListener(redo);

keyboard.addListener(removeSelection);

keyboard.addListener(zoomDefault);
keyboard.addListener(zoomIn);
keyboard.addListener(zoomOut);

keyboard.addListener(moveCanvas);


// undo
// (CTRL|CMD) + Z
function undo(context) {
Expand Down Expand Up @@ -195,6 +180,20 @@ 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);

keyboard.addListener(moveCanvas);
}

KeyboardBindings.$inject = [
Expand Down
42 changes: 42 additions & 0 deletions lib/features/keyboard/KeyboardUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { isArray } from 'min-dash';

/**
* Returns true if event was triggered with any modifier
* @param {KeyboardEvent} event
*/
export function hasModifier(event) {
return (event.ctrlKey || event.metaKey || event.shiftKey || event.altKey);
}

/**
* @param {KeyboardEvent} event
*/
export function isCmd(event) {

// ensure we don't react to AltGr
// (mapped to CTRL + ALT)
if (event.altKey) {
return false;
}

return event.ctrlKey || event.metaKey;
}

/**
* Checks if key pressed is one of provided keys.
*
* @param {String|String[]} keys
* @param {KeyboardEvent} event
*/
export function isKey(keys, event) {
keys = isArray(keys) ? keys : [ keys ];

return keys.indexOf(event.key) > -1;
}

/**
* @param {KeyboardEvent} event
*/
export function isShift(event) {
return event.shiftKey;
}
7 changes: 0 additions & 7 deletions lib/features/keyboard/helpers/hasModifier.js

This file was deleted.

4 changes: 0 additions & 4 deletions lib/features/keyboard/helpers/index.js

This file was deleted.

12 changes: 0 additions & 12 deletions lib/features/keyboard/helpers/isCmd.js

This file was deleted.

13 changes: 0 additions & 13 deletions lib/features/keyboard/helpers/isKey.js

This file was deleted.

6 changes: 0 additions & 6 deletions lib/features/keyboard/helpers/isShift.js

This file was deleted.

30 changes: 1 addition & 29 deletions test/helper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import TestContainer from 'mocha-test-container-support';

import Diagram from '../../lib/Diagram';
import { event as domEvent } from 'min-dom';

var OPTIONS, DIAGRAM_JS;

Expand Down Expand Up @@ -159,31 +158,4 @@ export function insertCSS(name, css) {

export function getDiagramJS() {
return DIAGRAM_JS;
}

function DomEventTracker() {

this.install = function() {

domEvent.__bind = domEvent.bind;
domEvent.__unbind = domEvent.__unbind || domEvent.unbind;

domEvent.bind = function(el, type, fn, capture) {
el.$$listenerCount = (el.$$listenerCount || 0) + 1;
return domEvent.__bind(el, type, fn, capture);
};

domEvent.unbind = function(el, type, fn, capture) {
el.$$listenerCount = (el.$$listenerCount || 0) -1;
return domEvent.__unbind(el, type, fn, capture);
};
};

this.uninstall = function() {
domEvent.bind = domEvent.__bind;
domEvent.unbind = domEvent.__unbind;
};
}


export var DomMocking = new DomEventTracker();
}
31 changes: 17 additions & 14 deletions test/spec/features/keyboard/CopySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import keyboardModule from 'lib/features/keyboard';

import { createKeyEvent } from 'test/util/KeyEvents';

var spy = sinon.spy;

var KEYS = [ 'c', 'C' ];


describe('features/keyboard - copy', function() {

var defaultDiagramConfig = {
Expand All @@ -30,14 +33,20 @@ 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
}];

beforeEach(bootstrapDiagram(defaultDiagramConfig));

/* eslint-disable no-multi-spaces */
var decisionTable = [
{ desc: 'should call copy', keys: KEYS, ctrlKey: true, called: true },
{ desc: 'should not call copy', keys: KEYS, ctrlKey: false, called: false },
];
/* eslint-enable */

forEach(decisionTable, function(testCase) {

Expand All @@ -46,21 +55,15 @@ describe('features/keyboard - copy', function() {
it(testCase.desc, inject(function(keyboard, editorActions) {

// given
var copySpy = sinon.spy(editorActions, 'trigger');
var copySpy = spy(editorActions, 'trigger');

var event = createKeyEvent(
key,
{
ctrlKey: testCase.ctrlKey
}
);
var event = createKeyEvent(key, { ctrlKey: testCase.ctrlKey });

// when
keyboard._keyHandler(event);

// then
expect(copySpy.calledWith('copy')).to.be.equal(testCase.called);

}));

});
Expand Down
Loading

0 comments on commit b967046

Please sign in to comment.