This commit is contained in:
nub31
2026-03-17 17:53:17 +01:00
parent ceb05f4a75
commit 73c1343b0d
10 changed files with 61 additions and 167 deletions

View File

@@ -5,7 +5,6 @@
export class RouteError extends Error {}
export class MissingRouteError extends RouteError {}
export class RouteFormatError extends RouteError {}
export class DuplicateRouteError extends RouteError {}
type RouteInfo = {
path: string;
@@ -67,7 +66,8 @@
*/
export function route(path: string, component: Component<{}>): void {
if (routes.has(path)) {
throw new DuplicateRouteError(`Route already registered for path: '${path}'.`);
console.warn(`Route already registered for path: '${path}'.`);
return;
}
const params = Array.from(path.matchAll(/\[([^\]]+)\]/g)).map((x) => x[1]);
@@ -179,53 +179,6 @@
// @ts-ignore
window.navigation.removeEventListener("navigate", handleNavigate);
};
} else {
function refresh() {
currentUrl = new URL(window.location.href);
}
function makeProxy(target: any) {
return new Proxy(target, {
apply: (target, thisArg, argArray) => {
const result = target.apply(thisArg, argArray);
refresh();
return result;
}
});
}
window.history.pushState = makeProxy(window.history.pushState);
window.history.replaceState = makeProxy(window.history.replaceState);
window.history.go = makeProxy(window.history.go);
window.history.forward = makeProxy(window.history.forward);
window.history.back = makeProxy(window.history.back);
function handleClick(e: MouseEvent) {
if (e.ctrlKey || e.shiftKey || e.altKey || e.metaKey) {
return;
}
const anchor = (e.target as Element)?.closest("a");
if (!(anchor instanceof HTMLAnchorElement)) return;
if (anchor.target === "_blank") return;
const targetUrl = new URL(anchor.href, document.baseURI);
const documentUrl = new URL(document.baseURI);
if (targetUrl.origin === documentUrl.origin) {
e.preventDefault();
history.pushState({}, "", targetUrl);
currentUrl = targetUrl;
}
}
window.addEventListener("popstate", refresh);
document.addEventListener("click", handleClick);
return () => {
window.removeEventListener("popstate", refresh);
document.removeEventListener("click", handleClick);
};
}
});
</script>

View File

@@ -1,6 +1,6 @@
import homeIcon from "../../assets/icons/home_icon.svg";
import contactIcon from "../../assets/icons/contact_icon.svg";
import busIcon from "../../assets/icons/bus_icon.svg";
import homeIcon from "../assets/icons/home_icon.svg";
import contactIcon from "../assets/icons/contact_icon.svg";
import busIcon from "../assets/icons/bus_icon.svg";
type Route = {
url: string;

View File

@@ -1,36 +0,0 @@
import { writable } from "svelte/store";
function createThemeToggler() {
let defaultValue = false;
let savedValue = localStorage.getItem("dark_theme");
if (savedValue) {
defaultValue = JSON.parse(savedValue);
} else if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
defaultValue = true;
}
if (defaultValue) {
document.body.dataset.theme = "dark";
} else {
document.body.dataset.theme = "light";
}
const { subscribe, update } = writable(defaultValue);
return {
subscribe,
set: (value: boolean) => {
update(() => value);
if (value) {
document.body.dataset.theme = "dark";
} else {
document.body.dataset.theme = "light";
}
localStorage.setItem("dark_theme", JSON.stringify(value));
}
};
}
export const darkTheme = createThemeToggler();

24
src/lib/theme.svelte.ts Normal file
View File

@@ -0,0 +1,24 @@
function loadTheme() {
let theme = localStorage.getItem("theme");
if (!theme) {
if (window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches) {
theme = "dark";
} else {
theme = "light";
}
}
return theme;
}
let currentTheme = $state(loadTheme());
export function toggleTheme() {
const newTheme = currentTheme === "dark" ? "light" : "dark";
localStorage.setItem("theme", newTheme);
currentTheme = newTheme;
}
export function getTheme(): string {
return currentTheme;
}

View File

@@ -1,8 +0,0 @@
export function debounce<T extends Function>(cb: T, wait = 500) {
let h: any;
let callable = () => {
clearTimeout(h);
h = setTimeout(() => cb(), wait);
};
return callable;
}