minio이미지 업로드 기능 테스트
This commit is contained in:
@ -23,10 +23,8 @@
|
||||
|
||||
<!-- 마크다운 에디터 -->
|
||||
<h2>Contents</h2>
|
||||
<textarea id="markdown-editor" name="contents" class="form-control" rows="10"></textarea>
|
||||
|
||||
<div class="col-md-12">
|
||||
{{ form.contents }}
|
||||
</div>
|
||||
<!-- 버튼 -->
|
||||
<div class="d-flex justify-content-end mt-4">
|
||||
<button type="submit" class="btn btn-primary me-2">Create Post</button>
|
||||
@ -75,6 +73,56 @@
|
||||
hljs.highlightElement(block);
|
||||
});
|
||||
});
|
||||
|
||||
// Ctrl+V로 이미지 붙여넣기 처리
|
||||
textarea.addEventListener("paste", async function (event) {
|
||||
const items = (event.clipboardData || event.originalEvent.clipboardData).items;
|
||||
|
||||
for (const item of items) {
|
||||
if (item.type.startsWith("image/")) {
|
||||
const file = item.getAsFile();
|
||||
if (!file)
|
||||
return;
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("image", file);
|
||||
|
||||
try {
|
||||
// 이미지 업로드 API 호출
|
||||
const response = await fetch("/obs_minio/upload/", {
|
||||
method: "POST",
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
const imageUrl = data.url; // 업로드된 이미지 URL
|
||||
|
||||
// 마크다운 에디터에 이미지 삽입
|
||||
const markdownImage = `\n`;
|
||||
textarea.value += markdownImage;
|
||||
|
||||
// 미리보기 업데이트
|
||||
const markdownContent = textarea.value;
|
||||
const renderedContent = md.render(markdownContent);
|
||||
preview.innerHTML = renderedContent;
|
||||
|
||||
// Highlight.js로 코드 블록 강조
|
||||
document
|
||||
.querySelectorAll("#preview pre code")
|
||||
.forEach((block) => {
|
||||
hljs.highlightElement(block);
|
||||
});
|
||||
} else {
|
||||
alert("Image upload failed. Please try again.");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error uploading image:", error);
|
||||
alert("An error occurred during image upload.");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
Reference in New Issue
Block a user