2
0

editor.js 4.0 KB

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