/**
* What the pages layout adds to the panes (pages.ts): the trail at the foot of the screen, where a
* thumb reaches, naming the focused branch pane by pane in each pane's colour, the one on screen
* marked; a tap on a step goes to that pane. Its last button opens the map: every open pane, as an
* outline of the forest of forks, each with its colour, to go to, pin or close.
*/
import { useLayoutEffect, useRef } from "preact/hooks";
import { label } from "../core/model";
import { store, type PaneRef } from "./store";
import { article } from "./wiki";
/** What a pane is called: its node's title, or its article's (a section of one by its heading). */
export function paneTitle(p: PaneRef): string {
if (p.kind === "node") return store.snap.nodes[p.id] ? label(store.snap, p.id) : "…";
const a = article(p.title, p.lang);
const sec = p.sec !== undefined && a.state === "ready" ? a.value.sections.find((x) => x.id === p.sec) : undefined;
return sec ? sec.heading : a.state === "ready" ? a.value.title : p.title;
}
const W = () => W;
/** The trail: the focused branch, a step per pane, and the map's button. */
export function Trail() {
const ref = useRef(null);
const nodes = store.branchNodes;
const at = store.paneAt;
const here = nodes[at]?.id;
// The step on screen is kept in view along the trail (only when it changes, never as anything scrolls).
useLayoutEffect(() => {
const row = ref.current;
const el = row?.querySelector(".trail-step.here");
if (!row || !el) return;
const l = el.offsetLeft - row.offsetLeft;
if (l < row.scrollLeft + 8) row.scrollLeft = Math.max(0, l - 24);
else if (l + el.offsetWidth > row.scrollLeft + row.clientWidth - 8) row.scrollLeft = l + el.offsetWidth - row.clientWidth + 24;
}, [here, nodes.length]);
const count = store.panesPlaced.length;
return (
);
}
const Pin = () => (
);
const Close = () => (
);
/**
* The map: every open pane, tree by tree, each indented by its column under the pane it was
* opened from, with its colour; the active pane marked, the focused branch in the text's ink. A
* tap on a pane goes to it; its pin and × act on it where it is. A press outside, Esc or the
* trail's map button puts it away.
*/
export function PaneMap() {
const ref = useRef(null);
const on = store.paneMap && store.pages;
useLayoutEffect(() => {
if (!on) return;
ref.current?.querySelector(".map-row.here")?.scrollIntoView({ block: "nearest" });
const outside = (e: PointerEvent) => {
const t = e.target as HTMLElement;
if (ref.current?.contains(t) || t.closest?.("[data-map-open]")) return;
store.showMap(false);
// The press that puts it away does nothing else.
const swallow = (c: MouseEvent) => (c.stopPropagation(), c.preventDefault());
window.addEventListener("click", swallow, { capture: true, once: true });
setTimeout(() => window.removeEventListener("click", swallow, { capture: true }), 800);
};
document.addEventListener("pointerdown", outside, true);
return () => document.removeEventListener("pointerdown", outside, true);
}, [on]);
if (!on) return null;
const placed = store.panesPlaced;
const branch = new Set(store.branchNodes.map((n) => n.id));
const active = store.branchNodes[store.paneAt]?.id;
return (