do_upload_paste.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. window.addEventListener('DOMContentLoaded', function() {
  3. if(window.location.pathname.match(/^\/edit\//i)) {
  4. const textarea = document.querySelector("textarea");
  5. if (textarea) {
  6. textarea.addEventListener("paste", pasteListener);
  7. }
  8. }
  9. });
  10. function pasteListener(e) {
  11. // find file
  12. if (e.clipboardData && e.clipboardData.items) {
  13. const items = e.clipboardData.items;
  14. let haveImageInClipboard = false;
  15. const formData = new FormData();
  16. for (let i = 0; i < items.length; i++) {
  17. if (items[i].type.indexOf("image") !== -1) {
  18. const file = items[i].getAsFile();
  19. const customName = prompt("파일 이름을 설정해주세요. (확장자는 생략)");
  20. if (!customName) {
  21. return alert("취소되었습니다.");
  22. }
  23. const customFile = new File([file], customName + ".png", { type: file.type });
  24. formData.append("f_data[]", customFile);
  25. haveImageInClipboard = true;
  26. e.preventDefault();
  27. break;
  28. }
  29. }
  30. if (!haveImageInClipboard) {
  31. return;
  32. }
  33. // send to server
  34. fetch("/upload", {
  35. method: "POST",
  36. body: formData,
  37. }).then((res) => {
  38. if (res.status === 200 || res.status === 201) {
  39. const url = res.url;
  40. alert(
  41. '클립보드의 이미지를 성공적으로 업로드했습니다. 아래 텍스트로 본문에 삽입할 수 있습니다.' +
  42. '[[' + decodeURIComponent(url.replace(/.*\/w\/file/, "file")) + ']]'
  43. );
  44. } else {
  45. console.error("[ERROR] PasteUpload Fail :", res.statusText);
  46. alert("클립보드의 이미지를 업로드하는데 실패했습니다. 파일 이름 중복일 수 있습니다.");
  47. }
  48. }).catch((err) => {
  49. console.error("[ERROR] PasteUpload Fail :", JSON.stringify(err), err);
  50. alert("클립보드의 이미지를 업로드하는데 실패했습니다. 파일 이름 중복일 수 있습니다.");
  51. });
  52. }
  53. }
  54. */