diff --git a/apps/web/content/docs/grab.md b/apps/web/content/docs/grab.md index 65b44bd..ecfecb7 100644 --- a/apps/web/content/docs/grab.md +++ b/apps/web/content/docs/grab.md @@ -18,16 +18,15 @@ the components that rendered it and where they live go on the clipboard: paste that into your agent and it can find the code. - drag a box, or shift and a click, to gather several elements. each gets a - number, and a tray at the bottom lists them, with a note for each and one - for all. `n` writes a note. -- enter, `c` or the tray's copy button copies them all, numbered, each note - under its element. + number, and a tray at the bottom lists them, with one note for them all. + `n` writes the note. +- enter, `c` or the tray's copy button copies them all, numbered, the note + last. - the arrows move to the parent, a child or a sibling. - escape or `⇧G` again turns grab off. ``` 1. [ in SaveButton (at src/App.jsx:5:5)] - Note: make it blue 2. [Docs in Nav (at src/Nav.jsx:12:9)] Note: same spacing for all of these diff --git a/apps/web/content/reference/grab.md b/apps/web/content/reference/grab.md index f9fb5d3..b2cd23c 100644 --- a/apps/web/content/reference/grab.md +++ b/apps/web/content/reference/grab.md @@ -52,11 +52,11 @@ while it is on: gathered, in a badge at its box's top left. the numbers close up when one goes. - a tray at the bottom of the window lists them: the number, the tag and the - component, a one-line note and a remove button. under the list are a note - for all and a "Copy N" button. `n` writes the boxed element's note where it - is gathered, or else the note for all. enter in a note, or `⌘↩` (`Ctrl -Enter` off a Mac) anywhere, copies. escape in the tray goes back to picking, - and what is typed in it never moves the box or copies. + component, and a remove button. under the list are one note for them all + and a "Copy N" button. `n` writes the note. `⌘↩` (`Ctrl Enter` off a Mac) + in the tray copies. escape in the tray goes back to picking, and what is + typed in it never moves the box or copies. the tray wears the page's theme: + dark on a dark page, light on a light one. - a click without shift, enter, `c` or the tray's button copies them all, the clicked one last where it is new, and grab ends. - up goes to the parent, down back the way it came or to the first child, left @@ -65,19 +65,18 @@ Enter` off a Mac) anywhere, copies. escape in the tray goes back to picking, copies. - a check and "Copied" show by the element for a moment, the box still on it. -one element with no notes copies as its line alone. more than one, or a note, -and each element is numbered, its note on the next line under it, and the note -for all goes last after a blank line. empty notes are left out: +one element with no note copies as its line alone. more than one, or a note, +and each element is numbered, and the note goes last after a blank line. an +empty note is left out: ``` 1. [ in SaveButton (at src/App.jsx:5:5)] - Note: make it blue 2. [Docs in Nav (at src/Nav.jsx:12:9)] Note: same spacing for all of these ``` -the json carries each element's `note`, and the note for all as `note`. +the json carries the note as `note`. the context of the element under the pointer is worked out while the pointer rests there, so a click copies it whole, inside the click, as Safari wants. diff --git a/packages/devknobs/CHANGELOG.md b/packages/devknobs/CHANGELOG.md index 9ff7312..693ceec 100644 --- a/packages/devknobs/CHANGELOG.md +++ b/packages/devknobs/CHANGELOG.md @@ -3,8 +3,8 @@ ## Unreleased - grab gathers many elements: drag a box around them, or shift and a click. each shows its number -- a tray lists what is gathered, with a note for each and one for all. `n` writes a note, enter, `c` or the tray's button copies them all -- more than one element, or a note, copies numbered, each note under its element and the note for all last +- a tray lists what is gathered, with one note for them all. it wears the page's theme. `n` writes the note, enter, `c` or the tray's button copies them all +- more than one element, or a note, copies numbered, the note last - letting go of shift no longer copies ## 0.7.5 diff --git a/packages/devknobs/src/grab/mode.ts b/packages/devknobs/src/grab/mode.ts index 9bf02b8..28be4bd 100644 --- a/packages/devknobs/src/grab/mode.ts +++ b/packages/devknobs/src/grab/mode.ts @@ -110,7 +110,7 @@ function onControl(event: Event): boolean { /** * Grab, on: the element under the pointer gets a box and a label, a click * copies it, shift and a click or a drag gather more, and the arrows walk the - * tree. What is gathered is listed in a tray, with a note for each and one for all. + * tree. What is gathered is listed in a tray, with one note for them all. * The context of the element under the pointer is worked out while it rests * there, so a click can copy it whole in the click itself, as Safari wants. * The box goes to an element once the pointer settles on it, and not to those @@ -142,7 +142,7 @@ export function startMode(options: ModeOptions, page: ModePage = PAGE): Mode { let hitTimer = 0; let settleTimer = 0; let stopped = false; - /** The gathered elements, with their notes, while there is one. */ + /** The gathered elements, with the note, while there is one. */ let tray: Tray | null = null; /** Where the primary button went down on the page, while it is down. */ let down: Point | null = null; @@ -184,7 +184,7 @@ export function startMode(options: ModeOptions, page: ModePage = PAGE): Mode { const kept = picked.filter((element) => element.isConnected); if (kept.length !== picked.length) { if (tray) { - // The tray drops the row's note too; its onRemove draws again, which takes the next one. + // The tray's onRemove draws again, which takes the next one. tray.remove(picked.findLastIndex((element) => !element.isConnected)); return; } @@ -218,7 +218,7 @@ export function startMode(options: ModeOptions, page: ModePage = PAGE): Mode { draw(); } - /** Take one out, its note with it where the tray has one. */ + /** Take one out, through the tray where there is one. */ function unpick(element: Element): void { const index = picked.indexOf(element); if (index < 0) return; @@ -255,8 +255,7 @@ export function startMode(options: ModeOptions, page: ModePage = PAGE): Mode { const entries = elements.map((element) => ready.get(element) ?? page.quickEntry(element)); const anchor = elements[elements.length - 1] ?? null; overlay.toast(copiedText(elements.length), anchor, true); - const notes = tray?.notes(); - void page.copyGrab(picksPayload(entries, notes?.each, notes?.all)).then((copied) => { + void page.copyGrab(picksPayload(entries, tray?.note())).then((copied) => { if (!copied) overlay.toast("Copy failed", anchor, false); }); for (const element of elements) warm(element); @@ -475,9 +474,7 @@ export function startMode(options: ModeOptions, page: ModePage = PAGE): Mode { if (tray && plain && !event.shiftKey && !isTyping(event) && letterMatches(event, "n")) { event.preventDefault(); event.stopImmediatePropagation(); - resolve(); - const index = current ? picked.indexOf(current) : -1; - tray.focusNote(index >= 0 ? index : null); + tray.focusNote(); return; } if (event.key !== "Enter" && !isCLike(event.key, event.code)) return; diff --git a/packages/devknobs/src/grab/overlay.ts b/packages/devknobs/src/grab/overlay.ts index 3aa8ab2..a6c5db3 100644 --- a/packages/devknobs/src/grab/overlay.ts +++ b/packages/devknobs/src/grab/overlay.ts @@ -493,7 +493,10 @@ export interface Overlay { * element it would gather. Null takes both away. */ marquee(rect: { left: number; top: number; right: number; bottom: number } | null, preview: Element[]): void; - /** Put a node of another part of grab on the layer, over all of it, with its own styles. It goes with the layer. */ + /** + * Put a node of another part of grab on the layer, over all of it, with its own styles. It + * gets the page's theme as `data-page`, not the label's inverted one, and goes with the layer. + */ mount(node: HTMLElement, css?: string): void; /** Fade out and go, once a toast is over where there is one. */ destroy(): void; @@ -722,8 +725,7 @@ export function createOverlay(quiet = false, model = false): BoxOverlay { if (isCustomColor(name)) layer.style.setProperty("--grab", name); else layer.style.removeProperty("--grab"); } - if (!bar) return; - for (const node of [label, ...mounted]) if (node.dataset.bar !== bar) node.dataset.bar = bar; + if (bar && label.dataset.bar !== bar) label.dataset.bar = bar; } function schedule(): void { @@ -1083,7 +1085,8 @@ export function createOverlay(quiet = false, model = false): BoxOverlay { mountedCss.add(css); addStyle(root, css); } - if (bar) node.dataset.bar = bar; + // The page's own theme, read now: a mount can come before any element was read. + node.dataset.page = read(null).theme; for (let at = mounted.length - 1; at >= 0; at--) if (!mounted[at]?.isConnected) mounted.splice(at, 1); layer.append(node); mounted.push(node); diff --git a/packages/devknobs/src/grab/picks.ts b/packages/devknobs/src/grab/picks.ts index 5aa3921..3f1caa1 100644 --- a/packages/devknobs/src/grab/picks.ts +++ b/packages/devknobs/src/grab/picks.ts @@ -1,8 +1,5 @@ import type { GrabEntry, GrabPayload } from "./types"; -/** Indent for a note under a numbered element. */ -const NOTE_INDENT = " "; - /** What a copy takes: the gathered elements, in order, and the one clicked last if it is new. */ export function flushPicks(picked: readonly T[], clicked: T | null): T[] { if (clicked === null || picked.includes(clicked)) return [...picked]; @@ -21,35 +18,20 @@ function cleanNote(note: string | undefined): string | undefined { /** * The text a copy of the picks puts on the clipboard. One element with no note is its - * line alone. Otherwise each element is numbered, its note on the next line under it, - * and the note for all goes last after a blank line. + * line alone. Otherwise each element is numbered, and the note goes last after a blank line. */ export function formatPicks(entries: readonly GrabEntry[], note?: string): string { - const overall = cleanNote(note); + const clean = cleanNote(note); const only = entries.length === 1 ? entries[0] : undefined; - if (only && !cleanNote(only.note) && !overall) return only.content; - const blocks = entries.map((entry, index) => { - const line = `${index + 1}. ${entry.content}`; - const own = cleanNote(entry.note); - if (!own) return line; - return `${line}\n${NOTE_INDENT}Note: ${own.split("\n").join(`\n${NOTE_INDENT}`)}`; - }); - if (overall) blocks.push(`${blocks.length > 0 ? "\n" : ""}Note: ${overall}`); + if (only && !clean) return only.content; + const blocks = entries.map((entry, index) => `${index + 1}. ${entry.content}`); + if (clean) blocks.push(`${blocks.length > 0 ? "\n" : ""}Note: ${clean}`); return blocks.join("\n"); } -/** A payload from the picks and the tray's notes, one per pick and one for all. Empty notes are left out. */ -export function picksPayload( - entries: readonly GrabEntry[], - notes: readonly (string | undefined)[] = [], - note?: string, -): GrabPayload { - const noted = entries.map((entry, index) => { - const { note: _old, ...rest } = entry; - const own = cleanNote(notes[index]); - return own ? { ...rest, note: own } : rest; - }); - const overall = cleanNote(note); - const content = formatPicks(noted, overall); - return overall ? { content, entries: noted, note: overall } : { content, entries: noted }; +/** A payload from the picks and the tray's note. An empty note is left out. */ +export function picksPayload(entries: readonly GrabEntry[], note?: string): GrabPayload { + const clean = cleanNote(note); + const content = formatPicks(entries, clean); + return clean ? { content, entries: [...entries], note: clean } : { content, entries: [...entries] }; } diff --git a/packages/devknobs/src/grab/tray.ts b/packages/devknobs/src/grab/tray.ts index 3331347..d1b6da7 100644 --- a/packages/devknobs/src/grab/tray.ts +++ b/packages/devknobs/src/grab/tray.ts @@ -6,8 +6,8 @@ const SHOW = 150; /** * The tray of gathered elements, at the bottom of the window inside grab's - * layer. It goes by the page as the label does: dark by default, light where - * the overlay sets `data-bar="light"` on it. + * layer. It wears the page's own theme, not the label's inverted one: dark by + * default, light where the overlay sets `data-page="light"` on it. */ export const TRAY_CSS = ` .tray { @@ -37,7 +37,7 @@ export const TRAY_CSS = ` font-size: 13px; line-height: 16px; } -.tray[data-bar="light"] { +.tray[data-page="light"] { --bar: #ffffff; --quiet: #737373; --rim: rgb(0 0 0 / 10%); @@ -56,6 +56,7 @@ export const TRAY_CSS = ` .layer[data-still] .tray { animation: none; } .tray .list { display: grid; + grid-template-columns: minmax(0, 1fr); gap: 2px; min-height: 0; overflow-y: auto; @@ -85,34 +86,28 @@ export const TRAY_CSS = ` font-variant-numeric: tabular-nums; } .tray .name { - flex: 0 1 auto; + flex: 1 1 auto; min-width: 0; - max-width: 45%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .tray .name .tag { color: var(--quiet); } -.tray input, .tray textarea { +.tray textarea { all: unset; + display: block; box-sizing: border-box; - min-width: 0; + width: 100%; padding: 4px 6px; color: inherit; background: var(--field); - border-radius: 4px; - font: inherit; -} -.tray input { flex: 1 1 auto; height: 24px; } -.tray textarea { - display: block; - width: 100%; border-radius: 8px; + font: inherit; white-space: pre-wrap; resize: none; } -.tray input::placeholder, .tray textarea::placeholder { color: var(--quiet); } -.tray input:focus-visible, .tray textarea:focus-visible, .tray button:focus-visible { +.tray textarea::placeholder { color: var(--quiet); } +.tray textarea:focus-visible, .tray button:focus-visible { outline: 1px solid var(--quiet, #a7a7a7); outline-offset: -1px; } @@ -159,7 +154,7 @@ export interface TrayItem { } export interface TrayOptions { - /** A row's remove button was pressed. The tray has dropped its note already. */ + /** A row's remove button was pressed. */ onRemove(index: number): void; onCopy(): void; } @@ -167,31 +162,20 @@ export interface TrayOptions { export interface Tray { node: HTMLElement; update(items: TrayItem[]): void; - /** Take a row out as its remove button does: its note goes, and `onRemove` is told. */ + /** Take a row out as its remove button does: `onRemove` is told. */ remove(index: number): void; - /** Each row's note and the overall one, as typed. */ - notes(): { each: string[]; all: string }; - /** Focus a row's note, or with null the overall note. */ - focusNote(index: number | null): void; + /** The note, as typed. */ + note(): string; + focusNote(): void; contains(target: EventTarget | null): boolean; destroy(): void; } -/** - * The notes kept when the list changes: the one at `removed` dropped, then - * cut or filled with empty ones to `length`. - */ -export function alignNotes(notes: readonly string[], removed: number | null, length: number): string[] { - const kept = removed === null ? [...notes] : notes.filter((_, index) => index !== removed); - return Array.from({ length }, (_, index) => kept[index] ?? ""); -} - interface Row { node: HTMLElement; num: HTMLElement; tag: HTMLElement; name: HTMLElement; - input: HTMLInputElement; remove: HTMLButtonElement; key: string; } @@ -202,8 +186,8 @@ function element(tag: K, className = ""): return node; } -function isField(target: EventTarget | null): target is HTMLInputElement | HTMLTextAreaElement { - return target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement; +function isField(target: EventTarget | null): target is HTMLTextAreaElement { + return target instanceof HTMLTextAreaElement; } export function createTray(options: TrayOptions): Tray { @@ -212,17 +196,17 @@ export function createTray(options: TrayOptions): Tray { node.setAttribute("role", "region"); node.setAttribute("aria-label", "Picked elements"); const list = element("div", "list"); - const all = element("textarea"); - all.rows = 2; - all.placeholder = "Note for all"; - all.setAttribute("aria-label", "Note for all"); + const note = element("textarea"); + note.rows = 2; + note.placeholder = "Note"; + note.setAttribute("aria-label", "Note"); const foot = element("div", "foot"); const hint = element("div", "hint"); hint.textContent = "⇧ click or drag to add · n note · ↩ copy · esc"; const copy = element("button", "copy"); copy.type = "button"; foot.append(hint, copy); - node.append(list, all, foot); + node.append(list, note, foot); const rows: Row[] = []; let shown = false; @@ -233,30 +217,18 @@ export function createTray(options: TrayOptions): Tray { num: element("span", "num"), tag: element("span", "tag"), name: element("span", "name"), - input: element("input"), remove: element("button", "remove"), key: "", }; - row.input.type = "text"; - row.input.placeholder = "Note"; - row.input.spellcheck = false; row.remove.type = "button"; row.remove.textContent = "×"; row.remove.addEventListener("click", () => removeAt(rows.indexOf(row))); - row.node.append(row.num, row.name, row.input, row.remove); + row.node.append(row.num, row.name, row.remove); return row; } function removeAt(index: number): void { if (index < 0 || index >= rows.length) return; - const notes = alignNotes( - rows.map((each) => each.input.value), - index, - rows.length - 1, - ); - rows.forEach((each, at) => { - if (at < notes.length) each.input.value = notes[at] ?? ""; - }); options.onRemove(index); } @@ -270,7 +242,6 @@ export function createTray(options: TrayOptions): Tray { row.name.replaceChildren(row.tag); if (item.label.name) row.name.append(` ${item.label.name}`); row.name.title = item.label.name ? `${item.label.tag} ${item.label.name}` : item.label.tag; - row.input.setAttribute("aria-label", `Note for ${number}`); row.remove.setAttribute("aria-label", `Remove ${number}`); } @@ -306,8 +277,7 @@ export function createTray(options: TrayOptions): Tray { return; } if (event.key === "Enter" && !event.isComposing) { - const command = event.metaKey || event.ctrlKey; - if (command || (target instanceof HTMLInputElement && !event.shiftKey)) { + if (event.metaKey || event.ctrlKey) { event.preventDefault(); event.stopPropagation(); options.onCopy(); @@ -336,13 +306,11 @@ export function createTray(options: TrayOptions): Tray { node, update, remove: removeAt, - notes() { - return { each: rows.map((row) => row.input.value), all: all.value }; + note() { + return note.value; }, - focusNote(index) { - const field = index === null ? all : rows[index]?.input; - field?.focus({ preventScroll: true }); - field?.scrollIntoView({ block: "nearest" }); + focusNote() { + note.focus({ preventScroll: true }); }, contains(target) { return target instanceof Node && node.contains(target); diff --git a/packages/devknobs/src/grab/types.ts b/packages/devknobs/src/grab/types.ts index 6eca3a4..1ab787c 100644 --- a/packages/devknobs/src/grab/types.ts +++ b/packages/devknobs/src/grab/types.ts @@ -45,15 +45,13 @@ export interface GrabEntry { source: ResolvedSource | null; stackContext: string; frames: GrabFrame[]; - /** What the user wrote about this element in the tray, trimmed. Left out when empty. */ - note?: string; } export interface GrabPayload { - /** Every entry's line, one per line, numbered with their notes when there is more than one or a note. */ + /** Every entry's line, one per line, numbered with the note last when there is more than one or a note. */ content: string; entries: GrabEntry[]; - /** The tray's note for all the elements, trimmed. Left out when empty. */ + /** The tray's note, trimmed. Left out when empty. */ note?: string; } diff --git a/packages/devknobs/test/grab-dwell.test.ts b/packages/devknobs/test/grab-dwell.test.ts index 5011c2c..fed4683 100644 --- a/packages/devknobs/test/grab-dwell.test.ts +++ b/packages/devknobs/test/grab-dwell.test.ts @@ -59,13 +59,12 @@ let marquees: (PageRect | null)[]; let exits: number; let mode: Mode | null = null; -/** A tray as the mode sees it: what it lists, the notes typed in it, and where the focus went. */ +/** A tray as the mode sees it: what it lists, the note typed in it, and how often the note took the focus. */ interface FakeTray { options: TrayOptions; items: string[]; - each: string[]; - all: string; - focused: (number | null)[]; + note: string; + focused: number; destroyed: boolean; } let trays: FakeTray[]; @@ -77,19 +76,18 @@ function tray(): FakeTray | undefined { } function createTray(options: TrayOptions): Tray { - const fake: FakeTray = { options, items: [], each: [], all: "", focused: [], destroyed: false }; + const fake: FakeTray = { options, items: [], note: "", focused: 0, destroyed: false }; trays.push(fake); return { node: {} as HTMLElement, update: (items) => { fake.items = items.map((item) => item.label.tag); }, - remove: (index) => { - fake.each = fake.each.filter((_, at) => at !== index); - options.onRemove(index); + remove: (index) => options.onRemove(index), + note: () => fake.note, + focusNote: () => { + fake.focused++; }, - notes: () => ({ each: fake.each, all: fake.all }), - focusNote: (index) => fake.focused.push(index), contains: (target) => target === inTray, destroy: () => { fake.destroyed = true; @@ -398,7 +396,7 @@ function gathered(): Element[] { } describe("gathering many", () => { - test("one element with no notes copies its line alone", () => { + test("one element with no note copies its line alone", () => { start(50); click(50); expect(texts).toEqual(["a"]); @@ -454,28 +452,26 @@ describe("gathering many", () => { expect(copies).toEqual([]); }); - test("backspace takes the boxed element out, and its note with it", () => { + test("backspace takes the boxed element out", () => { start(50); click(50, true); wait(40); move(250); click(250, true); - const fake = tray(); - if (fake) fake.each = ["", "for c"]; press("Backspace"); expect(gathered()).toEqual([as(a)]); - expect(fake?.each).toEqual([""]); + expect(tray()?.items).toEqual(["a"]); expect(copies).toEqual([]); }); - test("a gathered element taken off the page loses its own note, and the others keep theirs", () => { + test("gathered elements taken off the page leave the tray, the note stays", () => { start(50); click(50, true); click(150, true); click(250, true); const fake = tray(); expect(fake?.items).toEqual(["a", "b", "c"]); - if (fake) fake.each = ["for a", "for b", "for c"]; + if (fake) fake.note = "all of these"; a.isConnected = false; c.isConnected = false; wait(40); @@ -484,35 +480,32 @@ describe("gathering many", () => { move(150); expect(gathered()).toEqual([as(b)]); expect(fake?.items).toEqual(["b"]); - expect(fake?.each).toEqual(["for b"]); + expect(fake?.note).toBe("all of these"); }); - test("n writes a note for the boxed element, or for all", () => { + test("n focuses the note", () => { start(50); click(50, true); wait(40); move(250); click(250, true); press("n"); - expect(tray()?.focused).toEqual([1]); + expect(tray()?.focused).toBe(1); move(150); wait(40); press("n"); - expect(tray()?.focused).toEqual([1, null]); + expect(tray()?.focused).toBe(2); }); - test("a copy carries the notes, numbered", () => { + test("a copy carries the note last, the elements numbered", () => { start(50); click(50, true); click(250, true); const fake = tray(); - if (fake) { - fake.each = ["make it blue", " "]; - fake.all = "same spacing"; - } + if (fake) fake.note = " same spacing "; press("Enter"); expect(copies).toEqual([["a", "c"]]); - expect(texts).toEqual(["1. a\n Note: make it blue\n2. c\n\nNote: same spacing"]); + expect(texts).toEqual(["1. a\n2. c\n\nNote: same spacing"]); expect(exits).toBe(1); expect(fake?.destroyed).toBe(true); }); @@ -535,7 +528,7 @@ describe("gathering many", () => { expect(copies).toEqual([]); expect(gathered()).toEqual([as(a), as(c)]); expect(draws.length).toBe(before); - expect(tray()?.focused).toEqual([]); + expect(tray()?.focused).toBe(0); expect(exits).toBe(0); }); diff --git a/packages/devknobs/test/grab-picks.test.ts b/packages/devknobs/test/grab-picks.test.ts index 70b5058..f1aed97 100644 --- a/packages/devknobs/test/grab-picks.test.ts +++ b/packages/devknobs/test/grab-picks.test.ts @@ -3,8 +3,8 @@ import { joinEntries } from "../src/grab/context"; import { copiedText, flushPicks, formatPicks, picksPayload } from "../src/grab/picks"; import type { GrabEntry } from "../src/grab/types"; -function entry(content: string, note?: string): GrabEntry { - const base: GrabEntry = { +function entry(content: string): GrabEntry { + return { tagName: "button", componentName: "Save", content, @@ -13,7 +13,6 @@ function entry(content: string, note?: string): GrabEntry { stackContext: "", frames: [], }; - return note === undefined ? base : { ...base, note }; } const SAVE = '[