From 686cb213d63afc9bc355ea0f4ef7aa4dae78def2 Mon Sep 17 00:00:00 2001 From: Arkerthan <arkerthan@gmail.com> Date: Mon, 19 Oct 2020 20:25:54 +0200 Subject: [PATCH] add hotkeys to move between tabs --- src/002-config/mousetrapConfig.js | 10 ++++++++++ src/js/utilsSC.js | 27 ++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/002-config/mousetrapConfig.js b/src/002-config/mousetrapConfig.js index 48b236a9b47..da2641fb38a 100644 --- a/src/002-config/mousetrapConfig.js +++ b/src/002-config/mousetrapConfig.js @@ -294,6 +294,16 @@ App.UI.Hotkeys.add("next-child", { $("#next-child a.macro-link").trigger("click"); }, combinations: ["right", "e"], uiName: "Next Child" }); +App.UI.Hotkeys.add("Previous Tab", { + callback: function() { + App.UI.tabBar.openLeftTab(); + }, combinations: [] +}); +App.UI.Hotkeys.add("Next Tab", { + callback: function() { + App.UI.tabBar.openRightTab(); + }, combinations: [] +}); App.UI.Hotkeys.add("walkpast", { callback: function() { $("#walkpast a.macro-link").trigger("click"); diff --git a/src/js/utilsSC.js b/src/js/utilsSC.js index aadcaa3f260..ecf2ec5c071 100644 --- a/src/js/utilsSC.js +++ b/src/js/utilsSC.js @@ -14,7 +14,8 @@ globalThis.html5passage = function(passageFunction) { }; /** - * If you want to include a SugarCube passage in a JS function use this. The result must be printed using the <<print>> macro. + * If you want to include a SugarCube passage in a JS function use this. The result must be printed using the <<print>> + * macro. * @param {string} passageTitle * @returns {string} */ @@ -120,7 +121,9 @@ App.UI.tabBar = function() { tabButton: tabButton, makeTab: makeTab, handlePreSelectedTab: handlePreSelectedTab, - tabChoiceVarName: tabChoiceVarName + tabChoiceVarName: tabChoiceVarName, + openLeftTab: openLeft, + openRightTab: openRight }; function openTab(evt, tabName) { @@ -209,11 +212,29 @@ App.UI.tabBar = function() { } function openLeft() { - + const tabLinks = /** @type {HTMLCollection<HTMLButtonElement>} */ document.getElementsByClassName("tab-links"); + const index = currentIndex(tabLinks); + if (index - 1 >= 0) { + tabLinks[index - 1].click(); + } } function openRight() { + const tabLinks = /** @type {HTMLCollection<HTMLButtonElement>} */ document.getElementsByClassName("tab-links"); + const index = currentIndex(tabLinks); + if (index > -1 && index + 1 < tabLinks.length) { + tabLinks[index + 1].click(); + } + } + function currentIndex(collection) { + // get current tab button + for (let i = 0; i < collection.length; i++) { + if (collection[i].classList.contains("active")) { + return i; + } + } + return -1; } }(); -- GitLab