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.
187 lines
5.6 KiB
187 lines
5.6 KiB
3 years ago
|
import attachTribute from '../tribute.js';
|
||
|
|
||
3 years ago
|
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
|
||
|
|
||
|
const [{default: EasyMDE}, {default: CodeMirror}] = await Promise.all([
|
||
|
import(/* webpackChunkName: "easymde" */'easymde'),
|
||
|
import(/* webpackChunkName: "codemirror" */'codemirror'),
|
||
|
import(/* webpackChunkName: "easymde" */'easymde/dist/easymde.min.css'),
|
||
|
]);
|
||
|
|
||
|
// 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;
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* create an EasyMDE editor for comment
|
||
|
* @param textarea jQuery or HTMLElement
|
||
|
* @returns {null|EasyMDE}
|
||
|
*/
|
||
3 years ago
|
export async function createCommentEasyMDE(textarea) {
|
||
3 years ago
|
if (textarea instanceof jQuery) {
|
||
|
textarea = textarea[0];
|
||
|
}
|
||
|
if (!textarea) {
|
||
3 years ago
|
return null;
|
||
|
}
|
||
|
|
||
3 years ago
|
const EasyMDE = await importEasyMDE();
|
||
|
const easyMDE = new EasyMDE({
|
||
3 years ago
|
autoDownloadFontAwesome: false,
|
||
3 years ago
|
element: textarea,
|
||
3 years ago
|
forceSync: true,
|
||
|
renderingConfig: {
|
||
3 years ago
|
singleLineBreaks: false,
|
||
3 years ago
|
},
|
||
|
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',
|
||
|
},
|
||
3 years ago
|
],
|
||
3 years ago
|
});
|
||
3 years ago
|
const inputField = easyMDE.codemirror.getInputField();
|
||
|
inputField.classList.add('js-quick-submit');
|
||
|
easyMDE.codemirror.setOption('extraKeys', {
|
||
3 years ago
|
Enter: () => {
|
||
|
const tributeContainer = document.querySelector('.tribute-container');
|
||
|
if (!tributeContainer || tributeContainer.style.display === 'none') {
|
||
3 years ago
|
return window.CodeMirror.Pass;
|
||
3 years ago
|
}
|
||
|
},
|
||
|
Backspace: (cm) => {
|
||
|
if (cm.getInputField().trigger) {
|
||
|
cm.getInputField().trigger('input');
|
||
|
}
|
||
|
cm.execCommand('delCharBefore');
|
||
3 years ago
|
},
|
||
3 years ago
|
});
|
||
3 years ago
|
attachTribute(inputField, {mentions: true, emoji: true});
|
||
3 years ago
|
attachEasyMDEToElements(easyMDE);
|
||
|
return easyMDE;
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
/**
|
||
|
* attach the EasyMDE object to its input elements (InputField, TextArea)
|
||
|
* @param {EasyMDE} easyMDE
|
||
|
*/
|
||
|
export function attachEasyMDEToElements(easyMDE) {
|
||
3 years ago
|
// TODO: that's the only way we can do now to attach the EasyMDE object to a HTMLElement
|
||
3 years ago
|
|
||
|
// InputField is used by CodeMirror to accept user input
|
||
|
const inputField = easyMDE.codemirror.getInputField();
|
||
3 years ago
|
inputField._data_easyMDE = easyMDE;
|
||
3 years ago
|
|
||
|
// TextArea is the real textarea element in the form
|
||
|
const textArea = easyMDE.codemirror.getTextArea();
|
||
|
textArea._data_easyMDE = easyMDE;
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
|
||
3 years ago
|
/**
|
||
|
* 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;
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
/**
|
||
3 years ago
|
* validate if the given EasyMDE textarea is is non-empty.
|
||
|
* @param {jQuery} $textarea
|
||
3 years ago
|
* @returns {boolean} returns true if validation succeeded.
|
||
|
*/
|
||
3 years ago
|
export function validateTextareaNonEmpty($textarea) {
|
||
|
const $mdeInputField = $(getAttachedEasyMDE($textarea).codemirror.getInputField());
|
||
3 years ago
|
// 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
|
||
3 years ago
|
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();
|
||
|
}
|
||
3 years ago
|
return false;
|
||
|
}
|
||
3 years ago
|
$mdeInputField.prop('required', false);
|
||
3 years ago
|
return true;
|
||
|
}
|