import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import { chapterPathLabel } from "./chapters.js"; import { NavIcon } from "./icons.jsx"; export function useWorkspaceLayout() { const [width, setWidth] = useState(() => (typeof window === "undefined" ? 1280 : window.innerWidth)); const [structureOpen, setStructureOpen] = useState(false); useEffect(() => { const onResize = () => setWidth(window.innerWidth); window.addEventListener("resize", onResize); return () => window.removeEventListener("resize", onResize); }, []); return { width, structureOpen, setStructureOpen }; } export function Workspace({ width, structureOpen, setStructureOpen, hasEntry, nav, reader, listRef, children }) { const phone = width < 760; const navColumn = width >= 1500; const split = width >= 1180 && hasEntry; const showNav = phone ? structureOpen && !hasEntry : navColumn || structureOpen; const showList = phone ? !structureOpen && !hasEntry : !(hasEntry && width < 1180); const showReader = hasEntry && (phone ? !structureOpen : true); const overlay = showNav && !phone && !navColumn; useEffect(() => { document.body.classList.toggle("is-phone-reading", phone && hasEntry && !structureOpen); return () => document.body.classList.remove("is-phone-reading"); }, [phone, hasEntry, structureOpen]); const classes = ["workspace"]; if (phone && structureOpen && !hasEntry) classes.push("is-structure"); else if (!showNav || overlay) classes.push("is-nav-hidden"); if (split && showList && showReader) classes.push("is-split"); if (!showList && showReader) classes.push("is-reader-focus"); return (
{showNav ?
{nav}
: null} {overlay ? (
); } function structureIcon(kind) { if (kind === "Akte" || kind === "Unterakte") return "folder"; if (kind === "Kapitel" || kind === "Unterkapitel") return "chapter"; if (kind === "Inhalte") return "contents"; if (kind === "Querverweis" || kind === "Weitere Akte") return "link"; return "layers"; } function branchHasCurrent(node) { if (node.current) return true; return (node.children || []).some(branchHasCurrent); } function TreeRow({ node, onNavigate, ancestor = false, toggle = null }) { const className = node.current ? "nav-tree__link is-current" : ancestor ? "nav-tree__link is-ancestor" : "nav-tree__link"; const icon = ; const name = node.path ? `${node.kind}: ${node.path}` : `${node.kind}: ${node.title}`; const label = ( {node.title} {node.kind === "Kapitel" || node.kind === "Unterkapitel" || node.kind === "Teilvorgang" ? ( {node.kind} ) : null} ); let row; if (node.onSelect) { row = ( ); } else if (node.href) { row = ( {icon} {label} ); } else { row = ( {icon} {label} ); } const moves = node.moveUp !== undefined || node.moveDown !== undefined ? ( ) : null; return (
{toggle} {row} {moves}
); } function TreeNodes({ nodes, onNavigate, unfold = false, isOpen, onToggle }) { if (!nodes?.length) return null; return ( ); } function scopeContents({ chapters, chapterId, allLabel, overview, onChapter, canReorder, onMoveChapter }) { const items = []; if (overview) { items.push({ id: "overview", title: "Überblick", kind: "Inhalte", current: Boolean(overview.active), onSelect: overview.onSelect, }); } items.push({ id: "all-contents", title: allLabel, kind: "Inhalte", current: !chapterId && !overview?.active, onSelect: () => onChapter?.(""), }); const byParent = new Map(); chapters.forEach((chapter) => { const key = chapter.parent_chapter_id || ""; const list = byParent.get(key) || []; list.push(chapter); byParent.set(key, list); }); function chapterNodes(parentId) { return (byParent.get(parentId) || []).map((chapter, index, siblings) => ({ id: chapter.id, title: chapter.title, path: chapterPathLabel(chapters, chapter.id), kind: parentId ? "Unterkapitel" : "Kapitel", current: chapterId === chapter.id, onSelect: () => onChapter?.(chapter.id), children: chapterNodes(chapter.id), ...(canReorder ? { moveUp: index > 0 ? () => onMoveChapter(chapter.id, -1) : null, moveDown: index < siblings.length - 1 ? () => onMoveChapter(chapter.id, 1) : null, } : {}), })); } items.push(...chapterNodes("")); return items; } function attachScope(nodes, extras) { return (nodes || []).map((node) => { const children = attachScope(node.children, extras); if (!node.current) return { ...node, children }; return { ...node, children: [...extras, ...children] }; }); } function treeWithScope(nodes, extras) { const attached = attachScope(nodes, extras); if (attached.some(branchHasCurrent)) return attached; return [...extras, ...attached]; } export function StructureNavigator({ nodes = [], chapters = [], chapterId = "", allLabel = "Alle Inhalte", overview = null, onChapter, onAddChapter, onAddRootChapter, canAddChapter = false, onMoveChapter, canReorder = false, links = [], onNavigate, onClose, }) { const [opened, setOpened] = useState(() => new Set()); const [closed, setClosed] = useState(() => new Set()); const tree = treeWithScope(nodes, scopeContents({ chapters, chapterId, allLabel, overview, onChapter, canReorder, onMoveChapter })); function isOpen(node, unfold) { if (!node.children?.length) return false; if (closed.has(node.id)) return false; if (opened.has(node.id) || unfold || node.current || branchHasCurrent(node)) return true; return false; } function toggle(node, currently) { setOpened((current) => { const next = new Set(current); if (currently) next.delete(node.id); else next.add(node.id); return next; }); setClosed((current) => { const next = new Set(current); if (currently) next.add(node.id); else next.delete(node.id); return next; }); } return ( ); } export function rememberScroll(node, key) { if (node && key) sessionStorage.setItem(key, String(node.scrollTop)); } export function restoreScroll(node, key) { if (!node || !key) return; const saved = sessionStorage.getItem(key); if (saved) node.scrollTop = Number(saved); }