-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b490ff7
commit baf7d08
Showing
12 changed files
with
142 additions
and
398 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,36 @@ | ||
import { ref, onActivated, watchEffect } from 'vue'; | ||
|
||
// The global localStorage prefix | ||
const prefix = 'running-tools'; | ||
|
||
/* | ||
* Create a reactive value that is synced with a localStorage item | ||
* @param {String} key The localStorage item's key | ||
* @defaultValue {Object} defaultValue The default value | ||
*/ | ||
export default function useStorage(key, defaultValue) { | ||
const clonedDefault = JSON.parse(JSON.stringify(defaultValue)); | ||
const value = ref(clonedDefault); | ||
|
||
// (Re)load value from localStorage | ||
function updateValue() { | ||
let parsedValue; | ||
try { | ||
parsedValue = JSON.parse(localStorage.getItem(`${prefix}.${key}`)); | ||
} catch { | ||
parsedValue = null; | ||
} | ||
if (parsedValue !== null) value.value = parsedValue; | ||
} | ||
updateValue(); | ||
onActivated(updateValue); | ||
|
||
// Save value to localStorage when modified | ||
watchEffect(() => { | ||
if (typeof localStorage !== 'undefined') { | ||
localStorage.setItem(`${prefix}.${key}`, JSON.stringify(value.value)); | ||
} | ||
}) | ||
|
||
return value | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.