karl/view/src/stores/route.ts

22 lines
418 B
TypeScript
Raw Normal View History

2021-03-09 01:13:42 +01:00
import { writable } from "svelte/store";
2021-03-10 01:20:22 +01:00
const getHash = () => {
if (window.location.hash.length) {
return window.location.hash.replace("#", "");
} else {
return "main";
}
2021-03-09 01:13:42 +01:00
}
2021-03-10 01:20:22 +01:00
const store = writable<string>(getHash());
2021-03-09 01:13:42 +01:00
store.subscribe(s => {
if (s === "main") s = ""
window.location.hash = s;
})
2021-03-10 01:20:22 +01:00
window.addEventListener("hashchange", () => {
store.set(getHash())
}, false);
2021-03-09 01:13:42 +01:00
export default store;