Public: anyone can read this; the crew can change it.
A `:` palette jumps to any page by name, from the keyboard alone #10500
scout/forge:scout/10434-a-palette-jumps-to-any-page-by-name-from → main 4 commits fork
7 files
changed +191
−4
docs/features/hotkey-hints.md +7
| @@ -16,3 +16,10 @@ Every action with a key shows it without taking room. Hints are never a box in t | ||
| 16 | 16 | - Test: `test/hotkey-hints.test.ts`. It checks the stylesheet's rules and the rendered markup. It does not measure a rendered layout, because the test run has no layout engine. |
| 17 | 17 | |
| 18 | 18 | **Terminal:** none. Terminal screens already print their keys in their footer line, which is always visible, so there is nothing to reveal. |
| 19 | ||
| 20 | **`:` go to a page (#10434)** | |
| 21 | - `:` outside a field opens one line; typing narrows the forge's top-level pages (`src/web/go_pages.ts`, the one list), Enter opens the leading match (a name that starts with what you typed beats one that contains it), Esc closes. It reuses the palette's dialog classes: no new component, and the list has a fixed height so nothing shifts. | |
| 22 | - `?` help lists the same pages, read from the prompt's own markup. | |
| 23 | - Inside a field `:` types a colon; inside the open prompt the dialog owns the keys. Cmd/Ctrl K still opens the full command palette. | |
| 24 | - Left out: pages that belong to a repository (waves live at `/:owner/:name/waves`), so they are not top-level names. | |
| 25 | - Test: `test/keys-palette.test.ts`. |
src/web/client.ts +73 −2
| @@ -10,6 +10,7 @@ | ||
| 10 | 10 | * behaviour that a page depends on to be usable. |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | import { goPagesData } from "./go_pages.ts"; | |
| 13 | 14 | import { actNowJs } from "./act_now.ts"; |
| 14 | 15 | import { pickJs } from "./pick.ts"; |
| 15 | 16 | import { imageViewJs } from "./render/image.ts"; |
| @@ -1567,13 +1568,13 @@ const shellCore = /* js */ ` | ||
| 1567 | 1568 | // First press lays the keys over the page; the second clears them and opens the help. |
| 1568 | 1569 | if (doc.documentElement.hasAttribute('data-hints')) { hints(false); openHelp(); } else hints(true); |
| 1569 | 1570 | } |
| 1570 | else if (e.key === ':') { seq.length = 0; e.preventDefault(); openPalette(); } | |
| 1571 | else if (e.key === ':') { seq.length = 0; e.preventDefault(); openGoto(); } | |
| 1571 | 1572 | }); |
| 1572 | 1573 | |
| 1573 | 1574 | // ---- help overlay, generated from the shortcuts actually on the page ---- |
| 1574 | 1575 | var STATIC_KEYS = [ |
| 1575 | 1576 | ['j / k', 'Move between rows'], ['o or Enter', 'Open the focused row'], |
| 1576 | ['/', 'Focus search'], ['Cmd/Ctrl K', 'Command palette'], [':', 'Command palette'], ['?', 'This help'], | |
| 1577 | ['/', 'Focus search'], ['Cmd/Ctrl K', 'Command palette'], [':', 'Go to a page by name'], ['?', 'This help'], | |
| 1577 | 1578 | ['Esc', 'Leave a box, close a menu or dialog'], |
| 1578 | 1579 | ]; |
| 1579 | 1580 | // The rows a page earns by what it carries: a box to type in, a form |
| @@ -1622,6 +1623,14 @@ const shellCore = /* js */ ` | ||
| 1622 | 1623 | tbody.appendChild(keyRow(k, label)); |
| 1623 | 1624 | }); |
| 1624 | 1625 | pageKeys().concat(STATIC_KEYS).forEach(function (p) { tbody.appendChild(keyRow(p[0], p[1])); }); |
| 1626 | // The pages \`:\` reaches, read from the prompt's own list so the two cannot differ. | |
| 1627 | var names = gotoPages().map(function (p) { return p[0]; }); | |
| 1628 | if (names.length) { | |
| 1629 | var pr = doc.createElement('tr'); | |
| 1630 | var pk = doc.createElement('td'); var pkb = doc.createElement('kbd'); pkb.textContent = ':'; pk.appendChild(pkb); | |
| 1631 | var pl = doc.createElement('td'); pl.textContent = 'Pages: ' + names.join(', '); | |
| 1632 | pr.appendChild(pk); pr.appendChild(pl); tbody.appendChild(pr); | |
| 1633 | } | |
| 1625 | 1634 | table.appendChild(tbody); |
| 1626 | 1635 | if (body) { body.textContent = ''; body.appendChild(table); } |
| 1627 | 1636 | openModal(dlg); |
| @@ -1816,6 +1825,68 @@ const shellCore = /* js */ ` | ||
| 1816 | 1825 | }).catch(function () { say('danger', '${NOT_TOLD_WORDS}'); }); |
| 1817 | 1826 | } |
| 1818 | 1827 | |
| 1828 | // ---- go to a page: one line, the server's own list filtered in place ---- | |
| 1829 | var closeGoto = null; | |
| 1830 | // The pages ride in this script (cached once); the page wears only the viewer's level in data-go. | |
| 1831 | var GO_PAGES = ${JSON.stringify(goPagesData())}; | |
| 1832 | function gotoPages() { | |
| 1833 | var lv = +(doc.body.getAttribute('data-go') || 0); | |
| 1834 | return GO_PAGES.filter(function (p) { return p[2] <= lv; }); | |
| 1835 | } | |
| 1836 | function gotoDialog() { | |
| 1837 | var dlg = doc.getElementById('goto'); | |
| 1838 | if (dlg) return dlg; | |
| 1839 | dlg = doc.createElement('dialog'); | |
| 1840 | dlg.id = 'goto'; dlg.className = 'dialog palette'; dlg.setAttribute('aria-label', 'Go to a page'); | |
| 1841 | var input = doc.createElement('input'); | |
| 1842 | input.className = 'input palette-input'; input.type = 'text'; input.placeholder = ': page name…'; | |
| 1843 | input.setAttribute('aria-label', 'Go to a page'); input.setAttribute('autocomplete', 'off'); | |
| 1844 | input.setAttribute('autocapitalize', 'off'); input.spellcheck = false; | |
| 1845 | var box = doc.createElement('div'); box.className = 'palette-list'; box.id = 'goto-list'; | |
| 1846 | box.setAttribute('role', 'listbox'); box.setAttribute('aria-label', 'Pages'); | |
| 1847 | gotoPages().forEach(function (p) { | |
| 1848 | var a = doc.createElement('a'); a.className = 'palette-item'; a.setAttribute('role', 'option'); | |
| 1849 | a.href = p[1]; a.setAttribute('data-goto', p[0].toLowerCase()); | |
| 1850 | var sp = doc.createElement('span'); sp.className = 'grow'; sp.textContent = p[0]; a.appendChild(sp); box.appendChild(a); | |
| 1851 | }); | |
| 1852 | var foot = doc.createElement('div'); foot.className = 'palette-foot'; foot.textContent = 'Enter opens the first, Esc closes'; | |
| 1853 | dlg.appendChild(input); dlg.appendChild(box); dlg.appendChild(foot); | |
| 1854 | doc.body.appendChild(dlg); | |
| 1855 | return dlg; | |
| 1856 | } | |
| 1857 | function openGoto() { | |
| 1858 | var dlg = gotoDialog(); | |
| 1859 | if (!dlg.showModal) { location.href = '/search'; return; } | |
| 1860 | if (closeGoto) { closeGoto(); return; } | |
| 1861 | var input = dlg.querySelector('input'); | |
| 1862 | var links = Array.prototype.slice.call(dlg.querySelectorAll('[data-goto]')); | |
| 1863 | function first() { return links.filter(function (a) { return !a.hidden; })[0]; } | |
| 1864 | function paint() { | |
| 1865 | var q = input.value.trim().toLowerCase(); | |
| 1866 | var lead = null; | |
| 1867 | links.forEach(function (a) { | |
| 1868 | var n = a.getAttribute('data-goto'); | |
| 1869 | a.hidden = q !== '' && n.indexOf(q) < 0; | |
| 1870 | a.removeAttribute('aria-selected'); | |
| 1871 | }); | |
| 1872 | // A prefix leads a substring; ties keep the list's order. | |
| 1873 | links.forEach(function (a) { if (!a.hidden && !lead && (q === '' || a.getAttribute('data-goto').indexOf(q) === 0)) lead = a; }); | |
| 1874 | lead = lead || first(); | |
| 1875 | if (lead) { lead.setAttribute('aria-selected', 'true'); } | |
| 1876 | } | |
| 1877 | input.value = ''; | |
| 1878 | paint(); | |
| 1879 | closeGoto = openModal(dlg, function () { closeGoto = null; dlg.removeEventListener('keydown', onKey); input.removeEventListener('input', paint); }); | |
| 1880 | function onKey(e) { | |
| 1881 | if (e.key !== 'Enter' || e.isComposing) return; | |
| 1882 | var a = dlg.querySelector('[data-goto][aria-selected]'); | |
| 1883 | if (a) { e.preventDefault(); location.assign(a.getAttribute('href')); } | |
| 1884 | } | |
| 1885 | dlg.addEventListener('keydown', onKey); | |
| 1886 | input.addEventListener('input', paint); | |
| 1887 | input.focus(); | |
| 1888 | } | |
| 1889 | ||
| 1819 | 1890 | var closePalette = null; |
| 1820 | 1891 | function openPalette() { |
| 1821 | 1892 | var dlg = doc.getElementById('palette'); |
src/web/go_pages.ts added +56
| @@ -0,0 +1,56 @@ | ||
| 1 | /** | |
| 2 | * The forge's top-level pages, by name, as plain data: what the `:` prompt | |
| 3 | * filters and what `?` help lists beneath it. One list, one reader | |
| 4 | * (`goPagesFor`); the layout draws it and the keys script only hides and | |
| 5 | * shows what was drawn. | |
| 6 | */ | |
| 7 | ||
| 8 | export interface GoPage { | |
| 9 | /** The name typed to reach it. */ | |
| 10 | readonly name: string; | |
| 11 | readonly href: string; | |
| 12 | /** Who is shown it: anyone, a signed-in person, or a site admin. */ | |
| 13 | readonly who: "all" | "user" | "admin"; | |
| 14 | } | |
| 15 | ||
| 16 | export const GO_PAGES: readonly GoPage[] = [ | |
| 17 | { name: "Home", href: "/", who: "all" }, | |
| 18 | { name: "Apps", href: "/apps", who: "all" }, | |
| 19 | { name: "Explore", href: "/explore", who: "all" }, | |
| 20 | { name: "Packs", href: "/packs", who: "all" }, | |
| 21 | { name: "Front page", href: "/front", who: "all" }, | |
| 22 | { name: "Search", href: "/search", who: "all" }, | |
| 23 | { name: "Feed", href: "/feed", who: "user" }, | |
| 24 | { name: "Board", href: "/board", who: "user" }, | |
| 25 | { name: "Boards", href: "/boards", who: "user" }, | |
| 26 | { name: "Flow", href: "/flow", who: "user" }, | |
| 27 | { name: "Asks", href: "/asks", who: "user" }, | |
| 28 | { name: "Asked", href: "/asked", who: "user" }, | |
| 29 | { name: "Notes", href: "/notes", who: "user" }, | |
| 30 | { name: "Talk", href: "/talk", who: "user" }, | |
| 31 | { name: "Map", href: "/map", who: "user" }, | |
| 32 | { name: "Displays", href: "/displays", who: "user" }, | |
| 33 | { name: "Settings", href: "/settings", who: "user" }, | |
| 34 | { name: "System", href: "/system", who: "admin" }, | |
| 35 | { name: "Releases", href: "/releases", who: "admin" }, | |
| 36 | { name: "Forges", href: "/forges", who: "admin" }, | |
| 37 | { name: "Family", href: "/family", who: "admin" }, | |
| 38 | { name: "Audit", href: "/audit", who: "admin" }, | |
| 39 | ]; | |
| 40 | ||
| 41 | /** The pages this viewer may be sent to, in list order. */ | |
| 42 | export function goPagesFor(viewer: { signedIn: boolean; admin: boolean }): GoPage[] { | |
| 43 | return GO_PAGES.filter( | |
| 44 | (p) => p.who === "all" || (viewer.signedIn && (p.who === "user" || viewer.admin)), | |
| 45 | ); | |
| 46 | } | |
| 47 | ||
| 48 | /** The one digit the page wears so the keys script can pick its pages: 0 a stranger, 1 signed in, 2 admin. */ | |
| 49 | export function goLevel(viewer: { signedIn: boolean; admin: boolean }): 0 | 1 | 2 { | |
| 50 | return !viewer.signedIn ? 0 : viewer.admin ? 2 : 1; | |
| 51 | } | |
| 52 | ||
| 53 | /** The list as the keys script carries it: [name, href, level needed]. */ | |
| 54 | export function goPagesData(): [string, string, number][] { | |
| 55 | return GO_PAGES.map((p) => [p.name, p.href, p.who === "all" ? 0 : p.who === "user" ? 1 : 2]); | |
| 56 | } |
src/web/layout.ts +2 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * `/search`), and every dialog (a link to a real page). |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { goLevel } from "./go_pages.ts"; | |
| 10 | 11 | import { NEW_ISSUE_KEY } from "./script_keys.ts"; |
| 11 | 12 | import { newIssueHref } from "./issue_from.ts"; |
| 12 | 13 | import { OPENSEARCH_PATH, OPENSEARCH_TITLE, OPENSEARCH_TYPE } from "./opensearch.ts"; |
| @@ -865,7 +866,7 @@ export function page(options: PageOptions, status = 200): Response { | ||
| 865 | 866 | ? raw(`<style>${options.styleCss}</style>`) |
| 866 | 867 | : null} |
| 867 | 868 | </head> |
| 868 | <body> | |
| 869 | <body data-go="${goLevel({ signedIn: ctx.auth.user !== undefined, admin: ctx.auth.user?.isAdmin === true })}"> | |
| 869 | 870 | ${shell} |
| 870 | 871 | <script src="${shellAsset().path}" defer></script> |
| 871 | 872 | <script src="${beaconAsset().path}" defer></script> |
src/web/style/components.ts +1
| @@ -791,6 +791,7 @@ details[open] > summary.row > .row-lead { transform: rotate(90deg); } | ||
| 791 | 791 | background: none; color: var(--fg); font-size: 14px; text-align: left; |
| 792 | 792 | text-decoration: none; cursor: pointer; |
| 793 | 793 | } |
| 794 | .palette-item[hidden] { display: none; } | |
| 794 | 795 | .palette-item .grow { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } |
| 795 | 796 | .palette-item .ico { color: var(--faint); } |
| 796 | 797 | .palette-item .cat { |
test/help-keys.test.ts +1 −1
| @@ -52,5 +52,5 @@ test('every key STATIC_KEYS names is answered by the handler', () => { | ||
| 52 | 52 | |
| 53 | 53 | test(': opens the palette and is listed', () => { |
| 54 | 54 | expect(handler).toContain("e.key === ':'"); |
| 55 | expect(src).toContain("[':', 'Command palette']"); | |
| 55 | expect(src).toContain("[':', 'Go to a page by name']"); | |
| 56 | 56 | }); |
test/keys-palette.test.ts added +51
| @@ -0,0 +1,51 @@ | ||
| 1 | /** `:` opens a one-line go-to-page prompt built from one list of pages (#10434). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { readFileSync } from "node:fs"; | |
| 4 | import { snappyFixture, type SnappyFixture } from "../bench/snappy.ts"; | |
| 5 | import { GO_PAGES, goLevel, goPagesFor } from "../src/web/go_pages.ts"; | |
| 6 | ||
| 7 | let f: SnappyFixture; | |
| 8 | beforeAll(async () => { | |
| 9 | f = await snappyFixture(); | |
| 10 | }, 120_000); | |
| 11 | afterAll(async () => { | |
| 12 | await f.close(); | |
| 13 | }); | |
| 14 | ||
| 15 | const names = (v: { signedIn: boolean; admin: boolean }) => goPagesFor(v).map((p) => p.name); | |
| 16 | ||
| 17 | test("the page list is plain data, scoped by who is looking", () => { | |
| 18 | expect(names({ signedIn: false, admin: false })).toEqual(["Home", "Apps", "Explore", "Packs", "Front page", "Search"]); | |
| 19 | const user = names({ signedIn: true, admin: false }); | |
| 20 | for (const n of ["Board", "Flow", "Feed"]) expect(user).toContain(n); | |
| 21 | expect(user).not.toContain("System"); | |
| 22 | const admin = names({ signedIn: true, admin: true }); | |
| 23 | for (const n of ["System", "Releases"]) expect(admin).toContain(n); | |
| 24 | expect(new Set(GO_PAGES.map((p) => p.href)).size).toBe(GO_PAGES.length); | |
| 25 | for (const p of GO_PAGES) expect(p.href.startsWith("/")).toBe(true); | |
| 26 | }); | |
| 27 | ||
| 28 | test("a page wears only the viewer's level, and the script carries the list", async () => { | |
| 29 | const owner = await (await fetch(`${f.base}/`, { headers: { cookie: f.ownerCookie } })).text(); | |
| 30 | expect(owner).toMatch(/<body[^>]*data-go="[12]"/); | |
| 31 | expect(owner).not.toContain('id="goto"'); | |
| 32 | const anon = await (await fetch(`${f.base}/`)).text(); | |
| 33 | expect(anon).toMatch(/<body[^>]*data-go="0"/); | |
| 34 | expect(anon).not.toContain('href="/board"'); | |
| 35 | const src = readFileSync(new URL("../src/web/client.ts", import.meta.url), "utf8"); | |
| 36 | expect(src).toContain("goPagesData()"); | |
| 37 | expect(goLevel({ signedIn: false, admin: false })).toBe(0); | |
| 38 | expect(goLevel({ signedIn: true, admin: true })).toBe(2); | |
| 39 | }); | |
| 40 | ||
| 41 | test("the keys script binds : to the prompt, filters, and opens the first match", () => { | |
| 42 | const src = readFileSync(new URL("../src/web/client.ts", import.meta.url), "utf8"); | |
| 43 | expect(src).toContain("e.key === ':'"); | |
| 44 | expect(src).toMatch(/e\.key === ':'\) \{ seq\.length = 0; e\.preventDefault\(\); openGoto\(\)/); | |
| 45 | expect(src).toContain("function openGoto()"); | |
| 46 | expect(src).toContain("function gotoPages()"); | |
| 47 | expect(src).toContain("location.assign(a.getAttribute('href'))"); | |
| 48 | // The field guard returns before the key chain, so a colon typed in a box is a colon. | |
| 49 | expect(src.indexOf("t.matches('input, textarea, select, [contenteditable]')")).toBeLessThan(src.indexOf("openGoto();")); | |
| 50 | expect(src).toContain("Pages: "); | |
| 51 | }); |
new above
offline