The top node hands no colour down: every pane opened from it starts its own
Below it a first child still goes on in its parent's colour. A pane stored without a colour (a view from before colours, or from when the top's first trail had none) is coloured on load by the same rule, from the stored count. 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
8a54833680
commit
bc9557abec
2 files changed
+48
-34
No files matched your search
+19
-14
@@ -15,7 +15,8 @@
|
|||||||
*
|
*
|
||||||
* Each pane has its branch's colour (`hue`), fixed when it opens: a pane opened as its parent's first
|
* 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
|
* 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).
|
* is one colour and each fork starts a new one. The first tree's root (the top node) has none and so
|
||||||
|
* hands none down: every pane opened from it starts a colour of its own.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** How many colours branches cycle through (style.css `--hue-0` …). */
|
/** How many colours branches cycle through (style.css `--hue-0` …). */
|
||||||
@@ -59,10 +60,11 @@ export function chain<R, S>(refs: R[]): Tree<R, S> | null {
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A forest of one tree (none for no refs). */
|
/** A forest of one tree (none for no refs), the trail below its root in the first colour. */
|
||||||
export const plain = <R, S>(refs: R[]): Forest<R, S> => {
|
export const plain = <R, S>(refs: R[]): Forest<R, S> => {
|
||||||
const t = chain<R, S>(refs);
|
const t = chain<R, S>(refs);
|
||||||
return { trees: t ? [t] : [], at: 0 };
|
if (!t) return { trees: [], at: 0 };
|
||||||
|
return t.kids.length ? { trees: [{ ...t, kids: [tint(t.kids[0], 0)] }], at: 0, hues: 1 } : { trees: [t], at: 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
/** A tree with this colour on every pane (none: as it is). */
|
/** A tree with this colour on every pane (none: as it is). */
|
||||||
@@ -258,14 +260,14 @@ export function open<R, S>(f: Forest<R, S>, i: number, ref: R, fork: boolean, sa
|
|||||||
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 };
|
||||||
// The first child goes on in its parent's colour; another starts the next one.
|
// The first child goes on in its parent's colour; another, or one of the top node, starts the next one.
|
||||||
if (!n.kids.length) return { ...n, kids: [tint(leaf<R, S>(ref), n.hue)], pick: 0 };
|
if (!n.kids.length) return { ...n, kids: [tint(leaf<R, S>(ref), n.hue ?? hues.next())], pick: 0 };
|
||||||
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 };
|
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];
|
const was = n.kids[n.pick];
|
||||||
orphans.push(...survivors(was, colOf(t) + i + 1, true));
|
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 and its colour too.
|
// In its place, in its column and its colour too.
|
||||||
const put = tint(leaf<R, S>(ref), was.hue);
|
const put = tint(leaf<R, S>(ref), was.hue ?? hues.next(n.hue));
|
||||||
kids[n.pick] = was.rank === undefined ? put : { ...put, rank: was.rank };
|
kids[n.pick] = was.rank === undefined ? put : { ...put, rank: was.rank };
|
||||||
return { ...n, kids };
|
return { ...n, kids };
|
||||||
});
|
});
|
||||||
@@ -364,7 +366,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 { ...f, trees: [chain<R, S>(refs)!, ...orphans, ...rest], at: 0 };
|
const hues = palette(f);
|
||||||
|
const t = chain<R, S>(refs)!;
|
||||||
|
return hues.done({ ...f, trees: [t.kids.length ? { ...t, kids: [tint(t.kids[0], hues.next())] } : t, ...orphans, ...rest], at: 0 });
|
||||||
}
|
}
|
||||||
const hues = palette(f);
|
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> => {
|
||||||
@@ -377,10 +381,10 @@ 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: [tint(rest, n.hue)], pick: 0 };
|
if (!n.kids.length) return { ...n, kids: [tint(rest, n.hue ?? hues.next())], pick: 0 };
|
||||||
if (n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, tint(rest, hues.next(n.hue))], 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 ? tint(rest, c.hue) : c)) };
|
return { ...n, kids: n.kids.map((c, j) => (j === n.pick ? tint(rest, c.hue ?? hues.next(n.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 };
|
||||||
@@ -428,8 +432,9 @@ export function load<R, S>(o: unknown, ok: (r: unknown) => r is R): Tree<R, S> |
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 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. Stored from
|
* tree with one branch. An orphan stored first (its tree's root gone) is dropped. A pane stored
|
||||||
* before colours (no count of them), the panes are coloured as if opened in tree order.
|
* without a colour (from before colours, or before the top node's children had their own) is
|
||||||
|
* coloured as if opened in tree order.
|
||||||
*/
|
*/
|
||||||
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> {
|
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>;
|
let f: Forest<R, S>;
|
||||||
@@ -443,13 +448,13 @@ export function restore<R, S>(saved: { forest?: unknown; at?: unknown; forks?: u
|
|||||||
const t = saved.forks ? load<R, S>(saved.forks, ok) : chain<R, S>((saved.trail ?? []).filter(ok));
|
const t = saved.forks ? load<R, S>(saved.forks, ok) : chain<R, S>((saved.trail ?? []).filter(ok));
|
||||||
f = { trees: t ? [t] : [], at: 0 };
|
f = { trees: t ? [t] : [], at: 0 };
|
||||||
}
|
}
|
||||||
if (typeof saved.hues === "number" && Number.isInteger(saved.hues) && saved.hues >= 0) return { ...f, hues: saved.hues };
|
if (typeof saved.hues === "number" && Number.isInteger(saved.hues) && saved.hues >= 0) f.hues = saved.hues;
|
||||||
const hues = palette(f);
|
const hues = palette(f);
|
||||||
const paint = (t: Tree<R, S>, hue: number | undefined): Tree<R, S> => {
|
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)));
|
const kids = t.kids.map((k, j) => paint(k, k.hue ?? (j === 0 && hue !== undefined ? hue : hues.next(hue))));
|
||||||
return hue === undefined ? { ...t, kids } : { ...t, kids, 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)) });
|
return hues.done({ ...f, trees: f.trees.map((t) => paint(t, t.hue ?? (t.col ? hues.next() : undefined))) });
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 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. */
|
/** 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. */
|
||||||
|
|||||||
+29
-20
@@ -277,26 +277,30 @@ describe("branch colours", () => {
|
|||||||
const hues = (f: F) => Object.fromEntries(all(f).map((x) => [x.node.ref, x.node.hue ?? "-"]));
|
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);
|
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", () => {
|
it("the top node has none; every pane opened from it starts a colour, a plain trail below keeps it, each further child of a pane starts the next", () => {
|
||||||
let f: F = plain(["a", "b", "c"]);
|
let f: F = plain(["a", "b", "c"]);
|
||||||
expect(hues(f)).toEqual({ a: "-", b: "-", c: "-" });
|
expect(hues(f)).toEqual({ a: "-", b: 0, c: 0 });
|
||||||
f = open(f, 0, "x", true, same); // a second child of a: a new colour
|
f = open(f, 0, "x", true, same); // a second child of the top: a new colour
|
||||||
f = open(f, 1, "x1", true, same); // x's first child: x's 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, 2, "x2", false, same);
|
||||||
f = open(f, 1, "y", true, same); // a second child of x
|
f = open(f, 1, "y", true, same); // a second child of x
|
||||||
f = open(f, 0, "z", true, same); // a third child of a
|
f = open(f, 0, "z", true, same); // a third child of the top
|
||||||
expect(hues(f)).toEqual({ a: "-", b: "-", c: "-", x: 0, x1: 0, x2: 0, y: 1, z: 2 });
|
expect(hues(f)).toEqual({ a: "-", b: 0, c: 0, x: 1, x1: 1, x2: 1, y: 2, z: 3 });
|
||||||
expect(f.hues).toBe(3);
|
expect(f.hues).toBe(4);
|
||||||
|
// The first pane opened from the top, by an open, starts a colour too.
|
||||||
|
let g: F = plain(["a"]);
|
||||||
|
g = open(g, 0, "p", false, same);
|
||||||
|
g = open(g, 1, "q", false, same);
|
||||||
|
expect(hues(g)).toEqual({ a: "-", p: 0, q: 0 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("the palette goes round, never giving a fork its parent's colour", () => {
|
it("the palette goes round, never giving a fork its parent's colour", () => {
|
||||||
let f: F = plain(["a"]);
|
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);
|
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.
|
// k0, 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)]);
|
expect(all(f).slice(1).map((x) => x.node.hue ?? "-")).toEqual(Array.from({ length: HUES + 2 }, (_, i) => i % HUES));
|
||||||
// Forks of a coloured pane skip its colour when the palette comes round to it.
|
// Forks of a coloured pane skip its colour when the palette comes round to it.
|
||||||
let g: F = plain(["a"]);
|
let g: F = plain(["a"]);
|
||||||
g = open(g, 0, "b", true, same);
|
|
||||||
g = open(g, 0, "c", true, same); // 0
|
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
|
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, "c1", true, same); // c's first: 0
|
||||||
@@ -309,17 +313,18 @@ describe("branch colours", () => {
|
|||||||
f = open(f, 0, "x", true, same);
|
f = open(f, 0, "x", true, same);
|
||||||
f = open(f, 0, "y", true, same);
|
f = open(f, 0, "y", true, same);
|
||||||
f = open(focus(f, idOf(f, "x")), 0, "x'", false, same); // in x's place
|
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 });
|
expect(hues(f)).toEqual({ a: "-", b: 0, "x'": 1, y: 2 });
|
||||||
f = pin(f, idOf(f, "x'"), true);
|
f = pin(f, idOf(f, "x'"), true);
|
||||||
f = open(f, 0, "w", false, same); // beside a pinned pane: a fork
|
f = open(f, 0, "w", false, same); // beside a pinned pane: a fork
|
||||||
expect(hues(f).w).toBe(2);
|
expect(hues(f).w).toBe(3);
|
||||||
let g: F = plain(["a", "b"]);
|
let g: F = plain(["a", "b"]);
|
||||||
g = open(g, 0, "x", true, same);
|
g = open(g, 0, "x", true, same);
|
||||||
g = withBranch(g, ["a", "x", "x1", "x2"], same);
|
g = withBranch(g, ["a", "x", "x1", "x2"], same);
|
||||||
expect(hues(g)).toMatchObject({ x: 0, x1: 0, x2: 0 });
|
expect(hues(g)).toMatchObject({ x: 1, x1: 1, x2: 1 });
|
||||||
g = withBranch(g, ["a", "x", "q"], same); // in x1's place
|
g = withBranch(g, ["a", "x", "q"], same); // in x1's place
|
||||||
expect(hues(g).q).toBe(0);
|
expect(hues(g).q).toBe(1);
|
||||||
expect(hues(withBranch(g, ["r", "s"], same))).toMatchObject({ r: "-", s: "-" });
|
// A different top: its trail starts a colour; the top has none.
|
||||||
|
expect(hues(withBranch(g, ["r", "s", "t"], same))).toMatchObject({ r: "-", s: 2, t: 2 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("colours stay through a reorder, a close, a pin and a prune; an orphan keeps its own", () => {
|
it("colours stay through a reorder, a close, a pin and a prune; an orphan keeps its own", () => {
|
||||||
@@ -349,18 +354,22 @@ describe("branch colours", () => {
|
|||||||
f = open(f, 0, "y", true, same);
|
f = open(f, 0, "y", true, same);
|
||||||
f = pin(f, idOf(f, "x1"), true);
|
f = pin(f, idOf(f, "x1"), true);
|
||||||
f = close(f, idOf(f, "x"), idOf(f, "y")).forest;
|
f = close(f, idOf(f, "x"), idOf(f, "y")).forest;
|
||||||
expect(hues(f)).toEqual({ a: "-", b: "-", c: "-", y: 1, x1: 0 });
|
expect(hues(f)).toEqual({ a: "-", b: 0, c: 0, y: 2, x1: 1 });
|
||||||
const back = roundTrip(f);
|
const back = roundTrip(f);
|
||||||
expect(hues(back)).toEqual(hues(f));
|
expect(hues(back)).toEqual(hues(f));
|
||||||
expect(back.hues).toBe(f.hues);
|
expect(back.hues).toBe(f.hues);
|
||||||
// From before colours: the root trail plain, each further child and each orphan a new colour.
|
// From before colours: the top plain, each of its children, 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);
|
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(hues(old)).toEqual({ a: "-", b: 0, c: 0, c2: 1, x: 2, x1: 2, o: 3, o1: 3 });
|
||||||
expect(old.hues).toBe(3);
|
expect(old.hues).toBe(4);
|
||||||
expect(valid(old, same)).toBe(true);
|
expect(valid(old, same)).toBe(true);
|
||||||
|
// Stored with a count but the top's first trail uncoloured (as the first colours had it): coloured on, from the count.
|
||||||
|
const mid = restore<string, unknown>({ forest: [{ ref: "a", kids: [{ ref: "b", kids: [{ ref: "c" }] }, { ref: "x", hue: 0 }] }], at: 0, hues: 1 }, isStr);
|
||||||
|
expect(hues(mid)).toEqual({ a: "-", b: 1, c: 1, x: 0 });
|
||||||
|
expect(mid.hues).toBe(2);
|
||||||
// Older still: one tree, or a trail.
|
// 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>({ forks: { ref: "a", kids: [{ ref: "b" }, { ref: "c" }], pick: 1 } }, isStr))).toEqual({ a: "-", b: 0, c: 1 });
|
||||||
expect(hues(restore<string, unknown>({ trail: ["a", "b"] }, isStr))).toEqual({ a: "-", b: "-" });
|
expect(hues(restore<string, unknown>({ trail: ["a", "b"] }, isStr))).toEqual({ a: "-", b: 0 });
|
||||||
// A colour out of the palette is not trusted.
|
// A colour out of the palette is not trusted.
|
||||||
expect(load({ ref: "a", hue: HUES }, isStr)!.hue).toBeUndefined();
|
expect(load({ ref: "a", hue: HUES }, isStr)!.hue).toBeUndefined();
|
||||||
expect(load({ ref: "a", hue: 1.5 }, isStr)!.hue).toBeUndefined();
|
expect(load({ ref: "a", hue: 1.5 }, isStr)!.hue).toBeUndefined();
|
||||||
|
|||||||
Reference in new issue
Block a user