From 87ce4b934815e50a1692e6292511d0dd02dcd2ee Mon Sep 17 00:00:00 2001 From: Bellisario <72039923+Bellisario@users.noreply.github.com> Date: Mon, 1 Jul 2024 21:48:08 +0200 Subject: [PATCH] feat: use svelte-hash --- package-lock.json | 10 ++++++++ package.json | 1 + src/lib/player.ts | 65 +++++------------------------------------------ 3 files changed, 17 insertions(+), 59 deletions(-) diff --git a/package-lock.json b/package-lock.json index de39392..8333266 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "postcss-logical": "^7.0.1", "svelte": "^4.2.18", "svelte-check": "^3.8.4", + "svelte-hash": "^1.0.1", "svelte-preprocess": "^6.0.1", "tslib": "^2.6.3", "typescript": "^5.5.2", @@ -5368,6 +5369,15 @@ } } }, + "node_modules/svelte-hash": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/svelte-hash/-/svelte-hash-1.0.1.tgz", + "integrity": "sha512-zVowHApGWcYecOU1X6ZKzT8cA3PuOWQbjPJPps8j68z82BoZ5zSP3Cnq3Oww8XaNKl+PeiJDBlm3na4qOEmQHg==", + "dev": true, + "peerDependencies": { + "svelte": ">= 4.0.0" + } + }, "node_modules/svelte-hmr": { "version": "0.16.0", "resolved": "https://registry.npmjs.org/svelte-hmr/-/svelte-hmr-0.16.0.tgz", diff --git a/package.json b/package.json index 7811343..0058324 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "postcss-logical": "^7.0.1", "svelte": "^4.2.18", "svelte-check": "^3.8.4", + "svelte-hash": "^1.0.1", "svelte-preprocess": "^6.0.1", "tslib": "^2.6.3", "typescript": "^5.5.2", diff --git a/src/lib/player.ts b/src/lib/player.ts index 032f906..ce53527 100644 --- a/src/lib/player.ts +++ b/src/lib/player.ts @@ -1,7 +1,9 @@ -import { derived, get, writable } from 'svelte/store'; +import { derived, writable } from 'svelte/store'; import type { FavoriteStore } from '$types/FavoritesStore'; import { type MenuEntry } from '$types/MenuEntry'; +import { createHashStore } from 'svelte-hash'; + export const duration = writable(0); export const currentTime = writable(0); export const paused = writable(true); @@ -92,63 +94,8 @@ export function shuffle(array: T[]): T[] { return newArray; } - -// ------------------------------------------------------------ -// HASH Management -// ------------------------------------------------------------ - interface Hash { - search?: string; - album?: string; - [key: string]: string | undefined; -} - -export const hash = writable(loadHash()); - -function updateHash() { - - const hashValues = get(hash); - - // if hash is set to an empty object, remove it - if (Object.keys(hashValues).length === 0) return window.history.pushState(null, '', '#'); - - const urlHash = new URLSearchParams(window.location.hash.slice(1)); - - // get keys - Object.keys(hashValues).forEach((key) => { - // get value - const value = hashValues[key]; - - // if value is empty, return - if (!value) return urlHash.delete(key); - - // set value - urlHash.set(key, value); - }); - - if (window.location.hash.slice(1) === urlHash.toString()) return; - - // set hash - window.history.pushState(null, '', `#${urlHash.toString()}`); -} - -function loadHash() { - const urlHash = new URLSearchParams(window.location.hash.slice(1)); - - const hashValues: Hash = {}; - - // get keys - urlHash.forEach((value, key) => { - // set value - hashValues[key] = value; - }); - - return hashValues; + search: string; + album: string; } - -hash.subscribe(updateHash); - -// listen for history changes -window.addEventListener('popstate', () => { - hash.set(loadHash()); -}); +export const hash = createHashStore();