diff --git a/gui/src/components/gui/StepContainer.tsx b/gui/src/components/gui/StepContainer.tsx
index 245a1d57a..48f8cbc2a 100644
--- a/gui/src/components/gui/StepContainer.tsx
+++ b/gui/src/components/gui/StepContainer.tsx
@@ -124,7 +124,7 @@ function StepContainer(props: StepContainerProps) {
{(props.isLast || isHovered || typeof feedback !== "undefined") &&
!active && (
`
+ position: absolute;
+ ${(props) => (props.bottom ? "bottom: 3px;" : "top: -11px;")}
+ right: 10px;
+ display: flex;
+ padding: 1px 2px;
+ gap: 4px;
+ border: 0.5px solid #8888;
+ border-radius: ${defaultBorderRadius};
+ background-color: ${vscEditorBackground};
+`;
+
interface CodeBlockToolBarProps {
text: string;
bottom: boolean;
@@ -80,11 +111,92 @@ function isTerminalCodeBlock(language: string | undefined, text: string) {
);
}
+function getTerminalCommand(text: string): string {
+ return text.startsWith("$ ") ? text.slice(2) : text;
+}
+
+function useApplyAction(text: string, isTerminal: boolean) {
+ const [applying, setApplying] = useState(false);
+ const ideMessenger = useContext(IdeMessengerContext);
+
+ const handleApply = useCallback(() => {
+ if (applying) return;
+
+ if (isTerminal) {
+ ideMessenger.ide.runCommand(getTerminalCommand(text));
+ } else {
+ ideMessenger.post("applyToCurrentFile", { text });
+ setApplying(true);
+ setTimeout(() => setApplying(false), 2000);
+ }
+ }, [applying, isTerminal, text, ideMessenger]);
+
+ return { applying, handleApply };
+}
+
+function useCopyAction(text: string) {
+ const [copied, setCopied] = useState(false);
+ const ideMessenger = useContext(IdeMessengerContext);
+
+ const handleCopy = useCallback(() => {
+ if (isJetBrains()) {
+ ideMessenger.request("copyText", { text });
+ } else {
+ navigator.clipboard.writeText(text);
+ }
+
+ setCopied(true);
+ setTimeout(() => setCopied(false), 2000);
+ }, [text, ideMessenger]);
+
+ return { copied, handleCopy };
+}
+
+const ApplyButton = ({ isTerminal, applying, onClick }) => (
+
+
+ {applying ? (
+
+ ) : (
+
+ )}
+
+ {isTerminal ? "Run in terminal" : applying ? "Applying..." : "Apply"}
+
+
+
+);
+
+const CopyButton = ({ copied, onClick }) => (
+
+
+ {copied ? (
+ <>
+
+ Copied
+ >
+ ) : (
+ <>
+
+ Copy
+ >
+ )}
+
+
+);
+
function CodeBlockToolBar(props: CodeBlockToolBarProps) {
const ideMessenger = useContext(IdeMessengerContext);
- const [copied, setCopied] = useState
(false);
- const [applying, setApplying] = useState(false);
const dispatch = useDispatch();
+ const isTerminal = isTerminalCodeBlock(props.language, props.text);
+ const { applying, handleApply } = useApplyAction(props.text, isTerminal);
+ const { copied, handleCopy } = useCopyAction(props.text);
// Handle apply keyboard shortcut
useWebviewListener(
@@ -106,34 +218,33 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) {
});
}
- function onClickCopy(e) {
- const text = typeof props.text === "string" ? props.text : props.text;
- if (isJetBrains()) {
- ideMessenger.request("copyText", { text });
- } else {
- navigator.clipboard.writeText(text);
- }
-
- setCopied(true);
- setTimeout(() => setCopied(false), 2000);
- }
-
- function onClickActionButton() {
- if (isTerminalCodeBlock(props.language, props.text)) {
- let text = props.text;
- if (text.startsWith("$ ")) {
- text = text.slice(2);
- }
- ideMessenger.ide.runCommand(text);
- return;
- }
-
- if (applying) return;
- ideMessenger.post("applyToCurrentFile", {
- text: props.text,
- });
- setApplying(true);
- setTimeout(() => setApplying(false), 2000);
+ if (!props.filename) {
+ return (
+
+
+ {!isJetBrains() && isTerminal && (
+
+
+
+ )}
+
+ ideMessenger.post("insertAtCursor", { text: props.text })
+ }
+ >
+
+
+
+
+
+ );
}
return (
@@ -155,51 +266,13 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) {