293 lines
9.9 KiB
React
293 lines
9.9 KiB
React
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 (
|
|
<div className={classes.join(" ")}>
|
|
{showNav ? <div className={overlay ? "workspace-nav is-overlay" : "workspace-nav"}>{nav}</div> : null}
|
|
{overlay ? (
|
|
<button type="button" className="workspace-scrim" aria-label="Struktur schließen" onClick={() => setStructureOpen(false)} />
|
|
) : null}
|
|
{showList ? (
|
|
<div className="workspace-main" ref={listRef}>
|
|
{children}
|
|
</div>
|
|
) : null}
|
|
{showReader ? <div className="workspace-reader">{reader}</div> : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
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 = <NavIcon name={structureIcon(node.kind)} />;
|
|
const name = node.path ? `${node.kind}: ${node.path}` : `${node.kind}: ${node.title}`;
|
|
const label = (
|
|
<span className="nav-tree__title">
|
|
{node.title}
|
|
{node.kind === "Kapitel" || node.kind === "Unterkapitel" || node.kind === "Teilvorgang" ? (
|
|
<span className="nav-tree__kind">{node.kind}</span>
|
|
) : null}
|
|
</span>
|
|
);
|
|
let row;
|
|
if (node.onSelect) {
|
|
row = (
|
|
<button type="button" className={className} aria-current={node.current ? "true" : undefined} aria-label={name} title={name} onClick={() => { node.onSelect(); onNavigate?.(); }}>
|
|
{icon}
|
|
{label}
|
|
</button>
|
|
);
|
|
} else if (node.href) {
|
|
row = (
|
|
<Link className={className} to={node.href} aria-current={node.current ? "page" : undefined} aria-label={name} title={name} onClick={onNavigate}>
|
|
{icon}
|
|
{label}
|
|
</Link>
|
|
);
|
|
} else {
|
|
row = (
|
|
<span className={className} title={name}>
|
|
{icon}
|
|
{label}
|
|
</span>
|
|
);
|
|
}
|
|
const moves = node.moveUp !== undefined || node.moveDown !== undefined ? (
|
|
<span className="chapter-link__move">
|
|
<button type="button" aria-label={`${node.title} nach oben`} disabled={!node.moveUp} onClick={node.moveUp}>↑</button>
|
|
<button type="button" aria-label={`${node.title} nach unten`} disabled={!node.moveDown} onClick={node.moveDown}>↓</button>
|
|
</span>
|
|
) : null;
|
|
return (
|
|
<div className="nav-tree__line">
|
|
{toggle}
|
|
{row}
|
|
{moves}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function TreeNodes({ nodes, onNavigate, unfold = false, isOpen, onToggle }) {
|
|
if (!nodes?.length) return null;
|
|
return (
|
|
<ul className="nav-tree">
|
|
{nodes.map((node) => {
|
|
const hasChildren = Boolean(node.children?.length);
|
|
const open = hasChildren && isOpen(node, unfold);
|
|
const toggle = hasChildren ? (
|
|
<button
|
|
type="button"
|
|
className="nav-tree__toggle"
|
|
aria-expanded={open}
|
|
aria-label={open ? `${node.title} einklappen` : `${node.title} ausklappen`}
|
|
onClick={() => onToggle(node, open)}
|
|
>
|
|
{open ? "▾" : "▸"}
|
|
</button>
|
|
) : (
|
|
<span className="nav-tree__toggle is-empty" aria-hidden="true" />
|
|
);
|
|
return (
|
|
<li key={node.id}>
|
|
<TreeRow node={node} onNavigate={onNavigate} ancestor={!node.current && branchHasCurrent(node)} toggle={toggle} />
|
|
{open ? (
|
|
<TreeNodes nodes={node.children} onNavigate={onNavigate} unfold={unfold || node.current} isOpen={isOpen} onToggle={onToggle} />
|
|
) : null}
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<nav className="structure" aria-label="Struktur">
|
|
<div className="structure__bar">
|
|
<h2>Struktur</h2>
|
|
{onClose ? (
|
|
<button type="button" className="icon-button" aria-label="Struktur schließen" onClick={onClose}>
|
|
<NavIcon name="close" />
|
|
</button>
|
|
) : null}
|
|
</div>
|
|
<div className="structure__tree">
|
|
<TreeNodes nodes={tree} onNavigate={onNavigate} isOpen={isOpen} onToggle={toggle} />
|
|
{links.length ? (
|
|
<div className="structure__links">
|
|
<h3>Weitere Zuordnungen</h3>
|
|
<TreeNodes nodes={links} onNavigate={onNavigate} unfold isOpen={() => true} onToggle={() => {}} />
|
|
</div>
|
|
) : null}
|
|
{canAddChapter ? (
|
|
<button type="button" className="quiet chapter-add" onClick={onAddChapter}>
|
|
<NavIcon name="plus" /> {chapterId ? "Unterkapitel" : "Kapitel"}
|
|
</button>
|
|
) : null}
|
|
{canAddChapter && chapterId && onAddRootChapter ? (
|
|
<button type="button" className="quiet chapter-add" onClick={onAddRootChapter}>
|
|
<NavIcon name="plus" /> Kapitel auf oberster Ebene
|
|
</button>
|
|
) : null}
|
|
</div>
|
|
</nav>
|
|
);
|
|
}
|
|
|
|
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);
|
|
}
|