2022-01-28 21:00:11 +00:00
|
|
|
import $ from 'jquery';
|
2022-12-23 16:03:11 +00:00
|
|
|
import {attachTribute} from '../tribute.js';
|
2022-08-05 10:08:29 +00:00
|
|
|
import {handleGlobalEnterQuickSubmit} from './QuickSubmit.js';
|
2021-10-16 17:28:04 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
/**
|
|
|
|
* @returns {EasyMDE}
|
|
|
|
*/
|
|
|
|
export async function importEasyMDE() {
|
2022-02-26 15:47:52 +00:00
|
|
|
// EasyMDE's CSS should be loaded via webpack config, otherwise our own styles can
|
|
|
|
// not overwrite the default styles.
|
|
|
|
const {default: EasyMDE} = await import(/* webpackChunkName: "easymde" */'easymde');
|
2022-01-05 12:17:25 +00:00
|
|
|
return EasyMDE;
|
|
|
|
}
|
|
|
|
|
2021-12-10 02:51:27 +00:00
|
|
|
/**
|
|
|
|
* create an EasyMDE editor for comment
|
|
|
|
* @param textarea jQuery or HTMLElement
|
2022-01-18 16:57:57 +00:00
|
|
|
* @param easyMDEOptions the options for EasyMDE
|
2021-12-10 02:51:27 +00:00
|
|
|
* @returns {null|EasyMDE}
|
|
|
|
*/
|
2022-01-18 16:57:57 +00:00
|
|
|
export async function createCommentEasyMDE(textarea, easyMDEOptions = {}) {
|
2022-01-28 21:00:11 +00:00
|
|
|
if (textarea instanceof $) {
|
2021-12-10 02:51:27 +00:00
|
|
|
textarea = textarea[0];
|
|
|
|
}
|
|
|
|
if (!textarea) {
|
2021-10-16 17:28:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
const EasyMDE = await importEasyMDE();
|
2022-01-18 16:57:57 +00:00
|
|
|
|
2022-01-05 12:17:25 +00:00
|
|
|
const easyMDE = new EasyMDE({
|
2021-10-16 17:28:04 +00:00
|
|
|
autoDownloadFontAwesome: false,
|
2021-12-10 02:51:27 +00:00
|
|
|
element: textarea,
|
2021-10-16 17:28:04 +00:00
|
|
|
forceSync: true,
|
|
|
|
renderingConfig: {
|
2022-01-05 12:17:25 +00:00
|
|
|
singleLineBreaks: false,
|
2021-10-16 17:28:04 +00:00
|
|
|
},
|
|
|
|
indentWithTabs: false,
|
|
|
|
tabSize: 4,
|
|
|
|
spellChecker: false,
|
2022-06-28 17:52:58 +00:00
|
|
|
inputStyle: 'contenteditable', // nativeSpellcheck requires contenteditable
|
|
|
|
nativeSpellcheck: true,
|
2021-10-16 17:28:04 +00:00
|
|
|
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',
|
|
|
|
},
|
2022-01-18 16:57:57 +00:00
|
|
|
], ...easyMDEOptions});
|
2022-05-20 02:26:04 +00:00
|
|
|
|
2021-12-10 02:51:27 +00:00
|
|
|
const inputField = easyMDE.codemirror.getInputField();
|
2022-05-20 02:26:04 +00:00
|
|
|
|
2023-02-28 22:54:24 +00:00
|
|
|
easyMDE.codemirror.on('change', (...args) => {
|
|
|
|
easyMDEOptions?.onChange?.(...args);
|
|
|
|
});
|
2021-12-10 02:51:27 +00:00
|
|
|
easyMDE.codemirror.setOption('extraKeys', {
|
2022-05-20 02:26:04 +00:00
|
|
|
'Cmd-Enter': codeMirrorQuickSubmit,
|
|
|
|
'Ctrl-Enter': codeMirrorQuickSubmit,
|
2022-03-05 17:53:34 +00:00
|
|
|
Enter: (cm) => {
|
2021-10-16 17:28:04 +00:00
|
|
|
const tributeContainer = document.querySelector('.tribute-container');
|
|
|
|
if (!tributeContainer || tributeContainer.style.display === 'none') {
|
2022-03-05 17:53:34 +00:00
|
|
|
cm.execCommand('newlineAndIndent');
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Backspace: (cm) => {
|
|
|
|
if (cm.getInputField().trigger) {
|
|
|
|
cm.getInputField().trigger('input');
|
|
|
|
}
|
|
|
|
cm.execCommand('delCharBefore');
|
2022-01-05 12:17:25 +00:00
|
|
|
},
|
2022-09-03 09:43:27 +00:00
|
|
|
Up: (cm) => {
|
|
|
|
const tributeContainer = document.querySelector('.tribute-container');
|
|
|
|
if (!tributeContainer || tributeContainer.style.display === 'none') {
|
|
|
|
return cm.execCommand('goLineUp');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Down: (cm) => {
|
|
|
|
const tributeContainer = document.querySelector('.tribute-container');
|
|
|
|
if (!tributeContainer || tributeContainer.style.display === 'none') {
|
|
|
|
return cm.execCommand('goLineDown');
|
|
|
|
}
|
|
|
|
},
|
2021-10-16 17:28:04 +00:00
|
|
|
});
|
2022-09-02 07:58:49 +00:00
|
|
|
await attachTribute(inputField, {mentions: true, emoji: true});
|
2022-01-03 16:53:53 +00:00
|
|
|
attachEasyMDEToElements(easyMDE);
|
|
|
|
return easyMDE;
|
|
|
|
}
|
2021-12-10 02:51:27 +00:00
|
|
|
|
2022-01-03 16:53:53 +00:00
|
|
|
/**
|
|
|
|
* attach the EasyMDE object to its input elements (InputField, TextArea)
|
|
|
|
* @param {EasyMDE} easyMDE
|
|
|
|
*/
|
|
|
|
export function attachEasyMDEToElements(easyMDE) {
|
2021-12-10 02:51:27 +00:00
|
|
|
// TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
|
2022-01-03 16:53:53 +00:00
|
|
|
|
|
|
|
// InputField is used by CodeMirror to accept user input
|
|
|
|
const inputField = easyMDE.codemirror.getInputField();
|
2021-12-10 02:51:27 +00:00
|
|
|
inputField._data_easyMDE = easyMDE;
|
2022-01-03 16:53:53 +00:00
|
|
|
|
|
|
|
// TextArea is the real textarea element in the form
|
|
|
|
const textArea = easyMDE.codemirror.getTextArea();
|
|
|
|
textArea._data_easyMDE = easyMDE;
|
2021-12-10 02:51:27 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 16:53:53 +00:00
|
|
|
|
2021-12-10 02:51:27 +00:00
|
|
|
/**
|
|
|
|
* get the attached EasyMDE editor created by createCommentEasyMDE
|
|
|
|
* @param el jQuery or HTMLElement
|
|
|
|
* @returns {null|EasyMDE}
|
|
|
|
*/
|
|
|
|
export function getAttachedEasyMDE(el) {
|
2022-01-28 21:00:11 +00:00
|
|
|
if (el instanceof $) {
|
2021-12-10 02:51:27 +00:00
|
|
|
el = el[0];
|
|
|
|
}
|
|
|
|
if (!el) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return el._data_easyMDE;
|
2021-10-16 17:28:04 +00:00
|
|
|
}
|
2022-01-02 22:31:03 +00:00
|
|
|
|
|
|
|
/**
|
2022-01-03 16:53:53 +00:00
|
|
|
* validate if the given EasyMDE textarea is is non-empty.
|
|
|
|
* @param {jQuery} $textarea
|
2022-01-02 22:31:03 +00:00
|
|
|
* @returns {boolean} returns true if validation succeeded.
|
|
|
|
*/
|
2022-01-03 16:53:53 +00:00
|
|
|
export function validateTextareaNonEmpty($textarea) {
|
|
|
|
const $mdeInputField = $(getAttachedEasyMDE($textarea).codemirror.getInputField());
|
2022-01-02 22:31:03 +00:00
|
|
|
// 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
|
2022-01-03 16:53:53 +00:00
|
|
|
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();
|
|
|
|
}
|
2022-01-02 22:31:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-03 16:53:53 +00:00
|
|
|
$mdeInputField.prop('required', false);
|
2022-01-02 22:31:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-05-20 02:26:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* there is no guarantee that the CodeMirror object is inside the same form as the textarea,
|
|
|
|
* so can not call handleGlobalEnterQuickSubmit directly.
|
|
|
|
* @param {CodeMirror.EditorFromTextArea} codeMirror
|
|
|
|
*/
|
|
|
|
export function codeMirrorQuickSubmit(codeMirror) {
|
|
|
|
handleGlobalEnterQuickSubmit(codeMirror.getTextArea());
|
|
|
|
}
|