Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions apps/web/content/docs/grab.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. [<button type="button">Save</button> in SaveButton (at src/App.jsx:5:5)]
Note: make it blue
2. [<a href="/docs">Docs</a> in Nav (at src/Nav.jsx:12:9)]

Note: same spacing for all of these
Expand Down
19 changes: 9 additions & 10 deletions apps/web/content/reference/grab.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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. [<button type="button">Save</button> in SaveButton (at src/App.jsx:5:5)]
Note: make it blue
2. [<a href="/docs">Docs</a> 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.
Expand Down
4 changes: 2 additions & 2 deletions packages/devknobs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions packages/devknobs/src/grab/mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 7 additions & 4 deletions packages/devknobs/src/grab/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 10 additions & 28 deletions packages/devknobs/src/grab/picks.ts
Original file line number Diff line number Diff line change
@@ -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<T>(picked: readonly T[], clicked: T | null): T[] {
if (clicked === null || picked.includes(clicked)) return [...picked];
Expand All @@ -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] };
}
Loading
Loading