editor.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // ====================================
  2. // Markdown Editor
  3. // ====================================
  4. if($('#mk-editor').length === 1) {
  5. let mdeModalOpenState = false;
  6. let mdeCurrentEditor = null;
  7. //=include editor-codeblock.js
  8. var mde = new SimpleMDE({
  9. autofocus: true,
  10. autoDownloadFontAwesome: false,
  11. element: $("#mk-editor").get(0),
  12. placeholder: 'Enter Markdown formatted content here...',
  13. spellChecker: false,
  14. status: false,
  15. toolbar: [{
  16. name: "bold",
  17. action: SimpleMDE.toggleBold,
  18. className: "fa fa-bold",
  19. title: "Bold",
  20. },
  21. {
  22. name: "italic",
  23. action: SimpleMDE.toggleItalic,
  24. className: "fa fa-italic",
  25. title: "Italic",
  26. },
  27. {
  28. name: "strikethrough",
  29. action: SimpleMDE.toggleStrikethrough,
  30. className: "fa fa-strikethrough",
  31. title: "Strikethrough",
  32. },
  33. '|',
  34. {
  35. name: "heading-1",
  36. action: SimpleMDE.toggleHeading1,
  37. className: "fa fa-header fa-header-x fa-header-1",
  38. title: "Big Heading",
  39. },
  40. {
  41. name: "heading-2",
  42. action: SimpleMDE.toggleHeading2,
  43. className: "fa fa-header fa-header-x fa-header-2",
  44. title: "Medium Heading",
  45. },
  46. {
  47. name: "heading-3",
  48. action: SimpleMDE.toggleHeading3,
  49. className: "fa fa-header fa-header-x fa-header-3",
  50. title: "Small Heading",
  51. },
  52. {
  53. name: "quote",
  54. action: SimpleMDE.toggleBlockquote,
  55. className: "fa fa-quote-left",
  56. title: "Quote",
  57. },
  58. '|',
  59. {
  60. name: "unordered-list",
  61. action: SimpleMDE.toggleUnorderedList,
  62. className: "fa fa-list-ul",
  63. title: "Bullet List",
  64. },
  65. {
  66. name: "ordered-list",
  67. action: SimpleMDE.toggleOrderedList,
  68. className: "fa fa-list-ol",
  69. title: "Numbered List",
  70. },
  71. '|',
  72. {
  73. name: "link",
  74. action: (editor) => {
  75. if(!mdeModalOpenState) {
  76. mdeModalOpenState = true;
  77. $('#modal-editor-link').slideToggle();
  78. }
  79. },
  80. className: "fa fa-link",
  81. title: "Insert Link",
  82. },
  83. {
  84. name: "image",
  85. action: (editor) => {
  86. //todo
  87. },
  88. className: "fa fa-image",
  89. title: "Insert Image",
  90. },
  91. '|',
  92. {
  93. name: "inline-code",
  94. action: (editor) => {
  95. if(!editor.codemirror.doc.somethingSelected()) {
  96. return alerts.pushError('Invalid selection','You must select at least 1 character first.');
  97. }
  98. let curSel = editor.codemirror.doc.getSelections();
  99. curSel = _.map(curSel, (s) => {
  100. return '`' + s + '`';
  101. });
  102. editor.codemirror.doc.replaceSelections(curSel);
  103. },
  104. className: "fa fa-terminal",
  105. title: "Inline Code",
  106. },
  107. {
  108. name: "code-block",
  109. action: (editor) => {
  110. if(!mdeModalOpenState) {
  111. mdeModalOpenState = true;
  112. if(mde.codemirror.doc.somethingSelected()) {
  113. codeEditor.setValue(mde.codemirror.doc.getSelection());
  114. } else {
  115. codeEditor.setValue('');
  116. }
  117. $('#modal-editor-codeblock').slideDown(400, () => {
  118. codeEditor.resize();
  119. codeEditor.focus();
  120. });
  121. }
  122. },
  123. className: "fa fa-code",
  124. title: "Code Block",
  125. },
  126. '|',
  127. {
  128. name: "table",
  129. action: (editor) => {
  130. //todo
  131. },
  132. className: "fa fa-table",
  133. title: "Insert Table",
  134. },
  135. {
  136. name: "horizontal-rule",
  137. action: SimpleMDE.drawHorizontalRule,
  138. className: "fa fa-minus",
  139. title: "Horizontal Rule",
  140. }
  141. ],
  142. shortcuts: {
  143. "toggleBlockquote": null,
  144. "toggleFullScreen": null
  145. }
  146. });
  147. }
  148. //-> Save
  149. $('.btn-edit-save').on('click', (ev) => {
  150. $.ajax(window.location.href, {
  151. data: {
  152. markdown: mde.value()
  153. },
  154. dataType: 'json',
  155. method: 'PUT'
  156. }).then((rData, rStatus, rXHR) => {
  157. if(rData.ok) {
  158. window.location.assign('/' + pageEntryPath);
  159. } else {
  160. alerts.pushError('Something went wrong', rData.error);
  161. }
  162. }, (rXHR, rStatus, err) => {
  163. alerts.pushError('Something went wrong', 'Save operation failed.');
  164. });
  165. });