import type { UploadFileAttrs } from '@/components/common/UploadBox'; import { BlockViewport } from '@/blocks/Viewport'; import { FabricImage } from 'fabric'; import { useLocalStorageState } from 'ahooks'; import { AiFillCloseCircle, AiOutlineUpload } from 'react-icons/ai'; import React, { useCallback, useRef, useState } from 'react'; import { Upload, UploadProps } from 'antd'; import { uploadFileByBase64 } from '@/utils/image'; import { cn } from '@/utils/cn'; import { BlockBackground } from '@/blocks/Background'; import locale from '@/locale'; function Images({ maxImages, block, setBlock, ctx, dispatch, customRequest = uploadFileByBase64, ...rest }: Partial & { maxImages?: number; block: null | BlockBackground; setBlock: (block: BlockBackground) => void; ctx: IPicexContext; dispatch: IPicexDispatch; }) { const viewport = ctx.blocks[0]; const lastestBg = useRef(''); const [bgImage, setBgImage] = useState(''); const setBgImage4Root = useCallback( (v: string) => { if (!viewport) { return; } lastestBg.current = v; setBgImage(v); const size = { width: viewport?.width, height: viewport?.height, }; block && ctx.blocks.includes(block) ? BlockBackground.patternFromURL(v, size).then((pattern) => { if (lastestBg.current !== v) { return; } dispatch({ type: 'updateBlock', block, payload: { fill: pattern, }, }); }) : BlockBackground.fromURL(v, size).then((newBlock) => { if (lastestBg.current !== v) { return; } dispatch({ type: 'addBlock', block: newBlock, }); setBlock(newBlock); }); }, [viewport, setBgImage, block, setBlock, dispatch], ); const [bgImages, setBgImages] = useLocalStorageState>( 'picex-background-images', { defaultValue: [], }, ); return (
  • {...rest} className="w-full h-full" listType="picture-card" multiple={false} showUploadList={false} customRequest={customRequest} onChange={({ file }) => { if (file.response?.url && file.status === 'done') { setBgImages((prev = []) => (prev.some((x) => x.url === file.response!.url) ? prev : [file.response!, ...(prev || [])] ).slice(0, maxImages), ); setBgImage4Root(file.response!.url); } }} >
    {locale.t('background.panel.upload')}
  • {bgImages?.map((file) => !file?.url ? null : (
  • { setBgImage4Root(file.url); }} > { e.stopPropagation(); setBgImages((prev) => !prev ? [] : prev.filter((x) => x.url !== file.url), ); }} >
  • ), )}
); } export default Images;