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) {
- -
- {copied ? ( - <> - - Copied - - ) : ( - <> - - Copy - - )} -
-
- - {isJetBrains() || ( - -
- {applying ? ( - - ) : ( - - )} - - {isTerminalCodeBlock(props.language, props.text) - ? "Run in terminal" - : applying - ? "Applying..." - : props.isNextCodeBlock - ? `Apply (${getAltKeyLabel()}A)` - : "Apply"} - -
-
+ + {!isJetBrains() && ( + )}
diff --git a/gui/src/components/markdown/PreWithToolbar.tsx b/gui/src/components/markdown/PreWithToolbar.tsx index c15ddaa64..433d44d4f 100644 --- a/gui/src/components/markdown/PreWithToolbar.tsx +++ b/gui/src/components/markdown/PreWithToolbar.tsx @@ -8,7 +8,8 @@ import { RootState } from "../../redux/store"; import CodeBlockToolBar from "./CodeBlockToolbar"; const TopDiv = styled.div` - border: 0.5px solid ${lightGray}80; + outline: 0.5px solid rgba(153, 153, 152); + outline-offset: -0.5px; border-radius: ${defaultBorderRadius}; margin-bottom: 8px; `; @@ -63,13 +64,25 @@ function PreWithToolbar(props: { onMouseEnter={() => setHovering(true)} onMouseLeave={() => setHovering(false)} > - + {!!props.filename ? ( + + ) : ( + hovering && ( + + ) + )} + {props.children} ); diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index 845ed445d..02be7c0a6 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -30,7 +30,7 @@ const StyledMarkdown = styled.div<{ overflow-x: scroll; overflow-y: hidden; - padding: ${(props) => (props.showBorder ? "4px 12px" : "0px 2px")}; + padding: ${(props) => (props.showBorder ? "8px 12px" : "0px 2px")}; margin: 0px; }