You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
3.9 KiB

  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. $('#modal-editor-image').slideDown();
  87. },
  88. className: "fa fa-image",
  89. title: "Insert Image",
  90. },
  91. {
  92. name: "file",
  93. action: (editor) => {
  94. //todo
  95. },
  96. className: "fa fa-file-text-o",
  97. title: "Insert File",
  98. },
  99. '|',
  100. {
  101. name: "inline-code",
  102. action: (editor) => {
  103. if(!editor.codemirror.doc.somethingSelected()) {
  104. return alerts.pushError('Invalid selection','You must select at least 1 character first.');
  105. }
  106. let curSel = editor.codemirror.doc.getSelections();
  107. curSel = _.map(curSel, (s) => {
  108. return '`' + s + '`';
  109. });
  110. editor.codemirror.doc.replaceSelections(curSel);
  111. },
  112. className: "fa fa-terminal",
  113. title: "Inline Code",
  114. },
  115. {
  116. name: "code-block",
  117. action: (editor) => {
  118. if(!mdeModalOpenState) {
  119. mdeModalOpenState = true;
  120. if(mde.codemirror.doc.somethingSelected()) {
  121. codeEditor.setValue(mde.codemirror.doc.getSelection());
  122. } else {
  123. codeEditor.setValue('');
  124. }
  125. $('#modal-editor-codeblock').slideDown(400, () => {
  126. codeEditor.resize();
  127. codeEditor.focus();
  128. });
  129. }
  130. },
  131. className: "fa fa-code",
  132. title: "Code Block",
  133. },
  134. '|',
  135. {
  136. name: "table",
  137. action: (editor) => {
  138. //todo
  139. },
  140. className: "fa fa-table",
  141. title: "Insert Table",
  142. },
  143. {
  144. name: "horizontal-rule",
  145. action: SimpleMDE.drawHorizontalRule,
  146. className: "fa fa-minus",
  147. title: "Horizontal Rule",
  148. }
  149. ],
  150. shortcuts: {
  151. "toggleBlockquote": null,
  152. "toggleFullScreen": null
  153. }
  154. });
  155. }
  156. //-> Save
  157. $('.btn-edit-save').on('click', (ev) => {
  158. $.ajax(window.location.href, {
  159. data: {
  160. markdown: mde.value()
  161. },
  162. dataType: 'json',
  163. method: 'PUT'
  164. }).then((rData, rStatus, rXHR) => {
  165. if(rData.ok) {
  166. window.location.assign('/' + pageEntryPath);
  167. } else {
  168. alerts.pushError('Something went wrong', rData.error);
  169. }
  170. }, (rXHR, rStatus, err) => {
  171. alerts.pushError('Something went wrong', 'Save operation failed.');
  172. });
  173. });