Same as previous commit
This commit is contained in:
29
src/lib/stores/quick-links.js
Normal file
29
src/lib/stores/quick-links.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { writable } from 'svelte/store'
|
||||
import { browser } from '$app/environment';
|
||||
|
||||
export const ql = writable(fromLocalStorage('ql', []))
|
||||
toLocalStorage(ql, 'ql');
|
||||
|
||||
function fromLocalStorage(storageKey, fallback) {
|
||||
if (browser) {
|
||||
const storedValue = localStorage.getItem(storageKey);
|
||||
if (storedValue !== 'undefined' && storedValue !== null) {
|
||||
return (typeof fallback === 'object')
|
||||
? JSON.parse(storedValue)
|
||||
: storedValue
|
||||
}
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
function toLocalStorage(store, storageKey) {
|
||||
if (browser) {
|
||||
store.subscribe(value => {
|
||||
let storageValue = (typeof value === 'object')
|
||||
? JSON.stringify(value)
|
||||
: value
|
||||
|
||||
localStorage.setItem(storageKey, storageValue)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user