Wire presets

This commit is contained in:
igoroctaviano committed 2020-06-29 14:53:49 -03:00
1 parent f96c1f6648
commit 686acabd9f
8 files changed
+80 -63

No files matched your search

+13 -9
View File
@@ -1,3 +1,4 @@
import objectHash from 'object-hash';
import hotkeys from './../utils/hotkeys';
import log from './../log.js';
@@ -6,6 +7,7 @@ import log from './../log.js';
*
* @typedef {Object} HotkeyDefinition
* @property {String} commandName - Command to call
* @property {Object} commandOptions - Command options
* @property {String} label - Display name for hotkey
* @property {String[]} keys - Keys to bind; Follows Mousetrap.js binding syntax
*/
@@ -141,28 +143,30 @@ export class HotkeysManager {
* 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 {HotkeyDefinition} command
* @param {String} extension
* @returns {undefined}
*/
registerHotkeys({ commandName, keys, label } = {}, extension) {
registerHotkeys({ commandName, commandOptions = {}, keys, label } = {}, extension) {
if (!commandName) {
log.warn(`[hotkeys] No command was defined for hotkey "${keys}"`);
return;
}
const previouslyRegisteredDefinition = this.hotkeyDefinitions[commandName];
const commandHash = objectHash({ commandName, commandOptions });
const options = Object.keys(commandOptions).length ? JSON.stringify(commandOptions) : 'no';
const previouslyRegisteredDefinition = this.hotkeyDefinitions[commandHash];
if (previouslyRegisteredDefinition) {
const previouslyRegisteredKeys = previouslyRegisteredDefinition.keys;
this._unbindHotkeys(commandName, previouslyRegisteredKeys);
log.info(`[hotkeys] Unbinding ${commandName} from ${previouslyRegisteredKeys}`);
log.info(`[hotkeys] Unbinding ${commandName} with ${options} options from ${previouslyRegisteredKeys}`);
}
// Set definition & bind
this.hotkeyDefinitions[commandName] = { keys, label };
this._bindHotkeys(commandName, keys);
log.info(`[hotkeys] Binding ${commandName} to ${keys}`);
this.hotkeyDefinitions[commandHash] = { keys, label };
this._bindHotkeys(commandName, commandOptions, keys);
log.info(`[hotkeys] Binding ${commandName} with ${options} options to ${keys}`);
}
/**
@@ -191,7 +195,7 @@ export class HotkeysManager {
* @param {string[]} keys - One or more key combinations that should trigger command
* @returns {undefined}
*/
_bindHotkeys(commandName, keys) {
_bindHotkeys(commandName, commandOptions = {}, keys) {
const isKeyDefined = keys === '' || keys === undefined;
if (isKeyDefined) {
return;
@@ -203,7 +207,7 @@ export class HotkeysManager {
hotkeys.bind(combinedKeys, evt => {
evt.preventDefault();
evt.stopPropagation();
this._commandsManager.runCommand(commandName, { evt });
this._commandsManager.runCommand(commandName, { evt, ...commandOptions });
});
}