editor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // ====================================
  2. // Markdown Editor
  3. // ====================================
  4. if($('#mk-editor').length === 1) {
  5. require.config({paths: { "ace" : "/assets/js/ace"}});
  6. let scEditor = ace.edit("codeblock-editor");
  7. scEditor.setTheme("ace/theme/tomorrow_night");
  8. scEditor.getSession().setMode("ace/mode/markdown");
  9. scEditor.setOption('hScrollBarAlwaysVisible', false);
  10. scEditor.setOption('wrap', true);
  11. var modelist = ace.require("ace/ext/modelist");
  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. $('#modal-editor-link').slideToggle();
  80. },
  81. className: "fa fa-link",
  82. title: "Insert Link",
  83. },
  84. {
  85. name: "image",
  86. action: (editor) => {
  87. //todo
  88. },
  89. className: "fa fa-image",
  90. title: "Insert Image",
  91. },
  92. '|',
  93. {
  94. name: "inline-code",
  95. action: (editor) => {
  96. if(!editor.codemirror.doc.somethingSelected()) {
  97. return alerts.pushError('Invalid selection','You must select at least 1 character first.');
  98. }
  99. let curSel = editor.codemirror.doc.getSelections();
  100. curSel = _.map(curSel, (s) => {
  101. return '`' + s + '`';
  102. });
  103. editor.codemirror.doc.replaceSelections(curSel);
  104. },
  105. className: "fa fa-terminal",
  106. title: "Inline Code",
  107. },
  108. {
  109. name: "code-block",
  110. action: (editor) => {
  111. $('#modal-editor-codeblock').slideDown(400, () => {
  112. scEditor.resize();
  113. scEditor.focus();
  114. });
  115. },
  116. className: "fa fa-code",
  117. title: "Code Block",
  118. },
  119. '|',
  120. {
  121. name: "table",
  122. action: (editor) => {
  123. //todo
  124. },
  125. className: "fa fa-table",
  126. title: "Insert Table",
  127. },
  128. {
  129. name: "horizontal-rule",
  130. action: SimpleMDE.drawHorizontalRule,
  131. className: "fa fa-minus",
  132. title: "Horizontal Rule",
  133. }
  134. ],
  135. shortcuts: {
  136. "toggleBlockquote": null,
  137. "toggleFullScreen": null
  138. }
  139. });
  140. }
  141. //-> Save
  142. $('.btn-edit-save').on('click', (ev) => {
  143. $.ajax(window.location.href, {
  144. data: {
  145. markdown: mde.value()
  146. },
  147. dataType: 'json',
  148. method: 'PUT'
  149. }).then((rData, rStatus, rXHR) => {
  150. if(rData.ok) {
  151. window.location.assign('/' + pageEntryPath);
  152. } else {
  153. alerts.pushError('Something went wrong', rData.error);
  154. }
  155. }, (rXHR, rStatus, err) => {
  156. alerts.pushError('Something went wrong', 'Save operation failed.');
  157. });
  158. });