diff --git a/src/ui/pages.ts b/src/ui/pages.ts new file mode 100644 index 0000000..8fabbc0 --- /dev/null +++ b/src/ui/pages.ts @@ -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, 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(path: Tree[], 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(f: Forest, 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 | 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; +} diff --git a/tests/pages.test.ts b/tests/pages.test.ts new file mode 100644 index 0000000..605bc66 --- /dev/null +++ b/tests/pages.test.ts @@ -0,0 +1,98 @@ +import { describe, expect, it } from "vitest"; +import { close, open, plain, trace, type Forest } from "../src/ui/forks"; +import { arrived, findKey, loadVisits, pageAt, paneKey, paneOfPage, visit, type Visits } from "../src/ui/pages"; + +const same = (a: string, b: string) => a === b; +const k = (r: string) => r; + +describe("the pages layout", () => { + it("rests on the nearest page, within the pages there are", () => { + expect(pageAt(0, 390, 3)).toBe(0); + expect(pageAt(194, 390, 3)).toBe(0); + expect(pageAt(196, 390, 3)).toBe(1); + expect(pageAt(780, 390, 3)).toBe(2); + expect(pageAt(5000, 390, 3)).toBe(2); + expect(pageAt(-40, 390, 3)).toBe(0); + expect(pageAt(100, 0, 3)).toBe(0); + expect(pageAt(100, 390, 0)).toBe(0); + }); + + it("a page swiped to brings forward its pane on the focused branch, else the one last there, else the first", () => { + expect(paneOfPage([4, 5, 6], new Set([1, 5]), 6)).toBe(5); + expect(paneOfPage([4, 5, 6], new Set([1]), 6)).toBe(6); + expect(paneOfPage([4, 5, 6], new Set([1]), 9)).toBe(4); + expect(paneOfPage([4, 5, 6], new Set([1]))).toBe(4); + expect(paneOfPage([], new Set([1]))).toBeUndefined(); + }); + + it("names a pane by what the panes on the way to it show, and finds it again, in any tree; a closed one is not found", () => { + let f: Forest = plain(["top", "a"]); + f = open(f, 1, "b", false, same); + f = open(f, 0, "c", true, same); + const b = f.trees[0].kids[0].kids[0]; + const c = f.trees[0].kids[1]; + const kb = paneKey(trace(f.trees[0], b.id)!, k); + const kc = paneKey(trace(f.trees[0], c.id)!, k); + expect(kb).toBe(JSON.stringify(["top", "a", "b"])); + expect(findKey(f, kb, k)).toBe(b.id); + expect(findKey(f, kc, k)).toBe(c.id); + expect(findKey(f, JSON.stringify(["top"]), k)).toBe(f.trees[0].id); + expect(findKey(f, JSON.stringify(["top", "x"]), k)).toBeNull(); + expect(findKey(f, "not json", k)).toBeNull(); + expect(findKey(f, JSON.stringify([]), k)).toBeNull(); + expect(findKey(f, JSON.stringify([1, 2]), k)).toBeNull(); + // Closed: gone; and an orphan (pinned, its source closed) is found in its own tree. + const g = close(f, f.trees[0].kids[0].id, b.id).forest; + expect(findKey(g, kb, k)).toBeNull(); + let h = plain(["top", "a", "b"]); + const pb = h.trees[0].kids[0].kids[0]; + h = { ...h, trees: [{ ...h.trees[0], kids: [{ ...h.trees[0].kids[0], kids: [{ ...pb, pinned: true }] }] }] }; + const o = close(h, h.trees[0].kids[0].id, pb.id).forest; + expect(o.trees).toHaveLength(2); + expect(findKey(o, JSON.stringify(["b"]), k)).toBe(o.trees[1].id); + }); + + it("steps the history: back to the entry before, forward to the one after, else a new entry that drops what was ahead", () => { + let v: Visits = { keys: ["A"], i: 0 }; + let r = visit(v, "A"); + expect(r.step).toBe("same"); + expect(r.visits).toBe(v); + r = visit(v, "B"); + expect(r).toEqual({ step: "push", visits: { keys: ["A", "B"], i: 1 } }); + v = visit(r.visits, "C").visits; + expect(v).toEqual({ keys: ["A", "B", "C"], i: 2 }); + r = visit(v, "B"); + expect(r).toEqual({ step: "back", visits: { keys: ["A", "B", "C"], i: 1 } }); + r = visit(r.visits, "C"); + expect(r).toEqual({ step: "forward", visits: { keys: ["A", "B", "C"], i: 2 } }); + // Two back is not adjacent: a new entry. + r = visit({ keys: ["A", "B", "C"], i: 2 }, "A"); + expect(r).toEqual({ step: "push", visits: { keys: ["A", "B", "C", "A"], i: 3 } }); + // From the middle, somewhere new: what was ahead goes. + r = visit({ keys: ["A", "B", "C"], i: 1 }, "D"); + expect(r).toEqual({ step: "push", visits: { keys: ["A", "B", "D"], i: 2 } }); + // At the first entry, the pane before it is not Back (nothing is known before it). + expect(visit({ keys: ["A"], i: 0 }, "Z").step).toBe("push"); + }); + + it("follows the browser's own Back and Forward, starting again from an entry it does not know", () => { + const v: Visits = { keys: ["A", "B", "C"], i: 2 }; + expect(arrived(v, "A", 0)).toEqual({ keys: ["A", "B", "C"], i: 0 }); + expect(arrived(v, "B", 1)).toEqual({ keys: ["A", "B", "C"], i: 1 }); + expect(arrived(v, "X", 1)).toEqual({ keys: ["X"], i: 0 }); + expect(arrived(v, "A", 7)).toEqual({ keys: ["A"], i: 0 }); + expect(arrived(v, "A", -1)).toEqual({ keys: ["A"], i: 0 }); + }); + + it("reads back only well-formed visits, and keeps them short", () => { + expect(loadVisits({ keys: ["A", "B"], i: 1 })).toEqual({ keys: ["A", "B"], i: 1 }); + expect(loadVisits(null)).toBeNull(); + expect(loadVisits({ keys: ["A"], i: 1 })).toBeNull(); + expect(loadVisits({ keys: [1], i: 0 })).toBeNull(); + expect(loadVisits({ keys: ["A"], i: 0.5 })).toBeNull(); + const many = Array.from({ length: 250 }, (_, j) => `k${j}`); + const v = loadVisits({ keys: many, i: 240 })!; + expect(v.keys).toHaveLength(200); + expect(v.keys[v.i]).toBe("k240"); + }); +});