load_editor.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. var file_name = customName + ".png";
  63. const customFile = new File([file], file_name, { type: file.type });
  64. formData.append("f_data[]", customFile);
  65. haveImageInClipboard = true;
  66. e.preventDefault();
  67. break;
  68. }
  69. }
  70. if(!haveImageInClipboard) {
  71. return;
  72. }
  73. // send to server
  74. fetch("/upload", {
  75. method: "POST",
  76. body: formData,
  77. }).then((res) => {
  78. if (res.status === 200 || res.status === 201) {
  79. const url = res.url;
  80. alert(
  81. '업로드 완료 : ' +
  82. '[[파일:' + file_name + ']]'
  83. );
  84. } else {
  85. console.error("[ERROR] PasteUpload Fail :", res.statusText);
  86. if(res.status === 400) {
  87. alert("파일 이름 중복");
  88. } else if(res.status === 401) {
  89. alert("권한 부족");
  90. } else {
  91. alert("업로드 실패");
  92. }
  93. }
  94. }).catch((err) => {
  95. console.error("오류 내역 :", JSON.stringify(err), err);
  96. alert("업로드 실패");
  97. });
  98. }
  99. }
  100. function load_preview(name) {
  101. var s_data = new FormData();
  102. s_data.append('data', document.getElementById('content').value);
  103. var url = "/api/w/" + name;
  104. var url_2 = "/api/markup";
  105. var xhr = new XMLHttpRequest();
  106. xhr.open("POST", url, true);
  107. xhr.send(s_data);
  108. var xhr_2 = new XMLHttpRequest();
  109. xhr_2.open("GET", url_2, true);
  110. xhr_2.send(null);
  111. xhr.onreadystatechange = function() {
  112. if(xhr.readyState === 4 && xhr.status === 200) {
  113. var o_p_data = JSON.parse(xhr.responseText);
  114. document.getElementById('see_preview').innerHTML = o_p_data['data'];
  115. eval(o_p_data['js_data'])
  116. }
  117. }
  118. }
  119. function load_raw_preview(name_1, name_2) {
  120. document.getElementById(name_2).innerHTML = document.getElementById(name_1).value;
  121. }