Branch colours in the forest: a first child keeps its parent's, each further child starts the next
A pane's colour (an index into a palette of HUES) is fixed when it opens and stored with the view; the first tree's root trail has none. A view stored before colours is coloured as if opened in tree order. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
1 parent
ec19a23051
commit
30957260ce
3 files changed
+175
-23
No files matched your search
+69
-20
@@ -12,8 +12,15 @@
|
|||||||
* so a forest that did not change is the same object (the store memoises on identity). A node's
|
* so a forest that did not change is the same object (the store memoises on identity). A node's
|
||||||
* `id` names the pane for as long as it is open (keys, curves, the hosts of article panes off the
|
* `id` names the pane for as long as it is open (keys, curves, the hosts of article panes off the
|
||||||
* branch).
|
* branch).
|
||||||
|
*
|
||||||
|
* Each pane has its branch's colour (`hue`), fixed when it opens: a pane opened as its parent's first
|
||||||
|
* child takes its parent's, any further child starts the next colour of the palette, so a plain trail
|
||||||
|
* is one colour and each fork starts a new one. The first tree's root trail has none (plain ink).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/** How many colours branches cycle through (style.css `--hue-0` …). */
|
||||||
|
export const HUES = 7;
|
||||||
|
|
||||||
export interface Tree<R, S = unknown> {
|
export interface Tree<R, S = unknown> {
|
||||||
ref: R;
|
ref: R;
|
||||||
kids: Tree<R, S>[];
|
kids: Tree<R, S>[];
|
||||||
@@ -28,12 +35,16 @@ export interface Tree<R, S = unknown> {
|
|||||||
col?: number;
|
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. */
|
/** 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;
|
rank?: number;
|
||||||
|
/** Its branch's colour, 0 to HUES − 1; none on the first tree's root trail. Kept for the pane's life. */
|
||||||
|
hue?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The trees, the first starting at the top node; `at` is the one the focused branch is in. */
|
/** The trees, the first starting at the top node; `at` is the one the focused branch is in. */
|
||||||
export interface Forest<R, S = unknown> {
|
export interface Forest<R, S = unknown> {
|
||||||
trees: Tree<R, S>[];
|
trees: Tree<R, S>[];
|
||||||
at: number;
|
at: number;
|
||||||
|
/** How many branch colours have been started: the next is this one, round the palette. */
|
||||||
|
hues?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Same<R> = (a: R, b: R) => boolean;
|
export type Same<R> = (a: R, b: R) => boolean;
|
||||||
@@ -54,6 +65,25 @@ export const plain = <R, S>(refs: R[]): Forest<R, S> => {
|
|||||||
return { trees: t ? [t] : [], at: 0 };
|
return { trees: t ? [t] : [], at: 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** A tree with this colour on every pane (none: as it is). */
|
||||||
|
const tint = <R, S>(t: Tree<R, S>, hue: number | undefined): Tree<R, S> => (hue === undefined ? t : { ...t, hue, kids: t.kids.map((k) => tint(k, hue)) });
|
||||||
|
|
||||||
|
/** Colours started by one operation: the next of the palette each time, never the parent's own. */
|
||||||
|
function palette(f: Forest<unknown, unknown>) {
|
||||||
|
let n = f.hues ?? 0;
|
||||||
|
return {
|
||||||
|
next(parent?: number): number {
|
||||||
|
let h = n++ % HUES;
|
||||||
|
if (h === parent) h = n++ % HUES;
|
||||||
|
return h;
|
||||||
|
},
|
||||||
|
/** The forest, counting the colours started. */
|
||||||
|
done<R, S>(g: Forest<R, S>): Forest<R, S> {
|
||||||
|
return n === (g.hues ?? 0) ? g : { ...g, hues: n };
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/** The column a tree's root is in. */
|
/** The column a tree's root is in. */
|
||||||
export const colOf = (t: Tree<unknown, unknown>) => t.col ?? 0;
|
export const colOf = (t: Tree<unknown, unknown>) => t.col ?? 0;
|
||||||
|
|
||||||
@@ -185,7 +215,7 @@ export function focus<R, S>(f: Forest<R, S>, id: number): Forest<R, S> {
|
|||||||
if (!hit) return f;
|
if (!hit) return f;
|
||||||
const t = select(f.trees[hit.ti], id)!;
|
const t = select(f.trees[hit.ti], id)!;
|
||||||
if (t === f.trees[hit.ti] && hit.ti === f.at) return f;
|
if (t === f.trees[hit.ti] && hit.ti === f.at) return f;
|
||||||
return { trees: t === f.trees[hit.ti] ? f.trees : f.trees.map((x, j) => (j === hit.ti ? t : x)), at: hit.ti };
|
return { ...f, trees: t === f.trees[hit.ti] ? f.trees : f.trees.map((x, j) => (j === hit.ti ? t : x)), at: hit.ti };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -203,7 +233,7 @@ function replace<R, S>(f: Forest<R, S>, ti: number, t: Tree<R, S> | null, orphan
|
|||||||
const trees = [...f.trees.slice(0, ti), ...(t ? [t] : []), ...orphans, ...f.trees.slice(ti + 1)];
|
const trees = [...f.trees.slice(0, ti), ...(t ? [t] : []), ...orphans, ...f.trees.slice(ti + 1)];
|
||||||
const was = f.trees[f.at];
|
const was = f.trees[f.at];
|
||||||
const at = trees.indexOf(was);
|
const at = trees.indexOf(was);
|
||||||
return { trees, at: at >= 0 ? at : Math.min(ti, trees.length - 1) };
|
return { ...f, trees, at: at >= 0 ? at : Math.min(ti, trees.length - 1) };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Remove a child of a node, picking the one that takes its place (the one below, else the one above). */
|
/** Remove a child of a node, picking the one that takes its place (the one below, else the one above). */
|
||||||
@@ -224,18 +254,22 @@ export function open<R, S>(f: Forest<R, S>, i: number, ref: R, fork: boolean, sa
|
|||||||
const t = focused(f);
|
const t = focused(f);
|
||||||
if (!t) return plain([ref]);
|
if (!t) return plain([ref]);
|
||||||
const orphans: Tree<R, S>[] = [];
|
const orphans: Tree<R, S>[] = [];
|
||||||
|
const hues = palette(f);
|
||||||
const next = at(t, i, (n) => {
|
const next = at(t, i, (n) => {
|
||||||
const k = n.kids.findIndex((c) => same(c.ref, ref));
|
const k = n.kids.findIndex((c) => same(c.ref, ref));
|
||||||
if (k >= 0) return k === n.pick ? n : { ...n, pick: k };
|
if (k >= 0) return k === n.pick ? n : { ...n, pick: k };
|
||||||
if (!n.kids.length) return { ...n, kids: [leaf(ref)], pick: 0 };
|
// The first child goes on in its parent's colour; another starts the next one.
|
||||||
if (fork || n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, leaf(ref)], pick: n.kids.length };
|
if (!n.kids.length) return { ...n, kids: [tint(leaf<R, S>(ref), n.hue)], pick: 0 };
|
||||||
orphans.push(...survivors(n.kids[n.pick], colOf(t) + i + 1, true));
|
if (fork || n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, tint(leaf<R, S>(ref), hues.next(n.hue))], pick: n.kids.length };
|
||||||
|
const was = n.kids[n.pick];
|
||||||
|
orphans.push(...survivors(was, colOf(t) + i + 1, true));
|
||||||
const kids = n.kids.slice();
|
const kids = n.kids.slice();
|
||||||
// In its place, in its column too.
|
// In its place, in its column and its colour too.
|
||||||
kids[n.pick] = n.kids[n.pick].rank === undefined ? leaf(ref) : { ...leaf<R, S>(ref), rank: n.kids[n.pick].rank };
|
const put = tint(leaf<R, S>(ref), was.hue);
|
||||||
|
kids[n.pick] = was.rank === undefined ? put : { ...put, rank: was.rank };
|
||||||
return { ...n, kids };
|
return { ...n, kids };
|
||||||
});
|
});
|
||||||
return replace(f, f.at, next, orphans);
|
return hues.done(replace(f, f.at, next, orphans));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -330,8 +364,9 @@ export function withBranch<R, S>(f: Forest<R, S>, refs: R[], same: Same<R>): For
|
|||||||
const old = f.trees[0] && !f.trees[0].col ? f.trees[0] : null;
|
const old = f.trees[0] && !f.trees[0].col ? f.trees[0] : null;
|
||||||
if (old) orphans.push(...survivors(old, 0, false));
|
if (old) orphans.push(...survivors(old, 0, false));
|
||||||
const rest = old ? f.trees.slice(1) : f.trees;
|
const rest = old ? f.trees.slice(1) : f.trees;
|
||||||
return { trees: [chain<R, S>(refs)!, ...orphans, ...rest], at: 0 };
|
return { ...f, trees: [chain<R, S>(refs)!, ...orphans, ...rest], at: 0 };
|
||||||
}
|
}
|
||||||
|
const hues = palette(f);
|
||||||
const go = (n: Tree<R, S>, i: number): Tree<R, S> => {
|
const go = (n: Tree<R, S>, i: number): Tree<R, S> => {
|
||||||
if (i === refs.length) {
|
if (i === refs.length) {
|
||||||
const kept = n.kids.filter((k) => k.pinned);
|
const kept = n.kids.filter((k) => k.pinned);
|
||||||
@@ -342,20 +377,20 @@ export function withBranch<R, S>(f: Forest<R, S>, refs: R[], same: Same<R>): For
|
|||||||
const k = n.kids.findIndex((c) => same(c.ref, refs[i]));
|
const k = n.kids.findIndex((c) => same(c.ref, refs[i]));
|
||||||
if (k < 0) {
|
if (k < 0) {
|
||||||
const rest = chain<R, S>(refs.slice(i))!;
|
const rest = chain<R, S>(refs.slice(i))!;
|
||||||
if (!n.kids.length) return { ...n, kids: [rest], pick: 0 };
|
if (!n.kids.length) return { ...n, kids: [tint(rest, n.hue)], pick: 0 };
|
||||||
if (n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, rest], pick: n.kids.length };
|
if (n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, tint(rest, hues.next(n.hue))], pick: n.kids.length };
|
||||||
orphans.push(...survivors(n.kids[n.pick], i, true));
|
orphans.push(...survivors(n.kids[n.pick], i, true));
|
||||||
return { ...n, kids: n.kids.map((c, j) => (j === n.pick ? rest : c)) };
|
return { ...n, kids: n.kids.map((c, j) => (j === n.pick ? tint(rest, c.hue) : c)) };
|
||||||
}
|
}
|
||||||
const sub = go(n.kids[k], i + 1);
|
const sub = go(n.kids[k], i + 1);
|
||||||
return sub === n.kids[k] && k === n.pick ? n : { ...n, kids: n.kids.map((c, j) => (j === k ? sub : c)), pick: k };
|
return sub === n.kids[k] && k === n.pick ? n : { ...n, kids: n.kids.map((c, j) => (j === k ? sub : c)), pick: k };
|
||||||
};
|
};
|
||||||
const t = go(f.trees[0], 1);
|
const t = go(f.trees[0], 1);
|
||||||
const g = replace(f, 0, t, orphans);
|
const g = hues.done(replace(f, 0, t, orphans));
|
||||||
return g.at === 0 ? g : { ...g, at: 0 };
|
return g.at === 0 ? g : { ...g, at: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
/** As stored: refs (made storable by the caller), children, picks, pins and an orphan's column; no ids, nothing remembered. */
|
/** As stored: refs (made storable by the caller), children, picks, pins, an orphan's column, places and colours; no ids, nothing remembered. */
|
||||||
export interface Saved<R> {
|
export interface Saved<R> {
|
||||||
ref: R;
|
ref: R;
|
||||||
kids?: Saved<R>[];
|
kids?: Saved<R>[];
|
||||||
@@ -363,6 +398,7 @@ export interface Saved<R> {
|
|||||||
pin?: 1;
|
pin?: 1;
|
||||||
col?: number;
|
col?: number;
|
||||||
rank?: number;
|
rank?: number;
|
||||||
|
hue?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function save<R, S, O>(t: Tree<R, S>, ref: (r: R) => O): Saved<O> {
|
export function save<R, S, O>(t: Tree<R, S>, ref: (r: R) => O): Saved<O> {
|
||||||
@@ -372,6 +408,7 @@ export function save<R, S, O>(t: Tree<R, S>, ref: (r: R) => O): Saved<O> {
|
|||||||
if (t.pinned) out.pin = 1;
|
if (t.pinned) out.pin = 1;
|
||||||
if (t.col) out.col = t.col;
|
if (t.col) out.col = t.col;
|
||||||
if (t.rank !== undefined) out.rank = t.rank;
|
if (t.rank !== undefined) out.rank = t.rank;
|
||||||
|
if (t.hue !== undefined) out.hue = t.hue;
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,32 +422,44 @@ export function load<R, S>(o: unknown, ok: (r: unknown) => r is R): Tree<R, S> |
|
|||||||
if (s.pin) t.pinned = true;
|
if (s.pin) t.pinned = true;
|
||||||
if (typeof s.col === "number" && s.col >= 1) t.col = Math.floor(s.col);
|
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;
|
if (typeof s.rank === "number" && Number.isFinite(s.rank)) t.rank = s.rank;
|
||||||
|
if (typeof s.hue === "number" && Number.isInteger(s.hue) && s.hue >= 0 && s.hue < HUES) t.hue = s.hue;
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* What was stored: the forest; from before orphans, one tree; from before forks, the trail as a
|
* What was stored: the forest; from before orphans, one tree; from before forks, the trail as a
|
||||||
* tree with one branch. An orphan stored first (its tree's root gone) is dropped.
|
* tree with one branch. An orphan stored first (its tree's root gone) is dropped. Stored from
|
||||||
|
* before colours (no count of them), the panes are coloured as if opened in tree order.
|
||||||
*/
|
*/
|
||||||
export function restore<R, S>(saved: { forest?: unknown; at?: unknown; forks?: unknown; trail?: unknown[] }, ok: (r: unknown) => r is R): Forest<R, S> {
|
export function restore<R, S>(saved: { forest?: unknown; at?: unknown; forks?: unknown; trail?: unknown[]; hues?: unknown }, ok: (r: unknown) => r is R): Forest<R, S> {
|
||||||
|
let f: Forest<R, S>;
|
||||||
if (Array.isArray(saved.forest)) {
|
if (Array.isArray(saved.forest)) {
|
||||||
const trees = saved.forest.map((o) => load<R, S>(o, ok)).filter((t): t is Tree<R, S> => !!t);
|
const trees = saved.forest.map((o) => load<R, S>(o, ok)).filter((t): t is Tree<R, S> => !!t);
|
||||||
const first = trees.findIndex((t) => !t.col);
|
const first = trees.findIndex((t) => !t.col);
|
||||||
const kept = first < 0 ? trees.filter((t) => t.col) : [trees[first], ...trees.filter((t, j) => j !== first && t.col)];
|
const kept = first < 0 ? trees.filter((t) => t.col) : [trees[first], ...trees.filter((t, j) => j !== first && t.col)];
|
||||||
const at = typeof saved.at === "number" ? Math.floor(saved.at) : 0;
|
const at = typeof saved.at === "number" ? Math.floor(saved.at) : 0;
|
||||||
return { trees: kept, at: at >= 0 && at < kept.length ? at : 0 };
|
f = { trees: kept, at: at >= 0 && at < kept.length ? at : 0 };
|
||||||
|
} else {
|
||||||
|
const t = saved.forks ? load<R, S>(saved.forks, ok) : chain<R, S>((saved.trail ?? []).filter(ok));
|
||||||
|
f = { trees: t ? [t] : [], at: 0 };
|
||||||
}
|
}
|
||||||
const t = saved.forks ? load<R, S>(saved.forks, ok) : chain<R, S>((saved.trail ?? []).filter(ok));
|
if (typeof saved.hues === "number" && Number.isInteger(saved.hues) && saved.hues >= 0) return { ...f, hues: saved.hues };
|
||||||
return { trees: t ? [t] : [], at: 0 };
|
const hues = palette(f);
|
||||||
|
const paint = (t: Tree<R, S>, hue: number | undefined): Tree<R, S> => {
|
||||||
|
const kids = t.kids.map((k, j) => paint(k, j === 0 ? hue : hues.next(hue)));
|
||||||
|
return hue === undefined ? { ...t, kids } : { ...t, kids, hue };
|
||||||
|
};
|
||||||
|
return hues.done({ ...f, trees: f.trees.map((t) => paint(t, t.col ? hues.next() : undefined)) });
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Whether a forest is well formed: picks in range, no pane twice among siblings, ids unique, orphans in a column right of the first, the focused tree there. */
|
/** Whether a forest is well formed: picks in range, colours in the palette, no pane twice among siblings, ids unique, orphans in a column right of the first, the focused tree there. */
|
||||||
export function valid<R, S>(f: Forest<R, S>, same: Same<R>): boolean {
|
export function valid<R, S>(f: Forest<R, S>, same: Same<R>): boolean {
|
||||||
const seen = new Set<number>();
|
const seen = new Set<number>();
|
||||||
const ok = (n: Tree<R, S>): boolean => {
|
const ok = (n: Tree<R, S>): boolean => {
|
||||||
if (seen.has(n.id)) return false;
|
if (seen.has(n.id)) return false;
|
||||||
seen.add(n.id);
|
seen.add(n.id);
|
||||||
if (n.pick < 0 || n.pick >= Math.max(1, n.kids.length) || !Number.isInteger(n.pick)) return false;
|
if (n.pick < 0 || n.pick >= Math.max(1, n.kids.length) || !Number.isInteger(n.pick)) return false;
|
||||||
|
if (n.hue !== undefined && !(Number.isInteger(n.hue) && n.hue >= 0 && n.hue < HUES)) return false;
|
||||||
for (let a = 0; a < n.kids.length; a++) for (let b = a + 1; b < n.kids.length; b++) if (same(n.kids[a].ref, n.kids[b].ref)) return false;
|
for (let a = 0; a < n.kids.length; a++) for (let b = a + 1; b < n.kids.length; b++) if (same(n.kids[a].ref, n.kids[b].ref)) return false;
|
||||||
return n.kids.every((k) => k.col === undefined && ok(k));
|
return n.kids.every((k) => k.col === undefined && ok(k));
|
||||||
};
|
};
|
||||||
|
|||||||
+2
-2
@@ -1206,7 +1206,7 @@ class Store {
|
|||||||
}
|
}
|
||||||
private restoreView() {
|
private restoreView() {
|
||||||
const s = this.doc.snap();
|
const s = this.doc.snap();
|
||||||
const v = lsGet<{ open: NodeId[]; focus: Focus | null; hoist: NodeId | null; forest?: unknown; at?: number; forks?: unknown; trail?: PaneRef[]; paneAt?: number }>(this.viewKey(), { open: [], focus: null, hoist: null });
|
const v = lsGet<{ open: NodeId[]; focus: Focus | null; hoist: NodeId | null; forest?: unknown; at?: number; hues?: number; forks?: unknown; trail?: PaneRef[]; paneAt?: number }>(this.viewKey(), { open: [], focus: null, hoist: null });
|
||||||
this.open = new Set(v.open.filter((id) => s.nodes[id]));
|
this.open = new Set(v.open.filter((id) => s.nodes[id]));
|
||||||
this.hoist = v.hoist && s.nodes[v.hoist] ? v.hoist : null;
|
this.hoist = v.hoist && s.nodes[v.hoist] ? v.hoist : null;
|
||||||
this.focus = v.focus && s.nodes[v.focus.node] ? v.focus : { node: this.top, grain: "node" };
|
this.focus = v.focus && s.nodes[v.focus.node] ? v.focus : { node: this.top, grain: "node" };
|
||||||
@@ -1219,7 +1219,7 @@ class Store {
|
|||||||
saveView() {
|
saveView() {
|
||||||
const bare = (p: PaneRef): PaneRef => ({ ...p, from: undefined });
|
const bare = (p: PaneRef): PaneRef => ({ ...p, from: undefined });
|
||||||
// The trail too, for a build from before forks.
|
// The trail too, for a build from before forks.
|
||||||
lsSet(this.viewKey(), { open: [...this.open], focus: this.focus, hoist: this.hoist, forest: this.forest.trees.map((t) => saveTree(t, bare)), at: this.forest.at, trail: this.trail.map(bare), paneAt: this.paneAt });
|
lsSet(this.viewKey(), { open: [...this.open], focus: this.focus, hoist: this.hoist, forest: this.forest.trees.map((t) => saveTree(t, bare)), at: this.forest.at, hues: this.forest.hues ?? 0, trail: this.trail.map(bare), paneAt: this.paneAt });
|
||||||
}
|
}
|
||||||
/** Keep focus on something that exists and is on screen. */
|
/** Keep focus on something that exists and is on screen. */
|
||||||
private repairView() {
|
private repairView() {
|
||||||
|
|||||||
+104
-1
@@ -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. */
|
/** 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 fc from "fast-check";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
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";
|
import { HUES, 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<string>;
|
type F = Forest<string>;
|
||||||
type T = Tree<string>;
|
type T = Tree<string>;
|
||||||
@@ -201,6 +201,8 @@ describe("forks", () => {
|
|||||||
let f: F = plain(["root"]);
|
let f: F = plain(["root"]);
|
||||||
// The active pane, by id: on the focused branch.
|
// The active pane, by id: on the focused branch.
|
||||||
let active = f.trees[0].id;
|
let active = f.trees[0].id;
|
||||||
|
// Each pane's colour, as first seen: it never changes while the pane is open.
|
||||||
|
const hueOf = new Map<number, number | undefined>();
|
||||||
for (const o of ops) {
|
for (const o of ops) {
|
||||||
const p = path(focused(f));
|
const p = path(focused(f));
|
||||||
const i = Math.max(0, p.findIndex((n) => n.id === active));
|
const i = Math.max(0, p.findIndex((n) => n.id === active));
|
||||||
@@ -256,6 +258,11 @@ describe("forks", () => {
|
|||||||
// Exactly one active pane: it is open, once, and on the focused branch.
|
// Exactly one active pane: it is open, once, and on the focused branch.
|
||||||
if (all(f).filter((x) => x.node.id === active).length !== 1 || !b.some((n) => n.id === active)) return false;
|
if (all(f).filter((x) => x.node.id === active).length !== 1 || !b.some((n) => n.id === active)) return false;
|
||||||
if (!find(f, active) || f.trees[0].ref !== "root") return false;
|
if (!find(f, active) || f.trees[0].ref !== "root") return false;
|
||||||
|
if (f.trees[0].hue !== undefined) return false;
|
||||||
|
for (const x of all(f)) {
|
||||||
|
if (hueOf.has(x.node.id) && hueOf.get(x.node.id) !== x.node.hue) return false;
|
||||||
|
hueOf.set(x.node.id, x.node.hue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}),
|
}),
|
||||||
@@ -263,3 +270,99 @@ describe("forks", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("branch colours", () => {
|
||||||
|
const isStr = (r: unknown): r is string => typeof r === "string";
|
||||||
|
/** Each pane's colour by its ref, "-" for none. */
|
||||||
|
const hues = (f: F) => Object.fromEntries(all(f).map((x) => [x.node.ref, x.node.hue ?? "-"]));
|
||||||
|
const roundTrip = (f: F) => restore<string, unknown>(JSON.parse(JSON.stringify({ forest: f.trees.map((t) => save(t, (r) => r)), at: f.at, hues: f.hues ?? 0 })), isStr);
|
||||||
|
|
||||||
|
it("a plain trail is one colour, the first tree's none; each further child of a pane starts the next colour", () => {
|
||||||
|
let f: F = plain(["a", "b", "c"]);
|
||||||
|
expect(hues(f)).toEqual({ a: "-", b: "-", c: "-" });
|
||||||
|
f = open(f, 0, "x", true, same); // a second child of a: a new colour
|
||||||
|
f = open(f, 1, "x1", true, same); // x's first child: x's colour
|
||||||
|
f = open(f, 2, "x2", false, same);
|
||||||
|
f = open(f, 1, "y", true, same); // a second child of x
|
||||||
|
f = open(f, 0, "z", true, same); // a third child of a
|
||||||
|
expect(hues(f)).toEqual({ a: "-", b: "-", c: "-", x: 0, x1: 0, x2: 0, y: 1, z: 2 });
|
||||||
|
expect(f.hues).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("the palette goes round, never giving a fork its parent's colour", () => {
|
||||||
|
let f: F = plain(["a"]);
|
||||||
|
for (let i = 0; i < HUES + 2; i++) f = open(focus(f, f.trees[0].id), 0, `k${i}`, true, same);
|
||||||
|
// k0 is a's first child (no colour); k1… take 0, 1, … round the palette.
|
||||||
|
expect(all(f).slice(1).map((x) => x.node.hue ?? "-")).toEqual(["-", ...Array.from({ length: HUES + 1 }, (_, i) => i % HUES)]);
|
||||||
|
// Forks of a coloured pane skip its colour when the palette comes round to it.
|
||||||
|
let g: F = plain(["a"]);
|
||||||
|
g = open(g, 0, "b", true, same);
|
||||||
|
g = open(g, 0, "c", true, same); // 0
|
||||||
|
for (let i = 0; i < HUES - 1; i++) g = open(focus(g, g.trees[0].id), 0, `p${i}`, true, same); // 1 … HUES − 1
|
||||||
|
g = open(focus(g, idOf(g, "c")), 1, "c1", true, same); // c's first: 0
|
||||||
|
g = open(focus(g, idOf(g, "c")), 1, "c2", true, same); // next would be 0, c's own
|
||||||
|
expect(all(g).find((x) => x.node.ref === "c2")!.node.hue).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a pane opened in place of another takes its colour; one beside a pinned pane starts a new one; so does withBranch", () => {
|
||||||
|
let f: F = plain(["a", "b"]);
|
||||||
|
f = open(f, 0, "x", true, same);
|
||||||
|
f = open(f, 0, "y", true, same);
|
||||||
|
f = open(focus(f, idOf(f, "x")), 0, "x'", false, same); // in x's place
|
||||||
|
expect(hues(f)).toEqual({ a: "-", b: "-", "x'": 0, y: 1 });
|
||||||
|
f = pin(f, idOf(f, "x'"), true);
|
||||||
|
f = open(f, 0, "w", false, same); // beside a pinned pane: a fork
|
||||||
|
expect(hues(f).w).toBe(2);
|
||||||
|
let g: F = plain(["a", "b"]);
|
||||||
|
g = open(g, 0, "x", true, same);
|
||||||
|
g = withBranch(g, ["a", "x", "x1", "x2"], same);
|
||||||
|
expect(hues(g)).toMatchObject({ x: 0, x1: 0, x2: 0 });
|
||||||
|
g = withBranch(g, ["a", "x", "q"], same); // in x1's place
|
||||||
|
expect(hues(g).q).toBe(0);
|
||||||
|
expect(hues(withBranch(g, ["r", "s"], same))).toMatchObject({ r: "-", s: "-" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("colours stay through a reorder, a close, a pin and a prune; an orphan keeps its own", () => {
|
||||||
|
let f: F = plain(["a", "b", "b1"]);
|
||||||
|
f = open(f, 1, "c", true, same);
|
||||||
|
f = open(f, 2, "c1", true, same);
|
||||||
|
f = open(f, 0, "d", true, same);
|
||||||
|
f = open(f, 1, "d1", true, same);
|
||||||
|
const was = hues(f);
|
||||||
|
f = reorder(f, idOf(f, "d1"), idOf(f, "b1"));
|
||||||
|
expect(hues(f)).toEqual(was);
|
||||||
|
f = pin(f, idOf(f, "c1"), true);
|
||||||
|
f = close(f, idOf(f, "c"), idOf(f, "d1")).forest;
|
||||||
|
expect(f.trees[1].ref).toBe("c1");
|
||||||
|
const { c: _c, ...rest } = was;
|
||||||
|
expect(hues(f)).toEqual(rest);
|
||||||
|
const { d1: _d1, ...left } = rest;
|
||||||
|
expect(hues(prune(f, (r) => r !== "d1"))).toEqual(left);
|
||||||
|
// A new fork after all that still takes the next colour, not one used already.
|
||||||
|
expect(hues(open(focus(f, idOf(f, "a")), 0, "e", true, same)).e).toBe(f.hues);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("colours are stored with the view; a view stored before colours is coloured as if opened in tree order", () => {
|
||||||
|
let f: F = plain(["a", "b", "c"]);
|
||||||
|
f = open(f, 0, "x", true, same);
|
||||||
|
f = open(f, 1, "x1", true, same);
|
||||||
|
f = open(f, 0, "y", true, same);
|
||||||
|
f = pin(f, idOf(f, "x1"), true);
|
||||||
|
f = close(f, idOf(f, "x"), idOf(f, "y")).forest;
|
||||||
|
expect(hues(f)).toEqual({ a: "-", b: "-", c: "-", y: 1, x1: 0 });
|
||||||
|
const back = roundTrip(f);
|
||||||
|
expect(hues(back)).toEqual(hues(f));
|
||||||
|
expect(back.hues).toBe(f.hues);
|
||||||
|
// From before colours: the root trail plain, each further child and each orphan a new colour.
|
||||||
|
const old = restore<string, unknown>({ forest: [{ ref: "a", kids: [{ ref: "b", kids: [{ ref: "c" }, { ref: "c2" }] }, { ref: "x", kids: [{ ref: "x1" }] }] }, { ref: "o", col: 2, kids: [{ ref: "o1" }] }], at: 0 }, isStr);
|
||||||
|
expect(hues(old)).toEqual({ a: "-", b: "-", c: "-", c2: 0, x: 1, x1: 1, o: 2, o1: 2 });
|
||||||
|
expect(old.hues).toBe(3);
|
||||||
|
expect(valid(old, same)).toBe(true);
|
||||||
|
// Older still: one tree, or a trail.
|
||||||
|
expect(hues(restore<string, unknown>({ forks: { ref: "a", kids: [{ ref: "b" }, { ref: "c" }], pick: 1 } }, isStr))).toEqual({ a: "-", b: "-", c: 0 });
|
||||||
|
expect(hues(restore<string, unknown>({ trail: ["a", "b"] }, isStr))).toEqual({ a: "-", b: "-" });
|
||||||
|
// A colour out of the palette is not trusted.
|
||||||
|
expect(load({ ref: "a", hue: HUES }, isStr)!.hue).toBeUndefined();
|
||||||
|
expect(load({ ref: "a", hue: 1.5 }, isStr)!.hue).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user