werkstaetten-app/src/lib/server/pageTree.ts
Friederich Loheide bef8574e07 Werkstatt-App: öffentliche PWA und Redaktions-Backend
Werbefreie, barrierearme Info-App für Werkstätten (WfbM) auf Basis von
SvelteKit, Tailwind, SQLite/Drizzle:

- App: Startseite mit Kacheln, Meldungen, Seitenbaum, Termine inkl.
  Schließzeiten und iCal-Abo, Ansprechpartner, Suche, Kontaktformular,
  Merkliste, Vorlesen, Leichte Sprache, Schriftgrößen, Hell/Dunkel, PWA
- Redaktion: Dashboard, Meldungen, Seiten, Termine, Kacheln,
  Ansprechpartner, Mediathek mit Bildoptimierung, Postfach, Benutzer
  mit Rollen, Einstellungen (Name, Logo, Farbe, Rechtstexte)
- Demo-Seed, CLI zum Anlegen von Benutzern, Dockerfile und Compose

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-29 20:55:13 +00:00

56 lines
1.6 KiB
TypeScript

import { asc } from 'drizzle-orm';
import { db } from './db';
import { pages } from './db/schema';
export type TreeRow = {
id: number;
parentId: number | null;
title: string;
slug: string;
icon: string;
color: string;
published: boolean;
depth: number;
first: boolean;
last: boolean;
};
/** Alle Seiten als eingerückte, sortierte Liste (Baum in Lesereihenfolge). */
export function pageTree(): TreeRow[] {
const all = db.select().from(pages).orderBy(asc(pages.sortOrder), asc(pages.title)).all();
const byParent = new Map<number | null, typeof all>();
for (const p of all) {
const key = p.parentId && all.some((x) => x.id === p.parentId) ? p.parentId : null;
byParent.set(key, [...(byParent.get(key) ?? []), p]);
}
const out: TreeRow[] = [];
const walk = (parent: number | null, depth: number) => {
const kids = byParent.get(parent) ?? [];
kids.forEach((p, i) => {
out.push({
id: p.id, parentId: p.parentId, title: p.title, slug: p.slug, icon: p.icon, color: p.color,
published: p.published, depth, first: i === 0, last: i === kids.length - 1
});
if (depth < 12) walk(p.id, depth + 1);
});
};
walk(null, 0);
return out;
}
/** IDs einer Seite und aller Unterseiten (damit man nichts in sich selbst verschiebt). */
export function descendantsOf(id: number): Set<number> {
const tree = pageTree();
const result = new Set([id]);
let changed = true;
while (changed) {
changed = false;
for (const r of tree) {
if (r.parentId && result.has(r.parentId) && !result.has(r.id)) {
result.add(r.id);
changed = true;
}
}
}
return result;
}