* fix: Combined Hotkeys for special characters * add record method to hotkey manager * fix record plugin * remove unused component * add record to modal props * rename record method * replace handlers to use hotkeyRecord * fix combined keys * change expected result count from 18 to 17 * autoformat * Remove duplicate test, that was testing the wrong things; fix label; update configs * Revert "Remove duplicate test, that was testing the wrong things; fix label; update configs" This reverts commit 4292f4fe67351962d61cae623b920dcbd87dd71d. * Fix the record plugin's registration * fix exposed record method usage * adding logging for info level items * Hotkey definitions don't need to be globally reactive; use localstorage/appconfig as sources of truth; not redux * Tidy up test * Remove unused code from UserPreferencesForm * Log info when we run a command * fix hotkey preference restore * use application configured hotkeys if there are no user preferred * Avoid logging circular ref * Fix callouts * Fix small issue with array * Fix langua issue after refactor and merge * Refactor on recordCurrentCombo as Rodrigo did before * Separating components in 2 files * WIP Refactor to simplify the user preferences and move into each form the save and controll functionalities * Remove context * Remove unused import * Initial work on Field treatment * Refactor General preferences * Small refactor removing type from HotkeyField * small update on style * Refactor and layout fixed * Make hotkeys preferences working with old hotkeys row * Move error handling out of hotkey row/input component * WIP custom form * Moving validation function to component * Exposing hotkeyRecord as it does not depend on HotkeyManager Class * Making hotkeyField as much detached possible from parent component * Small refactors * Refactor on user preferences * Clean up into the changes * Small fix to let save working * Style finish * move about docs into about folder * Fix double tap on single keys * Style refactor * Remove log * Fix log issues on unit tests * Fix unit test breaking on ohif/core index * Fixing hotkeys unpause unit test issue * Rename file to adopt lowercase * Rename file to adopt lowercase * Fixing callouts * Big refactor miving some of the components into viewer and creating small components into ohif/ui * Typo on folder name * Updating ohif ui docs * Remove comments * Fix binding of combo keys * Fix some cypress tests failures * Fixing onCancel button * Fixing e2e tests * Small style update * Fixing unit tests failing after fix issue * Remove some not used code * Remove left over after debug * Adding prevent default on hotkeys events * Fixinf existing hotkeys validator with 3 keys pressed * Exposing hotkeys as root level on ohif-core * Clean up * Exposing all availableLanguages with labels and fixing an issue on language switcher * Fixing e2e cypress tests * Preveinting some simple errors * Treating error once we try to set hotkey definitions * Adding ui notification on setHotkeys errors * Implementing a service queue request to hold until functions are implemented * Making sure toFixed is only called on Numbers Co-authored-by: Danny Brown <danny.ri.brown@gmail.com> Co-authored-by: Gustavo André Lelis <galelis@gmail.com>
243 lines
6.5 KiB
JavaScript
243 lines
6.5 KiB
JavaScript
import hotkeys from './../utils/hotkeys';
|
|
import log from './../log.js';
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @typedef {Object} HotkeyDefinition
|
|
* @property {String} commandName - Command to call
|
|
* @property {String} label - Display name for hotkey
|
|
* @property {String[]} keys - Keys to bind; Follows Mousetrap.js binding syntax
|
|
*/
|
|
|
|
export class HotkeysManager {
|
|
constructor(commandsManager, servicesManager) {
|
|
this.hotkeyDefinitions = {};
|
|
this.hotkeyDefaults = [];
|
|
this.isEnabled = true;
|
|
|
|
if (!commandsManager) {
|
|
log.warn(
|
|
'HotkeysManager instantiated without a commandsManager. Hotkeys will be unable to find and run commands.'
|
|
);
|
|
}
|
|
|
|
this._servicesManager = servicesManager;
|
|
this._commandsManager = commandsManager;
|
|
}
|
|
|
|
/**
|
|
* Exposes Mousetrap.js's `.record` method, added by the record plugin.
|
|
*
|
|
* @param {*} event
|
|
*/
|
|
record(event) {
|
|
return hotkeys.record(event);
|
|
}
|
|
|
|
/**
|
|
* Disables all hotkeys. Hotkeys added while disabled will not listen for
|
|
* input.
|
|
*/
|
|
disable() {
|
|
this.isEnabled = false;
|
|
hotkeys.pause();
|
|
}
|
|
|
|
/**
|
|
* Enables all hotkeys.
|
|
*/
|
|
enable() {
|
|
this.isEnabled = true;
|
|
hotkeys.unpause();
|
|
}
|
|
|
|
/**
|
|
* Registers a list of hotkeydefinitions.
|
|
*
|
|
* @param {HotkeyDefinition[] | Object} [hotkeyDefinitions=[]] Contains hotkeys definitions
|
|
*/
|
|
setHotkeys(hotkeyDefinitions = []) {
|
|
try {
|
|
const definitions = this._getValidDefinitions(hotkeyDefinitions);
|
|
|
|
definitions.forEach(definition => this.registerHotkeys(definition));
|
|
} catch (error) {
|
|
const { UINotificationService } = this._servicesManager.services;
|
|
UINotificationService.show({
|
|
title: 'Hotkeys Manager',
|
|
message: 'Erro while setting hotkeys',
|
|
type: 'error',
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set default hotkey bindings. These
|
|
* values are used in `this.restoreDefaultBindings`.
|
|
*
|
|
* @param {HotkeyDefinition[] | Object} [hotkeyDefinitions=[]] Contains hotkeys definitions
|
|
*/
|
|
setDefaultHotKeys(hotkeyDefinitions = []) {
|
|
const definitions = this._getValidDefinitions(hotkeyDefinitions);
|
|
|
|
this.hotkeyDefaults = definitions;
|
|
}
|
|
|
|
/**
|
|
* Take hotkey definitions that can be an array or object and make sure that it
|
|
* returns an array of hotkeys
|
|
*
|
|
* @param {HotkeyDefinition[] | Object} [hotkeyDefinitions=[]] Contains hotkeys definitions
|
|
*/
|
|
_getValidDefinitions(hotkeyDefinitions) {
|
|
const definitions = Array.isArray(hotkeyDefinitions)
|
|
? [...hotkeyDefinitions]
|
|
: this._parseToArrayLike(hotkeyDefinitions);
|
|
|
|
return definitions;
|
|
}
|
|
|
|
/**
|
|
* It parses given object containing hotkeyDefinition to array like.
|
|
* Each property of given object will be mapped to an object of an array. And its property name will be the value of a property named as commandName
|
|
*
|
|
* @param {HotkeyDefinition[] | Object} [hotkeyDefinitions={}] Contains hotkeys definitions
|
|
* @returns {HotkeyDefinition[]}
|
|
*/
|
|
_parseToArrayLike(hotkeyDefinitionsObj = {}) {
|
|
const copy = { ...hotkeyDefinitionsObj };
|
|
return Object.entries(copy).map(entryValue =>
|
|
this._parseToHotKeyObj(entryValue[0], entryValue[1])
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Return HotkeyDefinition object like based on given property name and property value
|
|
* @param {string} propertyName property name of hotkey definition object
|
|
* @param {object} propertyValue property value of hotkey definition object
|
|
*
|
|
* @example
|
|
*
|
|
* const hotKeyObj = {hotKeyDefA: {keys:[],....}}
|
|
*
|
|
* const parsed = _parseToHotKeyObj(Object.keys(hotKeyDefA)[0], hotKeyObj[hotKeyDefA]);
|
|
* {
|
|
* commandName: hotKeyDefA,
|
|
* keys: [],
|
|
* ....
|
|
* }
|
|
*
|
|
*/
|
|
_parseToHotKeyObj(propertyName, propertyValue) {
|
|
return {
|
|
commandName: propertyName,
|
|
...propertyValue,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* (unbinds and) binds the specified command to one or more key combinations.
|
|
* When a hotkey combination is triggered, the command name and active contexts
|
|
* are used to locate the correct command to call.
|
|
*
|
|
* @param {HotkeyDefinition} commandName
|
|
* @param {String} extension
|
|
* @returns {undefined}
|
|
*/
|
|
registerHotkeys({ commandName, keys, label } = {}, extension) {
|
|
if (!commandName) {
|
|
log.warn(`No command was defined for hotkey "${keys}"`);
|
|
return;
|
|
}
|
|
|
|
const previouslyRegisteredDefinition = this.hotkeyDefinitions[commandName];
|
|
|
|
if (previouslyRegisteredDefinition) {
|
|
const previouslyRegisteredKeys = previouslyRegisteredDefinition.keys;
|
|
this._unbindHotkeys(commandName, previouslyRegisteredKeys);
|
|
log.info(`Unbinding ${commandName} from ${previouslyRegisteredKeys}`);
|
|
}
|
|
|
|
// Set definition & bind
|
|
this.hotkeyDefinitions[commandName] = { keys, label };
|
|
this._bindHotkeys(commandName, keys);
|
|
log.info(`Binding ${commandName} to ${keys}`);
|
|
}
|
|
|
|
/**
|
|
* Uses most recent
|
|
*
|
|
* @returns {undefined}
|
|
*/
|
|
restoreDefaultBindings() {
|
|
this.setHotkeys(this.hotkeyDefaults);
|
|
}
|
|
|
|
/**
|
|
*
|
|
*/
|
|
destroy() {
|
|
this.hotkeyDefaults = [];
|
|
this.hotkeyDefinitions = {};
|
|
hotkeys.reset();
|
|
}
|
|
|
|
/**
|
|
* Binds one or more set of hotkey combinations for a given command
|
|
*
|
|
* @private
|
|
* @param {string} commandName - The name of the command to trigger when hotkeys are used
|
|
* @param {string[]} keys - One or more key combinations that should trigger command
|
|
* @returns {undefined}
|
|
*/
|
|
_bindHotkeys(commandName, keys) {
|
|
const isKeyDefined = keys === '' || keys === undefined;
|
|
if (isKeyDefined) {
|
|
return;
|
|
}
|
|
|
|
const isKeyArray = keys instanceof Array;
|
|
const combinedKeys = isKeyArray ? keys.join('+') : keys;
|
|
|
|
hotkeys.bind(combinedKeys, evt => {
|
|
evt.preventDefault();
|
|
evt.stopPropagation();
|
|
this._commandsManager.runCommand(commandName, { evt });
|
|
});
|
|
}
|
|
|
|
/**
|
|
* unbinds one or more set of hotkey combinations for a given command
|
|
*
|
|
* @private
|
|
* @param {string} commandName - The name of the previously bound command
|
|
* @param {string[]} keys - One or more sets of previously bound keys
|
|
* @returns {undefined}
|
|
*/
|
|
_unbindHotkeys(commandName, keys) {
|
|
const isKeyDefined = keys !== '' && keys !== undefined;
|
|
if (!isKeyDefined) {
|
|
return;
|
|
}
|
|
|
|
const isKeyArray = keys instanceof Array;
|
|
if (isKeyArray) {
|
|
const combinedKeys = keys.join('+');
|
|
this._unbindHotkeys(commandName, combinedKeys);
|
|
return;
|
|
}
|
|
|
|
hotkeys.unbind(keys);
|
|
}
|
|
}
|
|
|
|
export default HotkeysManager;
|
|
|
|
// Commands Contexts:
|
|
|
|
// --> Name and Priority
|
|
// GLOBAL: 0
|
|
// VIEWER::CORNERSTONE: 1
|
|
// VIEWER::VTK: 1
|