diff --git a/README.md b/README.md index 08b0879..6ed3547 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,13 @@ Press `?` for every key. The short version: - **Curves.** A faint curve runs from the words, row or link each pane was opened from (joined to their underline) to its title; the focused branch's is drawn stronger. Curves down one gutter run side by side, ordered to cross as little as they can. `/curves off` hides them. +- **Fork colours.** Each fork has a colour of its own, quiet and the same lightness for all: a + pane opened as the first from its pane goes on in that pane's colour, any other starts the next + of seven, so a plain trail is one colour and the first trail plain ink. The colour runs along the + curve into the rule under the pane's title (a tab's foot, a dash on a spine), at full strength on + the focused branch; a pane keeps its colour while it is open, through a reorder, or as an orphan. + `/colors line|top|edge|off` (or the menu) puts it there, in a band across each pane's top, down + its left edge, or nowhere. - **The active pane**, where the keys act, has a heavy rule across its top; the rest of the focused branch, a hairline. `q` closes it and every pane opened from it (whatever it shows: a section, a note, an article); each pane's x does the same for that pane. `P`, or a pane's pin, pins it: a diff --git a/e2e/forks.spec.ts b/e2e/forks.spec.ts index f852cc4..9900011 100644 --- a/e2e/forks.spec.ts +++ b/e2e/forks.spec.ts @@ -300,13 +300,12 @@ test("a curve per open edge, from where each pane was opened to its title; it fo expect(start.y).toBeLessThan(link.y + link.height); await nearIn(kinds.locator(".node.forked > .row", { hasText: "Simple cells" }), edge("Two kinds of cell", "Simple cells")[0].a); const title = (t: string) => pane(page, t).locator(".wiki-head h2, .pane-head h2").first(); + // Each runs on into the rule under its pane's title: one line. for (const t of ["Eukaryote", "Simple cells", "Complex cells"]) { const end = edge("Two kinds of cell", t)[0].b; - const r = (await title(t).boundingBox())!; - expect(end.x).toBeLessThanOrEqual(r.x + 2); - expect(end.x, t).toBeGreaterThan(r.x - 30); - expect(end.y).toBeGreaterThan(r.y - 4); - expect(end.y).toBeLessThan(r.y + r.height + 4); + const r = (await pane(page, t).locator(".wiki-head, .pane-head").first().boundingBox())!; + expect(Math.abs(end.x - r.x), t).toBeLessThan(2); + expect(Math.abs(end.y - (r.y + r.height - 0.5)), t).toBeLessThan(1.5); } await near('.wiki-pane .wiki-text a[data-wiki="Bacteria"]', edge("Eukaryote", "Bacteria")[0].a); // The focused branch (Two kinds of cell → Complex cells) in its own ink; the others not. diff --git a/src/ui/Menu.tsx b/src/ui/Menu.tsx index d5a9160..44c5c97 100644 --- a/src/ui/Menu.tsx +++ b/src/ui/Menu.tsx @@ -56,7 +56,7 @@ export function DocBar() { onClick={(e) => { e.stopPropagation(); const s = store.settings; - showMenu(topEntries({ view: s.view, autolink: s.autolink, auto: s.auto, speculate: s.speculate, curves: s.curves, panes: store.panes, doc: !!store.currentDoc, canRedo: store.canRedo() }, act), e.currentTarget as HTMLElement); + showMenu(topEntries({ view: s.view, autolink: s.autolink, auto: s.auto, speculate: s.speculate, curves: s.curves, colors: s.colors, panes: store.panes, doc: !!store.currentDoc, canRedo: store.canRedo() }, act), e.currentTarget as HTMLElement); }} > menu diff --git a/src/ui/Panes.tsx b/src/ui/Panes.tsx index bd7ea7f..b1b2450 100644 --- a/src/ui/Panes.tsx +++ b/src/ui/Panes.tsx @@ -48,7 +48,7 @@ interface PaneProps { } // `off` is left out: a node pane draws the same on the branch or off it, and moving between forks should redraw only what changed. -const key = (p: PaneProps) => `${refKey(p.p)}|${p.index}|${p.kids}|${p.active}|${!!p.t.pinned}|${p.root}|${p.orphan}|${JSON.stringify(p.aux)}`; +const key = (p: PaneProps) => `${refKey(p.p)}|${p.t.hue}|${p.index}|${p.kids}|${p.active}|${!!p.t.pinned}|${p.root}|${p.orphan}|${JSON.stringify(p.aux)}`; /** Redraws a pane only when something drawn in it changed (see `store.paneDirty`). An article pane is cheap to redraw: its HTML is set once. */ class Pane extends Component { @@ -63,11 +63,12 @@ class Pane extends Component { } } -/** The attributes every pane has: which it is, its place in its tree, and its id there. */ +/** The attributes every pane has: which it is, its place in its tree, its id there, and its branch's colour (forks.ts; style.css draws it). */ const paneAttrs = (props: PaneProps, name: string) => ({ "data-pane": name, "data-pane-index": props.index, "data-tid": props.t.id, + "data-hue": props.t.hue, }); const paneClass = (props: PaneProps) => `${props.active ? " active" : ""}${props.t.pinned ? " pinned" : ""}${props.orphan ? " orphaned" : ""}`; @@ -650,7 +651,7 @@ export function Panes() { }, []); return ( -
+
{order.map((c) => { const items = cols.get(c)!; @@ -700,6 +701,8 @@ interface Edge { ey: number; on: boolean; lost: boolean; + /** The child's branch colour (forks.ts). */ + hue?: number; } /** A font's descent, by its CSS font string: where its baseline sits above the bottom of an inline box. */ @@ -750,6 +753,8 @@ function curves(el: HTMLElement | null, svg: SVGSVGElement | null) { const els = paneEls(el); const colEls = [...el.children] as HTMLElement[]; const branch = new Set(store.branchNodes.map((n) => n.id)); + // Coloured lines: a curve runs on into the rule under its pane's title (or its tab's), one line. + const ruled = store.settings.colors === "line"; const edges: Edge[] = []; for (const x of store.panesPlaced) { const parent = x.parent; @@ -764,12 +769,17 @@ function curves(el: HTMLElement | null, svg: SVGSVGElement | null) { // Where it arrives: just before the child's title, or its tab's; below a crowded column's foot, at the foot. const cbox = ce.getBoundingClientRect(); let ey: number; + let ex = left + ARRIVE; let lost = false; - if (ce.hasAttribute("data-covered")) ey = cbox.top + TAB / 2; + if (ce.hasAttribute("data-covered")) ey = ruled ? cbox.top + TAB - 0.5 : cbox.top + TAB / 2; else { const scroll = ce.querySelector(".pane-scroll")!.getBoundingClientRect(); - const tr = ce.querySelector(".pane-head h2, .top h1, .wiki-head h2")?.getBoundingClientRect(); + const t = ce.querySelector(".pane-head h2, .top h1, .wiki-head h2"); + const tr = t?.getBoundingClientRect(); ey = tr && tr.bottom > scroll.top + 4 ? Math.max(scroll.top + 6, tr.top + Math.min(tr.height, 32) / 2) : scroll.top + 14; + // The rule: a page title's own underline, a heading's under its head. + const rr = ruled && t ? (t.matches("h1") ? tr : t.parentElement?.getBoundingClientRect()) : undefined; + if (rr && rr.bottom > scroll.top + 4 && rr.bottom < scroll.bottom) (ey = rr.bottom - 0.5), (ex = Math.max(left + ARRIVE, rr.left)); } if (ey > ccolBox.bottom - 4) { ey = ccolBox.bottom - 4; @@ -825,7 +835,7 @@ function curves(el: HTMLElement | null, svg: SVGSVGElement | null) { } } const gutter = colEls.indexOf(ccol); - edges.push({ from: parent.id, to: x.node.id, gutter, left: left - box.left, sx: sx - box.left, sy: sy - box.top, under: under && { x: under.x - box.left, y: under.y - box.top }, textRight: textRight - box.left, ex: left + ARRIVE - box.left, ey: ey - box.top, on: branch.has(x.node.id) && branch.has(parent.id), lost }); + edges.push({ from: parent.id, to: x.node.id, gutter, left: left - box.left, sx: sx - box.left, sy: sy - box.top, under: under && { x: under.x - box.left, y: under.y - box.top }, textRight: textRight - box.left, ex: ex - box.left, ey: ey - box.top, on: branch.has(x.node.id) && branch.has(parent.id), lost, hue: x.node.hue }); } // Lanes, gutter by gutter. const lane = new Map(); @@ -842,7 +852,7 @@ function curves(el: HTMLElement | null, svg: SVGSVGElement | null) { } for (const g of [...lanesUsed.keys()]) if (!gutters.has(g)) lanesUsed.delete(g); const fx = (n: number) => Math.round(n * 2) / 2; - const out: { key: string; from: number; to: number; lane: number; cls: string; d: string }[] = []; + const out: { key: string; from: number; to: number; lane: number; cls: string; d: string; hue: string }[] = []; edges.sort((a, b) => Number(a.on) - Number(b.on)); for (const e of edges) { const i = lane.get(e)!; @@ -865,7 +875,7 @@ function curves(el: HTMLElement | null, svg: SVGSVGElement | null) { `C${fx(g)} ${fx(e.ey - sg * r2 * (1 - k))} ${fx(g + r2 - k * r2)} ${fx(e.ey)} ${fx(g + r2)} ${fx(e.ey)}` + `H${fx(e.ex)}`; } - out.push({ key: `${e.from}>${e.to}`, from: e.from, to: e.to, lane: i, cls: `${e.on ? "on" : ""}${e.lost ? " lost" : ""}`, d }); + out.push({ key: `${e.from}>${e.to}`, from: e.from, to: e.to, lane: i, cls: `${e.on ? "on" : ""}${e.lost ? " lost" : ""}`, d, hue: e.hue === undefined ? "" : String(e.hue) }); } // Each curve keeps its element, changed only where it changed, so only what moved is painted again. const had = new Map(); @@ -882,6 +892,7 @@ function curves(el: HTMLElement | null, svg: SVGSVGElement | null) { if (p.getAttribute("d") !== c.d) p.setAttribute("d", c.d); if (p.getAttribute("class") !== c.cls) p.setAttribute("class", c.cls); if (p.dataset.lane !== String(c.lane)) p.dataset.lane = String(c.lane); + if ((p.dataset.hue ?? "") !== c.hue) c.hue ? (p.dataset.hue = c.hue) : delete p.dataset.hue; // In drawing order: the focused branch's last, on top. if (svg.children[k] !== p) svg.insertBefore(p, svg.children[k] ?? null); }); diff --git a/src/ui/actions.ts b/src/ui/actions.ts index 12d74af..40a7bae 100644 --- a/src/ui/actions.ts +++ b/src/ui/actions.ts @@ -229,6 +229,8 @@ export interface TopCtx { auto: boolean; speculate: boolean; curves: boolean; + /** Where each fork's colour shows in the panes view (`/colors`). */ + colors: "line" | "top" | "edge" | "off"; panes: boolean; /** A document is open (its links can be looked for and reviewed). */ doc: boolean; @@ -255,6 +257,7 @@ export function topEntries(c: TopCtx, act: Act): Entry[] { setting("Reconcile after edits", "auto", c.auto), setting("Write ahead while reading", "speculate", c.speculate), c.panes && setting("Curves between panes", "curves", c.curves), + c.panes && { t: "choices", label: "Fork colours", options: (["line", "top", "edge", "off"] as const).map((v): Choice => ({ label: v, on: c.colors === v, run: () => act.command(`/colors ${v}`) })) }, SEP, key(act, "Command line…", ":"), key(act, "Keys and help", "?"), diff --git a/src/ui/keys.ts b/src/ui/keys.ts index f966925..eca4189 100644 --- a/src/ui/keys.ts +++ b/src/ui/keys.ts @@ -123,6 +123,7 @@ export const HELP: [string, [string, string][]][] = [ ["/speculate on|off", "start writing the focused sentence's expansion early"], ["/view mode", "page layout: auto, column or panes; auto shows panes on windows 1600 px wide or more"], ["/curves on|off", "in panes: the faint curves from where each pane was opened to it"], + ["/colors line|top|edge|off", "in panes: each fork's own colour, on its curves and the rule under its panes' titles (line, the default), across their tops, down their left edges, or none"], ["/user name", "set the name on your commits"], ["/new title", "new blank document"], ["/open name", "open a document by title or id"], diff --git a/src/ui/store.ts b/src/ui/store.ts index 3617e7d..0f9036d 100644 --- a/src/ui/store.ts +++ b/src/ui/store.ts @@ -126,6 +126,9 @@ export interface Menu { } /** Page layout: one column with a margin, or panes side by side. Auto picks panes on wide windows. */ export type View = "auto" | "column" | "panes"; +/** Where a fork's colour shows in the panes view: along its curves and title rules, across its panes' tops, down their left edges, or nowhere. */ +export type Colors = "line" | "top" | "edge" | "off"; +const COLORS: Colors[] = ["line", "top", "edge", "off"]; /** The narrowest window, in CSS px, on which `auto` chooses panes. */ export const PANES_MIN_WIDTH = 1600; @@ -231,7 +234,7 @@ class Store { branchFrom: Commit | null = null; /** A note being previewed on hover, and where to draw it. */ popup: { note: NodeId; x: number; top: number; bottom: number } | null = null; - settings: { auto: boolean; speculate: boolean; view: View; autolink: AutoMode; curves: boolean } = { auto: true, speculate: false, view: "auto", autolink: "suggest", curves: true, ...lsGet("ie:settings", {}) }; + settings: { auto: boolean; speculate: boolean; view: View; autolink: AutoMode; curves: boolean; colors: Colors } = { auto: true, speculate: false, view: "auto", autolink: "suggest", curves: true, colors: "line", ...lsGet("ie:settings", {}) }; /** Link discovery: scans after writes, suggestions, Related, the background job. */ links = new Linker({ doc: () => this.doc, @@ -2179,6 +2182,7 @@ class Store { auto: () => this.set("auto", arg !== "off"), speculate: () => this.set("speculate", arg !== "off"), curves: () => (this.set("curves", arg !== "off"), this.emit()), + colors: () => this.setColors(arg), view: () => (["auto", "column", "panes"].includes(arg) ? this.setView(arg as View) : this.say(`/view auto, column or panes (now ${this.settings.view}, showing ${this.panes ? "panes" : "column"})`)), resolve: () => this.resolveAll(), new: () => (arg ? this.newDoc(arg) : this.openMenu("new")), @@ -2964,6 +2968,15 @@ class Store { this.say(arg === "on" ? "autolink on: sure links are made at once (u undoes); the rest are suggested" : arg === "suggest" ? "autolink suggest: links are only suggested; y takes one, S reviews them" : "autolink off: nothing is scanned automatically"); } + /** /colors: where each fork's colour shows in the panes view. */ + setColors(arg: string) { + const c = COLORS.find((x) => x === arg); + if (!c) return this.say(`/colors line|top|edge|off (now ${this.settings.colors}): each fork's colour along its curves and title rules, across its panes' tops, down their left edges, or none`); + this.settings = { ...this.settings, colors: c }; + lsSet("ie:settings", this.settings); + this.say(c === "off" ? "colors off: forks in plain ink" : `colors ${c}`); + } + /** /linkdoc, /linkall: scan in the background on the server; progress shows in the status line. */ async linkJob(scope: "doc" | "library", doc?: NodeId, quiet = false) { const r = await this.links.startJob(scope, doc); diff --git a/src/ui/style.css b/src/ui/style.css index e27ca4b..546e5e5 100644 --- a/src/ui/style.css +++ b/src/ui/style.css @@ -450,6 +450,57 @@ kbd, .help code { font-family: var(--mono); font-size: 0.86em; border: 1px solid .curves path.on { stroke: var(--curve-on); } .curves path.lost { stroke-dasharray: 1 3; } +/* + * Branch colours (forks.ts): a fork's panes and curves share a colour, a plain trail keeps one, and + * the first trail has none. Seven hues at one OKLCH lightness and a low chroma, in an order that + * keeps neighbours apart for red-green and blue-yellow colour blindness too. The focused branch's at + * full strength, the others' fainter. Where it shows, by `/colors` (.panes[data-colors]): line, the + * curve and the rule under the pane's title (its tab's foot, a dash on its spine); top, a band + * across the pane's top; edge, a band down its left; off, nowhere. Never the pane's ground. + */ +:root { + --hue-0: oklch(60% 0.1 75); + --hue-1: oklch(60% 0.1 165); + --hue-2: oklch(60% 0.1 300); + --hue-3: oklch(60% 0.1 30); + --hue-4: oklch(60% 0.1 210); + --hue-5: oklch(60% 0.1 120); + --hue-6: oklch(60% 0.1 255); + --curve-ink: rgb(0 0 0 / 0.42); +} +@media (prefers-color-scheme: dark) { + :root { + --hue-0: oklch(74% 0.085 75); + --hue-1: oklch(74% 0.085 165); + --hue-2: oklch(74% 0.085 300); + --hue-3: oklch(74% 0.085 30); + --hue-4: oklch(74% 0.085 210); + --hue-5: oklch(74% 0.085 120); + --hue-6: oklch(74% 0.085 255); + --curve-ink: rgb(255 255 255 / 0.42); + } +} +[data-hue="0"] { --hue: var(--hue-0); } +[data-hue="1"] { --hue: var(--hue-1); } +[data-hue="2"] { --hue: var(--hue-2); } +[data-hue="3"] { --hue: var(--hue-3); } +[data-hue="4"] { --hue: var(--hue-4); } +[data-hue="5"] { --hue: var(--hue-5); } +[data-hue="6"] { --hue: var(--hue-6); } +.panes:not([data-colors="off"]) :is(.pane, .curves path)[data-hue] { --branch: color-mix(in oklab, var(--hue) 70%, transparent); } +.panes:not([data-colors="off"]) :is(.pane.on-branch, .curves path.on)[data-hue] { --branch: var(--hue); } +.panes:not([data-colors="off"]) .curves path[data-hue] { stroke: var(--branch); } +.panes:not([data-colors="off"]) .curves path.on:not([data-hue]) { stroke: var(--curve-ink); } +/* line */ +[data-colors="line"] .pane[data-hue] > .pane-scroll > .pane-body > :is(.pane-head:not(.focused), .top:not(.focused) h1, .wiki:not(.embedded) > .wiki-head:not(.focused)) { border-bottom-color: var(--branch); } +[data-colors="line"] .pane[data-hue] > .tabline { border-bottom-color: var(--branch); } +[data-colors="line"] .pane[data-hue] > .spine::before { content: ""; position: absolute; top: 1.72rem; left: 50%; width: 0.9rem; margin-left: -0.45rem; height: 1px; background: var(--branch); } +/* top: under the active pane's ink rule, above the focused branch's hairline */ +[data-colors="top"] .pane[data-hue] { background-image: linear-gradient(var(--branch), var(--branch)); background-size: 100% 2px; background-repeat: no-repeat; } +[data-colors="top"] .pane[data-hue].on-branch:not(.active)::after { top: 2px; } +/* edge */ +[data-colors="edge"] .pane[data-hue] { background-image: linear-gradient(var(--branch), var(--branch)); background-size: 2px 100%; background-position: 1px 0; background-repeat: no-repeat; } + /* A pane's head: a section heading, smaller than the page title the first pane keeps. */ .pane-head { display: flex; align-items: baseline; gap: 0.6em; margin: 0 0 0.85rem; padding-bottom: 0.3rem; border-bottom: 1px solid var(--faint); cursor: pointer; } .pane-head.focused { border-bottom-color: var(--fg); } diff --git a/tests/actions.test.ts b/tests/actions.test.ts index bd149b1..0ba481e 100644 --- a/tests/actions.test.ts +++ b/tests/actions.test.ts @@ -21,7 +21,7 @@ const find = (es: Entry[], label: string) => items(es).find((i) => i.label === l const node = (more: Partial = {}): NodeCtx => ({ title: "Training", text: "Training\n\nIt is trained.", note: false, locked: false, target: false, library: false, doc: false, generating: true, ...more }); const sel = (more: Partial = {}): SelCtx => ({ text: "a probability for each possible next piece", article: false, panes: false, ...more }); -const top = (more: Partial = {}): TopCtx => ({ view: "auto", autolink: "suggest", auto: true, speculate: false, curves: true, panes: false, doc: true, canRedo: false, ...more }); +const top = (more: Partial = {}): TopCtx => ({ view: "auto", autolink: "suggest", auto: true, speculate: false, curves: true, colors: "line", panes: false, doc: true, canRedo: false, ...more }); /** Every key the page's handler takes, read from keys.ts: an item's key must be one of them. */ const handled = new Set([...readFileSync(new URL("../src/ui/keys.ts", import.meta.url), "utf8").matchAll(/case "([^"]+)":/g)].map((m) => m[1])); @@ -134,6 +134,10 @@ describe("actions", () => { expect(calls).toEqual(["command /view panes", "command /auto off", "command /speculate on"]); expect(labels(es)).not.toContain("Curves between panes"); expect(labels(topEntries(top({ panes: true }), act))).toContain("Curves between panes"); + const colors = topEntries(top({ panes: true }), act).find((e) => e.t === "choices" && e.label === "Fork colours") as Extract; + expect(colors.options.map((o) => `${o.label}${o.on ? "*" : ""}`)).toEqual(["line*", "top", "edge", "off"]); + colors.options[3].run(); + expect(calls.at(-1)).toBe("command /colors off"); expect(labels(topEntries(top({ doc: false }), act))).not.toContain("Review suggested links"); });