Skip to content

Commit

Permalink
feat: 내용 작성 시, 이미지 붙여넣기에도 마크다운으로 삽입되는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
pjm2571 committed Oct 29, 2024
1 parent c4e6bf7 commit df9811e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/components/EditorPage/MdEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,29 @@ function MdEditor() {
});
};

const handlePasteImage = async (event) => {
const clipboardItems = event.clipboardData.items;
for (let item of clipboardItems) {
if (item.type.includes("image")) {
const file = item.getAsFile();
if (file) {
try {
const formData = new FormData();
formData.append("img", file);
const response = await postImageUpload(formData);
const imageUrl = response.data.uploadedUrl;
setPostData((prev) => ({
...prev,
content: `${prev.content}\n![image](${imageUrl})`,
}));
} catch (error) {
console.error("Image upload failed:", error);
}
}
}
}
};

useEffect(() => {
setPostData({
language: isEdit ? data?.til.language : "",
Expand Down Expand Up @@ -134,6 +157,7 @@ function MdEditor() {
textareaProps={{
placeholder: "내용을 입력하세요.",
}}
onPaste={handlePasteImage}
/>
<EditerFooter handlePostData={() => setIsModalOpen(true)} />
</div>
Expand Down

0 comments on commit df9811e

Please sign in to comment.