load_editor.js 4.4 KB

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