react-keybindings
Advanced tools
Comparing version 1.0.9 to 1.0.10
@@ -53,29 +53,32 @@ "use strict"; | ||
_this.state = { pressedKeys: [] }; | ||
_this.state = { keyActions: [] }; | ||
_this.handleKeyDown = function (e) { | ||
var _pressedKeys = _this.state.pressedKeys; | ||
var key = e.keyCode; | ||
if (!_pressedKeys.includes(key)) { | ||
var pressedKeys = [].concat(_toConsumableArray(_pressedKeys), [key]); | ||
if (!_this.pressedKeys.includes(key)) { | ||
_this.pressedKeys = [].concat(_toConsumableArray(_this.pressedKeys), [key]); | ||
// handle special keys | ||
if (e.ctrlKey && pressedKeys.indexOf(specialKeys.CTRL) < 0) { | ||
pressedKeys.push(specialKeys.CTRL); | ||
if (e.ctrlKey && _this.pressedKeys.indexOf(specialKeys.CTRL) < 0) { | ||
_this.pressedKeys.push(specialKeys.CTRL); | ||
} | ||
if (e.metaKey && pressedKeys.indexOf(specialKeys.META) < 0) { | ||
pressedKeys.push(specialKeys.META); | ||
if (e.metaKey && _this.pressedKeys.indexOf(specialKeys.META) < 0) { | ||
_this.pressedKeys.push(specialKeys.META); | ||
} | ||
if (e.shiftKey && pressedKeys.indexOf(specialKeys.SHIFT) < 0) { | ||
pressedKeys.push(specialKeys.SHIFT); | ||
if (e.shiftKey && _this.pressedKeys.indexOf(specialKeys.SHIFT) < 0) { | ||
_this.pressedKeys.push(specialKeys.SHIFT); | ||
} | ||
if (e.altKey && pressedKeys.indexOf(specialKeys.ALT) < 0) { | ||
pressedKeys.push(specialKeys.ALT); | ||
if (e.altKey && _this.pressedKeys.indexOf(specialKeys.ALT) < 0) { | ||
_this.pressedKeys.push(specialKeys.ALT); | ||
} | ||
_this.setState({ pressedKeys: pressedKeys }); | ||
var actions = (0, _keyActions2.default)(keymap, _this.pressedKeys); | ||
if (_this.diffActions(_this.state.keyActions, actions)) { | ||
_this.setState({ keyActions: actions }); | ||
} | ||
} | ||
@@ -85,30 +88,36 @@ }; | ||
_this.handleKeyUp = function (e) { | ||
var keyCode = _this.state.pressedKeys.indexOf(e.keyCode); | ||
var pressedKeys = [].concat(_toConsumableArray(_this.state.pressedKeys.slice(0, keyCode)), _toConsumableArray(_this.state.pressedKeys.slice(keyCode + 1))); | ||
var keyCode = _this.pressedKeys.indexOf(e.keyCode); | ||
var ctrl = pressedKeys.indexOf(specialKeys.CTRL); | ||
var meta = pressedKeys.indexOf(specialKeys.META); | ||
var shift = pressedKeys.indexOf(specialKeys.SHIFT); | ||
var alt = pressedKeys.indexOf(specialKeys.ALT); | ||
_this.pressedKeys = [].concat(_toConsumableArray(_this.pressedKeys.slice(0, keyCode)), _toConsumableArray(_this.pressedKeys.slice(keyCode + 1))); | ||
var ctrl = _this.pressedKeys.indexOf(specialKeys.CTRL); | ||
var meta = _this.pressedKeys.indexOf(specialKeys.META); | ||
var shift = _this.pressedKeys.indexOf(specialKeys.SHIFT); | ||
var alt = _this.pressedKeys.indexOf(specialKeys.ALT); | ||
// remove special keys | ||
if (!e.ctrlKey && ctrl > -1) { | ||
pressedKeys.splice(ctrl, 1); | ||
_this.pressedKeys.splice(ctrl, 1); | ||
} | ||
if (!e.metaKey && meta > -1) { | ||
pressedKeys.splice(meta, 1); | ||
_this.pressedKeys.splice(meta, 1); | ||
} | ||
if (!e.shiftKey && shift > -1) { | ||
pressedKeys.splice(shift, 1); | ||
_this.pressedKeys.splice(shift, 1); | ||
} | ||
if (!e.altKey && alt > -1) { | ||
pressedKeys.splice(alt, 1); | ||
_this.pressedKeys.splice(alt, 1); | ||
} | ||
_this.setState({ pressedKeys: pressedKeys }); | ||
var actions = (0, _keyActions2.default)(keymap, _this.pressedKeys); | ||
if (_this.diffActions(_this.state.keyActions, actions)) { | ||
_this.setState({ keyActions: actions }); | ||
} | ||
}; | ||
_this.pressedKeys = []; | ||
return _this; | ||
@@ -133,2 +142,9 @@ } | ||
} | ||
}, { | ||
key: "diffActions", | ||
value: function diffActions(actions, nextActions) { | ||
return actions.length !== nextActions.length || nextActions.some(function (nextAction) { | ||
return !actions.includes(nextAction); | ||
}); | ||
} | ||
@@ -152,4 +168,4 @@ /** | ||
}, | ||
keyActions: (0, _keyActions2.default)(keymap, this.state.pressedKeys) | ||
}, this.state, this.props)); | ||
keyActions: this.state.keyActions | ||
}, this.props)); | ||
} | ||
@@ -156,0 +172,0 @@ }]); |
{ | ||
"name": "react-keybindings", | ||
"version": "1.0.9", | ||
"version": "1.0.10", | ||
"description": "Add keybindings in React apps", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -13,5 +13,6 @@ import React, { Component } from "react" | ||
super(props) | ||
this.pressedKeys = [] | ||
} | ||
state = { pressedKeys: [] }; | ||
state = { keyActions: [] }; | ||
@@ -32,2 +33,7 @@ componentDidMount() { | ||
diffActions(actions, nextActions) { | ||
return actions.length !== nextActions.length || | ||
nextActions.some((nextAction) => !actions.includes(nextAction)) | ||
} | ||
/** | ||
@@ -37,26 +43,29 @@ * Add the current pressed key to the component state | ||
handleKeyDown = (e) => { | ||
const _pressedKeys = this.state.pressedKeys | ||
const key = e.keyCode | ||
if (!_pressedKeys.includes(key)) { | ||
const pressedKeys = [..._pressedKeys, key] | ||
if (!this.pressedKeys.includes(key)) { | ||
this.pressedKeys = [...this.pressedKeys, key] | ||
// handle special keys | ||
if (e.ctrlKey && pressedKeys.indexOf(specialKeys.CTRL) < 0) { | ||
pressedKeys.push(specialKeys.CTRL) | ||
if (e.ctrlKey && this.pressedKeys.indexOf(specialKeys.CTRL) < 0) { | ||
this.pressedKeys.push(specialKeys.CTRL) | ||
} | ||
if (e.metaKey && pressedKeys.indexOf(specialKeys.META) < 0) { | ||
pressedKeys.push(specialKeys.META) | ||
if (e.metaKey && this.pressedKeys.indexOf(specialKeys.META) < 0) { | ||
this.pressedKeys.push(specialKeys.META) | ||
} | ||
if (e.shiftKey && pressedKeys.indexOf(specialKeys.SHIFT) < 0) { | ||
pressedKeys.push(specialKeys.SHIFT) | ||
if (e.shiftKey && this.pressedKeys.indexOf(specialKeys.SHIFT) < 0) { | ||
this.pressedKeys.push(specialKeys.SHIFT) | ||
} | ||
if (e.altKey && pressedKeys.indexOf(specialKeys.ALT) < 0) { | ||
pressedKeys.push(specialKeys.ALT) | ||
if (e.altKey && this.pressedKeys.indexOf(specialKeys.ALT) < 0) { | ||
this.pressedKeys.push(specialKeys.ALT) | ||
} | ||
this.setState({ pressedKeys }) | ||
const actions = keyActions(keymap, this.pressedKeys) | ||
if (this.diffActions(this.state.keyActions, actions)) { | ||
this.setState({ keyActions: actions }) | ||
} | ||
} | ||
@@ -69,31 +78,36 @@ }; | ||
handleKeyUp = (e) => { | ||
const keyCode = this.state.pressedKeys.indexOf(e.keyCode) | ||
const pressedKeys = [ | ||
...this.state.pressedKeys.slice(0, keyCode), | ||
...this.state.pressedKeys.slice(keyCode + 1), | ||
const keyCode = this.pressedKeys.indexOf(e.keyCode) | ||
this.pressedKeys = [ | ||
...this.pressedKeys.slice(0, keyCode), | ||
...this.pressedKeys.slice(keyCode + 1), | ||
] | ||
const ctrl = pressedKeys.indexOf(specialKeys.CTRL) | ||
const meta = pressedKeys.indexOf(specialKeys.META) | ||
const shift = pressedKeys.indexOf(specialKeys.SHIFT) | ||
const alt = pressedKeys.indexOf(specialKeys.ALT) | ||
const ctrl = this.pressedKeys.indexOf(specialKeys.CTRL) | ||
const meta = this.pressedKeys.indexOf(specialKeys.META) | ||
const shift = this.pressedKeys.indexOf(specialKeys.SHIFT) | ||
const alt = this.pressedKeys.indexOf(specialKeys.ALT) | ||
// remove special keys | ||
if (!e.ctrlKey && ctrl > -1) { | ||
pressedKeys.splice(ctrl, 1) | ||
this.pressedKeys.splice(ctrl, 1) | ||
} | ||
if (!e.metaKey && meta > -1) { | ||
pressedKeys.splice(meta, 1) | ||
this.pressedKeys.splice(meta, 1) | ||
} | ||
if (!e.shiftKey && shift > -1) { | ||
pressedKeys.splice(shift, 1) | ||
this.pressedKeys.splice(shift, 1) | ||
} | ||
if (!e.altKey && alt > -1) { | ||
pressedKeys.splice(alt, 1) | ||
this.pressedKeys.splice(alt, 1) | ||
} | ||
this.setState({ pressedKeys }) | ||
const actions = keyActions(keymap, this.pressedKeys) | ||
if (this.diffActions(this.state.keyActions, actions)) { | ||
this.setState({ keyActions: actions }) | ||
} | ||
}; | ||
@@ -105,4 +119,3 @@ | ||
ref={ (wrapped) => this.wrapped = wrapped } | ||
keyActions={ keyActions(keymap, this.state.pressedKeys) } | ||
{ ...this.state } | ||
keyActions={ this.state.keyActions } | ||
{ ...this.props } /> | ||
@@ -109,0 +122,0 @@ ) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20503
540