✨ Run in terminal button

This commit is contained in:
Nate Sesti 2024-03-29 10:56:01 -07:00
parent 78a270050e
commit 2ddfb60240
3 changed files with 49 additions and 10 deletions

View file

@ -3,6 +3,7 @@ import { useState } from "react";
import styled from "styled-components";
import { defaultBorderRadius, vscEditorBackground } from "..";
import { isJetBrains, postToIde } from "../../util/ide";
import { WebviewIde } from "../../util/webviewIde";
import HeaderButtonWithText from "../HeaderButtonWithText";
import { CopyButton } from "./CopyButton";
@ -18,10 +19,12 @@ const TopDiv = styled.div`
const SecondDiv = styled.div<{ bottom: boolean }>`
position: absolute;
${(props) => (props.bottom ? "bottom: 5px;" : "top: -10px;")}
right: 4px;
${(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};
`;
@ -29,8 +32,11 @@ const SecondDiv = styled.div<{ bottom: boolean }>`
interface CodeBlockToolBarProps {
text: string;
bottom: boolean;
language: string | undefined;
}
const terminalLanguages = ["bash", "sh"];
function CodeBlockToolBar(props: CodeBlockToolBarProps) {
const [copied, setCopied] = useState(false);
const [applying, setApplying] = useState(false);
@ -40,10 +46,24 @@ function CodeBlockToolBar(props: CodeBlockToolBarProps) {
<SecondDiv bottom={props.bottom || false}>
{isJetBrains() || (
<HeaderButtonWithText
text={applying ? "Applying..." : "Apply to current file"}
text={
terminalLanguages.includes(props.language)
? "Run in terminal"
: applying
? "Applying..."
: "Apply to current file"
}
disabled={applying}
style={{ backgroundColor: vscEditorBackground }}
onClick={(e) => {
if (terminalLanguages.includes(props.language)) {
let text = props.text;
if (text.startsWith("$ ")) {
text = text.slice(2);
}
new WebviewIde().runCommand(text);
}
if (applying) return;
postToIde("applyToCurrentFile", { text: props.text });
setApplying(true);

View file

@ -19,9 +19,12 @@ function childrenToText(children: any) {
return children.map((child: any) => childToText(child)).join("");
}
function PreWithToolbar(props: { children: any }) {
const uiConfig = useUIConfig()
const toolbarBottom = uiConfig?.codeBlockToolbarPosition == 'bottom';
function PreWithToolbar(props: {
children: any;
language: string | undefined;
}) {
const uiConfig = useUIConfig();
const toolbarBottom = uiConfig?.codeBlockToolbarPosition == "bottom";
const [hovering, setHovering] = useState(false);
@ -46,9 +49,21 @@ function PreWithToolbar(props: { children: any }) {
onMouseEnter={() => setHovering(true)}
onMouseLeave={() => setHovering(false)}
>
{!toolbarBottom && hovering && <CodeBlockToolBar text={copyValue} bottom={toolbarBottom}></CodeBlockToolBar>}
{!toolbarBottom && hovering && (
<CodeBlockToolBar
text={copyValue}
bottom={toolbarBottom}
language={props.language}
></CodeBlockToolBar>
)}
{props.children}
{toolbarBottom && hovering && <CodeBlockToolBar text={copyValue} bottom={toolbarBottom}></CodeBlockToolBar>}
{toolbarBottom && hovering && (
<CodeBlockToolBar
text={copyValue}
bottom={toolbarBottom}
language={props.language}
></CodeBlockToolBar>
)}
</div>
);
}

View file

@ -110,7 +110,7 @@ const FadeInWords: React.FC = (props: any) => {
};
const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
props: StyledMarkdownPreviewProps
props: StyledMarkdownPreviewProps,
) {
const [reactContent, setMarkdownSource] = useRemark({
// remarkPlugins: [remarkMath],
@ -139,8 +139,12 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
);
},
pre: ({ node, ...preProps }) => {
const language = preProps?.children?.[0]?.props?.className
?.split(" ")
.find((word) => word.startsWith("language-"))
?.split("-")[1];
return props.showCodeBorder ? (
<PreWithToolbar>
<PreWithToolbar language={language}>
<SyntaxHighlightedPre {...preProps}></SyntaxHighlightedPre>
</PreWithToolbar>
) : (