'use client'; import { useState, useEffect, useRef } from 'react'; import { Swiper, SwiperSlide } from "swiper/react"; import { Navigation, Pagination, Thumbs, FreeMode } from "swiper/modules"; // 导入Swiper样式 import "swiper/css"; import "swiper/css/navigation"; import "swiper/css/pagination"; import "swiper/css/thumbs"; import "swiper/css/free-mode"; const ProductImageGallery = ({ data }) => { const [currentImageIndex, setCurrentImageIndex] = useState(0); const [thumbsSwiper, setThumbsSwiper] = useState(null); const thumbsSwiperRef = useRef(null); // 如果没有attachments,不显示组件 if (!data?.attachments || data.attachments.length === 0) { return null; } // 找到主图在attachments中的索引 const getMainImageIndex = () => { if (!data.image) return 0; const mainImageIndex = data.attachments.findIndex( attachment => attachment.file_url === data.image ); return mainImageIndex >= 0 ? mainImageIndex : 0; }; const mainImageIndex = getMainImageIndex(); const currentImage = data.attachments[currentImageIndex]?.file_url || data.attachments[0]?.file_url; // 初始化当前索引为主图索引 useEffect(() => { setCurrentImageIndex(mainImageIndex); }, [mainImageIndex]); // 键盘导航支持 useEffect(() => { const handleKeyDown = (event) => { if (data.attachments.length <= 1) return; switch (event.key) { case 'ArrowLeft': event.preventDefault(); changeMainImage('prev'); break; case 'ArrowRight': event.preventDefault(); changeMainImage('next'); break; case 'Home': event.preventDefault(); setCurrentImageIndex(0); break; case 'End': event.preventDefault(); setCurrentImageIndex(data.attachments.length - 1); break; default: break; } }; // 添加键盘事件监听器 document.addEventListener('keydown', handleKeyDown); return () => { document.removeEventListener('keydown', handleKeyDown); }; }, [data.attachments.length]); const changeMainImage = (direction) => { if (data.attachments.length <= 1) return; if (direction === 'next') { setCurrentImageIndex((prev) => (prev + 1) % data.attachments.length); } else { setCurrentImageIndex((prev) => (prev - 1 + data.attachments.length) % data.attachments.length); } }; const changeMainImageByIndex = (index) => { setCurrentImageIndex(index); // 智能滚动缩略图到可见区域 if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) { const swiper = thumbsSwiperRef.current.swiper; const slideIndex = index; // 计算目标滚动位置 const slideWidth = 64; // 64px 缩略图宽度 const spaceBetween = 16; // 16px 间距 const totalSlideWidth = slideWidth + spaceBetween; // 获取当前可见的slides数量 const visibleSlides = swiper.params.slidesPerView; // 计算当前滚动位置 const currentTranslate = swiper.translate; const currentSlideIndex = Math.round(Math.abs(currentTranslate) / totalSlideWidth); // 判断需要向左还是向右滚动 let targetSlideIndex; if (slideIndex < currentSlideIndex) { // 向左滚动:让选中的缩略图显示在左侧 targetSlideIndex = Math.max(0, slideIndex - 1); } else if (slideIndex >= currentSlideIndex + visibleSlides) { // 向右滚动:让选中的缩略图显示在右侧 targetSlideIndex = slideIndex - visibleSlides + 1; } else { // 当前缩略图已经在可见区域内,不需要滚动 return; } // 计算目标滚动位置 const targetTranslate = targetSlideIndex * totalSlideWidth; // 确保不超出边界 const maxTranslate = swiper.maxTranslate(); const finalTranslate = Math.max(0, Math.min(targetTranslate, maxTranslate)); // 平滑滚动到目标位置 swiper.slideTo(Math.round(finalTranslate / totalSlideWidth), 300); } }; // 缩略图导航控制 - 移除这些函数,不再需要 // const goToPrevThumbs = () => { // if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) { // thumbsSwiperRef.current.swiper.slidePrev(); // } // }; // const goToNextThumbs = () => { // if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) { // if (thumbsSwiperRef.current && thumbsSwiperRef.current.swiper) { // thumbsSwiperRef.current.swiper.slideNext(); // } // } // }; return (