Pages, the panes on a phone: the pure parts (pages.ts) with their tests

Which page a scroll rests on, which pane a page swiped to brings forward, a pane's name in the
browser's history (the path of what the panes on the way to it show, so it survives a reload),
and the steps of that history: back or forward to the entry next to it, else a new entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
thalladaandClaude Opus 5.5 committed 2026-11-17 04:02:02 -05:00
1 parent fc7c222407
commit e7c5634a38
2 files changed
+192

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
/**
* The pages layout: the panes view on a phone, and on any window narrower than PANES_MIN_WIDTH
* while the view is `auto`. The forest of forks is the same (forks.ts); what changes is how it is
* shown and moved through. Each column of the panes view is one page, as wide as the screen, with
* its panes stacked down it exactly as on a wide screen (tabs, the sticky tab, pins); the pages lie
* side by side and are swiped like pages, and the page on screen is always the active pane's
* column. A trail at the foot names the focused branch, pane by pane, in its colours; a map lists
* every open pane.
*
* The pure parts are here: which pane a page brings forward when it is swiped to, how a pane is
* named in the browser's history (so Back, the button or the edge swipe, goes back a pane), and the
* steps of that history.
*/
import type { Forest, Tree } from "./forks";
/** Which page a scroll offset rests on: the nearest, among `count`. */
export function pageAt(scrollLeft: number, width: number, count: number): number {
if (count <= 0 || width <= 0) return 0;
return Math.max(0, Math.min(count - 1, Math.round(scrollLeft / width)));
}
/**
* The pane a page brings forward when it is swiped to: the one on the focused branch, else the one
* last on it there (`last`), else the first; undefined for an empty page.
*/
export function paneOfPage(ids: number[], onBranch: Set<number>, last?: number): number | undefined {
return ids.find((id) => onBranch.has(id)) ?? (last !== undefined && ids.includes(last) ? last : ids[0]);
}
/**
* A pane's name in the browser's history: what each pane on the way to it from its tree's root
* shows (siblings never show the same thing, so the path names one pane). Unlike the pane's id it
* survives a reload.
*/
export function paneKey<R, S>(path: Tree<R, S>[], key: (r: R) => string): string {
return JSON.stringify(path.map((n) => key(n.ref)));
}
/** The pane a history key names, in whichever tree it is, or null if it has closed. */
export function findKey<R, S>(f: Forest<R, S>, k: string, key: (r: R) => string): number | null {
let want: unknown;
try {
want = JSON.parse(k);
} catch {
return null;
}
if (!Array.isArray(want) || !want.length || !want.every((x) => typeof x === "string")) return null;
for (const t of f.trees) {
let n: Tree<R, S> | undefined = key(t.ref) === want[0] ? t : undefined;
for (let i = 1; n && i < want.length; i++) n = n.kids.find((c) => key(c.ref) === want[i]);
if (n) return n.id;
}
return null;
}
/**
* The entries of the browser's history this tab made in the pages layout, as far as the page
* knows them: a key per entry, and which one the browser is on.
*/
export interface Visits {
keys: string[];
i: number;
}
/**
* The step the history takes when the active pane becomes the one named `key`: none if it is the
* entry the browser is on; back or forward if it is the entry just before or after (a swipe back
* to the pane a link was followed from is Back, so the browser's Back and Forward stay true);
* otherwise a new entry, which drops whatever was ahead.
*/
export function visit(v: Visits, key: string): { step: "same" | "back" | "forward" | "push"; visits: Visits } {
if (v.keys[v.i] === key) return { step: "same", visits: v };
if (v.i > 0 && v.keys[v.i - 1] === key) return { step: "back", visits: { keys: v.keys, i: v.i - 1 } };
if (v.i + 1 < v.keys.length && v.keys[v.i + 1] === key) return { step: "forward", visits: { keys: v.keys, i: v.i + 1 } };
return { step: "push", visits: { keys: [...v.keys.slice(0, v.i + 1), key], i: v.i + 1 } };
}
/**
* Where the browser has gone by its own Back or Forward (a popstate), to an entry that says it is
* entry `i` named `key`: the visits as they now stand. An entry the page does not know as such (from
* before a reload that lost them, or made elsewhere) starts them again from it.
*/
export function arrived(v: Visits, key: string, i: number): Visits {
if (Number.isInteger(i) && i >= 0 && v.keys[i] === key) return { keys: v.keys, i };
return { keys: [key], i: 0 };
}
/** The visits read back from storage, if they are well formed. */
export function loadVisits(o: unknown): Visits | null {
const v = o as Visits | null;
if (!v || typeof v !== "object" || !Array.isArray(v.keys) || !v.keys.every((k) => typeof k === "string") || !Number.isInteger(v.i) || v.i < 0 || v.i >= v.keys.length) return null;
// Kept short: what lies far back is reached by the browser's own history anyway.
return v.keys.length > 200 ? { keys: v.keys.slice(-200), i: Math.max(0, v.i - (v.keys.length - 200)) } : v;
}