mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-02-28 18:22:42 -08:00
initial commit
This commit is contained in:
287
vendor/yomitan/js/input/hotkey-handler.js
vendored
Normal file
287
vendor/yomitan/js/input/hotkey-handler.js
vendored
Normal file
@@ -0,0 +1,287 @@
|
||||
/*
|
||||
* Copyright (C) 2023-2025 Yomitan Authors
|
||||
* Copyright (C) 2021-2022 Yomichan Authors
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
import {EventDispatcher} from '../core/event-dispatcher.js';
|
||||
import {EventListenerCollection} from '../core/event-listener-collection.js';
|
||||
import {getActiveModifiers, isInputElementFocused} from '../dom/document-util.js';
|
||||
|
||||
/**
|
||||
* Class which handles hotkey events and actions.
|
||||
* @augments EventDispatcher<import('hotkey-handler').Events>
|
||||
*/
|
||||
export class HotkeyHandler extends EventDispatcher {
|
||||
/**
|
||||
* Creates a new instance of the class.
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
/** @type {Map<string, (argument: unknown) => (boolean|void)>} */
|
||||
this._actions = new Map();
|
||||
/** @type {Map<(string | null), import('hotkey-handler').HotkeyHandlers>} */
|
||||
this._hotkeys = new Map();
|
||||
/** @type {Map<import('settings').InputsHotkeyScope, import('settings').InputsHotkeyOptions[]>} */
|
||||
this._hotkeyRegistrations = new Map();
|
||||
/** @type {EventListenerCollection} */
|
||||
this._eventListeners = new EventListenerCollection();
|
||||
/** @type {boolean} */
|
||||
this._isPrepared = false;
|
||||
/** @type {boolean} */
|
||||
this._hasEventListeners = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Begins listening to key press events in order to detect hotkeys.
|
||||
* @param {import('../comm/cross-frame-api.js').CrossFrameAPI} crossFrameApi
|
||||
*/
|
||||
prepare(crossFrameApi) {
|
||||
this._isPrepared = true;
|
||||
this._updateEventHandlers();
|
||||
crossFrameApi.registerHandlers([
|
||||
['hotkeyHandlerForwardHotkey', this._onMessageForwardHotkey.bind(this)],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of actions that this hotkey handler supports.
|
||||
* @param {[name: string, handler: (argument: unknown) => (boolean|void)][]} actions An array of `[name, handler]` entries, where `name` is a string and `handler` is a function.
|
||||
*/
|
||||
registerActions(actions) {
|
||||
for (const [name, handler] of actions) {
|
||||
this._actions.set(name, handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a set of hotkeys for a given scope.
|
||||
* @param {import('settings').InputsHotkeyScope} scope The scope that the hotkey definitions must be for in order to be activated.
|
||||
* @param {import('settings').InputsHotkeyOptions[]} hotkeys An array of hotkey definitions.
|
||||
*/
|
||||
registerHotkeys(scope, hotkeys) {
|
||||
let registrations = this._hotkeyRegistrations.get(scope);
|
||||
if (typeof registrations === 'undefined') {
|
||||
registrations = [];
|
||||
this._hotkeyRegistrations.set(scope, registrations);
|
||||
}
|
||||
registrations.push(...hotkeys);
|
||||
this._updateHotkeyRegistrations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all registered hotkeys for a given scope.
|
||||
* @param {import('settings').InputsHotkeyScope} scope The scope that the hotkey definitions were registered in.
|
||||
*/
|
||||
clearHotkeys(scope) {
|
||||
const registrations = this._hotkeyRegistrations.get(scope);
|
||||
if (typeof registrations !== 'undefined') {
|
||||
registrations.length = 0;
|
||||
}
|
||||
this._updateHotkeyRegistrations();
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a set of hotkeys for a given scope. This is an optimized shorthand for calling
|
||||
* `clearHotkeys`, then calling `registerHotkeys`.
|
||||
* @param {import('settings').InputsHotkeyScope} scope The scope that the hotkey definitions must be for in order to be activated.
|
||||
* @param {import('settings').InputsHotkeyOptions[]} hotkeys An array of hotkey definitions.
|
||||
*/
|
||||
setHotkeys(scope, hotkeys) {
|
||||
let registrations = this._hotkeyRegistrations.get(scope);
|
||||
if (typeof registrations === 'undefined') {
|
||||
registrations = [];
|
||||
this._hotkeyRegistrations.set(scope, registrations);
|
||||
} else {
|
||||
registrations.length = 0;
|
||||
}
|
||||
for (const {action, argument, key, modifiers, scopes, enabled} of hotkeys) {
|
||||
registrations.push({
|
||||
action,
|
||||
argument,
|
||||
key,
|
||||
modifiers: [...modifiers],
|
||||
scopes: [...scopes],
|
||||
enabled,
|
||||
});
|
||||
}
|
||||
this._updateHotkeyRegistrations();
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {import('core').EventNames<import('hotkey-handler').Events>} TName
|
||||
* @param {TName} eventName
|
||||
* @param {(details: import('core').EventArgument<import('hotkey-handler').Events, TName>) => void} callback
|
||||
*/
|
||||
on(eventName, callback) {
|
||||
super.on(eventName, callback);
|
||||
this._updateHasEventListeners();
|
||||
this._updateEventHandlers();
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {import('core').EventNames<import('hotkey-handler').Events>} TName
|
||||
* @param {TName} eventName
|
||||
* @param {(details: import('core').EventArgument<import('hotkey-handler').Events, TName>) => void} callback
|
||||
* @returns {boolean}
|
||||
*/
|
||||
off(eventName, callback) {
|
||||
const result = super.off(eventName, callback);
|
||||
this._updateHasEventListeners();
|
||||
this._updateEventHandlers();
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to simulate an action for a given combination of key and modifiers.
|
||||
* @param {string} key A keyboard key code indicating which key needs to be pressed.
|
||||
* @param {import('input').ModifierKey[]} 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.
|
||||
*/
|
||||
simulate(key, modifiers) {
|
||||
const hotkeyInfo = this._hotkeys.get(key);
|
||||
return (
|
||||
typeof hotkeyInfo !== 'undefined' &&
|
||||
this._invokeHandlers(modifiers, hotkeyInfo, key)
|
||||
);
|
||||
}
|
||||
|
||||
// Message handlers
|
||||
|
||||
/** @type {import('cross-frame-api').ApiHandler<'hotkeyHandlerForwardHotkey'>} */
|
||||
_onMessageForwardHotkey({key, modifiers}) {
|
||||
return this.simulate(key, modifiers);
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
/**
|
||||
* @param {KeyboardEvent} event
|
||||
*/
|
||||
_onKeyDown(event) {
|
||||
let hotkeyInfo = this._hotkeys.get(event.code);
|
||||
const modifierKeycodes = ['ControlLeft', 'ControlRight', 'ShiftLeft', 'ShiftRight', 'AltLeft', 'AltRight', 'MetaLeft', 'MetaRight'];
|
||||
if (modifierKeycodes.includes(event.code)) {
|
||||
hotkeyInfo = this._hotkeys.get(null); // Hotkeys with only modifiers are stored as null
|
||||
}
|
||||
if (typeof hotkeyInfo !== 'undefined') {
|
||||
const eventModifiers = getActiveModifiers(event);
|
||||
if (this._invokeHandlers(eventModifiers, hotkeyInfo, event.key)) {
|
||||
event.preventDefault();
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.trigger('keydownNonHotkey', event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('input').ModifierKey[]} modifiers
|
||||
* @param {import('hotkey-handler').HotkeyHandlers} hotkeyInfo
|
||||
* @param {string} key
|
||||
* @returns {boolean}
|
||||
*/
|
||||
_invokeHandlers(modifiers, hotkeyInfo, key) {
|
||||
for (const {modifiers: handlerModifiers, action, argument} of hotkeyInfo.handlers) {
|
||||
if (!this._areSame(handlerModifiers, modifiers) || !this._isHotkeyPermitted(modifiers, key)) { continue; }
|
||||
|
||||
const actionHandler = this._actions.get(action);
|
||||
if (typeof actionHandler !== 'undefined') {
|
||||
const result = actionHandler(argument);
|
||||
if (result !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Set<unknown>} set
|
||||
* @param {unknown[]} array
|
||||
* @returns {boolean}
|
||||
*/
|
||||
_areSame(set, array) {
|
||||
if (set.size !== array.length) { return false; }
|
||||
for (const value of array) {
|
||||
if (!set.has(value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
*/
|
||||
_updateHotkeyRegistrations() {
|
||||
if (this._hotkeys.size === 0 && this._hotkeyRegistrations.size === 0) { return; }
|
||||
|
||||
this._hotkeys.clear();
|
||||
for (const [scope, registrations] of this._hotkeyRegistrations.entries()) {
|
||||
for (const {action, argument, key, modifiers, scopes, enabled} of registrations) {
|
||||
if (!(enabled && (key !== null || modifiers.length > 0) && action !== '' && scopes.includes(scope))) { continue; }
|
||||
let hotkeyInfo = this._hotkeys.get(key);
|
||||
if (typeof hotkeyInfo === 'undefined') {
|
||||
hotkeyInfo = {handlers: []};
|
||||
this._hotkeys.set(key, hotkeyInfo);
|
||||
}
|
||||
|
||||
hotkeyInfo.handlers.push({modifiers: new Set(modifiers), action, argument});
|
||||
}
|
||||
}
|
||||
this._updateEventHandlers();
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
*/
|
||||
_updateHasEventListeners() {
|
||||
this._hasEventListeners = this.hasListeners('keydownNonHotkey');
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
*/
|
||||
_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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('input').ModifierKey[]} modifiers
|
||||
* @param {string} key
|
||||
* @returns {boolean}
|
||||
*/
|
||||
_isHotkeyPermitted(modifiers, key) {
|
||||
return !(
|
||||
(modifiers.length === 0 || (modifiers.length === 1 && modifiers[0] === 'shift')) &&
|
||||
isInputElementFocused() &&
|
||||
this._isKeyCharacterInput(key)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @returns {boolean}
|
||||
*/
|
||||
_isKeyCharacterInput(key) {
|
||||
return key.length === 1 || key === 'Process';
|
||||
}
|
||||
}
|
||||
218
vendor/yomitan/js/input/hotkey-help-controller.js
vendored
Normal file
218
vendor/yomitan/js/input/hotkey-help-controller.js
vendored
Normal file
@@ -0,0 +1,218 @@
|
||||
/*
|
||||
* Copyright (C) 2023-2025 Yomitan Authors
|
||||
* Copyright (C) 2021-2022 Yomichan Authors
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
import {parseJson} from '../core/json.js';
|
||||
import {isObjectNotArray} from '../core/object-utilities.js';
|
||||
import {HotkeyUtil} from './hotkey-util.js';
|
||||
|
||||
export class HotkeyHelpController {
|
||||
constructor() {
|
||||
/** @type {HotkeyUtil} */
|
||||
this._hotkeyUtil = new HotkeyUtil();
|
||||
/** @type {Map<string, string>} */
|
||||
this._localActionHotkeys = new Map();
|
||||
/** @type {Map<string, string>} */
|
||||
this._globalActionHotkeys = new Map();
|
||||
/** @type {RegExp} */
|
||||
this._replacementPattern = /\{0\}/g;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('../comm/api.js').API} api
|
||||
*/
|
||||
async prepare(api) {
|
||||
const {platform: {os}} = await api.getEnvironmentInfo();
|
||||
this._hotkeyUtil.os = os;
|
||||
await this._setupGlobalCommands(this._globalActionHotkeys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('settings').ProfileOptions} options
|
||||
*/
|
||||
setOptions(options) {
|
||||
const hotkeys = options.inputs.hotkeys;
|
||||
const hotkeyMap = this._localActionHotkeys;
|
||||
hotkeyMap.clear();
|
||||
for (const {enabled, action, key, modifiers} of hotkeys) {
|
||||
if (!enabled || key === null || action === '' || hotkeyMap.has(action)) { continue; }
|
||||
hotkeyMap.set(action, this._hotkeyUtil.getInputDisplayValue(key, modifiers));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ParentNode} node
|
||||
*/
|
||||
setupNode(node) {
|
||||
const replacementPattern = this._replacementPattern;
|
||||
for (const node2 of /** @type {NodeListOf<HTMLElement>} */ (node.querySelectorAll('[data-hotkey]'))) {
|
||||
const info = this._getNodeInfo(node2);
|
||||
if (info === null) { continue; }
|
||||
const {action, global, attributes, values, defaultAttributeValues} = info;
|
||||
const multipleValues = Array.isArray(values);
|
||||
const hotkey = (global ? this._globalActionHotkeys : this._localActionHotkeys).get(action);
|
||||
for (let i = 0, ii = attributes.length; i < ii; ++i) {
|
||||
const attribute = attributes[i];
|
||||
/** @type {unknown} */
|
||||
let value;
|
||||
if (typeof hotkey !== 'undefined') {
|
||||
value = multipleValues ? values[i] : values;
|
||||
if (typeof value === 'string') {
|
||||
value = value.replace(replacementPattern, hotkey);
|
||||
}
|
||||
} else {
|
||||
value = defaultAttributeValues[i];
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
node2.setAttribute(attribute, value);
|
||||
} else {
|
||||
node2.removeAttribute(attribute);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
/**
|
||||
* @returns {Promise<chrome.commands.Command[]>}
|
||||
*/
|
||||
_getAllCommands() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!(isObjectNotArray(chrome.commands) && typeof chrome.commands.getAll === 'function')) {
|
||||
resolve([]);
|
||||
return;
|
||||
}
|
||||
|
||||
chrome.commands.getAll((result) => {
|
||||
const e = chrome.runtime.lastError;
|
||||
if (e) {
|
||||
reject(new Error(e.message));
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Map<string, string>} commandMap
|
||||
*/
|
||||
async _setupGlobalCommands(commandMap) {
|
||||
const commands = await this._getAllCommands();
|
||||
|
||||
commandMap.clear();
|
||||
for (const {name, shortcut} of commands) {
|
||||
if (typeof name !== 'string' || typeof shortcut !== 'string' || shortcut.length === 0) { continue; }
|
||||
const {key, modifiers} = this._hotkeyUtil.convertCommandToInput(shortcut);
|
||||
commandMap.set(name, this._hotkeyUtil.getInputDisplayValue(key, modifiers));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} node
|
||||
* @param {unknown[]} data
|
||||
* @param {string[]} attributes
|
||||
* @returns {unknown[]}
|
||||
*/
|
||||
_getDefaultAttributeValues(node, data, attributes) {
|
||||
if (data.length > 3) {
|
||||
const result = data[3];
|
||||
if (Array.isArray(result)) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/** @type {(?string)[]} */
|
||||
const defaultAttributeValues = [];
|
||||
for (let i = 0, ii = attributes.length; i < ii; ++i) {
|
||||
const attribute = attributes[i];
|
||||
const value = node.hasAttribute(attribute) ? node.getAttribute(attribute) : null;
|
||||
defaultAttributeValues.push(value);
|
||||
}
|
||||
data[3] = defaultAttributeValues;
|
||||
node.dataset.hotkey = JSON.stringify(data);
|
||||
return defaultAttributeValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} node
|
||||
* @returns {?{action: string, global: boolean, attributes: string[], values: unknown, defaultAttributeValues: unknown[]}}
|
||||
*/
|
||||
_getNodeInfo(node) {
|
||||
const {hotkey} = node.dataset;
|
||||
if (typeof hotkey !== 'string') { return null; }
|
||||
const data = /** @type {unknown} */ (parseJson(hotkey));
|
||||
if (!Array.isArray(data)) { return null; }
|
||||
const dataArray = /** @type {unknown[]} */ (data);
|
||||
const [action, attributes, values] = dataArray;
|
||||
if (typeof action !== 'string') { return null; }
|
||||
/** @type {string[]} */
|
||||
const attributesArray = [];
|
||||
if (Array.isArray(attributes)) {
|
||||
for (const item of attributes) {
|
||||
if (typeof item !== 'string') { continue; }
|
||||
attributesArray.push(item);
|
||||
}
|
||||
} else if (typeof attributes === 'string') {
|
||||
attributesArray.push(attributes);
|
||||
}
|
||||
const defaultAttributeValues = this._getDefaultAttributeValues(node, data, attributesArray);
|
||||
const globalPrexix = 'global:';
|
||||
const global = action.startsWith(globalPrexix);
|
||||
return {
|
||||
action: global ? action.substring(globalPrexix.length) : action,
|
||||
global,
|
||||
attributes: attributesArray,
|
||||
values,
|
||||
defaultAttributeValues,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} node
|
||||
* @returns {?string}
|
||||
*/
|
||||
getHotkeyLabel(node) {
|
||||
const {hotkey} = node.dataset;
|
||||
if (typeof hotkey !== 'string') { return null; }
|
||||
|
||||
const data = /** @type {unknown} */ (parseJson(hotkey));
|
||||
if (!Array.isArray(data)) { return null; }
|
||||
|
||||
const values = /** @type {unknown[]} */ (data)[2];
|
||||
if (typeof values !== 'string') { return null; }
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {HTMLElement} node
|
||||
* @param {string} label
|
||||
*/
|
||||
setHotkeyLabel(node, label) {
|
||||
const {hotkey} = node.dataset;
|
||||
if (typeof hotkey !== 'string') { return; }
|
||||
|
||||
const data = /** @type {unknown} */ (parseJson(hotkey));
|
||||
if (!Array.isArray(data)) { return; }
|
||||
|
||||
data[2] = label;
|
||||
node.dataset.hotkey = JSON.stringify(data);
|
||||
}
|
||||
}
|
||||
336
vendor/yomitan/js/input/hotkey-util.js
vendored
Normal file
336
vendor/yomitan/js/input/hotkey-util.js
vendored
Normal file
@@ -0,0 +1,336 @@
|
||||
/*
|
||||
* Copyright (C) 2023-2025 Yomitan Authors
|
||||
* Copyright (C) 2021-2022 Yomichan Authors
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Utility class to help display hotkeys and convert to/from commands.
|
||||
*/
|
||||
export class HotkeyUtil {
|
||||
/**
|
||||
* Creates a new instance.
|
||||
* @param {?import('environment').OperatingSystem} os The operating system for this instance.
|
||||
*/
|
||||
constructor(os = null) {
|
||||
/** @type {?import('environment').OperatingSystem} */
|
||||
this._os = os;
|
||||
/** @type {string} */
|
||||
this._inputSeparator = ' + ';
|
||||
/** @type {Map<import('input').Modifier, string>} */
|
||||
this._modifierKeyNames = new Map();
|
||||
/** @type {RegExp} */
|
||||
this._mouseInputNamePattern = /^mouse(\d+)$/;
|
||||
/** @type {Map<import('input').Modifier, number>} */
|
||||
this._modifierPriorities = new Map([
|
||||
['meta', -4],
|
||||
['ctrl', -3],
|
||||
['alt', -2],
|
||||
['shift', -1],
|
||||
]);
|
||||
/** @type {Intl.Collator} */
|
||||
this._stringComparer = new Intl.Collator('en-US'); // Invariant locale
|
||||
|
||||
this._updateModifierKeyNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the operating system for this instance.
|
||||
* The operating system is used to display system-localized modifier key names.
|
||||
* @type {?import('environment').OperatingSystem}
|
||||
*/
|
||||
get os() {
|
||||
return this._os;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the operating system for this instance.
|
||||
* @param {?import('environment').OperatingSystem} value The value to assign.
|
||||
* Valid values are: win, mac, linux, openbsd, cros, android.
|
||||
*/
|
||||
set os(value) {
|
||||
if (this._os === value) { return; }
|
||||
this._os = value;
|
||||
this._updateModifierKeyNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a display string for a key and a set of modifiers.
|
||||
* @param {?string} key The key code string, or `null` for no key.
|
||||
* @param {import('input').Modifier[]} modifiers An array of modifiers.
|
||||
* Valid values are: ctrl, alt, shift, meta, or mouseN, where N is an integer.
|
||||
* @returns {string} A user-friendly string for the combination of key and modifiers.
|
||||
*/
|
||||
getInputDisplayValue(key, modifiers) {
|
||||
const separator = this._inputSeparator;
|
||||
let displayValue = '';
|
||||
let first = true;
|
||||
for (const modifier of modifiers) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
displayValue += separator;
|
||||
}
|
||||
displayValue += this.getModifierDisplayValue(modifier);
|
||||
}
|
||||
if (typeof key === 'string') {
|
||||
if (!first) { displayValue += separator; }
|
||||
displayValue += this.getKeyDisplayValue(key);
|
||||
}
|
||||
return displayValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a display string for a single modifier.
|
||||
* @param {import('input').Modifier} modifier A string representing a modifier.
|
||||
* Valid values are: ctrl, alt, shift, meta, or mouseN, where N is an integer.
|
||||
* @returns {string} A user-friendly string for the modifier.
|
||||
*/
|
||||
getModifierDisplayValue(modifier) {
|
||||
const match = this._mouseInputNamePattern.exec(modifier);
|
||||
if (match !== null) {
|
||||
return `Mouse ${match[1]}`;
|
||||
}
|
||||
|
||||
const name = this._modifierKeyNames.get(modifier);
|
||||
return (typeof name !== 'undefined' ? name : modifier);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a display string for a key.
|
||||
* @param {?string} key The key code string, or `null` for no key.
|
||||
* @returns {?string} A user-friendly string for the combination of key and modifiers, or `null` if key was already `null`.
|
||||
*/
|
||||
getKeyDisplayValue(key) {
|
||||
if (typeof key === 'string' && key.length === 4 && key.startsWith('Key')) {
|
||||
key = key.substring(3);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a display string for a single modifier.
|
||||
* @param {import('input').Modifier} modifier A string representing a modifier.
|
||||
* Valid values are: ctrl, alt, shift, meta, or mouseN, where N is an integer.
|
||||
* @returns {import('input').ModifierType} `'mouse'` if the modifier represents a mouse button, `'key'` otherwise.
|
||||
*/
|
||||
getModifierType(modifier) {
|
||||
return (this._mouseInputNamePattern.test(modifier) ? 'mouse' : 'key');
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an extension command string into a standard input.
|
||||
* @param {string|undefined} command An extension command string.
|
||||
* @returns {{key: ?string, modifiers: import('input').ModifierKey[]}} An object `{key, modifiers}`, where key is a string (or `null`) representing the key, and modifiers is an array of modifier keys.
|
||||
*/
|
||||
convertCommandToInput(command) {
|
||||
let key = null;
|
||||
/** @type {Set<import('input').ModifierKey>} */
|
||||
const modifiers = new Set();
|
||||
if (typeof command === 'string' && command.length > 0) {
|
||||
const parts = command.split('+');
|
||||
const ii = parts.length - 1;
|
||||
key = this._convertCommandKeyToInputKey(parts[ii]);
|
||||
for (let i = 0; i < ii; ++i) {
|
||||
const modifier = this._convertCommandModifierToInputModifier(parts[i]);
|
||||
if (modifier !== null) {
|
||||
modifiers.add(modifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
return {key, modifiers: this.sortModifiers([...modifiers])};
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a command string for a specified input.
|
||||
* @param {?string} key The key code string, or `null` for no key.
|
||||
* @param {import('input').Modifier[]} modifiers An array of modifier keys.
|
||||
* Valid values are: ctrl, alt, shift, meta.
|
||||
* @returns {string} An extension command string representing the input.
|
||||
*/
|
||||
convertInputToCommand(key, modifiers) {
|
||||
const separator = '+';
|
||||
let command = '';
|
||||
let first = true;
|
||||
for (const modifier of modifiers) {
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
command += separator;
|
||||
}
|
||||
command += this._convertInputModifierToCommandModifier(modifier);
|
||||
}
|
||||
if (typeof key === 'string') {
|
||||
if (!first) { command += separator; }
|
||||
command += this._convertInputKeyToCommandKey(key);
|
||||
}
|
||||
return command;
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {import('input').Modifier} TModifier
|
||||
* Sorts an array of modifiers.
|
||||
* @param {TModifier[]} modifiers An array of modifiers.
|
||||
* Valid values are: ctrl, alt, shift, meta.
|
||||
* @returns {TModifier[]} A sorted array of modifiers. The array instance is the same as the input array.
|
||||
*/
|
||||
sortModifiers(modifiers) {
|
||||
const pattern = this._mouseInputNamePattern;
|
||||
const keyPriorities = this._modifierPriorities;
|
||||
const stringComparer = this._stringComparer;
|
||||
|
||||
const count = modifiers.length;
|
||||
/** @type {[modifier: TModifier, isMouse: 0|1, priority: number, index: number][]} */
|
||||
const modifierInfos = [];
|
||||
for (let i = 0; i < count; ++i) {
|
||||
const modifier = modifiers[i];
|
||||
const match = pattern.exec(modifier);
|
||||
/** @type {[modifier: TModifier, isMouse: 0|1, priority: number, index: number]} */
|
||||
let info;
|
||||
if (match !== null) {
|
||||
info = [modifier, 1, Number.parseInt(match[1], 10), i];
|
||||
} else {
|
||||
let priority = keyPriorities.get(modifier);
|
||||
if (typeof priority === 'undefined') { priority = 0; }
|
||||
info = [modifier, 0, priority, i];
|
||||
}
|
||||
modifierInfos.push(info);
|
||||
}
|
||||
|
||||
modifierInfos.sort((a, b) => {
|
||||
let i = a[1] - b[1];
|
||||
if (i !== 0) { return i; }
|
||||
|
||||
i = a[2] - b[2];
|
||||
if (i !== 0) { return i; }
|
||||
|
||||
i = stringComparer.compare(a[0], b[0]);
|
||||
if (i !== 0) { return i; }
|
||||
|
||||
i = a[3] - b[3];
|
||||
return i;
|
||||
});
|
||||
|
||||
for (let i = 0; i < count; ++i) {
|
||||
modifiers[i] = modifierInfos[i][0];
|
||||
}
|
||||
|
||||
return modifiers;
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
/**
|
||||
* @param {?import('environment').OperatingSystem} os
|
||||
* @returns {[modifier: import('input').ModifierKey, label: string][]}
|
||||
*/
|
||||
_getModifierKeyNames(os) {
|
||||
switch (os) {
|
||||
case 'win':
|
||||
return [
|
||||
['alt', 'Alt'],
|
||||
['ctrl', 'Ctrl'],
|
||||
['shift', 'Shift'],
|
||||
['meta', 'Windows'],
|
||||
];
|
||||
case 'mac':
|
||||
return [
|
||||
['alt', 'Opt'],
|
||||
['ctrl', 'Ctrl'],
|
||||
['shift', 'Shift'],
|
||||
['meta', 'Cmd'],
|
||||
];
|
||||
case 'linux':
|
||||
case 'openbsd':
|
||||
case 'cros':
|
||||
case 'android':
|
||||
return [
|
||||
['alt', 'Alt'],
|
||||
['ctrl', 'Ctrl'],
|
||||
['shift', 'Shift'],
|
||||
['meta', 'Super'],
|
||||
];
|
||||
default: // 'unknown', etc
|
||||
return [
|
||||
['alt', 'Alt'],
|
||||
['ctrl', 'Ctrl'],
|
||||
['shift', 'Shift'],
|
||||
['meta', 'Meta'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {void}
|
||||
*/
|
||||
_updateModifierKeyNames() {
|
||||
const map = this._modifierKeyNames;
|
||||
map.clear();
|
||||
for (const [key, value] of this._getModifierKeyNames(this._os)) {
|
||||
map.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @returns {string}
|
||||
*/
|
||||
_convertCommandKeyToInputKey(key) {
|
||||
if (key.length === 1) {
|
||||
key = `Key${key}`;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} modifier
|
||||
* @returns {?import('input').ModifierKey}
|
||||
*/
|
||||
_convertCommandModifierToInputModifier(modifier) {
|
||||
switch (modifier) {
|
||||
case 'Ctrl': return (this._os === 'mac' ? 'meta' : 'ctrl');
|
||||
case 'Alt': return 'alt';
|
||||
case 'Shift': return 'shift';
|
||||
case 'MacCtrl': return 'ctrl';
|
||||
case 'Command': return 'meta';
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @returns {string}
|
||||
*/
|
||||
_convertInputKeyToCommandKey(key) {
|
||||
if (key.length === 4 && key.startsWith('Key')) {
|
||||
key = key.substring(3);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import('input').Modifier} modifier
|
||||
* @returns {string}
|
||||
*/
|
||||
_convertInputModifierToCommandModifier(modifier) {
|
||||
switch (modifier) {
|
||||
case 'ctrl': return (this._os === 'mac' ? 'MacCtrl' : 'Ctrl');
|
||||
case 'alt': return 'Alt';
|
||||
case 'shift': return 'Shift';
|
||||
case 'meta': return 'Command';
|
||||
default: return modifier;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user