-
-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persist user settings to browser local storage (#280)
When the user changes the screen cursor or 'show history' setting, these changes will now persist across browser sessions.
- Loading branch information
Showing
2 changed files
with
62 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
let settings = {}; | ||
try { | ||
settings = JSON.parse(localStorage.getItem("settings")); | ||
} catch {} | ||
|
||
const defaults = { | ||
isKeyHistoryEnabled: true, | ||
cursor: "crosshair", | ||
}; | ||
|
||
// Initialize any undefined settings to their default values. | ||
if (!settings) { | ||
settings = {}; | ||
} | ||
for (const [key, value] of Object.entries(defaults)) { | ||
if (!(key in settings)) { | ||
settings[key] = value; | ||
} | ||
} | ||
|
||
function persistSettings() { | ||
window.localStorage.setItem("settings", JSON.stringify(settings)); | ||
} | ||
|
||
export function enableKeyHistory() { | ||
settings["isKeyHistoryEnabled"] = true; | ||
persistSettings(); | ||
} | ||
|
||
export function disableKeyHistory() { | ||
settings["isKeyHistoryEnabled"] = false; | ||
persistSettings(); | ||
} | ||
|
||
export function isKeyHistoryEnabled() { | ||
return settings["isKeyHistoryEnabled"]; | ||
} | ||
|
||
export function getScreenCursor() { | ||
return settings["cursor"]; | ||
} | ||
|
||
export function setScreenCursor(newCursor) { | ||
settings["cursor"] = newCursor; | ||
persistSettings(); | ||
} |