editor.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. (function () {
  2. var converter = new Markdown.Converter();
  3. Markdown.Extra.init(converter);
  4. var editor = new Markdown.Editor(converter);
  5. // Run once on DOM ready
  6. $('#insertImageDialog').on('hidden.bs.modal', function () {
  7. $('.wmd-prompt-background, .wmd-prompt-dialog').remove();
  8. });
  9. //======Image Uploader=====
  10. var callbackFunc;
  11. var dialogClose = function() {
  12. $('#insertImageDialog').modal('hide');
  13. $('#insertImageDialogURL').val('');
  14. $('#insertImageDialogFile').val('');
  15. $('#insertMediaDialogURL').val('');
  16. $('#insertMediaDialogFile').val('');
  17. $('#gallery-1').html(initial_image);
  18. $('#gallery-2').html(initial_image);
  19. // Also remove the PageDown/WMD overlay, if present
  20. $('.wmd-prompt-background').remove();
  21. $('.wmd-prompt-dialog').remove(); // WMD often spawns a paired dialog
  22. };
  23. $('#insertImageDialogInsert').click( function() {
  24. callbackFunc( $('#insertImageDialogURL').val().length > 0 ? $('#insertImageDialogURL').val() : null );
  25. dialogClose();
  26. });
  27. $('#insertImageDialogClose').click( function() {
  28. callbackFunc(null);
  29. dialogClose();
  30. });
  31. $('#insertImageDialogCancel').click( function() {
  32. callbackFunc(null);
  33. dialogClose();
  34. });
  35. $('#insertImageDialogFile').on('input', function(){
  36. var file = $("#insertImageDialogFile").prop("files");
  37. var formData = new FormData();
  38. formData.append('file', file[0], file[0].name);
  39. // Set up the request.
  40. $.ajax({
  41. type: "POST",
  42. url: base_path + 'upload.php',
  43. data: formData,
  44. processData: false,
  45. contentType: false,
  46. success: function (response) {
  47. if (response.error == '0')
  48. {
  49. callbackFunc(base_path + response.path);
  50. dialogClose();
  51. }
  52. else
  53. {
  54. if (response.error !== '') alert(response.error);
  55. else alert("An unknown error has occurred");
  56. console.error("Bad Response");
  57. console.error(response);
  58. $('#insertImageDialogFile').val('');
  59. }
  60. },
  61. failure: function (response) {
  62. if (response.error !== '') alert(response.error);
  63. else alert("An unknown error has occurred");
  64. console.error("Unable to Upload");
  65. console.error(response);
  66. $('#insertImageDialogFile').val('');
  67. }
  68. });//ajax
  69. });//oninput
  70. editor.hooks.set('insertImageDialog', function(callback) {
  71. $('#insertImageDialog').modal('show');
  72. callbackFunc = callback;
  73. return true; // tell the editor that we'll take care of getting the image url
  74. });
  75. //=====end image uploader=====
  76. editor.run();
  77. //=====drag and drop uploading=====
  78. function initDropZone(dropZoneId, fileInputId) {
  79. const dropZone = document.getElementById(dropZoneId);
  80. const fileInput = document.getElementById(fileInputId);
  81. if (!dropZone || !fileInput) return; // fail gracefully
  82. // Clicking the drop zone triggers the file picker
  83. dropZone.addEventListener('click', (e) => {
  84. e.preventDefault(); // prevent button form submit
  85. fileInput.click();
  86. });
  87. // Highlight on dragover (styles in css file)
  88. dropZone.addEventListener('dragover', (e) => {
  89. e.preventDefault();
  90. dropZone.classList.add('dragover');
  91. });
  92. // Remove highlight on dragleave
  93. dropZone.addEventListener('dragleave', () => {
  94. dropZone.classList.remove('dragover');
  95. });
  96. // Handle drop
  97. dropZone.addEventListener('drop', (e) => {
  98. e.preventDefault();
  99. dropZone.classList.remove('dragover');
  100. const files = e.dataTransfer.files;
  101. if (files.length > 0) {
  102. fileInput.files = files;
  103. // Fire both 'change' and 'input' to simulate a real user selection
  104. ['change', 'input'].forEach(eventType => {
  105. const event = new Event(eventType, { bubbles: true });
  106. fileInput.dispatchEvent(event);
  107. });
  108. }
  109. });
  110. }
  111. // Initialize dropZones
  112. initDropZone('dropZoneIMDF', 'insertMediaDialogFile'); // Featured image
  113. initDropZone('dropZoneIIDF', 'insertImageDialogFile'); // Content image
  114. //=====end drag and drop uploading=====
  115. })();