editor.js 4.3 KB

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