Isolate easymde instances so that they can be referenced

This commit is contained in:
tleb 2019-10-16 12:18:23 +02:00
parent e932abfa74
commit 7ecb057b68

View File

@ -5,11 +5,12 @@
{# The easymde script can be included twice, it's safe in the code #} {# The easymde script can be included twice, it's safe in the code #}
<script src="{{ statics.js }}"> </script> <script src="{{ statics.js }}"> </script>
<script type="text/javascript"> <script type="text/javascript">
var css = "{{ statics.css }}"; $(function() {
var lastAPICall; const css = "{{ statics.css }}";
let lastAPICall;
// Only import the css once // Only import the css once
if (!document.head.innerHTML.includes(css)){ if (!document.head.innerHTML.includes(css)) {
document.head.innerHTML += '<link rel="stylesheet" href="' + css + '">'; document.head.innerHTML += '<link rel="stylesheet" href="' + css + '">';
} }
@ -23,11 +24,11 @@
} }
// Pretty markdown input // Pretty markdown input
var easymde = new EasyMDE({ const easymde = new EasyMDE({
element: document.getElementById("{{ widget.attrs.id }}"), element: document.getElementById("{{ widget.attrs.id }}"),
spellChecker: false, spellChecker: false,
autoDownloadFontAwesome: false, autoDownloadFontAwesome: false,
previewRender: function(plainText, preview){ // Async method previewRender: function(plainText, preview) { // Async method
clearTimeout(lastAPICall); clearTimeout(lastAPICall);
lastAPICall = setTimeout(() => { lastAPICall = setTimeout(() => {
customMarkdownParser(plainText, (msg) => preview.innerHTML = msg); customMarkdownParser(plainText, (msg) => preview.innerHTML = msg);
@ -63,7 +64,7 @@
{ {
name: "underline", name: "underline",
action: function customFunction(editor){ action: function customFunction(editor){
var cm = editor.codemirror; let cm = editor.codemirror;
cm.replaceSelection('__' + cm.getSelection() + '__'); cm.replaceSelection('__' + cm.getSelection() + '__');
}, },
className: "fa fa-underline", className: "fa fa-underline",
@ -72,7 +73,7 @@
{ {
name: "superscript", name: "superscript",
action: function customFunction(editor){ action: function customFunction(editor){
var cm = editor.codemirror; let cm = editor.codemirror;
cm.replaceSelection('<sup>' + cm.getSelection() + '</sup>'); cm.replaceSelection('<sup>' + cm.getSelection() + '</sup>');
}, },
className: "fa fa-superscript", className: "fa fa-superscript",
@ -81,7 +82,7 @@
{ {
name: "subscript", name: "subscript",
action: function customFunction(editor){ action: function customFunction(editor){
var cm = editor.codemirror; let cm = editor.codemirror;
cm.replaceSelection('<sub>' + cm.getSelection() + '</sub>'); cm.replaceSelection('<sub>' + cm.getSelection() + '</sub>');
}, },
className: "fa fa-subscript", className: "fa fa-subscript",
@ -167,33 +168,35 @@
] ]
}); });
$(function() { const textarea = document.getElementById('{{ widget.attrs.id }}');
let textarea = document.getElementById('{{ widget.attrs.id }}') const submits = textarea
let submit = textarea
.closest('form') .closest('form')
.querySelector('input[type="submit"]') .querySelectorAll('input[type="submit"]');
let parentDiv = textarea.parentElement const parentDiv = textarea.parentElement;
let submitPressed = false let submitPressed = false;
function checkMarkdownInput(e) { function checkMarkdownInput(e) {
// an attribute is null if it does not exist, else a string // an attribute is null if it does not exist, else a string
let required = textarea.getAttribute('required') != null const required = textarea.getAttribute('required') != null;
let length = textarea.value.trim().length const length = textarea.value.trim().length;
if (required && length == 0) { if (required && length == 0) {
parentDiv.style.boxShadow = 'red 0px 0px 1.5px 1px' parentDiv.style.boxShadow = 'red 0px 0px 1.5px 1px';
e.preventDefault()
} else { } else {
parentDiv.style.boxShadow = '' parentDiv.style.boxShadow = '';
} }
} }
submit.addEventListener('click', (e) => { function onSubmitClick(e) {
if (!submitPressed) { if (!submitPressed) {
easymde.codemirror.on('change', checkMarkdownInput) easymde.codemirror.on('change', checkMarkdownInput);
} }
submitPressed = true submitPressed = true;
checkMarkdownInput(e) checkMarkdownInput(e);
}
submits.forEach((submit) => {
submit.addEventListener('click', onSubmitClick);
}) })
}) })
</script> </script>