2021-10-16 22:58:04 +05:30
|
|
|
import attachTribute from '../tribute.js';
|
|
|
|
|
2021-12-10 08:21:27 +05:30
|
|
|
/**
|
|
|
|
* create an EasyMDE editor for comment
|
|
|
|
* @param textarea jQuery or HTMLElement
|
|
|
|
* @returns {null|EasyMDE}
|
|
|
|
*/
|
|
|
|
export function createCommentEasyMDE(textarea) {
|
|
|
|
if (textarea instanceof jQuery) {
|
|
|
|
textarea = textarea[0];
|
|
|
|
}
|
|
|
|
if (!textarea) {
|
2021-10-16 22:58:04 +05:30
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-12-10 08:21:27 +05:30
|
|
|
const easyMDE = new window.EasyMDE({
|
2021-10-16 22:58:04 +05:30
|
|
|
autoDownloadFontAwesome: false,
|
2021-12-10 08:21:27 +05:30
|
|
|
element: textarea,
|
2021-10-16 22:58:04 +05:30
|
|
|
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',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
});
|
2021-12-10 08:21:27 +05:30
|
|
|
const inputField = easyMDE.codemirror.getInputField();
|
|
|
|
inputField.classList.add('js-quick-submit');
|
|
|
|
easyMDE.codemirror.setOption('extraKeys', {
|
2021-10-16 22:58:04 +05:30
|
|
|
Enter: () => {
|
|
|
|
const tributeContainer = document.querySelector('.tribute-container');
|
|
|
|
if (!tributeContainer || tributeContainer.style.display === 'none') {
|
|
|
|
return CodeMirror.Pass;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Backspace: (cm) => {
|
|
|
|
if (cm.getInputField().trigger) {
|
|
|
|
cm.getInputField().trigger('input');
|
|
|
|
}
|
|
|
|
cm.execCommand('delCharBefore');
|
|
|
|
}
|
|
|
|
});
|
2021-12-10 08:21:27 +05:30
|
|
|
attachTribute(inputField, {mentions: true, emoji: true});
|
|
|
|
|
|
|
|
// TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
|
|
|
|
inputField._data_easyMDE = easyMDE;
|
|
|
|
textarea._data_easyMDE = easyMDE;
|
|
|
|
return easyMDE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the attached EasyMDE editor created by createCommentEasyMDE
|
|
|
|
* @param el jQuery or HTMLElement
|
|
|
|
* @returns {null|EasyMDE}
|
|
|
|
*/
|
|
|
|
export function getAttachedEasyMDE(el) {
|
|
|
|
if (el instanceof jQuery) {
|
|
|
|
el = el[0];
|
|
|
|
}
|
|
|
|
if (!el) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return el._data_easyMDE;
|
2021-10-16 22:58:04 +05:30
|
|
|
}
|
2022-01-03 04:01:03 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* validate if the given textarea from a form, is non-empty.
|
|
|
|
* @param {jQuery | HTMLElement} form
|
|
|
|
* @param {jQuery | HTMLElement} textarea
|
|
|
|
* @returns {boolean} returns true if validation succeeded.
|
|
|
|
*/
|
|
|
|
export function validateTextareaNonEmpty(form, textarea) {
|
|
|
|
if (form instanceof jQuery) {
|
|
|
|
form = form[0];
|
|
|
|
}
|
|
|
|
if (textarea instanceof jQuery) {
|
|
|
|
textarea = textarea[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
const $markdownEditorTextArea = $(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.value.length) {
|
|
|
|
$markdownEditorTextArea.prop('required', true);
|
|
|
|
form.reportValidity();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$markdownEditorTextArea.prop('required', false);
|
|
|
|
return true;
|
|
|
|
}
|