aboutsummaryrefslogtreecommitdiff
path: root/ext/mixed/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-01-18 15:23:49 -0500
committerGitHub <noreply@github.com>2021-01-18 15:23:49 -0500
commitcbc5ab5696a64fe10d5a37d25cc6e87d8d44481f (patch)
tree1b4de6a6176926b1c2251de291264b33de67eb2c /ext/mixed/js
parent0420a29d77e7e91a87f8f70623e1dff067640fd8 (diff)
Hotkey help controller (#1272)
* Create HotkeyHelpController * Expose getAllTemplates * Update hotkeys on the context page * Update hotkeys on the display pages * Fix display issue where a hotkey used to be defined but no longer is
Diffstat (limited to 'ext/mixed/js')
-rw-r--r--ext/mixed/js/display-generator.js11
-rw-r--r--ext/mixed/js/display.js9
-rw-r--r--ext/mixed/js/hotkey-help-controller.js120
-rw-r--r--ext/mixed/js/html-template-collection.js4
4 files changed, 142 insertions, 2 deletions
diff --git a/ext/mixed/js/display-generator.js b/ext/mixed/js/display-generator.js
index b48ddadc..0102afbc 100644
--- a/ext/mixed/js/display-generator.js
+++ b/ext/mixed/js/display-generator.js
@@ -22,9 +22,10 @@
*/
class DisplayGenerator {
- constructor({japaneseUtil, mediaLoader}) {
+ constructor({japaneseUtil, mediaLoader, hotkeyHelpController}) {
this._japaneseUtil = japaneseUtil;
this._mediaLoader = mediaLoader;
+ this._hotkeyHelpController = hotkeyHelpController;
this._templates = null;
this._termPitchAccentStaticTemplateIsSetup = false;
}
@@ -32,6 +33,14 @@ class DisplayGenerator {
async prepare() {
const html = await api.getDisplayTemplatesHtml();
this._templates = new HtmlTemplateCollection(html);
+ this.updateHotkeys();
+ }
+
+ updateHotkeys() {
+ const hotkeyHelpController = this._hotkeyHelpController;
+ for (const template of this._templates.getAllTemplates()) {
+ hotkeyHelpController.setupNode(template.content);
+ }
}
preparePitchAccents() {
diff --git a/ext/mixed/js/display.js b/ext/mixed/js/display.js
index a9d59aff..6af35074 100644
--- a/ext/mixed/js/display.js
+++ b/ext/mixed/js/display.js
@@ -24,6 +24,7 @@
* DocumentUtil
* FrameEndpoint
* Frontend
+ * HotkeyHelpController
* MediaLoader
* PopupFactory
* QueryParser
@@ -50,9 +51,11 @@ class Display extends EventDispatcher {
this._eventListeners = new EventListenerCollection();
this._setContentToken = null;
this._mediaLoader = new MediaLoader();
+ this._hotkeyHelpController = new HotkeyHelpController();
this._displayGenerator = new DisplayGenerator({
japaneseUtil,
- mediaLoader: this._mediaLoader
+ mediaLoader: this._mediaLoader,
+ hotkeyHelpController: this._hotkeyHelpController
});
this._messageHandlers = new Map();
this._directMessageHandlers = new Map();
@@ -201,6 +204,7 @@ class Display extends EventDispatcher {
this._browser = browser;
// Prepare
+ await this._hotkeyHelpController.prepare();
await this._displayGenerator.prepare();
this._displayAudio.prepare();
this._queryParser.prepare();
@@ -285,6 +289,9 @@ class Display extends EventDispatcher {
this._updateTheme(options.general.popupTheme);
this.setCustomCss(options.general.customPopupCss);
this._displayAudio.updateOptions(options);
+ this._hotkeyHelpController.setOptions(options);
+ this._displayGenerator.updateHotkeys();
+ this._hotkeyHelpController.setupNode(document.documentElement);
this._queryParser.setOptions({
selectedParser: options.parsing.selectedParser,
diff --git a/ext/mixed/js/hotkey-help-controller.js b/ext/mixed/js/hotkey-help-controller.js
new file mode 100644
index 00000000..032573c1
--- /dev/null
+++ b/ext/mixed/js/hotkey-help-controller.js
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2021 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 <http://www.gnu.org/licenses/>.
+ */
+
+/* global
+ * HotkeyUtil
+ * api
+ */
+
+class HotkeyHelpController {
+ constructor() {
+ this._hotkeyUtil = new HotkeyUtil();
+ this._localActionHotseys = new Map();
+ this._globalActionHotkeys = new Map();
+ this._replacementPattern = /\{0\}/g;
+ }
+
+ async prepare() {
+ const {platform: {os}} = await api.getEnvironmentInfo();
+ 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) => {
+ 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;
+ }
+}
diff --git a/ext/mixed/js/html-template-collection.js b/ext/mixed/js/html-template-collection.js
index 65b316c8..52d5f3b0 100644
--- a/ext/mixed/js/html-template-collection.js
+++ b/ext/mixed/js/html-template-collection.js
@@ -44,6 +44,10 @@ class HtmlTemplateCollection {
return document.importNode(template.content, true);
}
+ getAllTemplates() {
+ return this._templates.values();
+ }
+
// Private
_prepareTemplate(template) {