2021-01-16 19:54:35 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2021-2022 Yomichan Authors
|
2021-01-16 19:54:35 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global
|
|
|
|
* DocumentUtil
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class which handles hotkey events and actions.
|
|
|
|
*/
|
|
|
|
class HotkeyHandler extends EventDispatcher {
|
2022-05-20 14:28:38 +00:00
|
|
|
/**
|
|
|
|
* Information describing a hotkey.
|
|
|
|
* @typedef {object} HotkeyDefinition
|
|
|
|
* @property {string} action A string indicating which action to perform.
|
|
|
|
* @property {string} key A keyboard key code indicating which key needs to be pressed.
|
|
|
|
* @property {string[]} modifiers An array of keyboard modifiers which also need to be pressed. Supports: `'alt', 'ctrl', 'shift', 'meta'`.
|
|
|
|
* @property {string[]} scopes An array of scopes for which the hotkey is valid. If this array does not contain `this.scope`, the hotkey will not be registered.
|
|
|
|
* @property {boolean} enabled A boolean indicating whether the hotkey is currently enabled.
|
|
|
|
*/
|
|
|
|
|
2021-01-16 19:54:35 +00:00
|
|
|
/**
|
|
|
|
* Creates a new instance of the class.
|
|
|
|
*/
|
2021-01-17 21:55:45 +00:00
|
|
|
constructor() {
|
2021-01-16 19:54:35 +00:00
|
|
|
super();
|
|
|
|
this._actions = new Map();
|
2021-01-17 21:55:45 +00:00
|
|
|
this._hotkeys = new Map();
|
|
|
|
this._hotkeyRegistrations = new Map();
|
2021-01-16 19:54:35 +00:00
|
|
|
this._eventListeners = new EventListenerCollection();
|
2021-01-17 04:07:21 +00:00
|
|
|
this._isPrepared = false;
|
|
|
|
this._hasEventListeners = false;
|
2021-01-16 19:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins listening to key press events in order to detect hotkeys.
|
|
|
|
*/
|
|
|
|
prepare() {
|
2021-01-17 04:07:21 +00:00
|
|
|
this._isPrepared = true;
|
|
|
|
this._updateEventHandlers();
|
2021-02-14 20:53:35 +00:00
|
|
|
yomichan.crossFrame.registerHandlers([
|
2021-11-21 20:54:58 +00:00
|
|
|
['HotkeyHandler.forwardHotkey', {async: false, handler: this._onMessageForwardHotkey.bind(this)}]
|
2021-01-17 21:55:45 +00:00
|
|
|
]);
|
2021-01-16 19:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a set of actions that this hotkey handler supports.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {*[][]} actions An array of `[name, handler]` entries, where `name` is a string and `handler` is a function.
|
2021-01-16 19:54:35 +00:00
|
|
|
*/
|
|
|
|
registerActions(actions) {
|
|
|
|
for (const [name, handler] of actions) {
|
|
|
|
this._actions.set(name, handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-17 21:55:45 +00:00
|
|
|
* Registers a set of hotkeys for a given scope.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} scope The scope that the hotkey definitions must be for in order to be activated.
|
|
|
|
* @param {HotkeyDefinition[]} hotkeys An array of hotkey definitions.
|
2021-01-16 19:54:35 +00:00
|
|
|
*/
|
2021-01-17 21:55:45 +00:00
|
|
|
registerHotkeys(scope, hotkeys) {
|
|
|
|
let registrations = this._hotkeyRegistrations.get(scope);
|
|
|
|
if (typeof registrations === 'undefined') {
|
|
|
|
registrations = [];
|
|
|
|
this._hotkeyRegistrations.set(scope, registrations);
|
2021-01-16 19:54:35 +00:00
|
|
|
}
|
2021-01-17 21:55:45 +00:00
|
|
|
registrations.push(...hotkeys);
|
|
|
|
this._updateHotkeyRegistrations();
|
2021-01-16 19:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-17 21:55:45 +00:00
|
|
|
* Removes all registered hotkeys for a given scope.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} scope The scope that the hotkey definitions were registered in.
|
2021-01-16 19:54:35 +00:00
|
|
|
*/
|
2021-01-17 21:55:45 +00:00
|
|
|
clearHotkeys(scope) {
|
|
|
|
const registrations = this._hotkeyRegistrations.get(scope);
|
|
|
|
if (typeof registrations !== 'undefined') {
|
|
|
|
registrations.length = 0;
|
|
|
|
}
|
|
|
|
this._updateHotkeyRegistrations();
|
2021-01-16 19:54:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 22:05:06 +00:00
|
|
|
/**
|
|
|
|
* Assigns a set of hotkeys for a given scope. This is an optimized shorthand for calling
|
|
|
|
* `clearHotkeys`, then calling `registerHotkeys`.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} scope The scope that the hotkey definitions must be for in order to be activated.
|
|
|
|
* @param {HotkeyDefinition[]} hotkeys An array of hotkey definitions.
|
2021-01-17 22:05:06 +00:00
|
|
|
*/
|
|
|
|
setHotkeys(scope, hotkeys) {
|
|
|
|
let registrations = this._hotkeyRegistrations.get(scope);
|
|
|
|
if (typeof registrations === 'undefined') {
|
|
|
|
registrations = [];
|
|
|
|
this._hotkeyRegistrations.set(scope, registrations);
|
|
|
|
} else {
|
|
|
|
registrations.length = 0;
|
|
|
|
}
|
|
|
|
registrations.push(...hotkeys);
|
|
|
|
this._updateHotkeyRegistrations();
|
|
|
|
}
|
|
|
|
|
2021-01-17 04:07:21 +00:00
|
|
|
/**
|
|
|
|
* Adds a single event listener to a specific event.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} eventName The string representing the event's name.
|
|
|
|
* @param {Function} callback The event listener callback to add.
|
|
|
|
* @returns {void}
|
2021-01-17 04:07:21 +00:00
|
|
|
*/
|
|
|
|
on(eventName, callback) {
|
|
|
|
const result = super.on(eventName, callback);
|
|
|
|
this._updateHasEventListeners();
|
|
|
|
this._updateEventHandlers();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a single event listener from a specific event.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} eventName The string representing the event's name.
|
|
|
|
* @param {Function} callback The event listener callback to add.
|
|
|
|
* @returns {boolean} `true` if the callback was removed, `false` otherwise.
|
2021-01-17 04:07:21 +00:00
|
|
|
*/
|
|
|
|
off(eventName, callback) {
|
|
|
|
const result = super.off(eventName, callback);
|
|
|
|
this._updateHasEventListeners();
|
|
|
|
this._updateEventHandlers();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:55:45 +00:00
|
|
|
/**
|
|
|
|
* Attempts to simulate an action for a given combination of key and modifiers.
|
2022-05-20 14:28:38 +00:00
|
|
|
* @param {string} key A keyboard key code indicating which key needs to be pressed.
|
|
|
|
* @param {string[]} modifiers An array of keyboard modifiers which also need to be pressed. Supports: `'alt', 'ctrl', 'shift', 'meta'`.
|
|
|
|
* @returns {boolean} `true` if an action was performed, `false` otherwise.
|
2021-01-17 21:55:45 +00:00
|
|
|
*/
|
|
|
|
simulate(key, modifiers) {
|
|
|
|
const hotkeyInfo = this._hotkeys.get(key);
|
|
|
|
return (
|
|
|
|
typeof hotkeyInfo !== 'undefined' &&
|
2021-02-28 19:29:43 +00:00
|
|
|
this._invokeHandlers(modifiers, hotkeyInfo)
|
2021-01-17 21:55:45 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message handlers
|
|
|
|
|
|
|
|
_onMessageForwardHotkey({key, modifiers}) {
|
|
|
|
return this.simulate(key, modifiers);
|
|
|
|
}
|
|
|
|
|
2021-01-16 19:54:35 +00:00
|
|
|
// Private
|
|
|
|
|
|
|
|
_onKeyDown(e) {
|
2021-09-26 22:14:00 +00:00
|
|
|
const hotkeyInfo = this._hotkeys.get(e.code);
|
2021-01-17 21:55:45 +00:00
|
|
|
if (typeof hotkeyInfo !== 'undefined') {
|
2021-01-16 19:54:35 +00:00
|
|
|
const eventModifiers = DocumentUtil.getActiveModifiers(e);
|
2021-09-26 22:14:00 +00:00
|
|
|
if (this._invokeHandlers(eventModifiers, hotkeyInfo, e.key)) {
|
2021-01-17 21:55:45 +00:00
|
|
|
e.preventDefault();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.trigger('keydownNonHotkey', e);
|
|
|
|
}
|
2021-01-16 19:54:35 +00:00
|
|
|
|
2021-09-26 22:14:00 +00:00
|
|
|
_invokeHandlers(modifiers, hotkeyInfo, key) {
|
2021-03-28 02:30:45 +00:00
|
|
|
for (const {modifiers: handlerModifiers, action, argument} of hotkeyInfo.handlers) {
|
2021-09-26 22:14:00 +00:00
|
|
|
if (!this._areSame(handlerModifiers, modifiers) || !this._isHotkeyPermitted(modifiers, key)) { continue; }
|
2021-01-16 19:54:35 +00:00
|
|
|
|
2021-01-17 21:55:45 +00:00
|
|
|
const actionHandler = this._actions.get(action);
|
|
|
|
if (typeof actionHandler !== 'undefined') {
|
2021-03-28 02:30:45 +00:00
|
|
|
const result = actionHandler(argument);
|
2021-01-16 19:54:35 +00:00
|
|
|
if (result !== false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:55:45 +00:00
|
|
|
return false;
|
2021-01-16 19:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_areSame(set, array) {
|
|
|
|
if (set.size !== array.length) { return false; }
|
|
|
|
for (const value of array) {
|
|
|
|
if (!set.has(value)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-17 04:07:21 +00:00
|
|
|
|
2021-01-17 21:55:45 +00:00
|
|
|
_updateHotkeyRegistrations() {
|
|
|
|
if (this._hotkeys.size === 0 && this._hotkeyRegistrations.size === 0) { return; }
|
|
|
|
|
|
|
|
this._hotkeys.clear();
|
|
|
|
for (const [scope, registrations] of this._hotkeyRegistrations.entries()) {
|
2021-03-28 02:30:45 +00:00
|
|
|
for (const {action, argument, key, modifiers, scopes, enabled} of registrations) {
|
2021-02-28 19:29:43 +00:00
|
|
|
if (!(enabled && key !== null && action !== '' && scopes.includes(scope))) { continue; }
|
2021-01-17 21:55:45 +00:00
|
|
|
|
|
|
|
let hotkeyInfo = this._hotkeys.get(key);
|
|
|
|
if (typeof hotkeyInfo === 'undefined') {
|
2021-02-28 19:29:43 +00:00
|
|
|
hotkeyInfo = {handlers: []};
|
2021-01-17 21:55:45 +00:00
|
|
|
this._hotkeys.set(key, hotkeyInfo);
|
|
|
|
}
|
|
|
|
|
2021-03-28 02:30:45 +00:00
|
|
|
hotkeyInfo.handlers.push({modifiers: new Set(modifiers), action, argument});
|
2021-01-17 21:55:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this._updateEventHandlers();
|
|
|
|
}
|
|
|
|
|
2021-01-17 04:07:21 +00:00
|
|
|
_updateHasEventListeners() {
|
|
|
|
this._hasEventListeners = this.hasListeners('keydownNonHotkey');
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateEventHandlers() {
|
|
|
|
if (this._isPrepared && (this._hotkeys.size > 0 || this._hasEventListeners)) {
|
|
|
|
if (this._eventListeners.size !== 0) { return; }
|
|
|
|
this._eventListeners.addEventListener(document, 'keydown', this._onKeyDown.bind(this), false);
|
|
|
|
} else {
|
|
|
|
this._eventListeners.removeAllEventListeners();
|
|
|
|
}
|
|
|
|
}
|
2021-09-26 22:14:00 +00:00
|
|
|
|
|
|
|
_isHotkeyPermitted(modifiers, key) {
|
|
|
|
return !(
|
|
|
|
(modifiers.length === 0 || (modifiers.length === 1 && modifiers[0] === 'shift')) &&
|
|
|
|
DocumentUtil.isInputElementFocused() &&
|
|
|
|
this._isKeyCharacterInput(key)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_isKeyCharacterInput(key) {
|
|
|
|
return key.length === 1;
|
|
|
|
}
|
2021-01-16 19:54:35 +00:00
|
|
|
}
|