load_editor.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. function do_insert_data(name, data) {
  2. // https://stackoverflow.com/questions/11076975/insert-text-into-textarea-at-cursor-position-javascript
  3. if(document.selection) {
  4. document.getElementById(name).focus();
  5. var sel = document.selection.createRange();
  6. sel.text = data;
  7. } else if(document.getElementById(name).selectionStart || document.getElementById(name).selectionStart == '0') {
  8. var startPos = document.getElementById(name).selectionStart;
  9. var endPos = document.getElementById(name).selectionEnd;
  10. var myPos = document.getElementById(name).value;
  11. document.getElementById(name).value = myPos.substring(0, startPos) + data + myPos.substring(endPos, myPos.length);
  12. } else {
  13. document.getElementById(name).value += data;
  14. }
  15. }
  16. function monaco_to_content() {
  17. try {
  18. document.getElementById('content').innerHTML = window.editor.getValue();
  19. } catch(e) {}
  20. }
  21. function do_not_out() {
  22. window.addEventListener('DOMContentLoaded', function() {
  23. window.onbeforeunload = function() {
  24. monaco_to_content();
  25. data = document.getElementById('content').value;
  26. origin = document.getElementById('origin').value;
  27. if(data !== origin) {
  28. return '';
  29. }
  30. }
  31. });
  32. }
  33. function save_stop_exit() {
  34. window.onbeforeunload = function () {}
  35. }
  36. function do_paste_image() {
  37. window.addEventListener('DOMContentLoaded', function() {
  38. if(
  39. document.cookie.match(main_css_regex_data('main_css_image_paste')) &&
  40. document.cookie.match(main_css_regex_data('main_css_image_paste'))[1] === '1'
  41. ) {
  42. const textarea = document.querySelector("textarea");
  43. if (textarea) {
  44. textarea.addEventListener("paste", pasteListener);
  45. }
  46. }
  47. });
  48. }
  49. function pasteListener(e) {
  50. // find file
  51. if(e.clipboardData && e.clipboardData.items) {
  52. const items = e.clipboardData.items;
  53. let haveImageInClipboard = false;
  54. const formData = new FormData();
  55. for(let i = 0; i < items.length; i++) {
  56. if (items[i].type.indexOf("image") !== -1) {
  57. const file = items[i].getAsFile();
  58. const customName = prompt("파일 이름 (확장자 제외)");
  59. if (!customName) {
  60. return alert("파일 이름 없음");
  61. }
  62. const customFile = new File([file], customName + ".png", { type: file.type });
  63. formData.append("f_data[]", customFile);
  64. haveImageInClipboard = true;
  65. e.preventDefault();
  66. break;
  67. }
  68. }
  69. if(!haveImageInClipboard) {
  70. return;
  71. }
  72. // send to server
  73. fetch("/upload", {
  74. method: "POST",
  75. body: formData,
  76. }).then((res) => {
  77. if (res.status === 200 || res.status === 201) {
  78. const url = res.url;
  79. alert(
  80. '업로드 완료 : ' +
  81. '[[' + decodeURIComponent(url.replace(/.*\/w\/file/, "file")) + ']]'
  82. );
  83. } else {
  84. console.error("[ERROR] PasteUpload Fail :", res.statusText);
  85. if(res.status === 400) {
  86. alert("파일 이름 중복");
  87. } else if(res.status === 401) {
  88. alert("권한 부족");
  89. } else {
  90. alert("업로드 실패");
  91. }
  92. }
  93. }).catch((err) => {
  94. console.error("오류 내역 :", JSON.stringify(err), err);
  95. alert("업로드 실패");
  96. });
  97. }
  98. }
  99. function load_preview(name) {
  100. var s_data = new FormData();
  101. s_data.append('data', document.getElementById('content').value);
  102. var url = "/api/w/" + name;
  103. var url_2 = "/api/markup";
  104. var xhr = new XMLHttpRequest();
  105. xhr.open("POST", url, true);
  106. xhr.send(s_data);
  107. var xhr_2 = new XMLHttpRequest();
  108. xhr_2.open("GET", url_2, true);
  109. xhr_2.send(null);
  110. xhr.onreadystatechange = function() {
  111. if(xhr.readyState === 4 && xhr.status === 200) {
  112. var o_p_data = JSON.parse(xhr.responseText);
  113. document.getElementById('see_preview').innerHTML = o_p_data['data'];
  114. eval(o_p_data['js_data'])
  115. }
  116. }
  117. }
  118. function load_raw_preview(name_1, name_2) {
  119. document.getElementById(name_2).innerHTML = document.getElementById(name_1).value;
  120. }