💄 copy code block button
This commit is contained in:
parent
4db11b1e54
commit
7cf142671d
3 changed files with 93 additions and 0 deletions
43
gui/src/components/markdown/CodeBlockToolbar.tsx
Normal file
43
gui/src/components/markdown/CodeBlockToolbar.tsx
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import { CheckIcon, ClipboardIcon } from "@heroicons/react/24/outline";
|
||||
import { useState } from "react";
|
||||
import styled from "styled-components";
|
||||
import HeaderButtonWithText from "../HeaderButtonWithText";
|
||||
|
||||
const TopDiv = styled.div`
|
||||
padding: 4px 8px;
|
||||
|
||||
position: absolute;
|
||||
top: 4px;
|
||||
right: 4px;
|
||||
|
||||
display: flex;
|
||||
`;
|
||||
|
||||
interface CodeBlockToolBarProps {
|
||||
text: string;
|
||||
}
|
||||
|
||||
function CodeBlockToolBar(props: CodeBlockToolBarProps) {
|
||||
const [copied, setCopied] = useState(false);
|
||||
|
||||
return (
|
||||
<TopDiv>
|
||||
<HeaderButtonWithText
|
||||
text={copied ? "Copied!" : "Copy"}
|
||||
onClick={(e) => {
|
||||
navigator.clipboard.writeText(props.text);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
}}
|
||||
>
|
||||
{copied ? (
|
||||
<CheckIcon className="w-4 h-4 text-green-500" />
|
||||
) : (
|
||||
<ClipboardIcon className="w-4 h-4" />
|
||||
)}
|
||||
</HeaderButtonWithText>
|
||||
</TopDiv>
|
||||
);
|
||||
}
|
||||
|
||||
export default CodeBlockToolBar;
|
||||
40
gui/src/components/markdown/PreWithToolbar.tsx
Normal file
40
gui/src/components/markdown/PreWithToolbar.tsx
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import { useState } from "react";
|
||||
import CodeBlockToolBar from "./CodeBlockToolbar";
|
||||
|
||||
function PreWithToolbar(props: any) {
|
||||
const [hovering, setHovering] = useState(false);
|
||||
|
||||
function childToText(child: any) {
|
||||
if (typeof child === "string") {
|
||||
return child;
|
||||
} else if (child?.props) {
|
||||
return childToText(child.props?.children);
|
||||
} else if (Array.isArray(child)) {
|
||||
return childrenToText(child);
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function childrenToText(children: any) {
|
||||
return children.map((child: any) => childToText(child)).join("");
|
||||
}
|
||||
|
||||
return (
|
||||
<pre
|
||||
{...props}
|
||||
className="relative"
|
||||
onMouseEnter={() => setHovering(true)}
|
||||
onMouseLeave={() => setHovering(false)}
|
||||
>
|
||||
{hovering && (
|
||||
<CodeBlockToolBar
|
||||
text={childrenToText(props.children)}
|
||||
></CodeBlockToolBar>
|
||||
)}
|
||||
{props.children}
|
||||
</pre>
|
||||
);
|
||||
}
|
||||
|
||||
export default PreWithToolbar;
|
||||
|
|
@ -10,6 +10,7 @@ import {
|
|||
vscForeground,
|
||||
} from "..";
|
||||
import { getFontSize } from "../../util";
|
||||
import PreWithToolbar from "./PreWithToolbar";
|
||||
import "./markdown.css";
|
||||
|
||||
const StyledMarkdown = styled.div<{
|
||||
|
|
@ -51,6 +52,7 @@ const StyledMarkdown = styled.div<{
|
|||
border-radius: ${defaultBorderRadius};
|
||||
background-color: ${vscEditorBackground};
|
||||
font-size: 12px;
|
||||
font-family: "Courier New", Courier, monospace;
|
||||
}
|
||||
|
||||
background-color: ${vscBackground};
|
||||
|
|
@ -102,6 +104,14 @@ const StyledMarkdownPreview = memo(function StyledMarkdownPreview(
|
|||
</a>
|
||||
);
|
||||
},
|
||||
pre: ({ node, ...preProps }) => {
|
||||
console.log(node, preProps);
|
||||
return props.showCodeBorder ? (
|
||||
<PreWithToolbar {...preProps}></PreWithToolbar>
|
||||
) : (
|
||||
<pre {...preProps}></pre>
|
||||
);
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue