2021-01-18 20:23:49 +00:00
|
|
|
/*
|
2022-02-03 01:43:10 +00:00
|
|
|
* Copyright (C) 2021-2022 Yomichan Authors
|
2021-01-18 20:23:49 +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
|
2021-02-26 01:21:05 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2021-01-18 20:23:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* global
|
|
|
|
* HotkeyUtil
|
|
|
|
*/
|
|
|
|
|
|
|
|
class HotkeyHelpController {
|
|
|
|
constructor() {
|
|
|
|
this._hotkeyUtil = new HotkeyUtil();
|
|
|
|
this._localActionHotseys = new Map();
|
|
|
|
this._globalActionHotkeys = new Map();
|
|
|
|
this._replacementPattern = /\{0\}/g;
|
|
|
|
}
|
|
|
|
|
|
|
|
async prepare() {
|
2021-02-14 20:53:35 +00:00
|
|
|
const {platform: {os}} = await yomichan.api.getEnvironmentInfo();
|
2021-01-18 20:23:49 +00:00
|
|
|
this._hotkeyUtil.os = os;
|
|
|
|
await this._setupGlobalCommands(this._globalActionHotkeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
setOptions(options) {
|
|
|
|
const hotkeys = options.inputs.hotkeys;
|
|
|
|
const hotkeyMap = this._localActionHotseys;
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setupNode(node) {
|
|
|
|
const globalPrexix = 'global:';
|
|
|
|
const replacementPattern = this._replacementPattern;
|
|
|
|
for (const node2 of node.querySelectorAll('[data-hotkey]')) {
|
|
|
|
const data = JSON.parse(node2.dataset.hotkey);
|
|
|
|
let [action, attributes, values] = data;
|
|
|
|
if (!Array.isArray(attributes)) { attributes = [attributes]; }
|
|
|
|
const multipleValues = Array.isArray(values);
|
|
|
|
|
|
|
|
const actionIsGlobal = action.startsWith(globalPrexix);
|
|
|
|
if (actionIsGlobal) { action = action.substring(globalPrexix.length); }
|
|
|
|
|
|
|
|
const defaultAttributeValues = this._getDefaultAttributeValues(node2, data, attributes);
|
|
|
|
|
|
|
|
const hotkey = (actionIsGlobal ? this._globalActionHotkeys : this._localActionHotseys).get(action);
|
|
|
|
|
|
|
|
for (let i = 0, ii = attributes.length; i < ii; ++i) {
|
|
|
|
const attribute = attributes[i];
|
|
|
|
let value = null;
|
|
|
|
if (typeof hotkey !== 'undefined') {
|
|
|
|
value = (multipleValues ? values[i] : values);
|
|
|
|
value = value.replace(replacementPattern, hotkey);
|
|
|
|
} else {
|
|
|
|
value = defaultAttributeValues[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof value === 'string') {
|
|
|
|
node2.setAttribute(attribute, value);
|
|
|
|
} else {
|
|
|
|
node2.removeAttribute(attribute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private
|
|
|
|
|
|
|
|
async _setupGlobalCommands(commandMap) {
|
|
|
|
const commands = await new Promise((resolve, reject) => {
|
2021-01-24 19:20:17 +00:00
|
|
|
if (!(isObject(chrome.commands) && typeof chrome.commands.getAll === 'function')) {
|
|
|
|
resolve([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-18 20:23:49 +00:00
|
|
|
chrome.commands.getAll((result) => {
|
|
|
|
const e = chrome.runtime.lastError;
|
|
|
|
if (e) {
|
|
|
|
reject(new Error(e.message));
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
commandMap.clear();
|
|
|
|
for (const {name, shortcut} of commands) {
|
|
|
|
if (shortcut.length === 0) { continue; }
|
|
|
|
const {key, modifiers} = this._hotkeyUtil.convertCommandToInput(shortcut);
|
|
|
|
commandMap.set(name, this._hotkeyUtil.getInputDisplayValue(key, modifiers));
|
|
|
|
}
|
|
|
|
return commandMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
_getDefaultAttributeValues(node, data, attributes) {
|
|
|
|
if (data.length > 3) {
|
|
|
|
return data[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|