|
| 1 | +import { useEffect, useRef, useState } from 'react'; |
| 2 | +import { uploadImageFile } from '@/app/lib/utils/imageUpload'; |
| 3 | + |
| 4 | +interface UsePasteImageUploadProps { |
| 5 | + content: string; |
| 6 | + setFormData: (updates: { content?: string }) => void; |
| 7 | + setUploadedImages: React.Dispatch<React.SetStateAction<string[]>>; |
| 8 | + toast: { |
| 9 | + success: (message: string) => void; |
| 10 | + error: (message: string) => void; |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +interface UploadProgress { |
| 15 | + current: number; |
| 16 | + total: number; |
| 17 | +} |
| 18 | + |
| 19 | +export function usePasteImageUpload({ |
| 20 | + content, |
| 21 | + setFormData, |
| 22 | + setUploadedImages, |
| 23 | + toast, |
| 24 | +}: UsePasteImageUploadProps) { |
| 25 | + const containerRef = useRef<HTMLDivElement>(null); |
| 26 | + const [isUploading, setIsUploading] = useState(false); |
| 27 | + const [uploadProgress, setUploadProgress] = useState<UploadProgress>({ |
| 28 | + current: 0, |
| 29 | + total: 0, |
| 30 | + }); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const container = containerRef.current; |
| 34 | + if (!container) return; |
| 35 | + |
| 36 | + const handlePaste = async (e: ClipboardEvent) => { |
| 37 | + const items = e.clipboardData?.items; |
| 38 | + if (!items) return; |
| 39 | + |
| 40 | + // 이미지가 복사되었는지 확인 |
| 41 | + const imageItems = Array.from(items).filter((item) => |
| 42 | + item.type.startsWith('image/') |
| 43 | + ); |
| 44 | + |
| 45 | + if (imageItems.length === 0) return; |
| 46 | + |
| 47 | + e.preventDefault(); |
| 48 | + |
| 49 | + const textarea = container.querySelector('textarea'); |
| 50 | + const cursorPosition = textarea?.selectionStart ?? content.length; |
| 51 | + |
| 52 | + setIsUploading(true); |
| 53 | + setUploadProgress({ current: 0, total: imageItems.length }); |
| 54 | + |
| 55 | + const uploadedUrls: string[] = []; |
| 56 | + |
| 57 | + // 이미지 업로드 |
| 58 | + for (let i = 0; i < imageItems.length; i++) { |
| 59 | + try { |
| 60 | + const file = imageItems[i].getAsFile(); |
| 61 | + if (!file) continue; |
| 62 | + |
| 63 | + toast.success( |
| 64 | + `이미지를 업로드하는 중입니다... (${i + 1}/${imageItems.length})` |
| 65 | + ); |
| 66 | + |
| 67 | + const url = await uploadImageFile(file); |
| 68 | + uploadedUrls.push(url); |
| 69 | + |
| 70 | + setUploadProgress({ current: i + 1, total: imageItems.length }); |
| 71 | + } catch (error) { |
| 72 | + const errorMessage = |
| 73 | + error instanceof Error ? error.message : '알 수 없는 오류'; |
| 74 | + toast.error(`이미지 업로드 실패: ${errorMessage}`); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // 업로드된 이미지 마크다운에 추가 |
| 79 | + if (uploadedUrls.length > 0) { |
| 80 | + const markdownImages = uploadedUrls |
| 81 | + .map((url) => ``) |
| 82 | + .join('\n'); |
| 83 | + |
| 84 | + const beforeCursor = content.slice(0, cursorPosition); |
| 85 | + const afterCursor = content.slice(cursorPosition); |
| 86 | + const newContent = beforeCursor + markdownImages + afterCursor; |
| 87 | + |
| 88 | + setFormData({ content: newContent }); |
| 89 | + setUploadedImages((prev) => [...prev, ...uploadedUrls]); |
| 90 | + |
| 91 | + toast.success('이미지가 성공적으로 삽입되었습니다.'); |
| 92 | + } |
| 93 | + |
| 94 | + setIsUploading(false); |
| 95 | + setUploadProgress({ current: 0, total: 0 }); |
| 96 | + }; |
| 97 | + |
| 98 | + container.addEventListener('paste', handlePaste); |
| 99 | + |
| 100 | + return () => { |
| 101 | + container.removeEventListener('paste', handlePaste); |
| 102 | + }; |
| 103 | + }, [content, setFormData, setUploadedImages, toast]); |
| 104 | + |
| 105 | + return { |
| 106 | + containerRef, |
| 107 | + isUploading, |
| 108 | + uploadProgress, |
| 109 | + }; |
| 110 | +} |
0 commit comments