fix: setting hotkeys was broken (#2496)

This commit is contained in:
Erik Ziegler authored and GitHub committed 2021-07-29 21:57:58 +02:00
1 parent 4f41ba742d
commit eff70ae22c
12 files changed
+281 -53

No files matched your search

+2 -25
View File
@@ -2,30 +2,7 @@ import Mousetrap from 'mousetrap';
import pausePlugin from './pausePlugin';
import recordPlugin from './recordPlugin';
Mousetrap.initialize = () => {
if (!Mousetrap._initialized) {
recordPlugin(Mousetrap);
pausePlugin(Mousetrap);
Mousetrap._initialized = true;
}
};
// These are only here so that Jest mocks them properly before .initialize() is called
Mousetrap.handleKey = () =>
console.debug('Mousetrap recordPlugin not yet initialized');
Mousetrap.startRecording = () =>
console.debug('Mousetrap recordPlugin not yet initialized');
Mousetrap.stopRecording = () =>
console.debug('Mousetrap recordPlugin not yet initialized');
Mousetrap.record = () =>
console.debug('Mousetrap recordPlugin not yet initialized');
Mousetrap.stopCallback = () =>
console.debug('Mousetrap pausePlugin not yet initialized');
Mousetrap.pause = () =>
console.debug('Mousetrap pausePlugin not yet initialized');
Mousetrap.unpause = () =>
console.debug('Mousetrap pausePlugin not yet initialized');
recordPlugin(Mousetrap);
pausePlugin(Mousetrap);
export default Mousetrap;
@@ -5,7 +5,7 @@
*
* https://github.com/ccampbell/mousetrap/blob/master/plugins/pause/mousetrap-pause.js
*/
export default function(Mousetrap) {
export default function pausePlugin(Mousetrap) {
var _originalStopCallback = Mousetrap.prototype.stopCallback;
Mousetrap.prototype.stopCallback = function(e, element, combo) {
@@ -1,10 +1,9 @@
/**
* This extension allows you to record a sequence using Mousetrap.
* {@link https://craig.is/killing/mice}
*
* @author Dan Tao <daniel.tao@gmail.com>
*/
export default function (Mousetrap) {
export default function recordPlugin(Mousetrap, options = { timeout: 100 }) {
/**
* the sequence currently being recorded
*
@@ -109,7 +108,7 @@ export default function (Mousetrap) {
_recordedSequence.push(_currentRecordedKeys);
_currentRecordedKeys = [];
_recordedCharacterKey = false;
_finishRecording();
_restartRecordTimer();
}
/**
@@ -123,7 +122,7 @@ export default function (Mousetrap) {
*/
function _normalizeSequence(sequence) {
for (let i = 0; i < sequence.length; ++i) {
sequence[i].sort(function (x, y) {
sequence[i].sort(function(x, y) {
// modifier keys always come first, in alphabetical order
if (x.length > 1 && y.length === 1) {
return -1;
@@ -168,7 +167,7 @@ export default function (Mousetrap) {
*/
function _restartRecordTimer() {
clearTimeout(_recordTimer);
_recordTimer = setTimeout(_finishRecording, 1000);
_recordTimer = setTimeout(_finishRecording, options.timeout);
}
/**
@@ -178,10 +177,10 @@ export default function (Mousetrap) {
* @param {Function} callback
* @returns void
*/
Mousetrap.prototype.record = function (callback) {
Mousetrap.prototype.record = function(callback) {
var self = this;
self.recording = true;
_recordedSequenceCallback = function () {
_recordedSequenceCallback = function() {
self.recording = false;
callback.apply(self, arguments);
};
@@ -193,7 +192,7 @@ export default function (Mousetrap) {
* @param {Function} callback
* @returns void
*/
Mousetrap.prototype.stopRecord = function () {
Mousetrap.prototype.stopRecord = function() {
var self = this;
self.recording = false;
};
@@ -204,12 +203,11 @@ export default function (Mousetrap) {
* @param {Function} callback
* @returns void
*/
Mousetrap.prototype.startRecording = function () {
Mousetrap.prototype.startRecording = function() {
var self = this;
self.recording = true;
};
Mousetrap.prototype.handleKey = function () {
Mousetrap.prototype.handleKey = function() {
var self = this;
_handleKey.apply(self, arguments);
};