Commit 7ecb057b authored by tleb's avatar tleb
Browse files

Isolate easymde instances so that they can be referenced

parent e932abfa
Pipeline #2097 passed with stage
in 39 minutes and 20 seconds
...@@ -5,195 +5,198 @@ ...@@ -5,195 +5,198 @@
{# 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 + '">';
} }
// Custom markdown parser // Custom markdown parser
function customMarkdownParser(plainText, cb) { function customMarkdownParser(plainText, cb) {
$.ajax({ $.ajax({
url: "{{ markdown_api_url }}", url: "{{ markdown_api_url }}",
method: "POST", method: "POST",
data: { text: plainText, csrfmiddlewaretoken: getCSRFToken() }, data: { text: plainText, csrfmiddlewaretoken: getCSRFToken() },
}).done(cb); }).done(cb);
} }
// 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);
}, 300); }, 300);
return preview.innerHTML; return preview.innerHTML;
}, },
forceSync: true, // Avoid validation error on generic create view forceSync: true, // Avoid validation error on generic create view
toolbar: [ toolbar: [
{ {
name: "heading-smaller", name: "heading-smaller",
action: EasyMDE.toggleHeadingSmaller, action: EasyMDE.toggleHeadingSmaller,
className: "fa fa-header", className: "fa fa-header",
title: "{{ translations.heading_smaller }}" title: "{{ translations.heading_smaller }}"
}, },
{ {
name: "italic", name: "italic",
action: EasyMDE.toggleItalic, action: EasyMDE.toggleItalic,
className: "fa fa-italic", className: "fa fa-italic",
title: "{{ translations.italic }}" title: "{{ translations.italic }}"
}, },
{ {
name: "bold", name: "bold",
action: EasyMDE.toggleBold, action: EasyMDE.toggleBold,
className: "fa fa-bold", className: "fa fa-bold",
title: "{{ translations.bold }}" title: "{{ translations.bold }}"
}, },
{ {
name: "strikethrough", name: "strikethrough",
action: EasyMDE.toggleStrikethrough, action: EasyMDE.toggleStrikethrough,
className: "fa fa-strikethrough", className: "fa fa-strikethrough",
title: "{{ translations.strikethrough }}" title: "{{ translations.strikethrough }}"
}, },
{ {
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",
title: "{{ translations.underline }}" title: "{{ translations.underline }}"
}, },
{ {
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",
title: "{{ translations.superscript }}" title: "{{ translations.superscript }}"
}, },
{ {
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",
title: "{{ translations.subscript }}" title: "{{ translations.subscript }}"
}, },
{ {
name: "code", name: "code",
action: EasyMDE.toggleCodeBlock, action: EasyMDE.toggleCodeBlock,
className: "fa fa-code", className: "fa fa-code",
title: "{{ translations.code }}" title: "{{ translations.code }}"
}, },
"|", "|",
{ {
name: "quote", name: "quote",
action: EasyMDE.toggleBlockquote, action: EasyMDE.toggleBlockquote,
className: "fa fa-quote-left", className: "fa fa-quote-left",
title: "{{ translations.quote }}" title: "{{ translations.quote }}"
}, },
{ {
name: "unordered-list", name: "unordered-list",
action: EasyMDE.toggleUnorderedList, action: EasyMDE.toggleUnorderedList,
className: "fa fa-list-ul", className: "fa fa-list-ul",
title: "{{ translations.unordered_list }}" title: "{{ translations.unordered_list }}"
}, },
{ {
name: "ordered-list", name: "ordered-list",
action: EasyMDE.toggleOrderedList, action: EasyMDE.toggleOrderedList,
className: "fa fa-list-ol", className: "fa fa-list-ol",
title: "{{ translations.ordered_list }}" title: "{{ translations.ordered_list }}"
}, },
"|", "|",
{ {
name: "link", name: "link",
action: EasyMDE.drawLink, action: EasyMDE.drawLink,
className: "fa fa-link", className: "fa fa-link",
title: "{{ translations.link }}" title: "{{ translations.link }}"
}, },
{ {
name: "image", name: "image",
action: EasyMDE.drawImage, action: EasyMDE.drawImage,
className: "fa fa-picture-o", className: "fa fa-picture-o",
title: "{{ translations.image }}" title: "{{ translations.image }}"
}, },
{ {
name: "table", name: "table",
action: EasyMDE.drawTable, action: EasyMDE.drawTable,
className: "fa fa-table", className: "fa fa-table",
title: "{{ translations.table }}" title: "{{ translations.table }}"
}, },
"|", "|",
{ {
name: "clean-block", name: "clean-block",
action: EasyMDE.cleanBlock, action: EasyMDE.cleanBlock,
className: "fa fa-eraser fa-clean-block", className: "fa fa-eraser fa-clean-block",
title: "{{ translations.clean_block }}" title: "{{ translations.clean_block }}"
}, },
"|", "|",
{ {
name: "preview", name: "preview",
action: EasyMDE.togglePreview, action: EasyMDE.togglePreview,
className: "fa fa-eye no-disable", className: "fa fa-eye no-disable",
title: "{{ translations.preview }}" title: "{{ translations.preview }}"
}, },
{ {
name: "side-by-side", name: "side-by-side",
action: EasyMDE.toggleSideBySide, action: EasyMDE.toggleSideBySide,
className: "fa fa-columns no-disable no-mobile", className: "fa fa-columns no-disable no-mobile",
title: "{{ translations.side_by_side }}" title: "{{ translations.side_by_side }}"
}, },
{ {
name: "fullscreen", name: "fullscreen",
action: EasyMDE.toggleFullScreen, action: EasyMDE.toggleFullScreen,
className: "fa fa-arrows-alt no-disable no-mobile", className: "fa fa-arrows-alt no-disable no-mobile",
title: "{{ translations.fullscreen }}" title: "{{ translations.fullscreen }}"
}, },
"|", "|",
{ {
name: "guide", name: "guide",
action: "/page/Aide_sur_la_syntaxe", action: "/page/Aide_sur_la_syntaxe",
className: "fa fa-question-circle", className: "fa fa-question-circle",
title: "{{ translations.guide }}" title: "{{ translations.guide }}"
}, },
] ]
}); });
$(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>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment