Skip to content
This repository has been archived by the owner on Dec 13, 2020. It is now read-only.

Commit

Permalink
Provide keymap in <ShortcutProvider> props so it can print readable s…
Browse files Browse the repository at this point in the history
…hortcut names

Issue #1283
  • Loading branch information
pablosichert committed Nov 16, 2017
1 parent 1fcacd6 commit 4a4b727
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
21 changes: 12 additions & 9 deletions src/components/Shortcuts/ShortcutProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import PropTypes from 'prop-types';
export default class ShortcutProvider extends Component {
static propTypes = {
children: PropTypes.node,
hotkeys: PropTypes.object.isRequired
hotkeys: PropTypes.object.isRequired,
keymap: PropTypes.object.isRequired
};

static childContextTypes = {
Expand Down Expand Up @@ -72,32 +73,34 @@ export default class ShortcutProvider extends Component {
};

subscribe = (name, handler) => {
const { hotkeys } = this.props;
const { hotkeys, keymap } = this.props;

if (!(name in hotkeys)) {
if (!(name in this.props.keymap)) {
console.warn(`There are no hotkeys defined for "${name}".`);

return;
}

const bucket = hotkeys[name];
const key = keymap[name].toUpperCase();
const bucket = hotkeys[key];

hotkeys[name] = [...bucket, handler];
hotkeys[key] = [...bucket, handler];
};

unsubscribe = (name, handler) => {
const { hotkeys } = this.props;
const { hotkeys, keymap } = this.props;

if (!(name in hotkeys)) {
if (!(name in this.props.keymap)) {
console.warn(`There are no hotkeys defined for "${name}".`);

return;
}

const bucket = hotkeys[name];
const key = keymap[name].toUpperCase();
const bucket = hotkeys[key];
let found = false;

hotkeys[name] = bucket.filter(_handler => {
hotkeys[key] = bucket.filter(_handler => {
if (_handler === handler) {
found = true;

Expand Down
40 changes: 29 additions & 11 deletions src/components/Shortcuts/ShortcutProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,22 @@ describe('ShortcutProvider', () => {
it('should be able to subscribe to hotkeys', () => {
const shortcutProvider = new ShortcutProvider;

const shortcut = 'ctrl+1';
const name = 'FOO';
const shortcut = 'CONTROL+1';

const hotkeys = {
[shortcut]: []
};

shortcutProvider.props = { hotkeys };
const keymap = {
[name]: shortcut
};

shortcutProvider.props = { hotkeys, keymap };

const handler = () => {};

shortcutProvider.subscribe(shortcut, handler);
shortcutProvider.subscribe(name, handler);

expect(shortcutProvider.props.hotkeys[shortcut]).to.include(handler);
});
Expand All @@ -106,8 +111,9 @@ describe('ShortcutProvider', () => {
const shortcutProvider = new ShortcutProvider;

const hotkeys = {};
const keymap = {}

shortcutProvider.props = { hotkeys };
shortcutProvider.props = { hotkeys, keymap };

const handler = () => {};

Expand All @@ -126,24 +132,29 @@ describe('ShortcutProvider', () => {
it('should be able to unsubscribe from hotkeys', () => {
const shortcutProvider = new ShortcutProvider;

const shortcut = 'ctrl+1';
const name = 'FOO';
const shortcut = 'CONTROL+1';
const handler = () => {};

shortcutProvider.props = {
hotkeys: {
[shortcut]: [ handler ]
},
keymap: {
[name]: shortcut
}
};

shortcutProvider.unsubscribe(shortcut, handler);
shortcutProvider.unsubscribe(name, handler);

expect(shortcutProvider.props.hotkeys[shortcut]).to.not.include(handler);
});

it('should not modify other handlers when unsubscribing', () => {
const shortcutProvider = new ShortcutProvider;

const shortcut = 'ctrl+1';
const name = 'FOO';
const shortcut = 'CONTROL+1';

const handler1 = () => {};
const handler2 = () => {};
Expand All @@ -152,10 +163,13 @@ describe('ShortcutProvider', () => {
shortcutProvider.props = {
hotkeys: {
[shortcut]: [ handler1, handler2, handler3 ]
},
keymap: {
[name]: shortcut
}
};

shortcutProvider.unsubscribe(shortcut, handler2);
shortcutProvider.unsubscribe(name, handler2);

expect(shortcutProvider.props.hotkeys[shortcut]).to.deep.equal([
handler1, handler3
Expand All @@ -168,7 +182,7 @@ describe('ShortcutProvider', () => {
try {
const shortcutProvider = new ShortcutProvider;

shortcutProvider.props = { hotkeys: {} };
shortcutProvider.props = { hotkeys: {}, keymap: {} };

const handler = () => {};

Expand All @@ -188,17 +202,21 @@ describe('ShortcutProvider', () => {
try {
const shortcutProvider = new ShortcutProvider;

const shortcut = 'ctrl+1';
const name = 'FOO';
const shortcut = 'CONTROL+1';

shortcutProvider.props = {
hotkeys: {
[shortcut]: []
},
keymap: {
[name]: shortcut
}
};

const handler = () => {};

shortcutProvider.unsubscribe(shortcut, handler);
shortcutProvider.unsubscribe(name, handler);

expect(warn).to.have.been.called;
} catch (error) {
Expand Down

0 comments on commit 4a4b727

Please sign in to comment.