Skip to content

Commit aa2c3c3

Browse files
committed
feat: 图片预览单独,修复空文件在 text 类型下显示 undefined
1 parent 9eceace commit aa2c3c3

3 files changed

Lines changed: 62 additions & 43 deletions

File tree

src/components/CodeRender.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,27 @@ type CodeRenderProps = ClassAttributes<HTMLElement> & HTMLAttributes<HTMLElement
2727
}
2828
function CodeRender({ children, className, theme = 'light', codeBoxClassName, codeClassName, enableCopy = true, ...rest }: CodeRenderProps) {
2929
const match = /language-(\w+)/.exec(className || '')
30+
const content = children === undefined ? '' : String(children).replace(/\n$/, '')
3031
return match ? (
3132
<div className={cn('relative', codeBoxClassName)}>
3233
<div className='absolute top-[-24px] left-[0] leading-6 z-10 text-gray-400 dark:text-gray-500'>{match[1]}</div>
3334
{enableCopy && (
3435
<div className='absolute top-[-24px] right-[-16px] z-10'>
35-
<CopyToClipboard text={String(children).replace(/\n$/, '')}></CopyToClipboard>
36+
<CopyToClipboard text={content}></CopyToClipboard>
3637
</div>
3738
)}
3839
<SyntaxHighlighter
3940
PreTag='div'
4041
className={cn('code-block', codeClassName)}
41-
children={String(children).replace(/\n$/, '')}
42+
children={content}
4243
language={match[1]}
4344
showLineNumbers={true}
4445
style={theme === 'dark' ? dark : undefined}
4546
/>
4647
</div>
4748
) : (
4849
<code {...rest} className={cn(className, codeBoxClassName)}>
49-
{children}
50+
{content}
5051
</code>
5152
)
5253
}

src/components/PreviewFile.tsx

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useEffect, useState } from 'react'
2-
import { Image, Modal } from 'antd'
2+
import { Modal } from 'antd'
33
import { getTextFromBlob } from '@/utils'
44
import MdRender from '@/components/MdRender'
55
import { imgTypes, textTypes } from '@/FileTypes'
6+
import PreviewImg from './PreviewImg'
67

78
export type PreviewFileProps = {
89
blob: Blob
@@ -12,7 +13,6 @@ export type PreviewFileProps = {
1213

1314
function PreviewFile({ blob, name, onClose }: PreviewFileProps) {
1415
const [previewFileSrc, setPreviewFileSrc] = useState('')
15-
const [imageOpen, setImageOpen] = useState(false)
1616
const [modalInfo, setModalInfo] = useState<{
1717
open: boolean
1818
title: string
@@ -22,14 +22,16 @@ function PreviewFile({ blob, name, onClose }: PreviewFileProps) {
2222
title: '',
2323
content: null,
2424
})
25+
2526
const fileType = name.split('.').pop()?.toLowerCase() || ''
27+
const isImg = imgTypes.includes(fileType)
28+
const isText = textTypes.includes(fileType)
2629

2730
useEffect(() => {
2831
async function initFn() {
29-
if (imgTypes.includes(fileType)) {
32+
if (isImg) {
3033
setPreviewFileSrc(URL.createObjectURL(blob))
31-
setImageOpen(true)
32-
} else if (textTypes.includes(fileType)) {
34+
} else if (isText) {
3335
const fileContent = await getTextFromBlob(blob)
3436
setModalInfo({
3537
open: true,
@@ -47,44 +49,34 @@ function PreviewFile({ blob, name, onClose }: PreviewFileProps) {
4749
content: null,
4850
})
4951
}
50-
}, [fileType, name, blob])
52+
}, [isImg, isText, fileType, name, blob])
5153

5254
return (
5355
<>
54-
{previewFileSrc && (
55-
<Image
56-
width={200}
57-
style={{ display: 'none' }}
58-
alt='img'
59-
src={previewFileSrc}
60-
preview={{
61-
open: imageOpen,
62-
src: previewFileSrc,
63-
onOpenChange: (value) => {
64-
setImageOpen(value)
65-
URL.revokeObjectURL(previewFileSrc)
66-
onClose()
67-
},
68-
}}
69-
/>)}
70-
{modalInfo?.open && <Modal
71-
open={modalInfo?.open}
72-
title={modalInfo?.title}
73-
width='68vw'
74-
closable={true}
75-
footer={null}
76-
onCancel={() => {
77-
setModalInfo({
78-
open: false,
79-
title: '',
80-
content: null,
81-
})
82-
onClose()
83-
}}
84-
>
85-
{modalInfo?.content}
86-
</Modal>
87-
}
56+
{isImg && previewFileSrc && (
57+
<PreviewImg url={previewFileSrc} onClose={() => {
58+
URL.revokeObjectURL(previewFileSrc)
59+
setPreviewFileSrc('')
60+
onClose()
61+
}} />
62+
)}
63+
<Modal
64+
open={modalInfo?.open}
65+
title={modalInfo?.title}
66+
width='68vw'
67+
closable={true}
68+
footer={null}
69+
onCancel={() => {
70+
setModalInfo({
71+
open: false,
72+
title: '',
73+
content: null,
74+
})
75+
onClose()
76+
}}
77+
>
78+
{modalInfo?.content}
79+
</Modal>
8880
</>
8981
)
9082
}

src/components/PreviewImg.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Image } from 'antd'
2+
3+
export type PreviewImgProps = {
4+
url: string
5+
onClose: () => void
6+
}
7+
8+
function PreviewImg({ url, onClose }: PreviewImgProps) {
9+
return (
10+
<Image
11+
width={200}
12+
style={{ display: 'none' }}
13+
alt='img'
14+
src={url}
15+
preview={{
16+
open: true,
17+
src: url,
18+
onOpenChange: () => {
19+
onClose()
20+
},
21+
}}
22+
/>
23+
)
24+
}
25+
26+
export default PreviewImg

0 commit comments

Comments
 (0)