Skip to content

Commit

Permalink
Calls to bindAll should by default not save to localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
pihart committed Jun 9, 2021
1 parent 2045748 commit 868b4d5
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/lib/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export default class KeyboardHandler {
}
}

/**
* Writes the map to `localStorage`
*/
save = (): void => {
window.localStorage.setItem("keyMap", JSON.stringify(this.keyMap));
};
Expand All @@ -96,9 +99,13 @@ export default class KeyboardHandler {
window.localStorage.removeItem("keyMap");
};

bindAll = (): void => {
/**
* 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 @@ -110,11 +117,16 @@ 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}
*/
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 Down

0 comments on commit 868b4d5

Please sign in to comment.