Add webapp subproject (Vite + TypeScript + Web Components)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Christoph K.
2026-04-05 20:25:50 +02:00
parent 37c56e7e3e
commit 07fdd3de31
17 changed files with 542 additions and 0 deletions

39
webapp/src/router.ts Normal file
View File

@@ -0,0 +1,39 @@
type RouteHandler = (params: Record<string, string>) => void
interface Route {
pattern: RegExp
keys: string[]
handler: RouteHandler
}
const routes: Route[] = []
export function route(path: string, handler: RouteHandler): void {
const keys: string[] = []
const pattern = new RegExp(
'^' + path.replace(/:([^/]+)/g, (_: string, k: string) => { keys.push(k); return '([^/]+)' }) + '$'
)
routes.push({ pattern, keys, handler })
}
export function navigate(path: string): void {
history.pushState(null, '', path)
dispatch(path)
}
function dispatch(path: string): void {
for (const r of routes) {
const m = path.match(r.pattern)
if (m) {
const params: Record<string, string> = {}
r.keys.forEach((k, i) => { params[k] = m[i + 1] })
r.handler(params)
return
}
}
}
export function startRouter(): void {
window.addEventListener('popstate', () => dispatch(location.pathname))
dispatch(location.pathname)
}