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.

194 lines
4.1 KiB

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