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

Commit

Permalink
Only take out one key of the sequence instead of resetting it completely
Browse files Browse the repository at this point in the history
Issue #1283
  • Loading branch information
pablosichert committed Nov 20, 2017
1 parent 1cb4254 commit ba9c8b7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
17 changes: 13 additions & 4 deletions src/components/Shortcuts/ShortcutProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ export default class ShortcutProvider extends Component {
}

handleKeyDown = event => {
const { key } = event;
/* .toUpperCase() is essential to normalize all keys when dealing with
* combinations containing the SHIFT key */
const key = event.key.toUpperCase();
const { keySequence, fired } = this;
const { hotkeys } = this.props;

Expand Down Expand Up @@ -73,9 +75,16 @@ export default class ShortcutProvider extends Component {
console.warn(`Handler defined for key sequence "${serializedSequence}" is not a function.`, handler);
};

handleKeyUp = () => {
this.keySequence = [];
this.fired = {};
handleKeyUp = event => {
/* .toUpperCase() is essential to normalize all keys when dealing with
* combinations containing the SHIFT key */
const key = event.key.toUpperCase();

this.keySequence = this.keySequence.filter(
_key => _key !== key
);

delete this.fired[key];
};

handleBlur = () => {
Expand Down
54 changes: 51 additions & 3 deletions src/components/Shortcuts/ShortcutProvider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,42 @@ describe('ShortcutProvider', () => {
const shortcutProvider = new ShortcutProvider;
shortcutProvider.props = { hotkeys: {} };

const key = 'a';
const key = 'A';

expect(shortcutProvider.fired[key]).to.equal(undefined);

shortcutProvider.handleKeyDown({ key });

expect(shortcutProvider.fired[key]).to.equal(true);
expect(shortcutProvider.keySequence).to.deep.equal(['a']);
expect(shortcutProvider.keySequence).to.deep.equal(['A']);

shortcutProvider.handleKeyDown({ key });

expect(shortcutProvider.fired[key]).to.equal(true);
expect(shortcutProvider.keySequence).to.deep.equal(['a']);
expect(shortcutProvider.keySequence).to.deep.equal(['A']);
});

it('should handle differently capitalized keys the same', () => {
const shortcutProvider = new ShortcutProvider;
shortcutProvider.props = { hotkeys: {} };

const keyLowerCase = 'a';
const keyUpperCase = 'A';

expect(shortcutProvider.fired[keyLowerCase]).to.equal(undefined);
expect(shortcutProvider.fired[keyUpperCase]).to.equal(undefined);

shortcutProvider.handleKeyDown({ key: keyUpperCase });

expect(shortcutProvider.fired[keyLowerCase]).to.equal(undefined);
expect(shortcutProvider.fired[keyUpperCase]).to.equal(true);
expect(shortcutProvider.keySequence).to.deep.equal(['A']);

shortcutProvider.handleKeyDown({ key: keyLowerCase });

expect(shortcutProvider.fired[keyLowerCase]).to.equal(undefined);
expect(shortcutProvider.fired[keyUpperCase]).to.equal(true);
expect(shortcutProvider.keySequence).to.deep.equal(['A']);
});

it('should call latest registered handler for that hotkey', () => {
Expand Down Expand Up @@ -320,6 +343,31 @@ describe('ShortcutProvider', () => {
});
});

describe('handleKeyUp', () => {
it('should take key out of sequence', () => {
const shortcutProvider = new ShortcutProvider;

const key1 = 'A';
const key2 = 'B';
const key3 = 'C';

shortcutProvider.keySequence = [ key1, key2, key3 ];
shortcutProvider.fired = {
[key1]: true,
[key2]: true,
[key3]: true
};

shortcutProvider.handleKeyUp({ key: key2 });

expect(shortcutProvider.keySequence).to.deep.equal([ key1, key3 ]);
expect(shortcutProvider.fired).to.deep.equal({
[key1]: true,
[key3]: true
});
});
});

describe('handleBlur', () => {
it('should reset key sequence', () => {
const shortcutProvider = new ShortcutProvider;
Expand Down

0 comments on commit ba9c8b7

Please sign in to comment.