load_editor.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. function do_insert_data(name, data, monaco = 0) {
  2. if(monaco === 0) {
  3. // https://stackoverflow.com/questions/11076975/insert-text-into-textarea-at-cursor-position-javascript
  4. if(document.selection) {
  5. document.getElementById(name).focus();
  6. var sel = document.selection.createRange();
  7. sel.text = data;
  8. } else if(
  9. document.getElementById(name).selectionStart ||
  10. document.getElementById(name).selectionStart == '0'
  11. ) {
  12. var startPos = document.getElementById(name).selectionStart;
  13. var endPos = document.getElementById(name).selectionEnd;
  14. var myPos = document.getElementById(name).value;
  15. document.getElementById(name).value = myPos.substring(0, startPos) + data + myPos.substring(endPos, myPos.length);
  16. } else {
  17. document.getElementById(name).value += data;
  18. }
  19. } else {
  20. var selection = editor.getSelection();
  21. var id = { major: 1, minor: 1 };
  22. var text = data;
  23. var op = {
  24. identifier: id,
  25. range: selection,
  26. text: text,
  27. forceMoveMarkers: true
  28. };
  29. editor.executeEdits("my-source", [op]);
  30. }
  31. }
  32. // 아직 개편이 더 필요함
  33. function do_paste_image() {
  34. window.addEventListener('DOMContentLoaded', function() {
  35. if(
  36. document.cookie.match(opennamu_cookie_split_regex('main_css_image_paste')) &&
  37. document.cookie.match(opennamu_cookie_split_regex('main_css_image_paste'))[1] === '1'
  38. ) {
  39. const textarea = document.querySelector("textarea");
  40. if (textarea) {
  41. textarea.addEventListener("paste", pasteListener);
  42. }
  43. }
  44. });
  45. }
  46. function pasteListener(e) {
  47. // find file
  48. if(e.clipboardData && e.clipboardData.items) {
  49. const items = e.clipboardData.items;
  50. let haveImageInClipboard = false;
  51. const formData = new FormData();
  52. for(let i = 0; i < items.length; i++) {
  53. if(items[i].type.indexOf("image") !== -1) {
  54. const file = items[i].getAsFile();
  55. const customName = prompt("파일 이름 (확장자 제외)");
  56. if (!customName) {
  57. return alert("파일 이름 없음");
  58. }
  59. var file_name = customName + ".png";
  60. const customFile = new File([file], file_name, { type: file.type });
  61. formData.append("f_data[]", customFile);
  62. haveImageInClipboard = true;
  63. e.preventDefault();
  64. break;
  65. }
  66. }
  67. if(!haveImageInClipboard) {
  68. return;
  69. }
  70. // send to server
  71. fetch("/upload", {
  72. method: "POST",
  73. body: formData,
  74. }).then((res) => {
  75. if (res.status === 200 || res.status === 201) {
  76. const url = res.url;
  77. alert(
  78. '업로드 완료 : ' +
  79. '[[파일:' + file_name + ']]'
  80. );
  81. } else {
  82. console.error("[ERROR] PasteUpload Fail :", res.statusText);
  83. if(res.status === 400) {
  84. alert("파일 이름 중복");
  85. } else if(res.status === 401) {
  86. alert("권한 부족");
  87. } else {
  88. alert("업로드 실패");
  89. }
  90. }
  91. }).catch((err) => {
  92. console.error("오류 내역 :", JSON.stringify(err), err);
  93. alert("업로드 실패");
  94. });
  95. }
  96. }
  97. function load_raw_preview(name_1, name_2) {
  98. document.getElementById(name_2).innerHTML = document.getElementById(name_1).value;
  99. }