💄 copy code block button

This commit is contained in:
Nate Sesti 2024-01-26 22:49:11 -08:00
parent 4db11b1e54
commit 7cf142671d
3 changed files with 93 additions and 0 deletions

View 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;

View 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;

View file

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