editor.js 4.5 KB

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