From b48a41e7597875af86f0f1c4be5df76a79fc6dc7 Mon Sep 17 00:00:00 2001 From: Arkerthan <arkerthan@gmail.com> Date: Mon, 8 Jun 2020 12:57:41 +0200 Subject: [PATCH] add save/load custom hotkeys --- src/002-config/mousetrapConfig.js | 45 ++++++++++++++++++++++++++++-- src/zz1-last/setupEventHandlers.js | 1 + 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/002-config/mousetrapConfig.js b/src/002-config/mousetrapConfig.js index 6c9db111a05..4f4138d57f2 100644 --- a/src/002-config/mousetrapConfig.js +++ b/src/002-config/mousetrapConfig.js @@ -97,7 +97,6 @@ App.UI.Hotkeys = (function() { if (c.length === 0) { return ""; } - // TODO make key combinations look nicer if (c.length === 1) { return `[${formatHotkey(c[0])}]`; } @@ -158,6 +157,7 @@ App.UI.Hotkeys = (function() { } else { button.onclick = () => { action.combinations = [...defaultCombinations[name]]; + saveToStorage(); App.UI.reload(); }; } @@ -206,16 +206,57 @@ App.UI.Hotkeys = (function() { } action.combinations[index] = combination; addBinding(actionKey, combination); + saveToStorage(); App.UI.reload(); recording = false; }); }; } + /** + * Saves custom hotkeys to browser storage + */ + function saveToStorage() { + const save = {}; + + for (const actionsKey in actions) { + if (!isDefault(actionsKey)) { + save[actionsKey] = actions[actionsKey].combinations; + } + } + + SugarCube.storage.set("hotkeys", save); + } + + /** + * Loads custom hotkeys from browser storage + */ + function loadFromStorage() { + const save = SugarCube.storage.get("hotkeys"); + + for (const saveKey in save) { + if (actions[saveKey]) { + actions[saveKey].combinations = save[saveKey]; + } + } + } + + /** + * Initialize custom hotkeys + */ + function init() { + loadFromStorage(); + // :storyready is to late to influence the page, but it's the earliest where SugarCube.storage is available so + // we refresh the passage if we happen to be on the settings passage. + if (State.passage === "Hotkey Settings") { + App.UI.reload(); + } + } + return { add: addDefault, hotkeys: hotkeysForAction, - //init: loadFromStorage, + init: init, settings: settingsMenu, }; })(); diff --git a/src/zz1-last/setupEventHandlers.js b/src/zz1-last/setupEventHandlers.js index 0a6ef416022..593787eae84 100644 --- a/src/zz1-last/setupEventHandlers.js +++ b/src/zz1-last/setupEventHandlers.js @@ -4,6 +4,7 @@ Config.saves.onSave = App.EventHandlers.onSave; $(document).on(':storyready', function() { App.EventHandlers.storyReady(); + App.UI.Hotkeys.init(); }); $(document).one(':passagestart', function() { -- GitLab