Skip to content

Commit

Permalink
Merge pull request #174 from pihart/clear-localstorage-keymap-on-rese…
Browse files Browse the repository at this point in the history
…t-172

Clear localStorage keyMap on reset
  • Loading branch information
pihart authored Jun 13, 2021
2 parents 8b0fa46 + 629a724 commit 50fc656
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/lib/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,27 @@ export default class KeyboardHandler {
}
}

/**
* Writes the map to `localStorage`
*/
save = (): void => {
window.localStorage.setItem("keyMap", JSON.stringify(this.keyMap));
};

bindAll = (): void => {
/**
* Removes the map from `localStorage`
*/
clear = (): void => {
window.localStorage.removeItem("keyMap");
};

/**
* Wrapper to bind every binding defined in `this.keyMap`,
* writing to `localStorage` if {@param save}
*/
bindAll = (save = false): void => {
for (const [key, value] of Object.entries(this.keyMap)) {
this.bind(key, value);
this.bind(key, value, save);
}
this.updateState();
};
Expand All @@ -103,11 +117,17 @@ export default class KeyboardHandler {
this.save();
};

bind = (key: string, action: Action): void => {
/**
* Creates a keyboard.js binding from the key combination {@param key} to the action {@param action},
* writing to `localStorage` if {@param save}.
* The binding reacts only to the `press` event and not the `release` event.
*/
bind = (key: string, action: Action, save = true): void => {
this.keyMap[key] = action;
keyboardJS.bind(key, () => this.doAction(this.keyMap[key]));
this.updateState();
this.save();

if (save) this.save();
};

reset = (): void => {
Expand All @@ -116,6 +136,6 @@ export default class KeyboardHandler {
}
this.keyMap = { ...defaultKeys };
this.bindAll();
this.save();
this.clear();
};
}

0 comments on commit 50fc656

Please sign in to comment.