fix: hotkeys editing in user preferences

This commit is contained in:
Alireza authored and Erik Ziegler committed 2021-07-08 10:09:20 +02:00
1 parent bcd4aeb9e4
commit f5ecc753f7
4 files changed
+53 -56

No files matched your search

@@ -11,6 +11,8 @@ import {
} from '@ohif/ui'; } from '@ohif/ui';
import i18n from '@ohif/i18n'; import i18n from '@ohif/i18n';
import { hotkeys } from '@ohif/core';
const { availableLanguages, defaultLanguage, currentLanguage } = i18n; const { availableLanguages, defaultLanguage, currentLanguage } = i18n;
@@ -115,7 +117,11 @@ function ViewerLayout({
currentLanguage: currentLanguage(), currentLanguage: currentLanguage(),
availableLanguages, availableLanguages,
defaultLanguage, defaultLanguage,
onCancel: hide, onCancel: () => {
hotkeys.stopRecord();
hotkeys.unpause();
hide();
},
onSubmit: ({ hotkeyDefinitions, language }) => { onSubmit: ({ hotkeyDefinitions, language }) => {
i18n.changeLanguage(language.value); i18n.changeLanguage(language.value);
hotkeysManager.setHotkeys(hotkeyDefinitions); hotkeysManager.setHotkeys(hotkeyDefinitions);
+34 -46
View File
@@ -1,41 +1,28 @@
"use strict";
/** /**
* This extension allows you to record a sequence using Mousetrap. * This extension allows you to record a sequence using Mousetrap.
* {@link https://craig.is/killing/mice}
* *
* @author Dan Tao <daniel.tao@gmail.com> * @author Dan Tao <daniel.tao@gmail.com>
*/ */
module.exports = function (Mousetrap, options) { export default function (Mousetrap) {
/**
* the default configurations, we merge with user options
*
* @type {Array}
*/
var _config = Object.assign({
timeout: 1000
}, options);
/** /**
* the sequence currently being recorded * the sequence currently being recorded
* *
* @type {Array} * @type {Array}
*/ */
var _recordedSequence = [], var _recordedSequence = [],
/** /**
* a callback to invoke after recording a sequence * a callback to invoke after recording a sequence
* *
* @type {Function|null} * @type {Function|null}
*/ */
_recordedSequenceCallback = null, _recordedSequenceCallback = null,
/** /**
* a list of all of the keys currently held down * a list of all of the keys currently held down
* *
* @type {Array} * @type {Array}
*/ */
_currentRecordedKeys = [], _currentRecordedKeys = [],
/** /**
* temporary state where we remember if we've already captured a * temporary state where we remember if we've already captured a
* character key in the current combo * character key in the current combo
@@ -43,28 +30,19 @@ module.exports = function (Mousetrap, options) {
* @type {boolean} * @type {boolean}
*/ */
_recordedCharacterKey = false, _recordedCharacterKey = false,
/** /**
* a handle for the timer of the current recording * a handle for the timer of the current recording
* *
* @type {null|number} * @type {null|number}
*/ */
_recordTimer = null, _recordTimer = null,
/** /**
* the original handleKey method to override when Mousetrap.record() is * the original handleKey method to override when Mousetrap.record() is
* called * called
* *
* @type {Function} * @type {Function}
*/ */
_origHandleKey = Mousetrap.prototype.handleKey, _origHandleKey = Mousetrap.prototype.handleKey;
/**
* the timeout that timer will wait for a key
*
* @type {number}
*/
_recordTimeout = _config.timeout;
/** /**
* handles a character key event * handles a character key event
@@ -83,19 +61,19 @@ module.exports = function (Mousetrap, options) {
} }
// remember this character if we're currently recording a sequence // remember this character if we're currently recording a sequence
if (e.type === 'keydown') { if (e.type == 'keydown') {
if (character.length === 1 && _recordedCharacterKey) { if (character.length === 1 && _recordedCharacterKey) {
_recordCurrentCombo(); _recordCurrentCombo();
} }
for (var i = 0; i < modifiers.length; ++i) { for (let i = 0; i < modifiers.length; ++i) {
_recordKey(modifiers[i]); _recordKey(modifiers[i]);
} }
_recordKey(character); _recordKey(character);
// once a key is released, all keys that were held down at the time // once a key is released, all keys that were held down at the time
// count as a keypress // count as a keypress
} else if (e.type === 'keyup' && _currentRecordedKeys.length > 0) { } else if (e.type == 'keyup' && _currentRecordedKeys.length > 0) {
_recordCurrentCombo(); _recordCurrentCombo();
} }
} }
@@ -107,10 +85,8 @@ module.exports = function (Mousetrap, options) {
* @returns void * @returns void
*/ */
function _recordKey(key) { function _recordKey(key) {
var i;
// one-off implementation of Array.indexOf, since IE6-9 don't support it // one-off implementation of Array.indexOf, since IE6-9 don't support it
for (i = 0; i < _currentRecordedKeys.length; ++i) { for (let i = 0; i < _currentRecordedKeys.length; ++i) {
if (_currentRecordedKeys[i] === key) { if (_currentRecordedKeys[i] === key) {
return; return;
} }
@@ -133,7 +109,7 @@ module.exports = function (Mousetrap, options) {
_recordedSequence.push(_currentRecordedKeys); _recordedSequence.push(_currentRecordedKeys);
_currentRecordedKeys = []; _currentRecordedKeys = [];
_recordedCharacterKey = false; _recordedCharacterKey = false;
_restartRecordTimer(); _finishRecording();
} }
/** /**
@@ -146,9 +122,7 @@ module.exports = function (Mousetrap, options) {
* @returns void * @returns void
*/ */
function _normalizeSequence(sequence) { function _normalizeSequence(sequence) {
var i; for (let i = 0; i < sequence.length; ++i) {
for (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 // modifier keys always come first, in alphabetical order
if (x.length > 1 && y.length === 1) { if (x.length > 1 && y.length === 1) {
@@ -194,7 +168,7 @@ module.exports = function (Mousetrap, options) {
*/ */
function _restartRecordTimer() { function _restartRecordTimer() {
clearTimeout(_recordTimer); clearTimeout(_recordTimer);
_recordTimer = setTimeout(_finishRecording, _recordTimeout); _recordTimer = setTimeout(_finishRecording, 1000);
} }
/** /**
@@ -204,27 +178,41 @@ module.exports = function (Mousetrap, options) {
* @param {Function} callback * @param {Function} callback
* @returns void * @returns void
*/ */
Mousetrap.prototype.record = function(callback, timeout) { Mousetrap.prototype.record = function (callback) {
var self = this; var self = this;
self.recording = true; self.recording = true;
// if the user doesn't want to change the timeout
// we still need to guarantee that it gets the default timeout
_recordTimeout = timeout || _config.timeout;
_recordedSequenceCallback = function () { _recordedSequenceCallback = function () {
self.recording = false; self.recording = false;
callback.apply(self, arguments); callback.apply(self, arguments);
}; };
}; };
/**
* stop recording
*
* @param {Function} callback
* @returns void
*/
Mousetrap.prototype.stopRecord = function () {
var self = this;
self.recording = false;
};
/**
* start recording
*
* @param {Function} callback
* @returns void
*/
Mousetrap.prototype.startRecording = function () {
var self = this;
self.recording = true;
};
Mousetrap.prototype.handleKey = function () { Mousetrap.prototype.handleKey = function () {
var self = this; var self = this;
_handleKey.apply(self, arguments); _handleKey.apply(self, arguments);
}; };
Mousetrap.init(); Mousetrap.init();
}
return Mousetrap;
};
@@ -1,11 +1,13 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
//import { hotkeys } from '@ohif/core'; import { hotkeys } from '@ohif/core';
import Input from '../Input'; import Input from '../Input';
//import { getKeys, formatKeysForInput } from './utils'; import { getKeys, formatKeysForInput } from './utils';
import { formatKeysForInput } from './utils';
/** /**
* HotkeyField * HotkeyField
@@ -25,16 +27,16 @@ const HotkeyField = ({ disabled, keys, onChange, className, modifierKeys }) => {
event.stopPropagation(); event.stopPropagation();
event.preventDefault(); event.preventDefault();
/*hotkeys.record(sequence => { hotkeys.record(sequence => {
const keys = getKeys({ sequence, modifierKeys }); const keys = getKeys({ sequence, modifierKeys });
hotkeys.unpause(); hotkeys.unpause();
onChange(keys); onChange(keys);
});*/ });
}; };
const onFocus = () => { const onFocus = () => {
/*hotkeys.pause(); hotkeys.pause();
hotkeys.startRecording();*/ hotkeys.startRecording();
}; };
return ( return (
@@ -35,9 +35,10 @@ const HotkeysPreferences = ({ disabled, hotkeyDefinitions, errors: controlledErr
setErrors(prevState => { setErrors(prevState => {
const errors = { ...prevState, [id]: error }; const errors = { ...prevState, [id]: error };
onChange(id, definition, errors);
return errors; return errors;
}); });
onChange(id, definition, { ...errors, [id]: error });
}; };
return ( return (
@@ -84,7 +85,7 @@ const HotkeysPreferences = ({ disabled, hotkeyDefinitions, errors: controlledErr
onChange={onChangeHandler} onChange={onChangeHandler}
className='text-lg h-8' className='text-lg h-8'
/> />
{error && <span className='p-2 text-red-600 text-sm'>{error}</span>} {error && <span className='p-2 text-left text-red-600 text-sm'>{error}</span>}
</div> </div>
</div> </div>
</div> </div>