import React, { useState, useRef } from 'react' import { ChevronLeft, ChevronRight } from 'lucide-react' import { cn } from '@/utils/cn' import { Button } from '@/components/ui/button' import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area' import { useSize } from '../../hooks/useSize' export interface SlideItem { content: string [key: string]: unknown } export interface PptPreviewProps { /** PPT 文件的 URL */ url: string } /** * PPT 预览组件 */ export function PptPreview({ url }: PptPreviewProps) { const [currentIndex, setCurrentIndex] = useState(0) const [slideList, setSlideList] = useState([]) const [loading, setLoading] = useState(true) React.useEffect(() => { if (!url) return setLoading(true) fetch(url) .then(res => res.json()) .then(data => { const slides = data.slide_list || [] setSlideList(slides) }) .catch(() => setSlideList([])) .finally(() => setLoading(false)) }, [url]) if (loading) { return (
加载中...
) } if (!slideList || slideList.length === 0) { return (
📊

暂无幻灯片内容

) } return } function PptSlideViewer({ slideList, currentIndex, setCurrentIndex }: { slideList: SlideItem[] currentIndex: number setCurrentIndex: (index: number) => void }) { const containerRef = useRef(null) const iframeRef = useRef(null) const size = useSize(containerRef) const [iframeHeight, setIframeHeight] = useState(720) const [loadState, setLoadState] = useState<'loading' | 'loaded' | 'error'>('loading') const currentSlide = slideList[currentIndex] const scale = size ? size.width / 1280 : 1 const handleIframeLoad = (event: React.SyntheticEvent) => { const iframe = event.currentTarget try { const actualHeight = iframe.contentDocument?.documentElement.scrollHeight if (actualHeight && actualHeight > 0) { setIframeHeight(actualHeight) } setLoadState('loaded') } catch (error) { console.warn('Cannot access iframe content:', error) setLoadState('loaded') } } const handleIframeError = () => { setLoadState('error') } // 切换幻灯片时重置加载状态 React.useEffect(() => { setLoadState('loading') setIframeHeight(720) }, [currentIndex]) return (
{/* 主预览区 */}
{loadState === 'loading' && (

加载中...

)}