You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
790 B

  1. import { writable } from "svelte/store";
  2. interface SelectionData {
  3. path: string,
  4. hash: string,
  5. }
  6. function createSelectionStore() {
  7. const {update, set, subscribe} = writable<SelectionData>({
  8. path: window.location.pathname,
  9. hash: window.location.hash.slice(1),
  10. });
  11. return {
  12. subscribe,
  13. refresh() {
  14. set({
  15. path: window.location.pathname,
  16. hash: window.location.hash.slice(1),
  17. });
  18. },
  19. change(key: keyof(SelectionData), value: string) {
  20. if (key === "hash") {
  21. window.location.hash = "#" + value;
  22. }
  23. update(d => ({...d, [key]: value}));
  24. },
  25. }
  26. }
  27. const selectionStore = createSelectionStore();
  28. window.addEventListener("hashchange", () => {
  29. selectionStore.refresh();
  30. });
  31. export default selectionStore;