summaryrefslogtreecommitdiffstats
path: root/web_src/js/features/comp/EasyMDE.js
blob: 9d711088d8f12a390e3af0c5160f20e1cf55bf60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import $ from 'jquery';
import attachTribute from '../tribute.js';

const {appSubUrl} = window.config;

function loadScript(url) {
  return new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.async = true;
    script.addEventListener('load', () => {
      resolve();
    });
    script.addEventListener('error', (e) => {
      reject(e.error);
    });
    script.src = url;
    document.body.appendChild(script);
  });
}

/**
 * @returns {EasyMDE}
 */
export async function importEasyMDE() {
  // for CodeMirror: the plugins should be loaded dynamically
  // https://github.com/codemirror/CodeMirror/issues/5484
  // https://github.com/codemirror/CodeMirror/issues/4838

  // EasyMDE's CSS should be loaded via webpack config, otherwise our own styles can not overwrite the default styles.
  const [{default: EasyMDE}, {default: CodeMirror}] = await Promise.all([
    import(/* webpackChunkName: "easymde" */'easymde'),
    import(/* webpackChunkName: "codemirror" */'codemirror'),
  ]);

  // CodeMirror plugins must be loaded by a "Plain browser env"
  window.CodeMirror = CodeMirror;
  await Promise.all([
    loadScript(`${appSubUrl}/assets/vendor/plugins/codemirror/addon/mode/loadmode.js`),
    loadScript(`${appSubUrl}/assets/vendor/plugins/codemirror/mode/meta.js`),
  ]);

  // the loadmode.js/meta.js would set the modeURL/modeInfo properties, so we check it to make sure our loading works
  if (!CodeMirror.modeURL || !CodeMirror.modeInfo) {
    throw new Error('failed to load plugins for CodeMirror');
  }

  CodeMirror.modeURL = `${appSubUrl}/assets/vendor/plugins/codemirror/mode/%N/%N.js`;
  return EasyMDE;
}

/**
 * create an EasyMDE editor for comment
 * @param textarea jQuery or HTMLElement
 * @param easyMDEOptions the options for EasyMDE
 * @returns {null|EasyMDE}
 */
export async function createCommentEasyMDE(textarea, easyMDEOptions = {}) {
  if (textarea instanceof $) {
    textarea = textarea[0];
  }
  if (!textarea) {
    return null;
  }

  const EasyMDE = await importEasyMDE();

  const easyMDE = new EasyMDE({
    autoDownloadFontAwesome: false,
    element: textarea,
    forceSync: true,
    renderingConfig: {
      singleLineBreaks: false,
    },
    indentWithTabs: false,
    tabSize: 4,
    spellChecker: false,
    toolbar: ['bold', 'italic', 'strikethrough', '|',
      'heading-1', 'heading-2', 'heading-3', 'heading-bigger', 'heading-smaller', '|',
      'code', 'quote', '|', {
        name: 'checkbox-empty',
        action(e) {
          const cm = e.codemirror;
          cm.replaceSelection(`\n- [ ] ${cm.getSelection()}`);
          cm.focus();
        },
        className: 'fa fa-square-o',
        title: 'Add Checkbox (empty)',
      },
      {
        name: 'checkbox-checked',
        action(e) {
          const cm = e.codemirror;
          cm.replaceSelection(`\n- [x] ${cm.getSelection()}`);
          cm.focus();
        },
        className: 'fa fa-check-square-o',
        title: 'Add Checkbox (checked)',
      }, '|',
      'unordered-list', 'ordered-list', '|',
      'link', 'image', 'table', 'horizontal-rule', '|',
      'clean-block', '|',
      {
        name: 'revert-to-textarea',
        action(e) {
          e.toTextArea();
        },
        className: 'fa fa-file',
        title: 'Revert to simple textarea',
      },
    ], ...easyMDEOptions});
  const inputField = easyMDE.codemirror.getInputField();
  inputField.classList.add('js-quick-submit');
  easyMDE.codemirror.setOption('extraKeys', {
    Enter: () => {
      const tributeContainer = document.querySelector('.tribute-container');
      if (!tributeContainer || tributeContainer.style.display === 'none') {
        return window.CodeMirror.Pass;
      }
    },
    Backspace: (cm) => {
      if (cm.getInputField().trigger) {
        cm.getInputField().trigger('input');
      }
      cm.execCommand('delCharBefore');
    },
  });
  attachTribute(inputField, {mentions: true, emoji: true});
  attachEasyMDEToElements(easyMDE);
  return easyMDE;
}

/**
 * attach the EasyMDE object to its input elements (InputField, TextArea)
 * @param {EasyMDE} easyMDE
 */
export function attachEasyMDEToElements(easyMDE) {
  // TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement

  // InputField is used by CodeMirror to accept user input
  const inputField = easyMDE.codemirror.getInputField();
  inputField._data_easyMDE = easyMDE;

  // TextArea is the real textarea element in the form
  const textArea = easyMDE.codemirror.getTextArea();
  textArea._data_easyMDE = easyMDE;
}


/**
 * get the attached EasyMDE editor created by createCommentEasyMDE
 * @param el jQuery or HTMLElement
 * @returns {null|EasyMDE}
 */
export function getAttachedEasyMDE(el) {
  if (el instanceof $) {
    el = el[0];
  }
  if (!el) {
    return null;
  }
  return el._data_easyMDE;
}

/**
 * validate if the given EasyMDE textarea is is non-empty.
 * @param {jQuery} $textarea
 * @returns {boolean} returns true if validation succeeded.
 */
export function validateTextareaNonEmpty($textarea) {
  const $mdeInputField = $(getAttachedEasyMDE($textarea).codemirror.getInputField());
  // The original edit area HTML element is hidden and replaced by the
  // SimpleMDE/EasyMDE editor, breaking HTML5 input validation if the text area is empty.
  // This is a workaround for this upstream bug.
  // See https://github.com/sparksuite/simplemde-markdown-editor/issues/324
  if (!$textarea.val()) {
    $mdeInputField.prop('required', true);
    const $form = $textarea.parents('form');
    if (!$form.length) {
      // this should never happen. we put a alert here in case the textarea would be forgotten to be put in a form
      alert('Require non-empty content');
    } else {
      $form[0].reportValidity();
    }
    return false;
  }
  $mdeInputField.prop('required', false);
  return true;
}