"use client"; import React, { useEffect, useState, useRef } from "react"; import { DotLoading, Image, Toast, Modal } from "antd-mobile"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faAdd, faClose, faPlay } from "@fortawesome/free-solid-svg-icons"; import { getVideoBase64 } from "@/utils/tools"; import { DragDropContext, Draggable, Droppable } from "@hello-pangea/dnd"; import ImagesMask from "@/components/ImagesMask"; export default function UploadImgs({ assets, getImgs, accept = "image/png, image/jpeg, image/jpg", existImages = [], type = 1, videoSrc = null, getVideoCover, id = "uploadAvatarBtn", maxLength = 9, }) { const imagesMaskRef = useRef(null); const [loading, setLoading] = useState(false); const [filesUrls, setFilesUrls] = useState([]); const [videoUrl, setVideoUrl] = useState(null); const [frameImage, setFrameImage] = useState({ src: null, h: 0, w: 0 }); const [columns, setColumns] = useState([]); useEffect(() => { if (existImages.length > 0) { setFilesUrls( existImages.map((it, index) => ({ url: it.url, id: `${index}` })) ); } if (videoSrc) { setVideoUrl(videoSrc); } }, [existImages]); useEffect(() => { getVideoCover && getVideoCover(frameImage); }, [frameImage]); useEffect(() => { if (assets && assets.length > 0) { const list = assets.map((it, index) => ({ url: it.url || URL.createObjectURL(it), id: `${index}`, })); setFilesUrls(list); // 将filesUrls转变为每组四个的二维数组 handleMakeColumns(list); } else { setFilesUrls([]); setColumns([]); } }, [assets]); const handleUploadImage = async (e) => { let file = e.target.files[0]; if (!file) return; if (e.target.files.length + assets.length > maxLength) { Toast.show({ icon: "fail", content: "最多上传" + maxLength + "张图片", position: "top", }); return; } var videoUrl = URL.createObjectURL(file); var videoObj = document.createElement("video"); const eles = Array.from(e.target.files); if (type == 1) { const newFils = eles.map((it, index) => ({ url: URL.createObjectURL(it), id: it.name, })); setFilesUrls((old) => [...old, ...newFils]); } else { videoObj.onloadedmetadata = function (evt) { URL.revokeObjectURL(videoUrl); const target = evt.target; setFrameImage({ src: videoUrl, w: target.videoWidth, h: target.videoHeight, }); setLoading(true); // let newFilesUrls = [...filesUrls]; if (type == 2) { if (typeof window == "undefined") return; // newFiles = [...assets, file]; // setFileList(newAssets); creatVideoCanvas(file); setVideoUrl(URL.createObjectURL(file)); } // setFileList(newFiles); setLoading(false); }; videoObj.src = videoUrl; videoObj.load(); } // console.log("assets", [...assets, ...eles]); getImgs([...assets, ...eles]); }; const handleRemoveItem = (index) => { let newArr = [...filesUrls]; let newAssets = [...assets]; newArr.splice(index, 1); newAssets.splice(index, 1); setFilesUrls(newAssets); getImgs(newAssets); }; const showPhotos = (images, index) => { if (type == 1) { imagesMaskRef.current.show(images, index); } else { Modal.show({ content: ( ), closeOnMaskClick: true, bodyStyle: { background: "none", maxHeight: "100vh", }, }); } }; const creatVideoCanvas = (file) => { if (typeof window == "undefined") return; // const videoD = document.getElementById("video_upload"); const url = URL.createObjectURL(file); // videoD.src = url; getVideoBase64(url).then((src) => { setFrameImage((old) => ({ ...old, src })); const list = [{ url: src, id: "0" }]; setFilesUrls(list); handleMakeColumns(list); }); }; const handleMakeColumns = (list) => { const newList = []; for (let i = 0; i < list.length; i += 4) { newList.push(list.slice(i, i + 4)); } const newColumns = newList.map((it, index) => { return { id: "column-" + index, title: "Column " + index, items: it, }; }); setColumns(newColumns); }; const reorder = (list, startIndex, endIndex) => { const result = Array.from(list); const removing = result[startIndex]; const removed = result[endIndex]; result[startIndex] = removed; result[endIndex] = removing; return result; }; const onDragEnd = (result) => { const { source, destination, draggableId } = result; if (!destination) return; if ( source.droppableId === destination.droppableId && source.index === destination.index ) { return; } const newList = reorder( assets, parseInt(source.droppableId.match(/\d+/)[0], 10) * 4 + source.index, parseInt(destination.droppableId.match(/\d+/)[0], 10) * 4 + destination.index ); const start = columns.find((column) => column.id === source.droppableId); const end = columns.find((column) => column.id === destination.droppableId); const startItems = Array.from(start.items); let endItems = Array.from(end.items); if (destination.droppableId === source.droppableId) { const itemToMove = startItems[source.index]; startItems.splice(source.index, 1); startItems.splice(destination.index, 0, itemToMove); const newColumns = columns.map((column) => { if (column.id === start.id) { return { ...column, items: startItems }; } if (column.id === end.id) { return { ...column, items: endItems }; } return column; }); setColumns(newColumns); getImgs([...newList]); } else { const newColumns = columns.map((column) => { if (column.id === start.id) { startItems.splice(source.index, 1, end.items[destination.index]); return { ...column, items: startItems }; } if (column.id === end.id) { endItems.splice(destination.index, 1, start.items[source.index]); return { ...column, items: endItems }; } return column; }); setColumns(newColumns); // console.log("newList", newList); getImgs([...newList]); } }; return (
上传中