From 84fa5b065b29fbca1e23eb4eef9e61039fe3eb1c Mon Sep 17 00:00:00 2001 From: kangfenmao Date: Mon, 18 Nov 2024 17:16:55 +0800 Subject: [PATCH] wip --- .../src/pages/home/Markdown/CodeBlock.tsx | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/src/renderer/src/pages/home/Markdown/CodeBlock.tsx b/src/renderer/src/pages/home/Markdown/CodeBlock.tsx index d8146324..f35ebdb6 100644 --- a/src/renderer/src/pages/home/Markdown/CodeBlock.tsx +++ b/src/renderer/src/pages/home/Markdown/CodeBlock.tsx @@ -1,4 +1,4 @@ -import { CheckOutlined } from '@ant-design/icons' +import { CheckOutlined, DownOutlined, RightOutlined } from '@ant-design/icons' import CopyIcon from '@renderer/components/Icons/CopyIcon' import { useSyntaxHighlighter } from '@renderer/context/SyntaxHighlighterProvider' import { useSettings } from '@renderer/hooks/useSettings' @@ -15,6 +15,36 @@ interface CodeBlockProps { [key: string]: any } +const CollapseIcon: React.FC<{ expanded: boolean; onClick: () => void }> = ({ expanded, onClick }) => { + return expanded ? ( + + ) : ( + + ) +} + +const ExpandButton: React.FC<{ + isExpanded: boolean + onClick: () => void + showButton: boolean +}> = ({ isExpanded, onClick, showButton }) => { + if (!showButton) return null + + return ( +
+ {isExpanded ? '收起' : '展开'} +
+ ) +} + const CodeBlock: React.FC = ({ children, className }) => { const match = /language-(\w+)/.exec(className || '') const showFooterCopyButton = children && children.length > 500 @@ -22,6 +52,7 @@ const CodeBlock: React.FC = ({ children, className }) => { const language = match?.[1] ?? 'text' const [html, setHtml] = useState('') const { codeToHtml } = useSyntaxHighlighter() + const [isExpanded, setIsExpanded] = useState(false) useEffect(() => { const loadHighlightedCode = async () => { @@ -38,7 +69,10 @@ const CodeBlock: React.FC = ({ children, className }) => { return match ? (
- {'<' + match[1].toUpperCase() + '>'} +
+ setIsExpanded(!isExpanded)} /> + {'<' + match[1].toUpperCase() + '>'} +
= ({ children, className }) => { borderTopLeftRadius: 0, borderTopRightRadius: 0, marginTop: 0, - fontSize + fontSize, + maxHeight: isExpanded ? 'none' : '300px', + overflow: 'hidden', + position: 'relative' }} /> + setIsExpanded(!isExpanded)} + showButton={!isExpanded || showFooterCopyButton} + /> {showFooterCopyButton && (