-
Notifications
You must be signed in to change notification settings - Fork 333
feat: implicit viewport grab + CLI peek command #464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ac22acf
47b95b6
b3b620e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import path from "node:path"; | ||
| import { Command } from "commander"; | ||
| import { | ||
| createReader, | ||
| extractPrompt, | ||
| isGrabText, | ||
| NO_READER_MESSAGE, | ||
| prepareWorkDir, | ||
| } from "../utils/clipboard.js"; | ||
| import { DEFAULT_WATCH_DIR } from "../utils/constants.js"; | ||
|
|
||
| interface PeekOptions { | ||
| dir: string; | ||
| textOnly?: boolean; | ||
| } | ||
|
|
||
| interface GrabPayload { | ||
| timestamp?: number; | ||
| version?: string; | ||
| content?: unknown; | ||
| entries?: unknown; | ||
| implicit?: boolean; | ||
| } | ||
|
|
||
| const fail = (message: string): never => { | ||
| process.stderr.write(`react-grab peek: ${message}\n`); | ||
| process.exit(1); | ||
| }; | ||
|
|
||
| export const peek = new Command() | ||
| .name("peek") | ||
| .description( | ||
| "read the current clipboard once and print it if it contains a React Grab grab (no daemon needed)", | ||
| ) | ||
| .option("-d, --dir <dir>", "work dir for the compiled native binary", DEFAULT_WATCH_DIR) | ||
| .option("--text-only", "skip the native reader and use the plain-text fallback") | ||
| .action((options: PeekOptions) => { | ||
| const dir = path.resolve(options.dir); | ||
|
|
||
| try { | ||
| prepareWorkDir(dir); | ||
| } catch (error) { | ||
| fail(String((error as Error)?.message ?? error)); | ||
| } | ||
|
|
||
| const reader = createReader({ textOnly: Boolean(options.textOnly), workDir: dir }); | ||
| if (!reader) { | ||
| fail(NO_READER_MESSAGE); | ||
| } | ||
|
|
||
| const snapshot = reader.read(); | ||
| if (!snapshot) { | ||
| process.exit(0); | ||
| } | ||
|
|
||
| let parsedGrab: GrabPayload | null = null; | ||
| if (snapshot.grab) { | ||
| try { | ||
| parsedGrab = JSON.parse(snapshot.grab) as GrabPayload; | ||
| } catch {} | ||
| } | ||
|
|
||
| if (parsedGrab && typeof parsedGrab.timestamp === "number") { | ||
| const record: Record<string, unknown> = { | ||
| source: parsedGrab.implicit ? "viewport" : "custom", | ||
| timestamp: parsedGrab.timestamp, | ||
| version: typeof parsedGrab.version === "string" ? parsedGrab.version : undefined, | ||
| content: typeof parsedGrab.content === "string" ? parsedGrab.content : "", | ||
| entries: Array.isArray(parsedGrab.entries) ? parsedGrab.entries : [], | ||
| implicit: Boolean(parsedGrab.implicit), | ||
| id: `peek-${parsedGrab.timestamp}`, | ||
| receivedAt: Date.now(), | ||
| }; | ||
| const prompt = extractPrompt(record); | ||
| if (prompt) record.prompt = prompt; | ||
| process.stdout.write(`${JSON.stringify(record)}\n`, () => process.exit(0)); | ||
| return; | ||
| } | ||
|
|
||
| if (snapshot.text && isGrabText(snapshot.text)) { | ||
| const now = Date.now(); | ||
| const record = { | ||
| source: "text", | ||
| timestamp: now, | ||
| content: snapshot.text, | ||
| entries: [], | ||
| implicit: false, | ||
| id: `peek-${now}`, | ||
| receivedAt: now, | ||
| }; | ||
| process.stdout.write(`${JSON.stringify(record)}\n`, () => process.exit(0)); | ||
| return; | ||
| } | ||
|
|
||
| process.exit(0); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,6 +138,7 @@ import { generateId } from "../utils/generate-id.js"; | |
| import { logRecoverableError } from "../utils/log-recoverable-error.js"; | ||
| import { getNearestEdge } from "../utils/get-nearest-edge.js"; | ||
| import { findShortcutAction } from "../utils/action-shortcuts.js"; | ||
| import { createViewportClipboard } from "./viewport-clipboard.js"; | ||
|
|
||
| const builtInPlugins = [copyPlugin, editPlugin, commentPlugin, openPlugin]; | ||
|
|
||
|
|
@@ -1996,6 +1997,7 @@ export const init = (rawOptions?: Options): ReactGrabAPI => { | |
| }; | ||
|
|
||
| const eventListenerManager = createEventListenerManager(); | ||
| const viewportClipboard = createViewportClipboard(); | ||
|
|
||
| const keyboardClaimer = setupKeyboardEventClaimer(); | ||
|
|
||
|
|
@@ -2570,6 +2572,9 @@ export const init = (rawOptions?: Options): ReactGrabAPI => { | |
| if (!event.isPrimary) return; | ||
| const isTouchPointer = event.pointerType === "touch"; | ||
| actions.setTouchMode(isTouchPointer); | ||
| if (!isActivated() && !isHoldingKeys() && !isCopying() && isEnabled()) { | ||
| viewportClipboard.scheduleWrite(); | ||
| } | ||
| if (isEventFromOverlay(event, "data-react-grab-ignore-events")) return; | ||
| if (isModalPopoverOpen()) return; | ||
| if (isSelectionInteractionLocked()) return; | ||
|
|
@@ -2594,6 +2599,16 @@ export const init = (rawOptions?: Options): ReactGrabAPI => { | |
| { passive: true }, | ||
| ); | ||
|
|
||
| eventListenerManager.addWindowListener( | ||
| "scroll", | ||
| () => { | ||
| if (!isActivated() && !isHoldingKeys() && !isCopying() && isEnabled()) { | ||
| viewportClipboard.scheduleWrite(); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Idle grab overwrites explicit copyMedium Severity Implicit Reviewed by Cursor Bugbot for commit 47b95b6. Configure here. |
||
| }, | ||
| { passive: true }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scroll listener misses nested scrollersMedium Severity The new Reviewed by Cursor Bugbot for commit ac22acf. Configure here. |
||
|
|
||
| eventListenerManager.addWindowListener( | ||
| "pointerdown", | ||
| (event: PointerEvent) => { | ||
|
|
@@ -3665,6 +3680,7 @@ export const init = (rawOptions?: Options): ReactGrabAPI => { | |
| dispose: () => { | ||
| disposed = true; | ||
| hasInited = false; | ||
| viewportClipboard.cancel(); | ||
| disposeRenderer?.(); | ||
| stopToolbarMenuTracking?.(); | ||
| stopToolbarMenuTracking = null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| import { | ||
| getFiberFromHostInstance, | ||
| isInstrumentationActive, | ||
| getDisplayName, | ||
| isCompositeFiber, | ||
| type Fiber, | ||
| } from "bippy"; | ||
| import { getSource, isSourceFile } from "bippy/source"; | ||
| import { VERSION, VIEWPORT_CLIPBOARD_DEBOUNCE_MS } from "../constants.js"; | ||
| import { collectViewportElements } from "../utils/collect-viewport-elements.js"; | ||
| import { getTagName } from "../utils/get-tag-name.js"; | ||
| import { normalizeFilePath } from "../utils/normalize-file-path.js"; | ||
| import { isUsefulComponentName } from "../utils/is-useful-component-name.js"; | ||
|
|
||
| const REACT_GRAB_MIME_TYPE = "application/x-react-grab"; | ||
| const WEB_CUSTOM_MIME_TYPE = `web ${REACT_GRAB_MIME_TYPE}`; | ||
|
|
||
| interface ViewportEntry { | ||
| tagName: string; | ||
| componentName?: string; | ||
| filePath?: string; | ||
| lineNumber?: number; | ||
| columnNumber?: number; | ||
| } | ||
|
|
||
| interface ViewportClipboardController { | ||
| scheduleWrite: () => void; | ||
| cancel: () => void; | ||
| } | ||
|
|
||
| const getComponentNameFromFiber = (fiber: Fiber): string | undefined => { | ||
| let currentFiber: Fiber | null = fiber.return ?? null; | ||
| while (currentFiber) { | ||
| if (isCompositeFiber(currentFiber)) { | ||
| const name = getDisplayName(currentFiber.type); | ||
| if (name && isUsefulComponentName(name)) { | ||
| return name; | ||
| } | ||
| } | ||
| currentFiber = currentFiber.return ?? null; | ||
| } | ||
| return undefined; | ||
| }; | ||
|
|
||
| const resolveElementEntry = async (element: Element): Promise<ViewportEntry | null> => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const fiber = getFiberFromHostInstance(element); | ||
| if (!fiber) return null; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fiber lookup skips parent walkMedium Severity
Reviewed by Cursor Bugbot for commit ac22acf. Configure here. |
||
|
|
||
| const tagName = getTagName(element); | ||
| const componentName = getComponentNameFromFiber(fiber); | ||
|
|
||
| let filePath: string | undefined; | ||
| let lineNumber: number | undefined; | ||
| let columnNumber: number | undefined; | ||
|
|
||
| try { | ||
| const source = await getSource(fiber); | ||
| if (source?.fileName && isSourceFile(source.fileName)) { | ||
| filePath = normalizeFilePath(source.fileName); | ||
| lineNumber = source.lineNumber ?? undefined; | ||
| columnNumber = source.columnNumber ?? undefined; | ||
| } | ||
| } catch {} | ||
|
|
||
| return { tagName, componentName, filePath, lineNumber, columnNumber }; | ||
| }; | ||
|
|
||
| const formatEntryReference = (entry: ViewportEntry): string => { | ||
| const tag = entry.componentName | ||
| ? `<${entry.tagName}>` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| : `<${entry.tagName} />`; | ||
|
|
||
| const locationParts: string[] = []; | ||
| if (entry.componentName) { | ||
| locationParts.push(`in ${entry.componentName}`); | ||
| } | ||
| if (entry.filePath) { | ||
| const locationSuffix = | ||
| entry.lineNumber != null && entry.columnNumber != null | ||
| ? `:${entry.lineNumber}:${entry.columnNumber}` | ||
| : entry.lineNumber != null | ||
| ? `:${entry.lineNumber}` | ||
| : ""; | ||
| locationParts.push(`(at ${entry.filePath}${locationSuffix})`); | ||
| } | ||
|
|
||
| const location = locationParts.length > 0 ? ` ${locationParts.join(" ")}` : ""; | ||
| return `[${tag}${location}]`; | ||
| }; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const writeToClipboard = async (content: string, metadata: string): Promise<boolean> => { | ||
| if (!navigator.clipboard) return false; | ||
|
|
||
| try { | ||
| if (typeof ClipboardItem !== "undefined") { | ||
| const textBlob = new Blob([content], { type: "text/plain" }); | ||
| const customBlob = new Blob([metadata], { type: REACT_GRAB_MIME_TYPE }); | ||
| const item = new ClipboardItem({ | ||
| "text/plain": textBlob, | ||
| [WEB_CUSTOM_MIME_TYPE]: customBlob, | ||
| }); | ||
| await navigator.clipboard.write([item]); | ||
| return true; | ||
| } | ||
| } catch {} | ||
|
|
||
| try { | ||
| await navigator.clipboard.writeText(content); | ||
| return true; | ||
| } catch {} | ||
|
Comment on lines
+91
to
+110
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚩 Clipboard API writes will silently fail in most contexts without user activation The Was this helpful? React with 👍 or 👎 to provide feedback.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is by design — the feature targets Chrome on localhost (where |
||
|
|
||
| return false; | ||
| }; | ||
|
|
||
| export const createViewportClipboard = (): ViewportClipboardController => { | ||
| let timerId: ReturnType<typeof setTimeout> | null = null; | ||
| let lastContentHash = ""; | ||
| let isWriting = false; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const performWrite = async (): Promise<void> => { | ||
| if (isWriting) return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Concurrent write skips scheduled flushLow Severity When Reviewed by Cursor Bugbot for commit ac22acf. Configure here. |
||
| if (!isInstrumentationActive()) return; | ||
|
|
||
| isWriting = true; | ||
| try { | ||
| const elements = collectViewportElements(); | ||
| if (elements.length === 0) return; | ||
|
|
||
| const entryResults = await Promise.allSettled( | ||
| elements.map(resolveElementEntry), | ||
| ); | ||
|
|
||
| const entries: ViewportEntry[] = []; | ||
| for (const result of entryResults) { | ||
| if (result.status === "fulfilled" && result.value) { | ||
| entries.push(result.value); | ||
| } | ||
| } | ||
|
|
||
| if (entries.length === 0) return; | ||
|
|
||
| const deduplicatedEntries: ViewportEntry[] = []; | ||
| const seenKeys = new Set<string>(); | ||
| for (const entry of entries) { | ||
| const deduplicationKey = entry.componentName && entry.filePath | ||
| ? `${entry.componentName}:${entry.filePath}:${entry.lineNumber ?? ""}` | ||
| : `${entry.tagName}:${entry.filePath ?? ""}:${entry.lineNumber ?? ""}`; | ||
| if (seenKeys.has(deduplicationKey)) continue; | ||
| seenKeys.add(deduplicationKey); | ||
| deduplicatedEntries.push(entry); | ||
| } | ||
|
|
||
| const references = deduplicatedEntries.map(formatEntryReference); | ||
| const content = references.join("\n"); | ||
|
vercel[bot] marked this conversation as resolved.
|
||
|
|
||
| if (content === lastContentHash) return; | ||
|
|
||
| const metadata = JSON.stringify({ | ||
| version: VERSION, | ||
| content, | ||
| entries: deduplicatedEntries.map((entry) => ({ | ||
| tagName: entry.tagName, | ||
| componentName: entry.componentName, | ||
| content: formatEntryReference(entry), | ||
| })), | ||
| timestamp: Date.now(), | ||
| implicit: true, | ||
| }); | ||
|
|
||
| const didWrite = await writeToClipboard(content, metadata); | ||
| if (didWrite) { | ||
| lastContentHash = content; | ||
| } | ||
| } catch {} finally { | ||
| isWriting = false; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Write may run after disposeLow Severity
Reviewed by Cursor Bugbot for commit 47b95b6. Configure here. |
||
| }; | ||
|
|
||
| const scheduleWrite = (): void => { | ||
| if (timerId !== null) { | ||
| clearTimeout(timerId); | ||
| } | ||
| timerId = setTimeout(() => { | ||
| timerId = null; | ||
| void performWrite(); | ||
| }, VIEWPORT_CLIPBOARD_DEBOUNCE_MS); | ||
|
cursor[bot] marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debounced write overwrites grabsHigh Severity Implicit viewport writes are scheduled while React Grab is idle, but Additional Locations (1)Reviewed by Cursor Bugbot for commit b3b620e. Configure here. |
||
| }; | ||
|
|
||
| const cancel = (): void => { | ||
| if (timerId !== null) { | ||
| clearTimeout(timerId); | ||
| timerId = null; | ||
| } | ||
| }; | ||
|
|
||
| return { scheduleWrite, cancel }; | ||
| }; | ||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Inconsistent timestamps in text-fallback record due to separate Date.now() calls
In the text-fallback path,
timestamp(line 83), the timestamp embedded inid(line 87), andreceivedAt(line 88) each callDate.now()independently, so they can produce different values. This is inconsistent with the structured-grab path (lines 66-72) whereparsedGrab.timestampis reused for bothtimestampandid. A downstream consumer matchingidagainsttimestampwould see a mismatch.Text-fallback path with three separate Date.now() calls
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in b3b620e — now captures
Date.now()once intonowand reuses it fortimestamp,id, andreceivedAt.