Add terminal selection copy action (#2904)
This commit is contained in:
1 parent
ebe8afb1df
commit
1735e27d9e
1 file changed
+43
-14
@@ -31,6 +31,7 @@ import {
|
||||
useState,
|
||||
} from "react";
|
||||
import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";
|
||||
import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
|
||||
import { cn } from "~/lib/utils";
|
||||
import { type TerminalContextSelection } from "~/lib/terminalContext";
|
||||
import { useOpenInPreferredEditor } from "../editorPreferences";
|
||||
@@ -330,7 +331,7 @@ export function TerminalViewport({
|
||||
const selectionPointerRef = useRef<{ x: number; y: number } | null>(null);
|
||||
const selectionGestureActiveRef = useRef(false);
|
||||
const selectionActionRequestIdRef = useRef(0);
|
||||
const selectionActionOpenRef = useRef(false);
|
||||
const selectionActionMenuOpenRef = useRef(false);
|
||||
const selectionActionTimerRef = useRef<number | null>(null);
|
||||
const keybindingsRef = useRef(keybindings);
|
||||
const runtimeEnvKey = useMemo(() => runtimeEnvSignature(runtimeEnv), [runtimeEnv]);
|
||||
@@ -417,6 +418,7 @@ export function TerminalViewport({
|
||||
|
||||
const readSelectionAction = (): {
|
||||
position: { x: number; y: number };
|
||||
clipboardText: string;
|
||||
selection: TerminalContextSelection;
|
||||
} | null => {
|
||||
const activeTerminal = terminalRef.current;
|
||||
@@ -445,6 +447,7 @@ export function TerminalViewport({
|
||||
});
|
||||
return {
|
||||
position,
|
||||
clipboardText: selectionText,
|
||||
selection: {
|
||||
terminalId,
|
||||
terminalLabel: readTerminalLabel(),
|
||||
@@ -460,7 +463,7 @@ export function TerminalViewport({
|
||||
clearSelectionAction();
|
||||
return;
|
||||
}
|
||||
if (selectionActionOpenRef.current) {
|
||||
if (selectionActionMenuOpenRef.current) {
|
||||
return;
|
||||
}
|
||||
const nextAction = readSelectionAction();
|
||||
@@ -469,20 +472,46 @@ export function TerminalViewport({
|
||||
return;
|
||||
}
|
||||
const requestId = ++selectionActionRequestIdRef.current;
|
||||
selectionActionOpenRef.current = true;
|
||||
try {
|
||||
const clicked = await localApi.contextMenu.show(
|
||||
[{ id: "add-to-chat", label: "Add to chat" }],
|
||||
selectionActionMenuOpenRef.current = true;
|
||||
const clicked = await localApi.contextMenu
|
||||
.show(
|
||||
[
|
||||
{ id: "add-to-chat", label: "Add to chat" },
|
||||
{ id: "copy", label: "Copy" },
|
||||
],
|
||||
nextAction.position,
|
||||
);
|
||||
if (requestId !== selectionActionRequestIdRef.current || clicked !== "add-to-chat") {
|
||||
)
|
||||
.finally(() => {
|
||||
selectionActionMenuOpenRef.current = false;
|
||||
});
|
||||
if (requestId !== selectionActionRequestIdRef.current || clicked === null) {
|
||||
return;
|
||||
}
|
||||
switch (clicked) {
|
||||
case "add-to-chat":
|
||||
handleAddTerminalContext(nextAction.selection);
|
||||
terminalRef.current?.clearSelection();
|
||||
terminalRef.current?.focus();
|
||||
return;
|
||||
case "copy":
|
||||
try {
|
||||
await writeTextToClipboard(nextAction.clipboardText, "terminal selection");
|
||||
} catch (error) {
|
||||
if (requestId !== selectionActionRequestIdRef.current) {
|
||||
return;
|
||||
}
|
||||
const activeTerminal = terminalRef.current;
|
||||
if (activeTerminal) {
|
||||
writeSystemMessage(
|
||||
activeTerminal,
|
||||
error instanceof Error ? error.message : "Unable to copy terminal selection",
|
||||
);
|
||||
}
|
||||
}
|
||||
if (requestId === selectionActionRequestIdRef.current) {
|
||||
terminalRef.current?.focus();
|
||||
}
|
||||
return;
|
||||
}
|
||||
handleAddTerminalContext(nextAction.selection);
|
||||
terminalRef.current?.clearSelection();
|
||||
terminalRef.current?.focus();
|
||||
} finally {
|
||||
selectionActionOpenRef.current = false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user