edit.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // "use strict";
  2. function do_stop_exit() {
  3. window.onbeforeunload = function() {
  4. do_insert_edit_data();
  5. let data = document.getElementById('opennamu_js_edit_textarea').value;
  6. let origin = document.getElementById('opennamu_js_edit_origin').value;
  7. if(data !== origin) {
  8. return '';
  9. }
  10. }
  11. }
  12. function do_insert_edit_data() {
  13. do_monaco_to_textarea();
  14. let get_data = document.getElementById('opennamu_js_edit_textarea_view').value;
  15. document.getElementById('opennamu_js_edit_textarea').value = get_data;
  16. }
  17. function do_stop_exit_release() {
  18. window.onbeforeunload = function () {}
  19. }
  20. function do_monaco_to_textarea() {
  21. try {
  22. document.getElementById('opennamu_js_edit_textarea_view').value = window.editor.getValue();
  23. } catch(e) {}
  24. }
  25. function do_insert_preview() {
  26. let s_data = new FormData();
  27. s_data.append('data', document.getElementById('opennamu_js_edit_textarea').value);
  28. let xhr = new XMLHttpRequest();
  29. xhr.open("POST", "/api/w/Test");
  30. xhr.send(s_data);
  31. xhr.onreadystatechange = function() {
  32. if(xhr.readyState === 4 && xhr.status === 200) {
  33. let o_p_data = JSON.parse(xhr.responseText);
  34. document.getElementById('opennamu_js_preview_area').innerHTML = o_p_data['data'];
  35. eval(o_p_data['js_data'])
  36. }
  37. }
  38. }
  39. if(window.location.pathname.match(/^\/(edit|edit_from|edit_section)\//)) {
  40. do_stop_exit();
  41. document.getElementById('opennamu_js_save').onclick = function() {
  42. do_insert_edit_data();
  43. do_stop_exit_release();
  44. };
  45. document.getElementById('opennamu_js_preview').onclick = function() {
  46. do_insert_edit_data();
  47. do_insert_preview();
  48. };
  49. }
  50. if(window.location.pathname.match(/^\/(thread)\//)) {
  51. document.getElementById('opennamu_js_preview').onclick = function() {
  52. do_insert_preview();
  53. };
  54. }