11import { useEffect , useState } from 'react'
2- import { Image , Modal } from 'antd'
2+ import { Modal } from 'antd'
33import { getTextFromBlob } from '@/utils'
44import MdRender from '@/components/MdRender'
55import { imgTypes , textTypes } from '@/FileTypes'
6+ import PreviewImg from './PreviewImg'
67
78export type PreviewFileProps = {
89 blob : Blob
@@ -12,7 +13,6 @@ export type PreviewFileProps = {
1213
1314function 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}
0 commit comments