edit.js 2.0 KB

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