diff --git a/src/002-config/mousetrapConfig.js b/src/002-config/mousetrapConfig.js index 6c9db111a05669a32c20ea70a9c958b3ddd167fc..4f4138d57f237be2f4f9e62738561a5ad76226c3 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 0a6ef4160227ad480de47f3b895c24cc870cd2ba..593787eae840212bd4fdbfc1beb51a72738348e5 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() {