use hover toolbar if no filename

This commit is contained in:
Test 2024-09-18 10:10:06 -07:00
parent 22352a95ad
commit 5cc345a22f
4 changed files with 173 additions and 87 deletions

View file

@ -124,7 +124,7 @@ function StepContainer(props: StepContainerProps) {
{(props.isLast || isHovered || typeof feedback !== "undefined") &&
!active && (
<div
className="flex items-center gap-1 absolute -bottom-3 right-2"
className="flex items-center gap-1 absolute -bottom-2 right-2"
style={{
color: lightGray,
fontSize: getFontSize() - 3,

View file

@ -1,17 +1,26 @@
import {
ArrowLeftEndOnRectangleIcon,
CheckIcon,
ClipboardIcon,
PlayIcon,
CommandLineIcon,
} from "@heroicons/react/24/outline";
import { useContext, useState } from "react";
import { useCallback, useContext, useState } from "react";
import { useDispatch } from "react-redux";
import styled from "styled-components";
import { lightGray, vscEditorBackground, vscForeground } from "..";
import {
defaultBorderRadius,
lightGray,
vscEditorBackground,
vscForeground,
} from "..";
import { IdeMessengerContext } from "../../context/IdeMessenger";
import { useWebviewListener } from "../../hooks/useWebviewListener";
import { incrementNextCodeBlockToApplyIndex } from "../../redux/slices/uiStateSlice";
import { getAltKeyLabel, getFontSize, isJetBrains } from "../../util";
import FileIcon from "../FileIcon";
import ButtonWithTooltip from "../ButtonWithTooltip";
import { CopyButton as CopyButtonHeader } from "./CopyButton";
const ToolbarDiv = styled.div`
display: flex;
@ -41,6 +50,28 @@ const ToolbarButton = styled.button`
}
`;
const HoverDiv = styled.div`
position: sticky;
top: 0;
left: 100%;
height: 0;
width: 0;
overflow: visible;
z-index: 100;
`;
const InnerHoverDiv = styled.div<{ bottom: boolean }>`
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 }) => (
<ToolbarButton disabled={applying} onClick={onClick}>
<div
className="flex items-center gap-1 hover:brightness-125 transition-colors duration-200"
style={{ color: lightGray }}
>
{applying ? (
<CheckIcon className="w-3 h-3 text-green-500" />
) : (
<PlayIcon className="w-3 h-3" />
)}
<span>
{isTerminal ? "Run in terminal" : applying ? "Applying..." : "Apply"}
</span>
</div>
</ToolbarButton>
);
const CopyButton = ({ copied, onClick }) => (
<ToolbarButton onClick={onClick}>
<div
className="flex items-center gap-1 hover:brightness-125 transition-colors duration-200"
style={{ color: lightGray }}
>
{copied ? (
<>
<CheckIcon className="w-3 h-3 text-green-500 hover:brightness-125" />
<span>Copied</span>
</>
) : (
<>
<ClipboardIcon className="w-3 h-3 hover:brightness-125" />
<span>Copy</span>
</>
)}
</div>
</ToolbarButton>
);
function CodeBlockToolBar(props: CodeBlockToolBarProps) {
const ideMessenger = useContext(IdeMessengerContext);
const [copied, setCopied] = useState<boolean>(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 (
<HoverDiv>
<InnerHoverDiv bottom={props.bottom || false}>
{!isJetBrains() && isTerminal && (
<ButtonWithTooltip
text="Run in terminal"
disabled={applying}
style={{ backgroundColor: vscEditorBackground }}
onClick={handleApply}
>
<CommandLineIcon className="w-4 h-4" />
</ButtonWithTooltip>
)}
<ButtonWithTooltip
text="Insert at cursor"
style={{ backgroundColor: vscEditorBackground }}
onClick={() =>
ideMessenger.post("insertAtCursor", { text: props.text })
}
>
<ArrowLeftEndOnRectangleIcon className="w-4 h-4" />
</ButtonWithTooltip>
<CopyButtonHeader text={props.text} />
</InnerHoverDiv>
</HoverDiv>
);
}
return (
@ -155,51 +266,13 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) {
</div>
<div className="flex items-center">
<ToolbarButton onClick={onClickCopy}>
<div
className={`flex items-center gap-1 ] hover:brightness-125 transition-colors duration-200`}
style={{ color: lightGray }}
>
{copied ? (
<>
<CheckIcon className="w-3 h-3 text-green-500 hover:brightness-125" />
<span>Copied</span>
</>
) : (
<>
<ClipboardIcon className="w-3 h-3 hover:brightness-125" />
<span>Copy</span>
</>
)}
</div>
</ToolbarButton>
{isJetBrains() || (
<ToolbarButton
disabled={applying}
style={{ backgroundColor: vscEditorBackground }}
onClick={onClickActionButton}
>
<div
className={`flex items-center gap-1 hover:brightness-125 transition-colors duration-200`}
style={{ color: lightGray }}
>
{applying ? (
<CheckIcon className="w-3 h-3 text-green-500" />
) : (
<PlayIcon className="w-3 h-3" />
)}
<span>
{isTerminalCodeBlock(props.language, props.text)
? "Run in terminal"
: applying
? "Applying..."
: props.isNextCodeBlock
? `Apply (${getAltKeyLabel()}A)`
: "Apply"}
</span>
</div>
</ToolbarButton>
<CopyButton copied={copied} onClick={handleCopy} />
{!isJetBrains() && (
<ApplyButton
isTerminal={isTerminal}
applying={applying}
onClick={handleApply}
/>
)}
</div>
</ToolbarDiv>

View file

@ -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)}
>
<CodeBlockToolBar
isNextCodeBlock={nextCodeBlockIndex === props.codeBlockIndex}
text={copyValue}
bottom={toolbarBottom}
language={props.language}
filename={props.filename}
/>
{!!props.filename ? (
<CodeBlockToolBar
isNextCodeBlock={nextCodeBlockIndex === props.codeBlockIndex}
text={copyValue}
bottom={toolbarBottom}
language={props.language}
filename={props.filename}
/>
) : (
hovering && (
<CodeBlockToolBar
isNextCodeBlock={nextCodeBlockIndex === props.codeBlockIndex}
text={copyValue}
bottom={toolbarBottom}
language={props.language}
/>
)
)}
{props.children}
</TopDiv>
);

View file

@ -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;
}