-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
게시글 작성 시 이미지 복사 붙혀넣기로 이미지 첨부 가능하도록 구현 #624
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,43 @@ | ||
import { ChangeEvent, useRef, useState } from 'react'; | ||
import { ChangeEvent, ClipboardEvent, useRef, useState } from 'react'; | ||
|
||
import { MAX_FILE_SIZE } from '@components/PostForm/constants'; | ||
|
||
import { convertImageToWebP } from '@utils/resizeImage'; | ||
import { uploadImage } from '@utils/post/uploadImage'; | ||
|
||
export const useContentImage = (imageUrl: string = '') => { | ||
const [contentImage, setContentImage] = useState(imageUrl); | ||
const contentInputRef = useRef<HTMLInputElement | null>(null); | ||
|
||
const handlePasteImage = (event: ClipboardEvent<HTMLTextAreaElement>) => { | ||
const file = event.clipboardData.files[0]; | ||
|
||
if (file.type.slice(0, 5) === 'image') { | ||
event.preventDefault(); | ||
|
||
uploadImage({ | ||
imageFile: file, | ||
inputElement: contentInputRef.current, | ||
setPreviewImageUrl: setContentImage, | ||
}); | ||
} | ||
}; | ||
|
||
const removeImage = () => { | ||
setContentImage(''); | ||
if (contentInputRef.current) contentInputRef.current.value = ''; | ||
}; | ||
|
||
const handleUploadImage = async (event: ChangeEvent<HTMLInputElement>) => { | ||
const handleUploadImage = (event: ChangeEvent<HTMLInputElement>) => { | ||
const { files } = event.target; | ||
|
||
if (!files) return; | ||
|
||
const file = files[0]; | ||
|
||
const webpFileList = await convertImageToWebP(file); | ||
|
||
event.target.files = webpFileList; | ||
|
||
const reader = new FileReader(); | ||
|
||
const webpFile = webpFileList[0]; | ||
|
||
reader.readAsDataURL(webpFile); | ||
|
||
event.target.setCustomValidity(''); | ||
|
||
if (file.size > MAX_FILE_SIZE) { | ||
event.target.setCustomValidity('사진의 용량은 1.5MB 이하만 가능합니다.'); | ||
event.target.reportValidity(); | ||
|
||
return; | ||
} | ||
|
||
reader.onloadend = () => { | ||
setContentImage(reader.result?.toString() ?? ''); | ||
}; | ||
uploadImage({ | ||
imageFile: file, | ||
inputElement: contentInputRef.current, | ||
setPreviewImageUrl: setContentImage, | ||
}); | ||
}; | ||
|
||
return { contentImage, contentInputRef, removeImage, handleUploadImage }; | ||
return { contentImage, contentInputRef, removeImage, handleUploadImage, handlePasteImage }; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { MAX_FILE_SIZE } from '@constants/post'; | ||
|
||
import { convertImageToWebP } from '@utils/resizeImage'; | ||
|
||
export const uploadImage = async ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍 |
||
imageFile, | ||
inputElement, | ||
setPreviewImageUrl, | ||
}: { | ||
imageFile: File; | ||
inputElement: HTMLInputElement | null; | ||
setPreviewImageUrl: (previewUrl: string) => void; | ||
}) => { | ||
if (!inputElement) return; | ||
|
||
const webpFileList = await convertImageToWebP(imageFile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오 혹시 정말 사소하게 궁금한 부분인데, 변환하는데 몇초가 걸리는지 궁금해요! 만약 시간이 걸리는 작업이거나, slow 3g 환경이라면 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제로의 제안으로 webp로 이미지 변환 속도를 측정해보았습니다 ~ fe-리뷰요청 |
||
|
||
inputElement.files = webpFileList; | ||
|
||
const reader = new FileReader(); | ||
|
||
const webpFile = webpFileList[0]; | ||
|
||
reader.readAsDataURL(webpFile); | ||
|
||
inputElement.setCustomValidity(''); | ||
|
||
if (imageFile.size > MAX_FILE_SIZE) { | ||
inputElement.setCustomValidity('사진의 용량은 1.5MB 이하만 가능합니다.'); | ||
inputElement.reportValidity(); | ||
|
||
return; | ||
} | ||
|
||
reader.onloadend = () => { | ||
setPreviewImageUrl(reader.result?.toString() ?? ''); | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 해당 file.type이 뭐라고 나오나요??
찾아도 관련없는 것들이 많이 나오네요
참고하신 자료가 있다면 공유 부탁들려요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'image/png' , 'image/webp'와 같이 나와요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
참고한 자료는 따로 없고 console.log를 통해 파일의 정보를 확인하고 코드를 작성했었습니다 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bb 대단합니다