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.

EasyMDE.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import $ from 'jquery';
  2. import attachTribute from '../tribute.js';
  3. import {handleGlobalEnterQuickSubmit} from '../common-global.js';
  4. /**
  5. * @returns {EasyMDE}
  6. */
  7. export async function importEasyMDE() {
  8. // EasyMDE's CSS should be loaded via webpack config, otherwise our own styles can
  9. // not overwrite the default styles.
  10. const {default: EasyMDE} = await import(/* webpackChunkName: "easymde" */'easymde');
  11. return EasyMDE;
  12. }
  13. /**
  14. * create an EasyMDE editor for comment
  15. * @param textarea jQuery or HTMLElement
  16. * @param easyMDEOptions the options for EasyMDE
  17. * @returns {null|EasyMDE}
  18. */
  19. export async function createCommentEasyMDE(textarea, easyMDEOptions = {}) {
  20. if (textarea instanceof $) {
  21. textarea = textarea[0];
  22. }
  23. if (!textarea) {
  24. return null;
  25. }
  26. const EasyMDE = await importEasyMDE();
  27. const easyMDE = new EasyMDE({
  28. autoDownloadFontAwesome: false,
  29. element: textarea,
  30. forceSync: true,
  31. renderingConfig: {
  32. singleLineBreaks: false,
  33. },
  34. indentWithTabs: false,
  35. tabSize: 4,
  36. spellChecker: false,
  37. toolbar: ['bold', 'italic', 'strikethrough', '|',
  38. 'heading-1', 'heading-2', 'heading-3', 'heading-bigger', 'heading-smaller', '|',
  39. 'code', 'quote', '|', {
  40. name: 'checkbox-empty',
  41. action(e) {
  42. const cm = e.codemirror;
  43. cm.replaceSelection(`\n- [ ] ${cm.getSelection()}`);
  44. cm.focus();
  45. },
  46. className: 'fa fa-square-o',
  47. title: 'Add Checkbox (empty)',
  48. },
  49. {
  50. name: 'checkbox-checked',
  51. action(e) {
  52. const cm = e.codemirror;
  53. cm.replaceSelection(`\n- [x] ${cm.getSelection()}`);
  54. cm.focus();
  55. },
  56. className: 'fa fa-check-square-o',
  57. title: 'Add Checkbox (checked)',
  58. }, '|',
  59. 'unordered-list', 'ordered-list', '|',
  60. 'link', 'image', 'table', 'horizontal-rule', '|',
  61. 'clean-block', '|',
  62. {
  63. name: 'revert-to-textarea',
  64. action(e) {
  65. e.toTextArea();
  66. },
  67. className: 'fa fa-file',
  68. title: 'Revert to simple textarea',
  69. },
  70. ], ...easyMDEOptions});
  71. const inputField = easyMDE.codemirror.getInputField();
  72. easyMDE.codemirror.setOption('extraKeys', {
  73. 'Cmd-Enter': codeMirrorQuickSubmit,
  74. 'Ctrl-Enter': codeMirrorQuickSubmit,
  75. Enter: (cm) => {
  76. const tributeContainer = document.querySelector('.tribute-container');
  77. if (!tributeContainer || tributeContainer.style.display === 'none') {
  78. cm.execCommand('newlineAndIndent');
  79. }
  80. },
  81. Backspace: (cm) => {
  82. if (cm.getInputField().trigger) {
  83. cm.getInputField().trigger('input');
  84. }
  85. cm.execCommand('delCharBefore');
  86. },
  87. });
  88. attachTribute(inputField, {mentions: true, emoji: true});
  89. attachEasyMDEToElements(easyMDE);
  90. return easyMDE;
  91. }
  92. /**
  93. * attach the EasyMDE object to its input elements (InputField, TextArea)
  94. * @param {EasyMDE} easyMDE
  95. */
  96. export function attachEasyMDEToElements(easyMDE) {
  97. // TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
  98. // InputField is used by CodeMirror to accept user input
  99. const inputField = easyMDE.codemirror.getInputField();
  100. inputField._data_easyMDE = easyMDE;
  101. // TextArea is the real textarea element in the form
  102. const textArea = easyMDE.codemirror.getTextArea();
  103. textArea._data_easyMDE = easyMDE;
  104. }
  105. /**
  106. * get the attached EasyMDE editor created by createCommentEasyMDE
  107. * @param el jQuery or HTMLElement
  108. * @returns {null|EasyMDE}
  109. */
  110. export function getAttachedEasyMDE(el) {
  111. if (el instanceof $) {
  112. el = el[0];
  113. }
  114. if (!el) {
  115. return null;
  116. }
  117. return el._data_easyMDE;
  118. }
  119. /**
  120. * validate if the given EasyMDE textarea is is non-empty.
  121. * @param {jQuery} $textarea
  122. * @returns {boolean} returns true if validation succeeded.
  123. */
  124. export function validateTextareaNonEmpty($textarea) {
  125. const $mdeInputField = $(getAttachedEasyMDE($textarea).codemirror.getInputField());
  126. // The original edit area HTML element is hidden and replaced by the
  127. // SimpleMDE/EasyMDE editor, breaking HTML5 input validation if the text area is empty.
  128. // This is a workaround for this upstream bug.
  129. // See https://github.com/sparksuite/simplemde-markdown-editor/issues/324
  130. if (!$textarea.val()) {
  131. $mdeInputField.prop('required', true);
  132. const $form = $textarea.parents('form');
  133. if (!$form.length) {
  134. // this should never happen. we put a alert here in case the textarea would be forgotten to be put in a form
  135. alert('Require non-empty content');
  136. } else {
  137. $form[0].reportValidity();
  138. }
  139. return false;
  140. }
  141. $mdeInputField.prop('required', false);
  142. return true;
  143. }
  144. /**
  145. * there is no guarantee that the CodeMirror object is inside the same form as the textarea,
  146. * so can not call handleGlobalEnterQuickSubmit directly.
  147. * @param {CodeMirror.EditorFromTextArea} codeMirror
  148. */
  149. export function codeMirrorQuickSubmit(codeMirror) {
  150. handleGlobalEnterQuickSubmit(codeMirror.getTextArea());
  151. }