wip
This commit is contained in:
parent
7ecb35dfa7
commit
84fa5b065b
@ -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 CopyIcon from '@renderer/components/Icons/CopyIcon'
|
||||||
import { useSyntaxHighlighter } from '@renderer/context/SyntaxHighlighterProvider'
|
import { useSyntaxHighlighter } from '@renderer/context/SyntaxHighlighterProvider'
|
||||||
import { useSettings } from '@renderer/hooks/useSettings'
|
import { useSettings } from '@renderer/hooks/useSettings'
|
||||||
@ -15,6 +15,36 @@ interface CodeBlockProps {
|
|||||||
[key: string]: any
|
[key: string]: any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CollapseIcon: React.FC<{ expanded: boolean; onClick: () => void }> = ({ expanded, onClick }) => {
|
||||||
|
return expanded ? (
|
||||||
|
<DownOutlined style={{ cursor: 'pointer' }} onClick={onClick} />
|
||||||
|
) : (
|
||||||
|
<RightOutlined style={{ cursor: 'pointer' }} onClick={onClick} />
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const ExpandButton: React.FC<{
|
||||||
|
isExpanded: boolean
|
||||||
|
onClick: () => void
|
||||||
|
showButton: boolean
|
||||||
|
}> = ({ isExpanded, onClick, showButton }) => {
|
||||||
|
if (!showButton) return null
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
textAlign: 'center',
|
||||||
|
cursor: 'pointer',
|
||||||
|
padding: '8px',
|
||||||
|
color: 'var(--color-text-3)',
|
||||||
|
borderTop: '0.5px solid var(--color-code-background)'
|
||||||
|
}}
|
||||||
|
onClick={onClick}>
|
||||||
|
{isExpanded ? '收起' : '展开'}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
const CodeBlock: React.FC<CodeBlockProps> = ({ children, className }) => {
|
const CodeBlock: React.FC<CodeBlockProps> = ({ children, className }) => {
|
||||||
const match = /language-(\w+)/.exec(className || '')
|
const match = /language-(\w+)/.exec(className || '')
|
||||||
const showFooterCopyButton = children && children.length > 500
|
const showFooterCopyButton = children && children.length > 500
|
||||||
@ -22,6 +52,7 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ children, className }) => {
|
|||||||
const language = match?.[1] ?? 'text'
|
const language = match?.[1] ?? 'text'
|
||||||
const [html, setHtml] = useState<string>('')
|
const [html, setHtml] = useState<string>('')
|
||||||
const { codeToHtml } = useSyntaxHighlighter()
|
const { codeToHtml } = useSyntaxHighlighter()
|
||||||
|
const [isExpanded, setIsExpanded] = useState(false)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const loadHighlightedCode = async () => {
|
const loadHighlightedCode = async () => {
|
||||||
@ -38,7 +69,10 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ children, className }) => {
|
|||||||
return match ? (
|
return match ? (
|
||||||
<div className="code-block">
|
<div className="code-block">
|
||||||
<CodeHeader>
|
<CodeHeader>
|
||||||
|
<div style={{ display: 'flex', alignItems: 'center', gap: '8px' }}>
|
||||||
|
<CollapseIcon expanded={isExpanded} onClick={() => setIsExpanded(!isExpanded)} />
|
||||||
<CodeLanguage>{'<' + match[1].toUpperCase() + '>'}</CodeLanguage>
|
<CodeLanguage>{'<' + match[1].toUpperCase() + '>'}</CodeLanguage>
|
||||||
|
</div>
|
||||||
<CopyButton text={children} />
|
<CopyButton text={children} />
|
||||||
</CodeHeader>
|
</CodeHeader>
|
||||||
<CodeContent
|
<CodeContent
|
||||||
@ -49,9 +83,17 @@ const CodeBlock: React.FC<CodeBlockProps> = ({ children, className }) => {
|
|||||||
borderTopLeftRadius: 0,
|
borderTopLeftRadius: 0,
|
||||||
borderTopRightRadius: 0,
|
borderTopRightRadius: 0,
|
||||||
marginTop: 0,
|
marginTop: 0,
|
||||||
fontSize
|
fontSize,
|
||||||
|
maxHeight: isExpanded ? 'none' : '300px',
|
||||||
|
overflow: 'hidden',
|
||||||
|
position: 'relative'
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
<ExpandButton
|
||||||
|
isExpanded={isExpanded}
|
||||||
|
onClick={() => setIsExpanded(!isExpanded)}
|
||||||
|
showButton={!isExpanded || showFooterCopyButton}
|
||||||
|
/>
|
||||||
{showFooterCopyButton && (
|
{showFooterCopyButton && (
|
||||||
<CodeFooter>
|
<CodeFooter>
|
||||||
<CopyButton text={children} style={{ marginTop: -40, marginRight: 10 }} />
|
<CopyButton text={children} style={{ marginTop: -40, marginRight: 10 }} />
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user