diff --git a/src/ui/forks.ts b/src/ui/forks.ts index b234691..03c4149 100644 --- a/src/ui/forks.ts +++ b/src/ui/forks.ts @@ -26,6 +26,8 @@ export interface Tree { pinned?: boolean; /** On the root of an orphan: the column it stays in. */ col?: number; + /** Its place in its column, once the panes there have been put in an order by hand (dragged); unranked panes follow, in tree order. */ + rank?: number; } /** The trees, the first starting at the top node; `at` is the one the focused branch is in. */ @@ -116,6 +118,37 @@ export function all(f: Forest): Placed[] { return out; } +/** + * The panes of each column, top to bottom: in the order the reader has put them in (by rank), then + * those never placed by hand, in tree order (tree by tree, depth first, earlier forks first). + */ +export function columns(f: Forest): Map[]> { + const out = new Map[]>(); + for (const x of all(f)) { + const c = out.get(x.col); + if (c) c.push(x); + else out.set(x.col, [x]); + } + for (const c of out.values()) { + const at = new Map(c.map((x, i) => [x, i])); + c.sort((a, b) => (a.node.rank ?? Infinity) - (b.node.rank ?? Infinity) || at.get(a)! - at.get(b)!); + } + return out; +} + +/** Move a pane within its column, to just above another pane there (or to the bottom): the column's order is then set by hand. */ +export function reorder(f: Forest, id: number, before: number | null): Forest { + const me = all(f).find((x) => x.node.id === id); + if (!me || id === before) return f; + const col = columns(f).get(me.col)!.map((x) => x.node.id); + const rest = col.filter((x) => x !== id); + const at = before === null ? rest.length : rest.indexOf(before); + if (at < 0) return f; + rest.splice(at, 0, id); + if (rest.every((x, i) => x === col[i]) && col.every((x) => (all(f).find((y) => y.node.id === x)!.node.rank ?? -1) >= 0)) return f; + return rest.reduce((g, x, i) => updateIn(g, x, (n) => (n.rank === i ? n : { ...n, rank: i })), f); +} + /** Change the node with this id, copying the path to it. */ export function update(t: Tree, id: number, f: (n: Tree) => Tree): Tree { if (t.id === id) return f(t); @@ -198,7 +231,8 @@ export function open(f: Forest, i: number, ref: R, fork: boolean, sa if (fork || n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, leaf(ref)], pick: n.kids.length }; orphans.push(...survivors(n.kids[n.pick], colOf(t) + i + 1, true)); const kids = n.kids.slice(); - kids[n.pick] = leaf(ref); + // In its place, in its column too. + kids[n.pick] = n.kids[n.pick].rank === undefined ? leaf(ref) : { ...leaf(ref), rank: n.kids[n.pick].rank }; return { ...n, kids }; }); return replace(f, f.at, next, orphans); @@ -239,11 +273,11 @@ export const pin = (f: Forest, id: number, on: boolean): Forest(f: Forest, id: number, delta: number): number | null { - const placed = all(f); - const me = placed.find((x) => x.node.id === id); - if (!me) return null; - const col = placed.filter((x) => x.col === me.col); - return col[col.indexOf(me) + delta]?.node.id ?? null; + for (const col of columns(f).values()) { + const i = col.findIndex((x) => x.node.id === id); + if (i >= 0) return col[i + delta]?.node.id ?? null; + } + return null; } /** Drop every pane whose ref fails `keep` (with everything right of it but pinned panes, which stay as orphans). Unchanged forests are returned as they are. */ @@ -328,6 +362,7 @@ export interface Saved { pick?: number; pin?: 1; col?: number; + rank?: number; } export function save(t: Tree, ref: (r: R) => O): Saved { @@ -336,6 +371,7 @@ export function save(t: Tree, ref: (r: R) => O): Saved { if (t.pick) out.pick = t.pick; if (t.pinned) out.pin = 1; if (t.col) out.col = t.col; + if (t.rank !== undefined) out.rank = t.rank; return out; } @@ -348,6 +384,7 @@ export function load(o: unknown, ok: (r: unknown) => r is R): Tree | const t: Tree = { ref: s.ref as R, kids, pick, id: ++ids }; if (s.pin) t.pinned = true; if (typeof s.col === "number" && s.col >= 1) t.col = Math.floor(s.col); + if (typeof s.rank === "number" && Number.isFinite(s.rank)) t.rank = s.rank; return t; } diff --git a/tests/forks.test.ts b/tests/forks.test.ts index 995d283..f84468d 100644 --- a/tests/forks.test.ts +++ b/tests/forks.test.ts @@ -1,7 +1,7 @@ /** The panes view's forest of forks (src/ui/forks.ts): each operation, and any sequence of them leaving a well-formed forest with one focused branch and one active pane. */ import fc from "fast-check"; import { describe, expect, it } from "vitest"; -import { all, besideInColumn, close, find, focus, focused, load, open, path, pin, plain, prune, restore, save, valid, withBranch, type Forest, type Tree } from "../src/ui/forks"; +import { all, besideInColumn, close, columns, find, focus, focused, load, open, path, pin, plain, prune, reorder, restore, save, valid, withBranch, type Forest, type Tree } from "../src/ui/forks"; type F = Forest; type T = Tree; @@ -121,6 +121,30 @@ describe("forks", () => { expect(focus(g, idOf(f, "b1"))).toBe(g); }); + it("a column's panes can be put in an order by hand, across forks; new panes come after; a replaced pane keeps its place; the order is stored", () => { + let f: F = plain(["a", "b", "b1"]); + f = open(f, 1, "b2", true, same); + f = open(f, 0, "c", true, same); + f = open(f, 1, "c1", false, same); + const col2 = (g: F) => columns(g).get(2)!.map((x) => x.node.ref); + expect(col2(f)).toEqual(["b1", "b2", "c1"]); + f = reorder(f, idOf(f, "c1"), idOf(f, "b1")); + expect(col2(f)).toEqual(["c1", "b1", "b2"]); + expect(besideInColumn(f, idOf(f, "b1"), -1)).toBe(idOf(f, "c1")); + f = reorder(f, idOf(f, "c1"), null); + expect(col2(f)).toEqual(["b1", "b2", "c1"]); + f = reorder(f, idOf(f, "b2"), idOf(f, "b1")); + expect(col2(f)).toEqual(["b2", "b1", "c1"]); + // A new pane in that column goes to the bottom; a pane opened in place of another takes its place. + f = open(focus(f, idOf(f, "c")), 1, "c2", true, same); + expect(col2(f)).toEqual(["b2", "b1", "c1", "c2"]); + f = open(focus(f, idOf(f, "b1")), 1, "bx", false, same); + expect(col2(f)).toEqual(["b2", "bx", "c1", "c2"]); + const isStr = (r: unknown): r is string => typeof r === "string"; + const back = restore(JSON.parse(JSON.stringify({ forest: f.trees.map((t) => save(t, (r) => r)), at: f.at })), isStr); + expect(col2(back)).toEqual(["b2", "bx", "c1", "c2"]); + }); + it("drops a deleted node's pane wherever it is, with what is right of it but pinned panes", () => { let f: F = plain(["a", "b", "c"]); f = open(f, 0, "x", true, same); @@ -169,9 +193,9 @@ describe("forks", () => { expect(restore({ forest: [{ ref: "o", col: 2 }, { ref: "a" }], at: 7 }, isStr).trees.map((t) => t.ref)).toEqual(["a", "o"]); }); - it("any sequence of open, fork, close, pin, unpin and focus leaves a valid forest with exactly one active pane, and closing removes no pinned pane but the one closed", () => { + it("any sequence of open, fork, close, pin, unpin, focus and reorder leaves a valid forest with exactly one active pane, and closing removes no pinned pane but the one closed", () => { const refs = ["a", "b", "c", "d", "e"]; - const op = fc.record({ kind: fc.integer({ min: 0, max: 8 }), i: fc.nat(40), r: fc.integer({ min: 0, max: refs.length - 1 }), d: fc.boolean() }); + const op = fc.record({ kind: fc.integer({ min: 0, max: 9 }), i: fc.nat(40), r: fc.integer({ min: 0, max: refs.length - 1 }), d: fc.boolean() }); fc.assert( fc.property(fc.array(op, { maxLength: 60 }), (ops) => { let f: F = plain(["root"]); @@ -215,6 +239,14 @@ describe("forks", () => { f = withBranch(f, ["root", ...refs.slice(0, o.i % 4)], same); active = f.trees[0].id; break; + case 9: { + const col = [...columns(f).values()].find((c) => c.some((x) => x.node.id === any.id))!; + const before = col[o.r % col.length].node.id; + const was = all(f).length; + f = reorder(f, any.id, o.d ? before : null); + if (all(f).length !== was) return false; + break; + } } if (!valid(f, same)) return false; // One branch, from the focused tree's root to a pane with nothing right of it, each step a picked child.