From 7cf142671de79ecadab0c1c38cbd098a418c2d3a Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Fri, 26 Jan 2024 22:49:11 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20copy=20code=20block=20button?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/markdown/CodeBlockToolbar.tsx | 43 +++++++++++++++++++ .../components/markdown/PreWithToolbar.tsx | 40 +++++++++++++++++ .../markdown/StyledMarkdownPreview.tsx | 10 +++++ 3 files changed, 93 insertions(+) create mode 100644 gui/src/components/markdown/CodeBlockToolbar.tsx create mode 100644 gui/src/components/markdown/PreWithToolbar.tsx diff --git a/gui/src/components/markdown/CodeBlockToolbar.tsx b/gui/src/components/markdown/CodeBlockToolbar.tsx new file mode 100644 index 000000000..60e481c68 --- /dev/null +++ b/gui/src/components/markdown/CodeBlockToolbar.tsx @@ -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 ( + + { + navigator.clipboard.writeText(props.text); + setCopied(true); + setTimeout(() => setCopied(false), 2000); + }} + > + {copied ? ( + + ) : ( + + )} + + + ); +} + +export default CodeBlockToolBar; diff --git a/gui/src/components/markdown/PreWithToolbar.tsx b/gui/src/components/markdown/PreWithToolbar.tsx new file mode 100644 index 000000000..adcae7772 --- /dev/null +++ b/gui/src/components/markdown/PreWithToolbar.tsx @@ -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 ( +
 setHovering(true)}
+      onMouseLeave={() => setHovering(false)}
+    >
+      {hovering && (
+        
+      )}
+      {props.children}
+    
+ ); +} + +export default PreWithToolbar; diff --git a/gui/src/components/markdown/StyledMarkdownPreview.tsx b/gui/src/components/markdown/StyledMarkdownPreview.tsx index e2939cac2..4471c3016 100644 --- a/gui/src/components/markdown/StyledMarkdownPreview.tsx +++ b/gui/src/components/markdown/StyledMarkdownPreview.tsx @@ -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( ); }, + pre: ({ node, ...preProps }) => { + console.log(node, preProps); + return props.showCodeBorder ? ( + + ) : ( +

+          );
+        },
       },
     },
   });